@adobe/aio-commerce-lib-auth 0.2.0 → 0.3.1
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.
- package/CHANGELOG.md +21 -0
- package/README.md +2 -107
- package/dist/cjs/index.cjs +68 -197
- package/dist/cjs/index.d.cts +196 -57
- package/dist/es/index.d.ts +196 -57
- package/dist/es/index.js +64 -169
- package/package.json +27 -17
- package/.turbo/turbo-build.log +0 -18
- package/source/index.ts +0 -35
- package/source/lib/ims-auth/provider.ts +0 -160
- package/source/lib/ims-auth/schema.ts +0 -87
- package/source/lib/integration-auth/provider.ts +0 -145
- package/source/lib/integration-auth/schema.ts +0 -87
- package/test/ims-auth.test.ts +0 -147
- package/test/integration-auth.test.ts +0 -124
- package/tsconfig.json +0 -15
- package/tsdown.config.ts +0 -7
- package/vitest.config.ts +0 -16
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,88 +1,227 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as valibot21 from "valibot";
|
|
16
|
+
import { InferOutput } from "valibot";
|
|
6
17
|
|
|
7
18
|
//#region source/lib/ims-auth/schema.d.ts
|
|
19
|
+
/** The environments accepted by the IMS auth service. */
|
|
8
20
|
declare const IMS_AUTH_ENV: {
|
|
9
21
|
readonly PROD: "prod";
|
|
10
22
|
readonly STAGE: "stage";
|
|
11
23
|
};
|
|
12
|
-
|
|
24
|
+
/** Validation schema for IMS auth environment values. */
|
|
25
|
+
declare const ImsAuthEnvSchema: valibot21.EnumSchema<{
|
|
13
26
|
readonly PROD: "prod";
|
|
14
27
|
readonly STAGE: "stage";
|
|
15
28
|
}, undefined>;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
readonly
|
|
19
|
-
readonly
|
|
20
|
-
readonly
|
|
21
|
-
readonly
|
|
22
|
-
readonly
|
|
29
|
+
/** Defines the schema to validate the necessary parameters for the IMS auth service. */
|
|
30
|
+
declare const ImsAuthParamsSchema: valibot21.ObjectSchema<{
|
|
31
|
+
readonly clientId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
|
|
32
|
+
readonly clientSecrets: valibot21.SchemaWithPipe<readonly [valibot21.SchemaWithPipe<readonly [valibot21.ArraySchema<valibot21.StringSchema<undefined>, `Expected a stringified JSON array value for the IMS auth parameter ${string}`>]>, valibot21.MinLengthAction<string[], 1, "Expected at least one client secret for IMS auth">]>;
|
|
33
|
+
readonly technicalAccountId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
|
|
34
|
+
readonly technicalAccountEmail: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<"Expected a string value for the IMS auth parameter technicalAccountEmail">, valibot21.EmailAction<string, "Expected a valid email format for technicalAccountEmail">]>;
|
|
35
|
+
readonly imsOrgId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
|
|
36
|
+
readonly environment: valibot21.SchemaWithPipe<readonly [valibot21.OptionalSchema<valibot21.EnumSchema<{
|
|
23
37
|
readonly PROD: "prod";
|
|
24
38
|
readonly STAGE: "stage";
|
|
25
39
|
}, undefined>, "prod">]>;
|
|
26
|
-
readonly
|
|
27
|
-
readonly
|
|
40
|
+
readonly context: valibot21.SchemaWithPipe<readonly [valibot21.OptionalSchema<valibot21.StringSchema<undefined>, "aio-commerce-sdk-creds">]>;
|
|
41
|
+
readonly scopes: valibot21.SchemaWithPipe<readonly [valibot21.SchemaWithPipe<readonly [valibot21.ArraySchema<valibot21.StringSchema<undefined>, `Expected a stringified JSON array value for the IMS auth parameter ${string}`>]>, valibot21.MinLengthAction<string[], 1, "Expected at least one scope for IMS auth">]>;
|
|
28
42
|
}, undefined>;
|
|
43
|
+
/** Defines the parameters for the IMS auth service. */
|
|
29
44
|
type ImsAuthParams = InferOutput<typeof ImsAuthParamsSchema>;
|
|
45
|
+
/** Defines the environments accepted by the IMS auth service. */
|
|
30
46
|
type ImsAuthEnv = InferOutput<typeof ImsAuthEnvSchema>;
|
|
31
47
|
//#endregion
|
|
32
48
|
//#region source/lib/ims-auth/provider.d.ts
|
|
33
|
-
|
|
49
|
+
/** Defines the header keys used for IMS authentication. */
|
|
34
50
|
type ImsAuthHeader = "Authorization" | "x-api-key";
|
|
51
|
+
/** Defines the headers required for IMS authentication. */
|
|
35
52
|
type ImsAuthHeaders = Record<ImsAuthHeader, string>;
|
|
36
|
-
|
|
37
|
-
type ImsAuthError<TError = unknown> = ErrorType<"ImsAuthError", {
|
|
38
|
-
message: string;
|
|
39
|
-
error: TError;
|
|
40
|
-
}>;
|
|
41
|
-
interface ImsAuthConfig {
|
|
42
|
-
clientId: string;
|
|
43
|
-
clientSecrets: string[];
|
|
44
|
-
technicalAccountId: string;
|
|
45
|
-
technicalAccountEmail: string;
|
|
46
|
-
imsOrgId: string;
|
|
47
|
-
scopes: string[];
|
|
48
|
-
environment: ImsAuthEnv;
|
|
49
|
-
context: string;
|
|
50
|
-
}
|
|
53
|
+
/** Defines an authentication provider for Adobe IMS. */
|
|
51
54
|
interface ImsAuthProvider {
|
|
52
|
-
getAccessToken: () => Promise<
|
|
53
|
-
getHeaders: () => Promise<
|
|
55
|
+
getAccessToken: () => Promise<string>;
|
|
56
|
+
getHeaders: () => Promise<ImsAuthHeaders>;
|
|
54
57
|
}
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Asserts the provided configuration for an {@link ImsAuthProvider}.
|
|
60
|
+
* @param config The configuration to validate.
|
|
61
|
+
* @throws {CommerceSdkValidationError} If the configuration is invalid.
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const config = {
|
|
65
|
+
* clientId: "your-client-id",
|
|
66
|
+
* clientSecrets: ["your-client-secret"],
|
|
67
|
+
* technicalAccountId: "your-technical-account-id",
|
|
68
|
+
* technicalAccountEmail: "your-account@example.com",
|
|
69
|
+
* imsOrgId: "your-ims-org-id@AdobeOrg",
|
|
70
|
+
* scopes: ["AdobeID", "openid"],
|
|
71
|
+
* environment: "prod", // or "stage"
|
|
72
|
+
* context: "my-app-context"
|
|
73
|
+
* };
|
|
74
|
+
*
|
|
75
|
+
* // This will validate the config and throw if invalid
|
|
76
|
+
* assertImsAuthParams(config);
|
|
77
|
+
*```
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* // Example of a failing assert:
|
|
81
|
+
* try {
|
|
82
|
+
* assertImsAuthParams({
|
|
83
|
+
* clientId: "valid-client-id",
|
|
84
|
+
* // Missing required fields like clientSecrets, technicalAccountId, etc.
|
|
85
|
+
* });
|
|
86
|
+
* } catch (error) {
|
|
87
|
+
* console.error(error.message); // "Invalid ImsAuthProvider configuration"
|
|
88
|
+
* console.error(error.issues); // Array of validation issues
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare function assertImsAuthParams(config: Record<PropertyKey, unknown>): asserts config is ImsAuthParams;
|
|
93
|
+
/**
|
|
94
|
+
* Creates an {@link ImsAuthProvider} based on the provided configuration.
|
|
95
|
+
* @param authParams An {@link ImsAuthParams} parameter that contains the configuration for the {@link ImsAuthProvider}.
|
|
96
|
+
* @returns An {@link ImsAuthProvider} instance that can be used to get access token and auth headers.
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const config = {
|
|
100
|
+
* clientId: "your-client-id",
|
|
101
|
+
* clientSecrets: ["your-client-secret"],
|
|
102
|
+
* technicalAccountId: "your-technical-account-id",
|
|
103
|
+
* technicalAccountEmail: "your-account@example.com",
|
|
104
|
+
* imsOrgId: "your-ims-org-id@AdobeOrg",
|
|
105
|
+
* scopes: ["AdobeID", "openid"],
|
|
106
|
+
* environment: "prod",
|
|
107
|
+
* context: "my-app-context"
|
|
108
|
+
* };
|
|
109
|
+
*
|
|
110
|
+
* const authProvider = getImsAuthProvider(config);
|
|
111
|
+
*
|
|
112
|
+
* // Get access token
|
|
113
|
+
* const token = await authProvider.getAccessToken();
|
|
114
|
+
* console.log(token); // "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9..."
|
|
115
|
+
*
|
|
116
|
+
* // Get headers for API requests
|
|
117
|
+
* const headers = await authProvider.getHeaders();
|
|
118
|
+
* console.log(headers);
|
|
119
|
+
* // {
|
|
120
|
+
* // Authorization: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...",
|
|
121
|
+
* // "x-api-key": "your-client-id"
|
|
122
|
+
* // }
|
|
123
|
+
*
|
|
124
|
+
* // Use headers in API calls
|
|
125
|
+
* const response = await fetch('https://api.adobe.io/some-endpoint', {
|
|
126
|
+
* headers: await authProvider.getHeaders()
|
|
127
|
+
* });
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
declare function getImsAuthProvider(authParams: ImsAuthParams): {
|
|
131
|
+
getAccessToken: () => Promise<string>;
|
|
132
|
+
getHeaders: () => Promise<{
|
|
133
|
+
Authorization: string;
|
|
134
|
+
"x-api-key": string;
|
|
135
|
+
}>;
|
|
136
|
+
};
|
|
57
137
|
//#endregion
|
|
58
138
|
//#region source/lib/integration-auth/schema.d.ts
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
139
|
+
/**
|
|
140
|
+
* The HTTP methods supported by Commerce.
|
|
141
|
+
* This is used to determine which headers to include in the signing of the authorization header.
|
|
142
|
+
*/
|
|
143
|
+
type HttpMethodInput = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
144
|
+
/** Validation schema that accepts either a URL string or URL instance and normalizes to string. */
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The schema for the Commerce Integration parameters.
|
|
148
|
+
* This is used to validate the parameters passed to the Commerce Integration provider.
|
|
149
|
+
*/
|
|
150
|
+
declare const IntegrationAuthParamsSchema: valibot21.NonOptionalSchema<valibot21.ObjectSchema<{
|
|
151
|
+
readonly consumerKey: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
152
|
+
readonly consumerSecret: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
153
|
+
readonly accessToken: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
154
|
+
readonly accessTokenSecret: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
68
155
|
}, undefined>, undefined>;
|
|
69
|
-
|
|
156
|
+
/** Defines the parameters required for Commerce Integration authentication. */
|
|
157
|
+
type IntegrationAuthParams = InferOutput<typeof IntegrationAuthParamsSchema>;
|
|
70
158
|
//#endregion
|
|
71
159
|
//#region source/lib/integration-auth/provider.d.ts
|
|
160
|
+
/** Defines the header key used for Commerce Integration authentication. */
|
|
72
161
|
type IntegrationAuthHeader = "Authorization";
|
|
162
|
+
/** Defines the headers required for Commerce Integration authentication. */
|
|
73
163
|
type IntegrationAuthHeaders = Record<IntegrationAuthHeader, string>;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
accessToken: string;
|
|
78
|
-
accessTokenSecret: string;
|
|
79
|
-
}
|
|
80
|
-
type ValidationIssues = InferIssue<typeof IntegrationAuthParamsSchema>[] | InferIssue<typeof UrlSchema>[];
|
|
81
|
-
type IntegrationAuthError = ValidationErrorType<"IntegrationAuthValidationError", ValidationIssues>;
|
|
164
|
+
/** Represents a URL for Adobe Commerce endpoints, accepting either string or URL object. */
|
|
165
|
+
type AdobeCommerceUrl = string | URL;
|
|
166
|
+
/** Defines an authentication provider for Adobe Commerce integrations. */
|
|
82
167
|
interface IntegrationAuthProvider {
|
|
83
|
-
getHeaders: (method: HttpMethodInput, url:
|
|
168
|
+
getHeaders: (method: HttpMethodInput, url: AdobeCommerceUrl) => IntegrationAuthHeaders;
|
|
84
169
|
}
|
|
85
|
-
|
|
86
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Asserts the provided configuration for an Adobe Commerce {@link IntegrationAuthProvider}.
|
|
172
|
+
* @param config The configuration to validate.
|
|
173
|
+
* @throws {CommerceSdkValidationError} If the configuration is invalid.
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* const config = {
|
|
177
|
+
* consumerKey: "your-consumer-key",
|
|
178
|
+
* consumerSecret: "your-consumer-secret",
|
|
179
|
+
* accessToken: "your-access-token",
|
|
180
|
+
* accessTokenSecret: "your-access-token-secret"
|
|
181
|
+
* };
|
|
182
|
+
*
|
|
183
|
+
* // This will validate the config and throw if invalid
|
|
184
|
+
* assertIntegrationAuthParams(config);
|
|
185
|
+
* ```
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* // Example of a failing assert:
|
|
189
|
+
* try {
|
|
190
|
+
* assertIntegrationAuthParams({
|
|
191
|
+
* consumerKey: "valid-consumer-key",
|
|
192
|
+
* // Missing required fields like consumerSecret, accessToken, accessTokenSecret
|
|
193
|
+
* });
|
|
194
|
+
* } catch (error) {
|
|
195
|
+
* console.error(error.message); // "Invalid IntegrationAuthProvider configuration"
|
|
196
|
+
* console.error(error.issues); // Array of validation issues
|
|
197
|
+
* }
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
declare function assertIntegrationAuthParams(config: Record<PropertyKey, unknown>): asserts config is IntegrationAuthParams;
|
|
201
|
+
/**
|
|
202
|
+
* Creates an {@link IntegrationAuthProvider} based on the provided configuration.
|
|
203
|
+
* @param authParams The configuration for the integration.
|
|
204
|
+
* @returns An {@link IntegrationAuthProvider} instance that can be used to get auth headers.
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const config = {
|
|
208
|
+
* consumerKey: "your-consumer-key",
|
|
209
|
+
* consumerSecret: "your-consumer-secret",
|
|
210
|
+
* accessToken: "your-access-token",
|
|
211
|
+
* accessTokenSecret: "your-access-token-secret"
|
|
212
|
+
* };
|
|
213
|
+
*
|
|
214
|
+
* const authProvider = getIntegrationAuthProvider(config);
|
|
215
|
+
*
|
|
216
|
+
* // Get OAuth headers for a REST API call
|
|
217
|
+
* const headers = authProvider.getHeaders("GET", "https://your-store.com/rest/V1/products");
|
|
218
|
+
* console.log(headers); // { Authorization: "OAuth oauth_consumer_key=..., oauth_signature=..." }
|
|
219
|
+
*
|
|
220
|
+
* // Can also be used with URL objects
|
|
221
|
+
* const url = new URL("https://your-store.com/rest/V1/customers");
|
|
222
|
+
* const postHeaders = authProvider.getHeaders("POST", url);
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
declare function getIntegrationAuthProvider(authParams: IntegrationAuthParams): IntegrationAuthProvider;
|
|
87
226
|
//#endregion
|
|
88
|
-
export { IMS_AUTH_ENV,
|
|
227
|
+
export { IMS_AUTH_ENV, type ImsAuthEnv, type ImsAuthParams, type ImsAuthProvider, type IntegrationAuthParams, type IntegrationAuthProvider, assertImsAuthParams, assertIntegrationAuthParams, getImsAuthProvider, getIntegrationAuthProvider };
|
package/dist/es/index.d.ts
CHANGED
|
@@ -1,88 +1,227 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2025 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import * as valibot21 from "valibot";
|
|
16
|
+
import { InferOutput } from "valibot";
|
|
6
17
|
|
|
7
18
|
//#region source/lib/ims-auth/schema.d.ts
|
|
19
|
+
/** The environments accepted by the IMS auth service. */
|
|
8
20
|
declare const IMS_AUTH_ENV: {
|
|
9
21
|
readonly PROD: "prod";
|
|
10
22
|
readonly STAGE: "stage";
|
|
11
23
|
};
|
|
12
|
-
|
|
24
|
+
/** Validation schema for IMS auth environment values. */
|
|
25
|
+
declare const ImsAuthEnvSchema: valibot21.EnumSchema<{
|
|
13
26
|
readonly PROD: "prod";
|
|
14
27
|
readonly STAGE: "stage";
|
|
15
28
|
}, undefined>;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
readonly
|
|
19
|
-
readonly
|
|
20
|
-
readonly
|
|
21
|
-
readonly
|
|
22
|
-
readonly
|
|
29
|
+
/** Defines the schema to validate the necessary parameters for the IMS auth service. */
|
|
30
|
+
declare const ImsAuthParamsSchema: valibot21.ObjectSchema<{
|
|
31
|
+
readonly clientId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
|
|
32
|
+
readonly clientSecrets: valibot21.SchemaWithPipe<readonly [valibot21.SchemaWithPipe<readonly [valibot21.ArraySchema<valibot21.StringSchema<undefined>, `Expected a stringified JSON array value for the IMS auth parameter ${string}`>]>, valibot21.MinLengthAction<string[], 1, "Expected at least one client secret for IMS auth">]>;
|
|
33
|
+
readonly technicalAccountId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
|
|
34
|
+
readonly technicalAccountEmail: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<"Expected a string value for the IMS auth parameter technicalAccountEmail">, valibot21.EmailAction<string, "Expected a valid email format for technicalAccountEmail">]>;
|
|
35
|
+
readonly imsOrgId: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the IMS auth parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the IMS auth parameter ${string}`>]>;
|
|
36
|
+
readonly environment: valibot21.SchemaWithPipe<readonly [valibot21.OptionalSchema<valibot21.EnumSchema<{
|
|
23
37
|
readonly PROD: "prod";
|
|
24
38
|
readonly STAGE: "stage";
|
|
25
39
|
}, undefined>, "prod">]>;
|
|
26
|
-
readonly
|
|
27
|
-
readonly
|
|
40
|
+
readonly context: valibot21.SchemaWithPipe<readonly [valibot21.OptionalSchema<valibot21.StringSchema<undefined>, "aio-commerce-sdk-creds">]>;
|
|
41
|
+
readonly scopes: valibot21.SchemaWithPipe<readonly [valibot21.SchemaWithPipe<readonly [valibot21.ArraySchema<valibot21.StringSchema<undefined>, `Expected a stringified JSON array value for the IMS auth parameter ${string}`>]>, valibot21.MinLengthAction<string[], 1, "Expected at least one scope for IMS auth">]>;
|
|
28
42
|
}, undefined>;
|
|
43
|
+
/** Defines the parameters for the IMS auth service. */
|
|
29
44
|
type ImsAuthParams = InferOutput<typeof ImsAuthParamsSchema>;
|
|
45
|
+
/** Defines the environments accepted by the IMS auth service. */
|
|
30
46
|
type ImsAuthEnv = InferOutput<typeof ImsAuthEnvSchema>;
|
|
31
47
|
//#endregion
|
|
32
48
|
//#region source/lib/ims-auth/provider.d.ts
|
|
33
|
-
|
|
49
|
+
/** Defines the header keys used for IMS authentication. */
|
|
34
50
|
type ImsAuthHeader = "Authorization" | "x-api-key";
|
|
51
|
+
/** Defines the headers required for IMS authentication. */
|
|
35
52
|
type ImsAuthHeaders = Record<ImsAuthHeader, string>;
|
|
36
|
-
|
|
37
|
-
type ImsAuthError<TError = unknown> = ErrorType<"ImsAuthError", {
|
|
38
|
-
message: string;
|
|
39
|
-
error: TError;
|
|
40
|
-
}>;
|
|
41
|
-
interface ImsAuthConfig {
|
|
42
|
-
clientId: string;
|
|
43
|
-
clientSecrets: string[];
|
|
44
|
-
technicalAccountId: string;
|
|
45
|
-
technicalAccountEmail: string;
|
|
46
|
-
imsOrgId: string;
|
|
47
|
-
scopes: string[];
|
|
48
|
-
environment: ImsAuthEnv;
|
|
49
|
-
context: string;
|
|
50
|
-
}
|
|
53
|
+
/** Defines an authentication provider for Adobe IMS. */
|
|
51
54
|
interface ImsAuthProvider {
|
|
52
|
-
getAccessToken: () => Promise<
|
|
53
|
-
getHeaders: () => Promise<
|
|
55
|
+
getAccessToken: () => Promise<string>;
|
|
56
|
+
getHeaders: () => Promise<ImsAuthHeaders>;
|
|
54
57
|
}
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Asserts the provided configuration for an {@link ImsAuthProvider}.
|
|
60
|
+
* @param config The configuration to validate.
|
|
61
|
+
* @throws {CommerceSdkValidationError} If the configuration is invalid.
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const config = {
|
|
65
|
+
* clientId: "your-client-id",
|
|
66
|
+
* clientSecrets: ["your-client-secret"],
|
|
67
|
+
* technicalAccountId: "your-technical-account-id",
|
|
68
|
+
* technicalAccountEmail: "your-account@example.com",
|
|
69
|
+
* imsOrgId: "your-ims-org-id@AdobeOrg",
|
|
70
|
+
* scopes: ["AdobeID", "openid"],
|
|
71
|
+
* environment: "prod", // or "stage"
|
|
72
|
+
* context: "my-app-context"
|
|
73
|
+
* };
|
|
74
|
+
*
|
|
75
|
+
* // This will validate the config and throw if invalid
|
|
76
|
+
* assertImsAuthParams(config);
|
|
77
|
+
*```
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* // Example of a failing assert:
|
|
81
|
+
* try {
|
|
82
|
+
* assertImsAuthParams({
|
|
83
|
+
* clientId: "valid-client-id",
|
|
84
|
+
* // Missing required fields like clientSecrets, technicalAccountId, etc.
|
|
85
|
+
* });
|
|
86
|
+
* } catch (error) {
|
|
87
|
+
* console.error(error.message); // "Invalid ImsAuthProvider configuration"
|
|
88
|
+
* console.error(error.issues); // Array of validation issues
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
declare function assertImsAuthParams(config: Record<PropertyKey, unknown>): asserts config is ImsAuthParams;
|
|
93
|
+
/**
|
|
94
|
+
* Creates an {@link ImsAuthProvider} based on the provided configuration.
|
|
95
|
+
* @param authParams An {@link ImsAuthParams} parameter that contains the configuration for the {@link ImsAuthProvider}.
|
|
96
|
+
* @returns An {@link ImsAuthProvider} instance that can be used to get access token and auth headers.
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const config = {
|
|
100
|
+
* clientId: "your-client-id",
|
|
101
|
+
* clientSecrets: ["your-client-secret"],
|
|
102
|
+
* technicalAccountId: "your-technical-account-id",
|
|
103
|
+
* technicalAccountEmail: "your-account@example.com",
|
|
104
|
+
* imsOrgId: "your-ims-org-id@AdobeOrg",
|
|
105
|
+
* scopes: ["AdobeID", "openid"],
|
|
106
|
+
* environment: "prod",
|
|
107
|
+
* context: "my-app-context"
|
|
108
|
+
* };
|
|
109
|
+
*
|
|
110
|
+
* const authProvider = getImsAuthProvider(config);
|
|
111
|
+
*
|
|
112
|
+
* // Get access token
|
|
113
|
+
* const token = await authProvider.getAccessToken();
|
|
114
|
+
* console.log(token); // "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9..."
|
|
115
|
+
*
|
|
116
|
+
* // Get headers for API requests
|
|
117
|
+
* const headers = await authProvider.getHeaders();
|
|
118
|
+
* console.log(headers);
|
|
119
|
+
* // {
|
|
120
|
+
* // Authorization: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...",
|
|
121
|
+
* // "x-api-key": "your-client-id"
|
|
122
|
+
* // }
|
|
123
|
+
*
|
|
124
|
+
* // Use headers in API calls
|
|
125
|
+
* const response = await fetch('https://api.adobe.io/some-endpoint', {
|
|
126
|
+
* headers: await authProvider.getHeaders()
|
|
127
|
+
* });
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
declare function getImsAuthProvider(authParams: ImsAuthParams): {
|
|
131
|
+
getAccessToken: () => Promise<string>;
|
|
132
|
+
getHeaders: () => Promise<{
|
|
133
|
+
Authorization: string;
|
|
134
|
+
"x-api-key": string;
|
|
135
|
+
}>;
|
|
136
|
+
};
|
|
57
137
|
//#endregion
|
|
58
138
|
//#region source/lib/integration-auth/schema.d.ts
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
139
|
+
/**
|
|
140
|
+
* The HTTP methods supported by Commerce.
|
|
141
|
+
* This is used to determine which headers to include in the signing of the authorization header.
|
|
142
|
+
*/
|
|
143
|
+
type HttpMethodInput = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
144
|
+
/** Validation schema that accepts either a URL string or URL instance and normalizes to string. */
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The schema for the Commerce Integration parameters.
|
|
148
|
+
* This is used to validate the parameters passed to the Commerce Integration provider.
|
|
149
|
+
*/
|
|
150
|
+
declare const IntegrationAuthParamsSchema: valibot21.NonOptionalSchema<valibot21.ObjectSchema<{
|
|
151
|
+
readonly consumerKey: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
152
|
+
readonly consumerSecret: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
153
|
+
readonly accessToken: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
154
|
+
readonly accessTokenSecret: valibot21.SchemaWithPipe<readonly [valibot21.StringSchema<`Expected a string value for the Commerce Integration parameter ${string}`>, valibot21.NonEmptyAction<string, `Expected a non-empty string value for the Commerce Integration parameter ${string}`>]>;
|
|
68
155
|
}, undefined>, undefined>;
|
|
69
|
-
|
|
156
|
+
/** Defines the parameters required for Commerce Integration authentication. */
|
|
157
|
+
type IntegrationAuthParams = InferOutput<typeof IntegrationAuthParamsSchema>;
|
|
70
158
|
//#endregion
|
|
71
159
|
//#region source/lib/integration-auth/provider.d.ts
|
|
160
|
+
/** Defines the header key used for Commerce Integration authentication. */
|
|
72
161
|
type IntegrationAuthHeader = "Authorization";
|
|
162
|
+
/** Defines the headers required for Commerce Integration authentication. */
|
|
73
163
|
type IntegrationAuthHeaders = Record<IntegrationAuthHeader, string>;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
accessToken: string;
|
|
78
|
-
accessTokenSecret: string;
|
|
79
|
-
}
|
|
80
|
-
type ValidationIssues = InferIssue<typeof IntegrationAuthParamsSchema>[] | InferIssue<typeof UrlSchema>[];
|
|
81
|
-
type IntegrationAuthError = ValidationErrorType<"IntegrationAuthValidationError", ValidationIssues>;
|
|
164
|
+
/** Represents a URL for Adobe Commerce endpoints, accepting either string or URL object. */
|
|
165
|
+
type AdobeCommerceUrl = string | URL;
|
|
166
|
+
/** Defines an authentication provider for Adobe Commerce integrations. */
|
|
82
167
|
interface IntegrationAuthProvider {
|
|
83
|
-
getHeaders: (method: HttpMethodInput, url:
|
|
168
|
+
getHeaders: (method: HttpMethodInput, url: AdobeCommerceUrl) => IntegrationAuthHeaders;
|
|
84
169
|
}
|
|
85
|
-
|
|
86
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Asserts the provided configuration for an Adobe Commerce {@link IntegrationAuthProvider}.
|
|
172
|
+
* @param config The configuration to validate.
|
|
173
|
+
* @throws {CommerceSdkValidationError} If the configuration is invalid.
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* const config = {
|
|
177
|
+
* consumerKey: "your-consumer-key",
|
|
178
|
+
* consumerSecret: "your-consumer-secret",
|
|
179
|
+
* accessToken: "your-access-token",
|
|
180
|
+
* accessTokenSecret: "your-access-token-secret"
|
|
181
|
+
* };
|
|
182
|
+
*
|
|
183
|
+
* // This will validate the config and throw if invalid
|
|
184
|
+
* assertIntegrationAuthParams(config);
|
|
185
|
+
* ```
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* // Example of a failing assert:
|
|
189
|
+
* try {
|
|
190
|
+
* assertIntegrationAuthParams({
|
|
191
|
+
* consumerKey: "valid-consumer-key",
|
|
192
|
+
* // Missing required fields like consumerSecret, accessToken, accessTokenSecret
|
|
193
|
+
* });
|
|
194
|
+
* } catch (error) {
|
|
195
|
+
* console.error(error.message); // "Invalid IntegrationAuthProvider configuration"
|
|
196
|
+
* console.error(error.issues); // Array of validation issues
|
|
197
|
+
* }
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
declare function assertIntegrationAuthParams(config: Record<PropertyKey, unknown>): asserts config is IntegrationAuthParams;
|
|
201
|
+
/**
|
|
202
|
+
* Creates an {@link IntegrationAuthProvider} based on the provided configuration.
|
|
203
|
+
* @param authParams The configuration for the integration.
|
|
204
|
+
* @returns An {@link IntegrationAuthProvider} instance that can be used to get auth headers.
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const config = {
|
|
208
|
+
* consumerKey: "your-consumer-key",
|
|
209
|
+
* consumerSecret: "your-consumer-secret",
|
|
210
|
+
* accessToken: "your-access-token",
|
|
211
|
+
* accessTokenSecret: "your-access-token-secret"
|
|
212
|
+
* };
|
|
213
|
+
*
|
|
214
|
+
* const authProvider = getIntegrationAuthProvider(config);
|
|
215
|
+
*
|
|
216
|
+
* // Get OAuth headers for a REST API call
|
|
217
|
+
* const headers = authProvider.getHeaders("GET", "https://your-store.com/rest/V1/products");
|
|
218
|
+
* console.log(headers); // { Authorization: "OAuth oauth_consumer_key=..., oauth_signature=..." }
|
|
219
|
+
*
|
|
220
|
+
* // Can also be used with URL objects
|
|
221
|
+
* const url = new URL("https://your-store.com/rest/V1/customers");
|
|
222
|
+
* const postHeaders = authProvider.getHeaders("POST", url);
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
declare function getIntegrationAuthProvider(authParams: IntegrationAuthParams): IntegrationAuthProvider;
|
|
87
226
|
//#endregion
|
|
88
|
-
export { IMS_AUTH_ENV,
|
|
227
|
+
export { IMS_AUTH_ENV, type ImsAuthEnv, type ImsAuthParams, type ImsAuthProvider, type IntegrationAuthParams, type IntegrationAuthProvider, assertImsAuthParams, assertIntegrationAuthParams, getImsAuthProvider, getIntegrationAuthProvider };
|