From a034a3ae0316c4060c68265719cd7b19c4a76f52 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sun, 8 Dec 2024 10:44:50 +0000 Subject: [PATCH] build(deps-dev): replace standard with neostandard --- README.md | 2 +- package.json | 7 +- types/index.d.ts | 302 +++++++++++++------------- types/index.test-d.ts | 477 +++++++++++++++++++++--------------------- 4 files changed, 393 insertions(+), 395 deletions(-) diff --git a/README.md b/README.md index b191c2e..90751a4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![CI](https://github.com/fastify/fastify-oauth2/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-oauth2/actions/workflows/ci.yml) [![NPM version](https://img.shields.io/npm/v/@fastify/oauth2.svg?style=flat)](https://www.npmjs.com/package/@fastify/oauth2) -[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) +[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard) Wrapper around the [`simple-oauth2`](https://github.com/lelylan/simple-oauth2) library. diff --git a/package.json b/package.json index d1d1311..1ac67fe 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "type": "commonjs", "types": "types/index.d.ts", "scripts": { - "lint": "standard | snazzy", - "lint:fix": "standard --fix", + "lint": "eslint", + "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", "test:coverage": "npm run test:unit -- --cov --coverage-report=html", "test:typescript": "tsd", @@ -32,10 +32,9 @@ "@types/node": "^22.0.0", "@types/simple-oauth2": "^5.0.7", "fastify": "^5.0.0", + "neostandard": "^0.11.9", "nock": "^13.5.4", "simple-get": "^4.0.1", - "snazzy": "^9.0.0", - "standard": "^17.1.0", "tap": "^18.7.1", "tsd": "^0.31.0" }, diff --git a/types/index.d.ts b/types/index.d.ts index 6ea9da1..78bb5c4 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,22 +1,22 @@ -import { FastifyPluginCallback, FastifyReply, FastifyRequest, FastifyInstance } from 'fastify'; -import { CookieSerializeOptions } from "@fastify/cookie"; +import { FastifyPluginCallback, FastifyReply, FastifyRequest, FastifyInstance } from 'fastify' +import { CookieSerializeOptions } from '@fastify/cookie' interface FastifyOauth2 extends FastifyPluginCallback { - APPLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - DISCORD_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - FACEBOOK_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - GITHUB_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - GITLAB_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - LINKEDIN_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - GOOGLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - MICROSOFT_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - SPOTIFY_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - VKONTAKTE_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - TWITCH_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - VATSIM_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - VATSIM_DEV_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - EPIC_GAMES_CONFIGURATION: fastifyOauth2.ProviderConfiguration; - YANDEX_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + APPLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + DISCORD_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + FACEBOOK_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + GITHUB_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + GITLAB_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + LINKEDIN_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + GOOGLE_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + MICROSOFT_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + SPOTIFY_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + VKONTAKTE_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + TWITCH_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + VATSIM_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + VATSIM_DEV_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + EPIC_GAMES_CONFIGURATION: fastifyOauth2.ProviderConfiguration; + YANDEX_CONFIGURATION: fastifyOauth2.ProviderConfiguration; } declare namespace fastifyOauth2 { @@ -50,163 +50,163 @@ declare namespace fastifyOauth2 { verifierCookieName?: string; } - export type TToken = 'access_token' | 'refresh_token' + export type TToken = 'access_token' | 'refresh_token' - export interface Token { - token_type: 'Bearer'; - access_token: string; - refresh_token?: string; - id_token?: string; - expires_in: number; - expires_at: Date; - } + export interface Token { + token_type: 'Bearer'; + access_token: string; + refresh_token?: string; + id_token?: string; + expires_in: number; + expires_at: Date; + } - export interface OAuth2Token { - /** + export interface OAuth2Token { + /** * Immutable object containing the token object provided while constructing a new access token instance. * This property will usually have the schema as specified by RFC6750, * but the exact properties may vary between authorization servers. */ - token: Token; + token: Token; - /** + /** * Determines if the current access token is definitely expired or not * @param expirationWindowSeconds Window of time before the actual expiration to refresh the token. Defaults to 0. */ - expired(expirationWindowSeconds?: number): boolean; - - /** Refresh the access token */ - refresh(params?: {}): Promise; - - /** Revoke access or refresh token */ - revoke(tokenType: 'access_token' | 'refresh_token'): Promise; - - /** Revoke both the existing access and refresh tokens */ - revokeAll(): Promise; - } - - export interface ProviderConfiguration { - /** String used to set the host to request the tokens to. Required. */ - tokenHost: string; - /** String path to request an access token. Default to /oauth/token. */ - tokenPath?: string | undefined; - /** String path to revoke an access token. Default to /oauth/revoke. */ - revokePath?: string | undefined; - /** String used to set the host to request an "authorization code". Default to the value set on auth.tokenHost. */ - authorizeHost?: string | undefined; - /** String path to request an authorization code. Default to /oauth/authorize. */ - authorizePath?: string | undefined; - } - - export interface Credentials { - client: { - /** Service registered client id. Required. */ - id: string; - /** Service registered client secret. Required. */ - secret: string; - /** Parameter name used to send the client secret. Default to client_secret. */ - secretParamName?: string | undefined; - /** Parameter name used to send the client id. Default to client_id. */ - idParamName?: string | undefined; - }; - auth?: ProviderConfiguration; - /** + expired(expirationWindowSeconds?: number): boolean; + + /** Refresh the access token */ + refresh(params?: {}): Promise; + + /** Revoke access or refresh token */ + revoke(tokenType: 'access_token' | 'refresh_token'): Promise; + + /** Revoke both the existing access and refresh tokens */ + revokeAll(): Promise; + } + + export interface ProviderConfiguration { + /** String used to set the host to request the tokens to. Required. */ + tokenHost: string; + /** String path to request an access token. Default to /oauth/token. */ + tokenPath?: string | undefined; + /** String path to revoke an access token. Default to /oauth/revoke. */ + revokePath?: string | undefined; + /** String used to set the host to request an "authorization code". Default to the value set on auth.tokenHost. */ + authorizeHost?: string | undefined; + /** String path to request an authorization code. Default to /oauth/authorize. */ + authorizePath?: string | undefined; + } + + export interface Credentials { + client: { + /** Service registered client id. Required. */ + id: string; + /** Service registered client secret. Required. */ + secret: string; + /** Parameter name used to send the client secret. Default to client_secret. */ + secretParamName?: string | undefined; + /** Parameter name used to send the client id. Default to client_id. */ + idParamName?: string | undefined; + }; + auth?: ProviderConfiguration; + /** * Used to set global options to the internal http library (wreck). * All options except baseUrl are allowed * Defaults to header.Accept = "application/json" */ - http?: {} | undefined; - options?: { - /** Format of data sent in the request body. Defaults to form. */ - bodyFormat?: "json" | "form" | undefined; - /** + http?: {} | undefined; + options?: { + /** Format of data sent in the request body. Defaults to form. */ + bodyFormat?: 'json' | 'form' | undefined; + /** * Indicates the method used to send the client.id/client.secret authorization params at the token request. * If set to body, the bodyFormat option will be used to format the credentials. * Defaults to header */ - authorizationMethod?: "header" | "body" | undefined; - } | undefined; - } - - export interface OAuth2Namespace { - getAccessTokenFromAuthorizationCodeFlow( - request: FastifyRequest, - ): Promise; - - getAccessTokenFromAuthorizationCodeFlow( - request: FastifyRequest, - reply: FastifyReply, - ): Promise; - - getAccessTokenFromAuthorizationCodeFlow( - request: FastifyRequest, - callback: (err: any, token: OAuth2Token) => void, - ): void; - - getAccessTokenFromAuthorizationCodeFlow( - request: FastifyRequest, - reply: FastifyReply, - callback: (err: any, token: OAuth2Token) => void, - ): void; - - getNewAccessTokenUsingRefreshToken( - refreshToken: Token, - params: Object, - callback: (err: any, token: OAuth2Token) => void, - ): void; - - getNewAccessTokenUsingRefreshToken(refreshToken: Token, params: Object): Promise; - - generateAuthorizationUri( - request: FastifyRequest, - reply: FastifyReply, - callback: (err: any, uri: string) => void - ): void - - generateAuthorizationUri( - request: FastifyRequest, - reply: FastifyReply, - ): Promise; - - revokeToken( - revokeToken: Token, - tokenType: TToken, - httpOptions: Object | undefined, - callback: (err: any) => void - ): void; - - revokeToken(revokeToken: Token, tokenType: TToken, httpOptions: Object | undefined): Promise; - - revokeAllToken( - revokeToken: Token, - httpOptions: Object | undefined, - callback: (err: any) => void - ): void; - - revokeAllToken(revokeToken: Token, httpOptions: Object | undefined): Promise; - - userinfo(tokenSetOrToken: Token | string): Promise; - - userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined): Promise; - - userinfo(tokenSetOrToken: Token | string, callback: (err: any, userinfo: Object) => void): void; - - userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined, callback: (err: any, userinfo: Object) => void): void; - } - export type UserInfoExtraOptions = { method?: 'GET' | 'POST', via?: 'header' | 'body', params?: object }; - export const fastifyOauth2: FastifyOauth2 - export {fastifyOauth2 as default} + authorizationMethod?: 'header' | 'body' | undefined; + } | undefined; + } + + export interface OAuth2Namespace { + getAccessTokenFromAuthorizationCodeFlow( + request: FastifyRequest, + ): Promise; + + getAccessTokenFromAuthorizationCodeFlow( + request: FastifyRequest, + reply: FastifyReply, + ): Promise; + + getAccessTokenFromAuthorizationCodeFlow( + request: FastifyRequest, + callback: (err: any, token: OAuth2Token) => void, + ): void; + + getAccessTokenFromAuthorizationCodeFlow( + request: FastifyRequest, + reply: FastifyReply, + callback: (err: any, token: OAuth2Token) => void, + ): void; + + getNewAccessTokenUsingRefreshToken( + refreshToken: Token, + params: Object, + callback: (err: any, token: OAuth2Token) => void, + ): void; + + getNewAccessTokenUsingRefreshToken(refreshToken: Token, params: Object): Promise; + + generateAuthorizationUri( + request: FastifyRequest, + reply: FastifyReply, + callback: (err: any, uri: string) => void + ): void + + generateAuthorizationUri( + request: FastifyRequest, + reply: FastifyReply, + ): Promise; + + revokeToken( + revokeToken: Token, + tokenType: TToken, + httpOptions: Object | undefined, + callback: (err: any) => void + ): void; + + revokeToken(revokeToken: Token, tokenType: TToken, httpOptions: Object | undefined): Promise; + + revokeAllToken( + revokeToken: Token, + httpOptions: Object | undefined, + callback: (err: any) => void + ): void; + + revokeAllToken(revokeToken: Token, httpOptions: Object | undefined): Promise; + + userinfo(tokenSetOrToken: Token | string): Promise; + + userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined): Promise; + + userinfo(tokenSetOrToken: Token | string, callback: (err: any, userinfo: Object) => void): void; + + userinfo(tokenSetOrToken: Token | string, userInfoExtraOptions: UserInfoExtraOptions | undefined, callback: (err: any, userinfo: Object) => void): void; + } + export type UserInfoExtraOptions = { method?: 'GET' | 'POST', via?: 'header' | 'body', params?: object } + export const fastifyOauth2: FastifyOauth2 + export { fastifyOauth2 as default } } -declare function fastifyOauth2(...params: Parameters): ReturnType +declare function fastifyOauth2 (...params: Parameters): ReturnType export = fastifyOauth2 -type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'; +type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' declare module 'fastify' { - interface FastifyInstance { - // UpperCaseCharacters ensures that the name has at least one character in it + is a simple camel-case:ification - [key: `oauth2${UpperCaseCharacters}${string}`]: fastifyOauth2.OAuth2Namespace | undefined; - } + interface FastifyInstance { + // UpperCaseCharacters ensures that the name has at least one character in it + is a simple camel-case:ification + [key: `oauth2${UpperCaseCharacters}${string}`]: fastifyOauth2.OAuth2Namespace | undefined; + } } diff --git a/types/index.test-d.ts b/types/index.test-d.ts index f98e181..ce55788 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,304 +1,303 @@ -import fastify from 'fastify'; -import {expectAssignable, expectError, expectNotAssignable, expectType} from 'tsd'; +import fastify from 'fastify' +import { expectAssignable, expectError, expectNotAssignable, expectType } from 'tsd' import fastifyOauth2, { - FastifyOAuth2Options, - Credentials, - OAuth2Namespace, - OAuth2Token, - ProviderConfiguration, - UserInfoExtraOptions -} from '..'; -import type { ModuleOptions } from 'simple-oauth2'; -import { FastifyInstance } from 'fastify'; + FastifyOAuth2Options, + Credentials, + OAuth2Namespace, + OAuth2Token, + ProviderConfiguration, + UserInfoExtraOptions +} from '..' +import type { ModuleOptions } from 'simple-oauth2' +import { FastifyInstance } from 'fastify' /** * Preparing some data for testing. */ -const auth = fastifyOauth2.GOOGLE_CONFIGURATION; -const scope = ['r_emailaddress', 'r_basicprofile']; -const tags = ['oauth2', 'oauth']; +const auth = fastifyOauth2.GOOGLE_CONFIGURATION +const scope = ['r_emailaddress', 'r_basicprofile'] +const tags = ['oauth2', 'oauth'] const credentials: Credentials = { - client: { - id: 'test_id', - secret: 'test_secret', - }, - auth: auth, -}; + client: { + id: 'test_id', + secret: 'test_secret', + }, + auth, +} const simpleOauth2Options: ModuleOptions = { - client: { - id: 'test_id', - secret: 'test_secret', - }, - auth: auth, -}; + client: { + id: 'test_id', + secret: 'test_secret', + }, + auth, +} const OAuth2NoneOptional: FastifyOAuth2Options = { - name: 'testOAuthName', - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback' -}; + name: 'testOAuthName', + credentials, + callbackUri: 'http://localhost/testOauth/callback' +} const OAuth2Options: FastifyOAuth2Options = { - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - callbackUriParams: {}, - generateStateFunction: function () { - expectType(this) - return 'test' - }, - checkStateFunction: function () { - expectType(this) - return true - }, - startRedirectPath: '/login/testOauth', - cookie: { - secure: true, - sameSite: 'none' - }, - redirectStateCookieName: 'redirect-state-cookie', - verifierCookieName: 'verifier-cookie', -}; + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + callbackUriParams: {}, + generateStateFunction: function () { + expectType(this) + return 'test' + }, + checkStateFunction: function () { + expectType(this) + return true + }, + startRedirectPath: '/login/testOauth', + cookie: { + secure: true, + sameSite: 'none' + }, + redirectStateCookieName: 'redirect-state-cookie', + verifierCookieName: 'verifier-cookie', +} expectAssignable({ - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - callbackUriParams: {}, - startRedirectPath: '/login/testOauth', - pkce: 'S256' + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + callbackUriParams: {}, + startRedirectPath: '/login/testOauth', + pkce: 'S256' }) expectAssignable({ - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: req => `${req.protocol}://${req.hostname}/callback`, - callbackUriParams: {}, - startRedirectPath: '/login/testOauth', - pkce: 'S256' + name: 'testOAuthName', + scope, + credentials, + callbackUri: req => `${req.protocol}://${req.hostname}/callback`, + callbackUriParams: {}, + startRedirectPath: '/login/testOauth', + pkce: 'S256' }) expectAssignable({ - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - callbackUriParams: {}, - startRedirectPath: '/login/testOauth', - discovery: { issuer: 'https://idp.mycompany.com' } + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + callbackUriParams: {}, + startRedirectPath: '/login/testOauth', + discovery: { issuer: 'https://idp.mycompany.com' } }) expectNotAssignable({ - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - callbackUriParams: {}, - startRedirectPath: '/login/testOauth', - discovery: { issuer: 1 } + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + callbackUriParams: {}, + startRedirectPath: '/login/testOauth', + discovery: { issuer: 1 } }) - expectAssignable({ - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - callbackUriParams: {}, - startRedirectPath: '/login/testOauth', - pkce: 'plain' + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + callbackUriParams: {}, + startRedirectPath: '/login/testOauth', + pkce: 'plain' }) expectNotAssignable({ - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - callbackUriParams: {}, - generateStateFunction: () => { - }, - checkStateFunction: () => { - }, - startRedirectPath: '/login/testOauth', - pkce: 'SOMETHING' + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + callbackUriParams: {}, + generateStateFunction: () => { + }, + checkStateFunction: () => { + }, + startRedirectPath: '/login/testOauth', + pkce: 'SOMETHING' }) -const server = fastify(); +const server = fastify() server.register(fastifyOauth2, OAuth2NoneOptional) -server.register(fastifyOauth2, OAuth2Options); +server.register(fastifyOauth2, OAuth2Options) server.register(fastifyOauth2, { - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - checkStateFunction: () => true, + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + checkStateFunction: () => true, }) expectError(server.register(fastifyOauth2, { - name: 'testOAuthName', - scope: scope, - credentials: credentials, - callbackUri: 'http://localhost/testOauth/callback', - checkStateFunction: () => true, - startRedirectPath: 2, + name: 'testOAuthName', + scope, + credentials, + callbackUri: 'http://localhost/testOauth/callback', + checkStateFunction: () => true, + startRedirectPath: 2, })) declare module 'fastify' { - // Developers need to define this in their code like they have to do with all decorators. - interface FastifyInstance { - testOAuthName: OAuth2Namespace; - } + // Developers need to define this in their code like they have to do with all decorators. + interface FastifyInstance { + testOAuthName: OAuth2Namespace; + } } /** * Actual testing. */ -expectType(auth); -expectType(scope); -expectType(tags); -expectType(credentials); +expectType(auth) +expectType(scope) +expectType(tags) +expectType(credentials) // Ensure duplicayed simple-oauth2 are compatible with simple-oauth2 -expectAssignable>({auth: {tokenHost: ''}, ...credentials }); -expectAssignable(auth); +expectAssignable>({ auth: { tokenHost: '' }, ...credentials }) +expectAssignable(auth) // Ensure published types of simple-oauth2 are accepted -expectAssignable(simpleOauth2Options); -expectAssignable(simpleOauth2Options.auth); +expectAssignable(simpleOauth2Options) +expectAssignable(simpleOauth2Options.auth) -expectError(fastifyOauth2()); // error because missing required arguments +expectError(fastifyOauth2()) // error because missing required arguments expectError(fastifyOauth2(server, {}, () => { -})); // error because missing required options +})) // error because missing required options -expectAssignable(fastifyOauth2.DISCORD_CONFIGURATION); -expectAssignable(fastifyOauth2.FACEBOOK_CONFIGURATION); -expectAssignable(fastifyOauth2.GITHUB_CONFIGURATION); -expectAssignable(fastifyOauth2.GITLAB_CONFIGURATION); -expectAssignable(fastifyOauth2.GOOGLE_CONFIGURATION); -expectAssignable(fastifyOauth2.LINKEDIN_CONFIGURATION); -expectAssignable(fastifyOauth2.MICROSOFT_CONFIGURATION); -expectAssignable(fastifyOauth2.SPOTIFY_CONFIGURATION); -expectAssignable(fastifyOauth2.VKONTAKTE_CONFIGURATION); -expectAssignable(fastifyOauth2.TWITCH_CONFIGURATION); -expectAssignable(fastifyOauth2.VATSIM_CONFIGURATION); -expectAssignable(fastifyOauth2.VATSIM_DEV_CONFIGURATION); -expectAssignable(fastifyOauth2.EPIC_GAMES_CONFIGURATION); -expectAssignable(fastifyOauth2.YANDEX_CONFIGURATION); +expectAssignable(fastifyOauth2.DISCORD_CONFIGURATION) +expectAssignable(fastifyOauth2.FACEBOOK_CONFIGURATION) +expectAssignable(fastifyOauth2.GITHUB_CONFIGURATION) +expectAssignable(fastifyOauth2.GITLAB_CONFIGURATION) +expectAssignable(fastifyOauth2.GOOGLE_CONFIGURATION) +expectAssignable(fastifyOauth2.LINKEDIN_CONFIGURATION) +expectAssignable(fastifyOauth2.MICROSOFT_CONFIGURATION) +expectAssignable(fastifyOauth2.SPOTIFY_CONFIGURATION) +expectAssignable(fastifyOauth2.VKONTAKTE_CONFIGURATION) +expectAssignable(fastifyOauth2.TWITCH_CONFIGURATION) +expectAssignable(fastifyOauth2.VATSIM_CONFIGURATION) +expectAssignable(fastifyOauth2.VATSIM_DEV_CONFIGURATION) +expectAssignable(fastifyOauth2.EPIC_GAMES_CONFIGURATION) +expectAssignable(fastifyOauth2.YANDEX_CONFIGURATION) server.get('/testOauth/callback', async (request, reply) => { - expectType(server.testOAuthName); - expectType(server.oauth2TestOAuthName); + expectType(server.testOAuthName) + expectType(server.oauth2TestOAuthName) + + expectType(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)) + expectType>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)) - expectType(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); - expectType>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); + expectType(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply)) + expectType>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply)) + expectType( + server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (err: any, t: OAuth2Token): void => { + }) + ) + expectType( + server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply, (err: any, t: OAuth2Token): void => { + }) + ) + // error because Promise should not return void + expectError(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)) + // error because non-Promise function call should return void and have a callback argument + expectError( + server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (err: any, t: OAuth2Token): void => { + }) + ) - expectType(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply)); - expectType>(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply)); + // error because function call does not pass a callback as second argument. + expectError(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)) + + const token = await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request) + if (token.token.refresh_token) { + expectType( + await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {}) + ) + expectType>( + server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {}) + ) expectType( - server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (err: any, t: OAuth2Token): void => { - }), - ); + server.testOAuthName.getNewAccessTokenUsingRefreshToken( + token.token, + {}, + (err: any, t: OAuth2Token): void => { + } + ) + ) + // Expect error because Promise should not return void + expectError(server.testOAuthName.revokeToken(token.token, 'access_token', undefined)) + // Correct way + expectType>(server.testOAuthName.revokeToken(token.token, 'access_token', undefined)) + // Expect error because invalid Type test isn't an access_token or refresh_token + expectError>(server.testOAuthName.revokeToken(token.token, 'test', undefined)) + // Correct way expectType( - server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, reply, (err: any, t: OAuth2Token): void => { - }), - ); + server.testOAuthName.revokeToken(token.token, 'refresh_token', undefined, (err: any): void => { + }) + ) + // Expect error because invalid Type test isn't an access_token or refresh_token + expectError( + server.testOAuthName.revokeToken(token.token, 'test', undefined, (err: any): void => { + }) + ) + // Expect error because invalid Type test isn't an access_token or refresh_token + expectError( + server.testOAuthName.revokeToken(token.token, 'access_token', undefined, undefined) + ) + + // Expect error because Promise should not return void + expectError(server.testOAuthName.revokeAllToken(token.token, undefined)) + // Correct way + expectType>(server.testOAuthName.revokeAllToken(token.token, undefined)) + // Correct way too + expectType(server.testOAuthName.revokeAllToken(token.token, undefined, (err: any): void => { + })) + // Invalid content + expectError(server.testOAuthName.revokeAllToken(token.token, undefined, undefined)) // error because Promise should not return void - expectError(await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); + expectError(await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})) // error because non-Promise function call should return void and have a callback argument expectError( - server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request, (err: any, t: OAuth2Token): void => { - }), - ); - + server.testOAuthName.getNewAccessTokenUsingRefreshToken( + token.token, + {}, + (err: any, t: OAuth2Token): void => { + } + ) + ) // error because function call does not pass a callback as second argument. - expectError(server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request)); + expectError(server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})) + } - const token = await server.testOAuthName.getAccessTokenFromAuthorizationCodeFlow(request); - if (token.token.refresh_token) { - expectType( - await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {}), - ); - expectType>( - server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {}), - ); - expectType( - server.testOAuthName.getNewAccessTokenUsingRefreshToken( - token.token, - {}, - (err: any, t: OAuth2Token): void => { - }, - ), - ); - // Expect error because Promise should not return void - expectError(server.testOAuthName.revokeToken(token.token, "access_token", undefined)) - // Correct way - expectType>(server.testOAuthName.revokeToken(token.token, "access_token", undefined)) - // Expect error because invalid Type test isn't an access_token or refresh_token - expectError>(server.testOAuthName.revokeToken(token.token, "test", undefined)) - // Correct way - expectType( - server.testOAuthName.revokeToken(token.token, "refresh_token", undefined, (err: any): void => { - }), - ); - // Expect error because invalid Type test isn't an access_token or refresh_token - expectError( - server.testOAuthName.revokeToken(token.token, "test", undefined, (err: any): void => { - }), - ); - // Expect error because invalid Type test isn't an access_token or refresh_token - expectError( - server.testOAuthName.revokeToken(token.token, "access_token", undefined, undefined), - ); + expectType>(server.testOAuthName.generateAuthorizationUri(request, reply)) + expectType(server.testOAuthName.generateAuthorizationUri(request, reply, (err) => {})) + // BEGIN userinfo tests + expectType>(server.testOAuthName.userinfo(token.token)) + expectType>(server.testOAuthName.userinfo(token.token.access_token)) + expectType(await server.testOAuthName.userinfo(token.token.access_token)) + expectType(server.testOAuthName.userinfo(token.token.access_token, () => {})) + expectType(server.testOAuthName.userinfo(token.token.access_token, undefined, () => {})) + expectAssignable({ method: 'GET', params: {}, via: 'header' }) + expectAssignable({ method: 'POST', params: { a: 1 }, via: 'header' }) + expectAssignable({ via: 'body' }) + expectNotAssignable({ via: 'donkey' }) + expectNotAssignable({ something: 1 }) + // END userinfo tests - // Expect error because Promise should not return void - expectError(server.testOAuthName.revokeAllToken(token.token, undefined)) - // Correct way - expectType>(server.testOAuthName.revokeAllToken(token.token, undefined)) - // Correct way too - expectType(server.testOAuthName.revokeAllToken(token.token, undefined, (err: any): void => { - })) - // Invalid content - expectError(server.testOAuthName.revokeAllToken(token.token, undefined, undefined)) - // error because Promise should not return void - expectError(await server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})) - // error because non-Promise function call should return void and have a callback argument - expectError( - server.testOAuthName.getNewAccessTokenUsingRefreshToken( - token.token, - {}, - (err: any, t: OAuth2Token): void => { - }, - ), - ); - // error because function call does not pass a callback as second argument. - expectError(server.testOAuthName.getNewAccessTokenUsingRefreshToken(token.token, {})) - } + expectType(await server.testOAuthName.generateAuthorizationUri(request, reply)) + // error because missing reply argument + expectError(server.testOAuthName.generateAuthorizationUri(request)) - expectType>(server.testOAuthName.generateAuthorizationUri(request, reply)); - expectType(server.testOAuthName.generateAuthorizationUri(request, reply, (err) => {})) - // BEGIN userinfo tests - expectType>(server.testOAuthName.userinfo(token.token)); - expectType>(server.testOAuthName.userinfo(token.token.access_token)); - expectType(await server.testOAuthName.userinfo(token.token.access_token)); - expectType(server.testOAuthName.userinfo(token.token.access_token, () => {})); - expectType(server.testOAuthName.userinfo(token.token.access_token, undefined, () => {})); - expectAssignable({ method: 'GET', params: {}, via: 'header' }); - expectAssignable({ method: 'POST', params: { a: 1 }, via: 'header' }); - expectAssignable({ via: 'body' }); - expectNotAssignable({ via: 'donkey' }); - expectNotAssignable({ something: 1 }); - // END userinfo tests - - expectType(await server.testOAuthName.generateAuthorizationUri(request, reply)); - // error because missing reply argument - expectError(server.testOAuthName.generateAuthorizationUri(request)); - - return { - access_token: token.token.access_token, - }; -}); + return { + access_token: token.token.access_token, + } +})