From 915d5cabea914de76f07a02216c5e54b55bbbbc3 Mon Sep 17 00:00:00 2001 From: Spencer Cain Date: Thu, 24 Oct 2024 13:38:30 -0400 Subject: [PATCH 1/2] feat(auto-init): init on request if not already initialized This allows developers to call the API without manually initializing. This does not prevent manual initializtion from occurring, nor does it override it. This also shows a proper error if the API cannot initialize, instead of showing non-informative failed API responses. --- lib/api/core/OpenAPI.ts | 2 ++ lib/api/core/request.ts | 6 ++++++ lib/config.ts | 3 +++ 3 files changed, 11 insertions(+) diff --git a/lib/api/core/OpenAPI.ts b/lib/api/core/OpenAPI.ts index 382fe66..91fe28c 100644 --- a/lib/api/core/OpenAPI.ts +++ b/lib/api/core/OpenAPI.ts @@ -37,6 +37,7 @@ export type OpenAPIConfig = { request: Interceptors; response: Interceptors; }; + INITIALIZED: boolean; }; export const OpenAPI: OpenAPIConfig = { @@ -53,4 +54,5 @@ export const OpenAPI: OpenAPIConfig = { request: new Interceptors(), response: new Interceptors(), }, + INITIALIZED: false, }; diff --git a/lib/api/core/request.ts b/lib/api/core/request.ts index 870c3c1..a7522b6 100644 --- a/lib/api/core/request.ts +++ b/lib/api/core/request.ts @@ -1,3 +1,4 @@ +import { init } from "../../config"; import { ApiError } from "./ApiError"; import type { ApiRequestOptions } from "./ApiRequestOptions"; import type { ApiResult } from "./ApiResult"; @@ -348,6 +349,11 @@ export const request = ( config: OpenAPIConfig, options: ApiRequestOptions, ): CancelablePromise => { + // Verify that config has been initialized + if (!config.INITIALIZED) { + init(); + } + return new CancelablePromise(async (resolve, reject, onCancel) => { try { const url = getUrl(config, options); diff --git a/lib/config.ts b/lib/config.ts index 2cd4506..364586f 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -62,6 +62,9 @@ export const init = ( return await getToken(); }, }); + + // Mark OpenAPI as initialized + OpenAPI.INITIALIZED = true; }; function _merge(target: object = {}, ...objects: object[]): object { From d7026ee8152babf42e062a29ef8354a2b22f16d4 Mon Sep 17 00:00:00 2001 From: Spencer Cain Date: Thu, 24 Oct 2024 13:46:52 -0400 Subject: [PATCH 2/2] chore(auto-init): :memo: update readme --- readme.md | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 57dd6d8..f21415a 100644 --- a/readme.md +++ b/readme.md @@ -28,25 +28,35 @@ The following ENV variables are required to run the project: ## Usage ```js -import { Users, init } from "@kinde/management-api-js"; +import { Users } from "@kinde/management-api-js"; -init(); const { users } = await Users.getUsers(); ``` ### Params can be passed to the function as an object ```js -import { Users, init } from "@kinde/management-api-js"; +import { Users } from "@kinde/management-api-js"; const params = { id: "kp_xxx", }; -init(); const userData = await Users.getUserData(params); ``` +### Manually Initializing the API + +```js +import { init } from "@kinde/management-api-js"; + +init({ + domain: "mybusiness.kinde.com", + clientId: "client_id", + clientSecret: "client_secret", +}); +``` + ## API documentation You can find management API documentation here: [Kinde Management API Documentation](https://kinde.com/api/docs/#kinde-management-api)