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 { 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)