From 0d3ca63737bab95c309f44341f2f490ef9bea566 Mon Sep 17 00:00:00 2001 From: Daniel Rivers Date: Tue, 19 Nov 2024 22:07:15 +0000 Subject: [PATCH 1/2] feat: optional KindeAPI params --- lib/main.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/main.ts b/lib/main.ts index 9f5a973..4fa6d20 100644 --- a/lib/main.ts +++ b/lib/main.ts @@ -253,7 +253,7 @@ export async function createKindeAPI( const callKindeAPI = async ( method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH", endpoint: string, - params: Record, + params?: Record, ) => { const result = await kinde.fetch( `${event.context.domains.kindeDomain}/api/v1/${endpoint}`, @@ -265,7 +265,7 @@ export async function createKindeAPI( "Content-Type": "application/json", accept: "application/json", }, - body: new URLSearchParams(params), + body: params && new URLSearchParams(params), }, ); @@ -273,13 +273,13 @@ export async function createKindeAPI( }; return { - get: async (endpoint: string, params: Record) => + get: async (endpoint: string, params?: Record) => await callKindeAPI("GET", endpoint, params), - post: async (endpoint: string, params: Record) => + post: async (endpoint: string, params?: Record) => await callKindeAPI("PATCH", endpoint, params), - put: async (endpoint: string, params: Record) => + put: async (endpoint: string, params?: Record) => await callKindeAPI("PUT", endpoint, params), - delete: async (endpoint: string, params: Record) => + delete: async (endpoint: string, params?: Record) => await callKindeAPI("DELETE", endpoint, params), }; } From 0bb99af0e34ddb523cad3a9321286ff2504e97ed Mon Sep 17 00:00:00 2001 From: Daniel Rivers Date: Tue, 19 Nov 2024 22:23:32 +0000 Subject: [PATCH 2/2] feat: improve typing and KindeAPI request --- lib/main.ts | 33 ++++++++++++++++++--------------- lib/types.ts | 7 +++++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/main.ts b/lib/main.ts index 4fa6d20..d1c976f 100644 --- a/lib/main.ts +++ b/lib/main.ts @@ -4,7 +4,7 @@ import { KindeAccessTokenProhibitedClaims, Kindem2mTokenProhibitedClaims, } from "./prohibitedClaims.ts"; -import { KindeFetchOptions, WorkflowEvents } from "./types"; +import { KindeAPIRequest, KindeFetchOptions, WorkflowEvents } from "./types"; import { version as packageVersion } from "../package.json"; export const version = packageVersion; @@ -250,11 +250,12 @@ export async function createKindeAPI( }, ); - const callKindeAPI = async ( - method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH", - endpoint: string, - params?: Record, - ) => { + const callKindeAPI = async ({ + method, + endpoint, + params, + contentType = "application/json", + }: KindeAPIRequest) => { const result = await kinde.fetch( `${event.context.domains.kindeDomain}/api/v1/${endpoint}`, { @@ -262,7 +263,7 @@ export async function createKindeAPI( responseFormat: "json", headers: { authorization: `Bearer ${token.access_token}`, - "Content-Type": "application/json", + "Content-Type": contentType, accept: "application/json", }, body: params && new URLSearchParams(params), @@ -273,13 +274,15 @@ export async function createKindeAPI( }; return { - get: async (endpoint: string, params?: Record) => - await callKindeAPI("GET", endpoint, params), - post: async (endpoint: string, params?: Record) => - await callKindeAPI("PATCH", endpoint, params), - put: async (endpoint: string, params?: Record) => - await callKindeAPI("PUT", endpoint, params), - delete: async (endpoint: string, params?: Record) => - await callKindeAPI("DELETE", endpoint, params), + get: async (params: Omit) => + await callKindeAPI({ method: "GET", ...params}), + post: async (params: Omit) => + await callKindeAPI({ method: "POST", ...params}), + patch: async (params: Omit) => + await callKindeAPI({ method: "PATCH", ...params}), + put: async (params: Omit) => + await callKindeAPI({ method: "PUT", ...params}), + delete: async (params: Omit) => + await callKindeAPI({ method: "DELETE", ...params}), }; } diff --git a/lib/types.ts b/lib/types.ts index 9f54dc5..a73bc2f 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -127,3 +127,10 @@ export type KindeFetchOptions = { headers: Record; body?: URLSearchParams; }; + +export type KindeAPIRequest = { + method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH", + endpoint: string, + params?: Record, + contentType?: "application/json" | "application/x-www-form-urlencoded" +} \ No newline at end of file