Skip to content

Commit

Permalink
Merge pull request #2 from kinde-oss/fix/type-export
Browse files Browse the repository at this point in the history
fix: optional parameters
  • Loading branch information
DanielRivers authored Nov 19, 2024
2 parents 1e73229 + 0bb99af commit b1c1009
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -250,36 +250,39 @@ export async function createKindeAPI(
},
);

const callKindeAPI = async (
method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH",
endpoint: string,
params: Record<string, string>,
) => {
const callKindeAPI = async ({
method,
endpoint,
params,
contentType = "application/json",
}: KindeAPIRequest) => {
const result = await kinde.fetch(
`${event.context.domains.kindeDomain}/api/v1/${endpoint}`,
{
method,
responseFormat: "json",
headers: {
authorization: `Bearer ${token.access_token}`,
"Content-Type": "application/json",
"Content-Type": contentType,
accept: "application/json",
},
body: new URLSearchParams(params),
body: params && new URLSearchParams(params),
},
);

return { data: result.json };
};

return {
get: async (endpoint: string, params: Record<string, string>) =>
await callKindeAPI("GET", endpoint, params),
post: async (endpoint: string, params: Record<string, string>) =>
await callKindeAPI("PATCH", endpoint, params),
put: async (endpoint: string, params: Record<string, string>) =>
await callKindeAPI("PUT", endpoint, params),
delete: async (endpoint: string, params: Record<string, string>) =>
await callKindeAPI("DELETE", endpoint, params),
get: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "GET", ...params}),
post: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "POST", ...params}),
patch: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "PATCH", ...params}),
put: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "PUT", ...params}),
delete: async (params: Omit<KindeAPIRequest, "method">) =>
await callKindeAPI({ method: "DELETE", ...params}),
};
}
7 changes: 7 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@ export type KindeFetchOptions = {
headers: Record<string, string>;
body?: URLSearchParams;
};

export type KindeAPIRequest = {
method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH",
endpoint: string,
params?: Record<string, string>,
contentType?: "application/json" | "application/x-www-form-urlencoded"
}

0 comments on commit b1c1009

Please sign in to comment.