@logto/api 1.35.0 → 1.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,20 @@ export type CreateManagementApiOptions = {
27
27
  */
28
28
  apiIndicator?: string;
29
29
  };
30
+ /**
31
+ * Options for creating an API client with custom token authentication.
32
+ */
33
+ export type CreateApiClientOptions = {
34
+ /**
35
+ * The base URL for the Management API.
36
+ */
37
+ baseUrl: string;
38
+ /**
39
+ * A function that returns a promise resolving to the access token.
40
+ * This function will be called for each request that requires authentication.
41
+ */
42
+ getToken: () => Promise<string>;
43
+ };
30
44
  /**
31
45
  * Returns the base URL for the Management API based on the tenant ID.
32
46
  * @param tenantId The tenant ID to construct the base URL.
@@ -45,6 +59,31 @@ export declare const getManagementApiIndicator: (tenantId: string) => string;
45
59
  * This is used when requesting an access token for the Management API.
46
60
  */
47
61
  export declare const allScope = "all";
62
+ /**
63
+ * Creates an API client with custom token authentication.
64
+ *
65
+ * This function is useful when you need full control over the authentication flow,
66
+ * such as custom token sources.
67
+ *
68
+ * The client automatically skips authentication for `.well-known` endpoints.
69
+ *
70
+ * @param options The options including base URL and token getter function.
71
+ * @returns A configured API client with type-safe methods.
72
+ * @example
73
+ * ```ts
74
+ * import { createApiClient } from '@logto/api/management';
75
+ *
76
+ * const client = createApiClient({
77
+ * baseUrl: 'https://my-tenant.logto.app',
78
+ * getToken: async () => getYourToken(),
79
+ * });
80
+ *
81
+ * const response = await client.GET('/api/applications/{id}', {
82
+ * params: { path: { id: 'app-id' } },
83
+ * });
84
+ * ```
85
+ */
86
+ export declare function createApiClient(options: CreateApiClientOptions): Client<paths>;
48
87
  type ManagementApiReturnType = {
49
88
  /**
50
89
  * The API client for the Management API.
package/lib/management.js CHANGED
@@ -18,6 +18,45 @@ export const getManagementApiIndicator = (tenantId) => `${getBaseUrl(tenantId)}/
18
18
  * This is used when requesting an access token for the Management API.
19
19
  */
20
20
  export const allScope = 'all';
21
+ /**
22
+ * Creates an API client with custom token authentication.
23
+ *
24
+ * This function is useful when you need full control over the authentication flow,
25
+ * such as custom token sources.
26
+ *
27
+ * The client automatically skips authentication for `.well-known` endpoints.
28
+ *
29
+ * @param options The options including base URL and token getter function.
30
+ * @returns A configured API client with type-safe methods.
31
+ * @example
32
+ * ```ts
33
+ * import { createApiClient } from '@logto/api/management';
34
+ *
35
+ * const client = createApiClient({
36
+ * baseUrl: 'https://my-tenant.logto.app',
37
+ * getToken: async () => getYourToken(),
38
+ * });
39
+ *
40
+ * const response = await client.GET('/api/applications/{id}', {
41
+ * params: { path: { id: 'app-id' } },
42
+ * });
43
+ * ```
44
+ */
45
+ export function createApiClient(options) {
46
+ const { baseUrl, getToken } = options;
47
+ const client = createClient({ baseUrl });
48
+ client.use({
49
+ async onRequest({ schemaPath, request }) {
50
+ if (schemaPath.includes('/.well-known/')) {
51
+ return;
52
+ }
53
+ const token = await getToken();
54
+ request.headers.set('Authorization', `Bearer ${token}`);
55
+ return request;
56
+ },
57
+ });
58
+ return client;
59
+ }
21
60
  /**
22
61
  * Creates a Management API client with the specified tenant ID and options.
23
62
  *
@@ -72,21 +111,14 @@ export function createManagementApi(tenantId, options) {
72
111
  scope: allScope,
73
112
  },
74
113
  });
75
- const apiClient = createClient({
114
+ const apiClient = createApiClient({
76
115
  baseUrl,
77
- });
78
- apiClient.use({
79
- async onRequest({ schemaPath, request }) {
80
- if (schemaPath.includes('/.well-known/')) {
81
- // Skip auth for well-known endpoints
82
- return;
83
- }
116
+ getToken: async () => {
84
117
  const { value, scope } = await clientCredentials.getAccessToken();
85
118
  if (scope !== allScope) {
86
119
  console.warn(`The scope "${scope}" is not equal to the expected value "${allScope}". This may cause issues with API access. See https://a.logto.io/m2m-mapi to learn more about configuring machine-to-machine access to the Management API.`);
87
120
  }
88
- request.headers.set('Authorization', `Bearer ${value}`);
89
- return request;
121
+ return value;
90
122
  },
91
123
  });
92
124
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/api",
3
- "version": "1.35.0",
3
+ "version": "1.37.0",
4
4
  "description": "Logto API types and clients.",
5
5
  "author": "Silverhand Inc. <contact@silverhand.io>",
6
6
  "homepage": "https://github.com/logto-io/logto#readme",
@@ -11,9 +11,9 @@
11
11
  },
12
12
  "exports": {
13
13
  "./management": {
14
- "default": "./lib/management.js",
15
14
  "types": "./lib/management.d.ts",
16
- "import": "./lib/management.js"
15
+ "import": "./lib/management.js",
16
+ "default": "./lib/management.js"
17
17
  }
18
18
  },
19
19
  "files": [
@@ -37,6 +37,7 @@
37
37
  "@silverhand/ts-config": "6.0.0",
38
38
  "@vitest/coverage-v8": "^3.1.1",
39
39
  "eslint": "^8.57.0",
40
+ "lint-staged": "^15.0.0",
40
41
  "openapi-typescript": "^7.8.0",
41
42
  "prettier": "^3.5.3",
42
43
  "typescript": "^5.5.3",