@logto/api 0.0.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.
@@ -0,0 +1,5 @@
1
+ /**
2
+ * This file was auto-generated by openapi-typescript.
3
+ * Do not make direct changes to the file.
4
+ */
5
+ export {};
@@ -0,0 +1,103 @@
1
+ import { type Client } from 'openapi-fetch';
2
+ import { ClientCredentials } from './client-credentials.js';
3
+ import { type paths } from './generated-types/management.js';
4
+ /**
5
+ * Options for creating a Management API client.
6
+ */
7
+ export type CreateManagementApiOptions = {
8
+ /**
9
+ * The client ID for the machine-to-machine application in Logto. This application must be
10
+ * granted access to the Management API.
11
+ * @see https://a.logto.io/m2m-mapi for more details on configuring machine-to-machine access.
12
+ */
13
+ clientId: string;
14
+ /**
15
+ * The client secret for the machine-to-machine application in Logto.
16
+ * This should be kept secure and not exposed in client-side code.
17
+ */
18
+ clientSecret: string;
19
+ /**
20
+ * Override the base URL generated from the tenant ID.
21
+ * Useful for testing or custom deployments.
22
+ */
23
+ baseUrl?: string;
24
+ /**
25
+ * Override the API indicator for the management API.
26
+ * Useful for testing or custom deployments.
27
+ */
28
+ apiIndicator?: string;
29
+ };
30
+ /**
31
+ * Returns the base URL for the Management API based on the tenant ID.
32
+ * @param tenantId The tenant ID to construct the base URL.
33
+ * @returns The base URL for the Management API.
34
+ */
35
+ export declare const getBaseUrl: (tenantId: string) => string;
36
+ /**
37
+ * Returns the API indicator for the Management API based on the tenant ID.
38
+ * This will be used as the `resource` parameter when requesting an access token.
39
+ * @param tenantId The tenant ID to construct the API indicator.
40
+ * @returns The API indicator for the Management API.
41
+ */
42
+ export declare const getManagementApiIndicator: (tenantId: string) => string;
43
+ /**
44
+ * The scope used for accessing all endpoints of the Management API.
45
+ * This is used when requesting an access token for the Management API.
46
+ */
47
+ export declare const allScope = "all";
48
+ type ManagementApiReturnType = {
49
+ /**
50
+ * The API client for the Management API.
51
+ *
52
+ * This client is configured to use the provided client credentials
53
+ * and will automatically include the access token in requests.
54
+ */
55
+ apiClient: Client<paths>;
56
+ /**
57
+ * The client credentials instance used for authentication.
58
+ */
59
+ clientCredentials: ClientCredentials;
60
+ };
61
+ /**
62
+ * Creates a Management API client with the specified tenant ID and options.
63
+ *
64
+ * Before using this function, ensure that you have created a machine-to-machine application in
65
+ * Logto and granted it access to the Management API. See the documentation for more details:
66
+ *
67
+ * https://a.logto.io/m2m-mapi
68
+ *
69
+ * This function sets up the API client with the necessary authentication using client credentials.
70
+ * It will automatically handle token retrieval and renewal as needed.
71
+ *
72
+ * @param tenantId The tenant ID for which to create the Management API client. For OSS deployments,
73
+ * you can pass any string as the tenant ID, for example, 'default'.
74
+ * @param options The options for creating the Management API client, including client ID and secret.
75
+ * @returns An object containing the API client and client credentials instance.
76
+ * @example
77
+ * ```ts
78
+ * import { createManagementApi } from '@logto/api/management';
79
+ *
80
+ * // Logto Cloud example
81
+ * const { apiClient, clientCredentials } = createManagementApi('my-tenant-id', {
82
+ * clientId: 'my-client-id',
83
+ * clientSecret: 'my-client-secret',
84
+ * });
85
+ *
86
+ * // Use apiClient to make requests to the Management API
87
+ * const response = await apiClient.GET('/api/users');
88
+ * console.log(response.data);
89
+ * ```
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * // OSS example
94
+ * const { apiClient, clientCredentials } = createManagementApi('default', {
95
+ * clientId: 'my-client-id',
96
+ * clientSecret: 'my-client-secret',
97
+ * baseUrl: 'https://my-oss-logto-instance.com',
98
+ * apiIndicator: 'https://default.logto.app/api',
99
+ * });
100
+ * ```
101
+ */
102
+ export declare function createManagementApi(tenantId: string, options: CreateManagementApiOptions): ManagementApiReturnType;
103
+ export {};
@@ -0,0 +1,96 @@
1
+ import createClient from 'openapi-fetch';
2
+ import { ClientCredentials } from './client-credentials.js';
3
+ /**
4
+ * Returns the base URL for the Management API based on the tenant ID.
5
+ * @param tenantId The tenant ID to construct the base URL.
6
+ * @returns The base URL for the Management API.
7
+ */
8
+ export const getBaseUrl = (tenantId) => `https://${tenantId}.logto.app`;
9
+ /**
10
+ * Returns the API indicator for the Management API based on the tenant ID.
11
+ * This will be used as the `resource` parameter when requesting an access token.
12
+ * @param tenantId The tenant ID to construct the API indicator.
13
+ * @returns The API indicator for the Management API.
14
+ */
15
+ export const getManagementApiIndicator = (tenantId) => `${getBaseUrl(tenantId)}/api`;
16
+ /**
17
+ * The scope used for accessing all endpoints of the Management API.
18
+ * This is used when requesting an access token for the Management API.
19
+ */
20
+ export const allScope = 'all';
21
+ /**
22
+ * Creates a Management API client with the specified tenant ID and options.
23
+ *
24
+ * Before using this function, ensure that you have created a machine-to-machine application in
25
+ * Logto and granted it access to the Management API. See the documentation for more details:
26
+ *
27
+ * https://a.logto.io/m2m-mapi
28
+ *
29
+ * This function sets up the API client with the necessary authentication using client credentials.
30
+ * It will automatically handle token retrieval and renewal as needed.
31
+ *
32
+ * @param tenantId The tenant ID for which to create the Management API client. For OSS deployments,
33
+ * you can pass any string as the tenant ID, for example, 'default'.
34
+ * @param options The options for creating the Management API client, including client ID and secret.
35
+ * @returns An object containing the API client and client credentials instance.
36
+ * @example
37
+ * ```ts
38
+ * import { createManagementApi } from '@logto/api/management';
39
+ *
40
+ * // Logto Cloud example
41
+ * const { apiClient, clientCredentials } = createManagementApi('my-tenant-id', {
42
+ * clientId: 'my-client-id',
43
+ * clientSecret: 'my-client-secret',
44
+ * });
45
+ *
46
+ * // Use apiClient to make requests to the Management API
47
+ * const response = await apiClient.GET('/api/users');
48
+ * console.log(response.data);
49
+ * ```
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * // OSS example
54
+ * const { apiClient, clientCredentials } = createManagementApi('default', {
55
+ * clientId: 'my-client-id',
56
+ * clientSecret: 'my-client-secret',
57
+ * baseUrl: 'https://my-oss-logto-instance.com',
58
+ * apiIndicator: 'https://default.logto.app/api',
59
+ * });
60
+ * ```
61
+ */
62
+ export function createManagementApi(tenantId, options) {
63
+ const { clientId, clientSecret } = options;
64
+ const baseUrl = options.baseUrl ?? getBaseUrl(tenantId);
65
+ const apiIndicator = options.apiIndicator ?? getManagementApiIndicator(tenantId);
66
+ const clientCredentials = new ClientCredentials({
67
+ clientId,
68
+ clientSecret,
69
+ tokenEndpoint: `${baseUrl}/oidc/token`,
70
+ tokenParams: {
71
+ resource: apiIndicator,
72
+ scope: allScope,
73
+ },
74
+ });
75
+ const apiClient = createClient({
76
+ 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
+ }
84
+ const { value, scope } = await clientCredentials.getAccessToken();
85
+ if (scope !== allScope) {
86
+ 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
+ }
88
+ request.headers.set('Authorization', `Bearer ${value}`);
89
+ return request;
90
+ },
91
+ });
92
+ return {
93
+ apiClient,
94
+ clientCredentials,
95
+ };
96
+ }
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@logto/api",
3
+ "version": "0.0.0",
4
+ "description": "Logto API types and clients.",
5
+ "author": "Silverhand Inc. <contact@silverhand.io>",
6
+ "homepage": "https://github.com/logto-io/logto#readme",
7
+ "license": "MPL-2.0",
8
+ "type": "module",
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "exports": {
13
+ "./management": {
14
+ "default": "./lib/management.js",
15
+ "types": "./lib/management.d.ts",
16
+ "import": "./lib/management.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "lib"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/logto-io/logto.git"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/logto-io/logto/issues"
28
+ },
29
+ "engines": {
30
+ "node": "^22.14.0 || ^24.0.0"
31
+ },
32
+ "dependencies": {
33
+ "openapi-fetch": "^0.14.0"
34
+ },
35
+ "devDependencies": {
36
+ "@silverhand/eslint-config": "6.0.1",
37
+ "@silverhand/ts-config": "6.0.0",
38
+ "@vitest/coverage-v8": "^3.1.1",
39
+ "eslint": "^8.57.0",
40
+ "openapi-typescript": "^7.8.0",
41
+ "prettier": "^3.5.3",
42
+ "typescript": "^5.5.3",
43
+ "vitest": "^3.1.1"
44
+ },
45
+ "eslintConfig": {
46
+ "ignorePatterns": [
47
+ "src/generated-types/*"
48
+ ],
49
+ "extends": "@silverhand"
50
+ },
51
+ "prettier": "@silverhand/eslint-config/.prettierrc",
52
+ "scripts": {
53
+ "precommit": "lint-staged",
54
+ "generate-types": "./generate-types.sh",
55
+ "build": "./check-types.sh && rm -rf lib/ && tsc -p tsconfig.build.json",
56
+ "lint": "eslint --ext .ts src",
57
+ "lint:report": "pnpm lint --format json --output-file report.json",
58
+ "test": "vitest src",
59
+ "test:ci": "pnpm run test --silent --coverage"
60
+ }
61
+ }