From 7c9e78ab3e5481e1acdfc0e07cc2f89aa1ebb6ab Mon Sep 17 00:00:00 2001 From: Isaiah Thomason <47364027+ITenthusiasm@users.noreply.github.com> Date: Sat, 19 Oct 2024 02:21:59 -0400 Subject: [PATCH] refactor(types): Minor Type Improvements for the `ThirdParty` Recipe (#946) * refactor(types): Make `userContext` in `TypeProvider` methods optional It is safe to pass `undefined` for `userContext` in all of these cases. Therefore, to improve the DX for developers not leveraging `UserContext` for these methods, the argument should be optional. * refactor(types): Clarify `redirectURIQueryParams` Type This argument expects an object of strings. This should be clarified to developers so that they don't mistakenly pass, for instance, an instance of `URLSearchParams`. * chore: Changelog updates --------- Co-authored-by: Rishabh Poddar --- CHANGELOG.md | 1 + lib/build/recipe/thirdparty/providers/custom.js | 5 +++-- lib/build/recipe/thirdparty/types.d.ts | 10 +++++----- lib/ts/recipe/thirdparty/providers/custom.ts | 5 +++-- lib/ts/recipe/thirdparty/types.ts | 10 +++++----- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32eec5d61..6829b8a84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The `networkInterceptor` now also gets a new `params` prop in the request config. - Adds `customFramework` util functions to minimize code required in custom frameworks like remix, astro etc. - Replicates `fastify` types based on requirement for the SDK instead of using the original module. +- Improved type definitions for `TypeProvider` ### Breaking change diff --git a/lib/build/recipe/thirdparty/providers/custom.js b/lib/build/recipe/thirdparty/providers/custom.js index ddd2b7a00..ecaa8b77b 100644 --- a/lib/build/recipe/thirdparty/providers/custom.js +++ b/lib/build/recipe/thirdparty/providers/custom.js @@ -7,6 +7,7 @@ var __importDefault = Object.defineProperty(exports, "__esModule", { value: true }); exports.getActualClientIdFromDevelopmentClientId = exports.isUsingDevelopmentClientId = exports.DEV_OAUTH_REDIRECT_URL = void 0; const thirdpartyUtils_1 = require("../../../thirdpartyUtils"); +const utils_1 = require("../../../utils"); const pkce_challenge_1 = __importDefault(require("pkce-challenge")); const configUtils_1 = require("./configUtils"); const jose_1 = require("jose"); @@ -284,7 +285,7 @@ function NewProvider(input) { await impl.config.validateIdTokenPayload({ idTokenPayload: rawUserInfoFromProvider.fromIdTokenPayload, clientConfig: impl.config, - userContext, + userContext: utils_1.getUserContext(userContext), }); } } @@ -292,7 +293,7 @@ function NewProvider(input) { await impl.config.validateAccessToken({ accessToken: accessToken, clientConfig: impl.config, - userContext, + userContext: utils_1.getUserContext(userContext), }); } if (accessToken && impl.config.userInfoEndpoint !== undefined) { diff --git a/lib/build/recipe/thirdparty/types.d.ts b/lib/build/recipe/thirdparty/types.d.ts index 8d0198e3b..63d3bbc62 100644 --- a/lib/build/recipe/thirdparty/types.d.ts +++ b/lib/build/recipe/thirdparty/types.d.ts @@ -98,11 +98,11 @@ export declare type TypeProvider = { config: ProviderConfigForClientType; getConfigForClientType: (input: { clientType?: string; - userContext: UserContext; + userContext?: UserContext; }) => Promise; getAuthorisationRedirectURL: (input: { redirectURIOnProviderDashboard: string; - userContext: UserContext; + userContext?: UserContext; }) => Promise<{ urlWithQueryParams: string; pkceCodeVerifier?: string; @@ -110,12 +110,12 @@ export declare type TypeProvider = { exchangeAuthCodeForOAuthTokens: (input: { redirectURIInfo: { redirectURIOnProviderDashboard: string; - redirectURIQueryParams: any; + redirectURIQueryParams: Record; pkceCodeVerifier?: string; }; - userContext: UserContext; + userContext?: UserContext; }) => Promise; - getUserInfo: (input: { oAuthTokens: any; userContext: UserContext }) => Promise; + getUserInfo: (input: { oAuthTokens: any; userContext?: UserContext }) => Promise; }; export declare type ProviderConfig = CommonProviderConfig & { clients?: ProviderClientConfig[]; diff --git a/lib/ts/recipe/thirdparty/providers/custom.ts b/lib/ts/recipe/thirdparty/providers/custom.ts index ba7226836..4886dab6e 100644 --- a/lib/ts/recipe/thirdparty/providers/custom.ts +++ b/lib/ts/recipe/thirdparty/providers/custom.ts @@ -1,5 +1,6 @@ import { TypeProvider, ProviderInput, UserInfo, ProviderConfigForClientType } from "../types"; import { doGetRequest, doPostRequest, verifyIdTokenFromJWKSEndpointAndGetPayload } from "../../../thirdpartyUtils"; +import { getUserContext } from "../../../utils"; import pkceChallenge from "pkce-challenge"; import { getProviderConfigForClient } from "./configUtils"; import { JWTVerifyGetKey, createRemoteJWKSet } from "jose"; @@ -312,7 +313,7 @@ export default function NewProvider(input: ProviderInput): TypeProvider { await impl.config.validateIdTokenPayload({ idTokenPayload: rawUserInfoFromProvider.fromIdTokenPayload, clientConfig: impl.config, - userContext, + userContext: getUserContext(userContext), }); } } @@ -321,7 +322,7 @@ export default function NewProvider(input: ProviderInput): TypeProvider { await impl.config.validateAccessToken({ accessToken: accessToken, clientConfig: impl.config, - userContext, + userContext: getUserContext(userContext), }); } diff --git a/lib/ts/recipe/thirdparty/types.ts b/lib/ts/recipe/thirdparty/types.ts index 71e9847d3..73a02e4a9 100644 --- a/lib/ts/recipe/thirdparty/types.ts +++ b/lib/ts/recipe/thirdparty/types.ts @@ -98,21 +98,21 @@ export type TypeProvider = { getConfigForClientType: (input: { clientType?: string; - userContext: UserContext; + userContext?: UserContext; }) => Promise; getAuthorisationRedirectURL: (input: { redirectURIOnProviderDashboard: string; - userContext: UserContext; + userContext?: UserContext; }) => Promise<{ urlWithQueryParams: string; pkceCodeVerifier?: string }>; exchangeAuthCodeForOAuthTokens: (input: { redirectURIInfo: { redirectURIOnProviderDashboard: string; - redirectURIQueryParams: any; + redirectURIQueryParams: Record; pkceCodeVerifier?: string; }; - userContext: UserContext; + userContext?: UserContext; }) => Promise; - getUserInfo: (input: { oAuthTokens: any; userContext: UserContext }) => Promise; + getUserInfo: (input: { oAuthTokens: any; userContext?: UserContext }) => Promise; }; export type ProviderConfig = CommonProviderConfig & {