@adobe/aio-commerce-lib-webhooks 0.1.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.
- package/CHANGELOG.md +15 -0
- package/LICENSE +201 -0
- package/README.md +20 -0
- package/dist/cjs/api/index.cjs +201 -0
- package/dist/cjs/api/index.d.cts +199 -0
- package/dist/cjs/responses/index.cjs +151 -0
- package/dist/cjs/responses/index.d.cts +175 -0
- package/dist/es/api/index.d.mts +199 -0
- package/dist/es/api/index.mjs +166 -0
- package/dist/es/responses/index.d.mts +175 -0
- package/dist/es/responses/index.mjs +144 -0
- package/package.json +95 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 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 { AdobeCommerceHttpClient, ApiClient } from "@adobe/aio-commerce-lib-api";
|
|
16
|
+
import * as v from "valibot";
|
|
17
|
+
|
|
18
|
+
//#region source/api/webhooks/schema.ts
|
|
19
|
+
function nonEmptyString(fieldName) {
|
|
20
|
+
return v.pipe(v.string(`Expected a string for ${fieldName}`), v.nonEmpty(`${fieldName} must not be empty`));
|
|
21
|
+
}
|
|
22
|
+
function optionalString(fieldName) {
|
|
23
|
+
return v.optional(v.string(`Expected a string for ${fieldName}`));
|
|
24
|
+
}
|
|
25
|
+
/** Schema for a webhook field mapping ({ name, source? }). */
|
|
26
|
+
const WebhookFieldSchema = v.object({
|
|
27
|
+
name: nonEmptyString("field name"),
|
|
28
|
+
source: optionalString("field source")
|
|
29
|
+
});
|
|
30
|
+
/** Schema for a conditional webhook rule ({ field, operator, value }). */
|
|
31
|
+
const WebhookRuleSchema = v.object({
|
|
32
|
+
field: nonEmptyString("rule field"),
|
|
33
|
+
operator: nonEmptyString("rule operator"),
|
|
34
|
+
value: v.string("Expected a string for rule value")
|
|
35
|
+
});
|
|
36
|
+
/** Schema for a custom HTTP header ({ name, value }). */
|
|
37
|
+
const WebhookHeaderSchema = v.object({
|
|
38
|
+
name: nonEmptyString("header name"),
|
|
39
|
+
value: nonEmptyString("header value")
|
|
40
|
+
});
|
|
41
|
+
/** Schema for Developer Console OAuth credentials. */
|
|
42
|
+
const DeveloperConsoleOAuthSchema = v.object({
|
|
43
|
+
client_id: nonEmptyString("client_id"),
|
|
44
|
+
client_secret: nonEmptyString("client_secret"),
|
|
45
|
+
org_id: nonEmptyString("org_id"),
|
|
46
|
+
environment: optionalString("environment")
|
|
47
|
+
});
|
|
48
|
+
/**
|
|
49
|
+
* Schema for the webhook payload sent to POST /webhooks/subscribe.
|
|
50
|
+
* Matches the `webhook` property of the request body.
|
|
51
|
+
* Required fields per the Commerce API: webhook_method, webhook_type, batch_name, hook_name, url.
|
|
52
|
+
*/
|
|
53
|
+
const WebhookSubscribeParamsSchema = v.object({
|
|
54
|
+
webhook_method: nonEmptyString("webhook_method"),
|
|
55
|
+
webhook_type: nonEmptyString("webhook_type"),
|
|
56
|
+
batch_name: nonEmptyString("batch_name"),
|
|
57
|
+
batch_order: v.optional(v.number()),
|
|
58
|
+
hook_name: nonEmptyString("hook_name"),
|
|
59
|
+
url: nonEmptyString("url"),
|
|
60
|
+
priority: v.optional(v.number()),
|
|
61
|
+
required: v.optional(v.boolean()),
|
|
62
|
+
soft_timeout: v.optional(v.number()),
|
|
63
|
+
timeout: v.optional(v.number()),
|
|
64
|
+
method: optionalString("method"),
|
|
65
|
+
fallback_error_message: optionalString("fallback_error_message"),
|
|
66
|
+
ttl: v.optional(v.number()),
|
|
67
|
+
fields: v.optional(v.array(WebhookFieldSchema, "Expected an array of field objects")),
|
|
68
|
+
rules: v.optional(v.array(WebhookRuleSchema, "Expected an array of rule objects")),
|
|
69
|
+
headers: v.optional(v.array(WebhookHeaderSchema, "Expected an array of header objects")),
|
|
70
|
+
developer_console_oauth: v.optional(DeveloperConsoleOAuthSchema)
|
|
71
|
+
});
|
|
72
|
+
/**
|
|
73
|
+
* Schema for the parameters sent to POST /webhooks/unsubscribe.
|
|
74
|
+
* Required: webhook_method, webhook_type, batch_name, hook_name.
|
|
75
|
+
*/
|
|
76
|
+
const WebhookUnsubscribeParamsSchema = v.object({
|
|
77
|
+
webhook_method: nonEmptyString("webhook_method"),
|
|
78
|
+
webhook_type: nonEmptyString("webhook_type"),
|
|
79
|
+
batch_name: nonEmptyString("batch_name"),
|
|
80
|
+
hook_name: nonEmptyString("hook_name")
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region source/api/webhooks/endpoints.ts
|
|
85
|
+
/**
|
|
86
|
+
* Returns a list of all subscribed webhooks in the Commerce instance.
|
|
87
|
+
* @see https://developer.adobe.com/commerce/extensibility/webhooks/api/#get-a-list-of-all-subscribed-webhooks
|
|
88
|
+
*
|
|
89
|
+
* @param httpClient - The {@link AdobeCommerceHttpClient} to use to make the request.
|
|
90
|
+
* @param fetchOptions - The {@link Options} to use to make the request.
|
|
91
|
+
*/
|
|
92
|
+
function getWebhookList(httpClient, fetchOptions) {
|
|
93
|
+
return httpClient.get("webhooks/list", fetchOptions).json();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Subscribes a webhook in the Commerce instance.
|
|
97
|
+
* @see https://developer.adobe.com/commerce/extensibility/webhooks/api/#subscribe-a-webhook
|
|
98
|
+
*
|
|
99
|
+
* @param httpClient - The {@link AdobeCommerceHttpClient} to use to make the request.
|
|
100
|
+
* @param params - The webhook payload (webhook_method, webhook_type, batch_name, hook_name, url, etc.).
|
|
101
|
+
* @param fetchOptions - The {@link Options} to use to make the request.
|
|
102
|
+
*
|
|
103
|
+
* @throws A {@link CommerceSdkValidationError} If the parameters are in the wrong format.
|
|
104
|
+
* @throws An {@link HTTPError} If the status code is not 2XX.
|
|
105
|
+
*/
|
|
106
|
+
function subscribeWebhook(httpClient, params, fetchOptions) {
|
|
107
|
+
const validatedParams = v.parse(WebhookSubscribeParamsSchema, params);
|
|
108
|
+
return httpClient.post("webhooks/subscribe", {
|
|
109
|
+
...fetchOptions,
|
|
110
|
+
json: { webhook: validatedParams }
|
|
111
|
+
}).json();
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Unsubscribes a webhook from the Commerce instance.
|
|
115
|
+
* @see https://developer.adobe.com/commerce/extensibility/webhooks/api/#unsubscribe-a-webhook
|
|
116
|
+
*
|
|
117
|
+
* @param httpClient - The {@link AdobeCommerceHttpClient} to use to make the request.
|
|
118
|
+
* @param params - The webhook identifiers (webhook_method, webhook_type, batch_name, hook_name).
|
|
119
|
+
* @param fetchOptions - The {@link Options} to use to make the request.
|
|
120
|
+
*
|
|
121
|
+
* @throws A {@link CommerceSdkValidationError} If the parameters are in the wrong format.
|
|
122
|
+
* @throws An {@link HTTPError} If the status code is not 2XX.
|
|
123
|
+
*/
|
|
124
|
+
function unsubscribeWebhook(httpClient, params, fetchOptions) {
|
|
125
|
+
const validatedParams = v.parse(WebhookUnsubscribeParamsSchema, params);
|
|
126
|
+
return httpClient.post("webhooks/unsubscribe", {
|
|
127
|
+
...fetchOptions,
|
|
128
|
+
json: { webhook: validatedParams }
|
|
129
|
+
}).json();
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Returns the list of webhooks supported in Adobe Commerce as a Cloud Service (SaaS only).
|
|
133
|
+
* @see https://developer.adobe.com/commerce/extensibility/webhooks/api/#get-supported-webhooks-for-saas
|
|
134
|
+
*
|
|
135
|
+
* @param httpClient - The {@link AdobeCommerceHttpClient} to use to make the request.
|
|
136
|
+
* @param fetchOptions - The {@link Options} to use to make the request.
|
|
137
|
+
*/
|
|
138
|
+
function getSupportedWebhookList(httpClient, fetchOptions) {
|
|
139
|
+
return httpClient.get("webhooks/supportedList", fetchOptions).json();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region source/lib/api-client.ts
|
|
144
|
+
/**
|
|
145
|
+
* Creates a new API client for the Commerce Webhooks API.
|
|
146
|
+
* @param params - The parameters to build the Commerce HTTP client.
|
|
147
|
+
*/
|
|
148
|
+
function createCommerceWebhooksApiClient(params) {
|
|
149
|
+
return ApiClient.create(new AdobeCommerceHttpClient(params), {
|
|
150
|
+
getWebhookList,
|
|
151
|
+
subscribeWebhook,
|
|
152
|
+
unsubscribeWebhook,
|
|
153
|
+
getSupportedWebhookList
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Creates a customized Commerce Webhooks API client with a user-specified set of endpoint functions.
|
|
158
|
+
* @param params - The parameters to build the Commerce HTTP client.
|
|
159
|
+
* @param functions - The API functions to include in the client.
|
|
160
|
+
*/
|
|
161
|
+
function createCustomCommerceWebhooksApiClient(params, functions) {
|
|
162
|
+
return ApiClient.create(new AdobeCommerceHttpClient(params), functions);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
//#endregion
|
|
166
|
+
export { createCommerceWebhooksApiClient, createCustomCommerceWebhooksApiClient, getSupportedWebhookList, getWebhookList, subscribeWebhook, unsubscribeWebhook };
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 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 { SuccessResponse } from "@adobe/aio-commerce-lib-core/responses";
|
|
16
|
+
|
|
17
|
+
//#region source/responses/operations/types.d.ts
|
|
18
|
+
/**
|
|
19
|
+
* Success operation response
|
|
20
|
+
* The process that triggered the original event continues without any changes.
|
|
21
|
+
*/
|
|
22
|
+
type SuccessOperation = {
|
|
23
|
+
op: "success";
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Exception operation response
|
|
27
|
+
* Causes Commerce to terminate the process that triggered the original event.
|
|
28
|
+
*/
|
|
29
|
+
type ExceptionOperation = {
|
|
30
|
+
op: "exception"; /** Specifies the exception class. If not set, \Magento\Framework\Exception\LocalizedException will be thrown. */
|
|
31
|
+
class?: string; /** Specifies the exception message. If not set, fallbackErrorMessage or system default will be used. */
|
|
32
|
+
message?: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Add operation response
|
|
36
|
+
* Causes Commerce to add the provided value to the provided path in the triggered event arguments.
|
|
37
|
+
* @template TValue - The type of the value to be added (defaults to unknown)
|
|
38
|
+
*/
|
|
39
|
+
type AddOperation<TValue = unknown> = {
|
|
40
|
+
op: "add"; /** Specifies the path at which the value should be added to the triggered event arguments. */
|
|
41
|
+
path: string; /** Specifies the value to be added. This can be a single value or in an array format. */
|
|
42
|
+
value: TValue; /** Specifies the DataObject class name to create, based on the value and added to the provided path. */
|
|
43
|
+
instance?: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Replace operation response
|
|
47
|
+
* Causes Commerce to replace a value in triggered event arguments for the provided path.
|
|
48
|
+
* @template TValue - The type of the replacement value (defaults to unknown)
|
|
49
|
+
*/
|
|
50
|
+
type ReplaceOperation<TValue = unknown> = {
|
|
51
|
+
op: "replace"; /** Specifies the path at which the value should be replaced with the provided value. */
|
|
52
|
+
path: string; /** Specifies the replacement value. This can be a single value or in an array format. */
|
|
53
|
+
value: TValue; /** Specifies the DataObject class name to create, based on the value and added to the provided path. */
|
|
54
|
+
instance?: string;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Remove operation response
|
|
58
|
+
* Causes Commerce to remove a value or node in triggered event arguments by the provided path.
|
|
59
|
+
*/
|
|
60
|
+
type RemoveOperation = {
|
|
61
|
+
op: "remove"; /** Specifies the path at which the value should be removed. */
|
|
62
|
+
path: string;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Union type representing any webhook operation response
|
|
66
|
+
*
|
|
67
|
+
* @template TValue - The type of the value for operations that carry a value (defaults to unknown)
|
|
68
|
+
*/
|
|
69
|
+
type WebhookOperationResponse<TValue = unknown> = SuccessOperation | ExceptionOperation | AddOperation<TValue> | ReplaceOperation<TValue> | RemoveOperation;
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region source/responses/operations/presets.d.ts
|
|
72
|
+
/**
|
|
73
|
+
* Creates a success operation response
|
|
74
|
+
* The process that triggered the original event continues without any changes.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* return successOperation();
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
declare const successOperation: () => SuccessOperation;
|
|
82
|
+
/**
|
|
83
|
+
* Creates an exception operation response with a message
|
|
84
|
+
* Causes Commerce to terminate the process that triggered the original event.
|
|
85
|
+
*
|
|
86
|
+
* @param message - Exception message
|
|
87
|
+
* @param exceptionClass - Optional exception class name
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* return exceptionOperation("The product cannot be added to the cart because it is out of stock");
|
|
92
|
+
*
|
|
93
|
+
* return exceptionOperation(
|
|
94
|
+
* "Custom error occurred",
|
|
95
|
+
* "Path\\To\\Exception\\Class"
|
|
96
|
+
* );
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare const exceptionOperation: (message: string, exceptionClass?: string) => ExceptionOperation;
|
|
100
|
+
/**
|
|
101
|
+
* Creates an add operation response
|
|
102
|
+
* Causes Commerce to add the provided value to the provided path in the triggered event arguments.
|
|
103
|
+
*
|
|
104
|
+
* @template TValue - The type of the value to be added
|
|
105
|
+
* @param path - Path at which the value should be added
|
|
106
|
+
* @param value - Value to be added
|
|
107
|
+
* @param instance - Optional DataObject class name
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* return addOperation(
|
|
112
|
+
* "result",
|
|
113
|
+
* { data: { amount: "5", carrier_code: "newshipmethod" } },
|
|
114
|
+
* "Magento\\Quote\\Api\\Data\\ShippingMethodInterface"
|
|
115
|
+
* );
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare const addOperation: <TValue = unknown>(path: string, value: TValue, instance?: string) => AddOperation<TValue>;
|
|
119
|
+
/**
|
|
120
|
+
* Creates a replace operation response
|
|
121
|
+
* Causes Commerce to replace a value in triggered event arguments for the provided path.
|
|
122
|
+
*
|
|
123
|
+
* @template TValue - The type of the replacement value
|
|
124
|
+
* @param path - Path at which the value should be replaced
|
|
125
|
+
* @param value - Replacement value
|
|
126
|
+
* @param instance - Optional DataObject class name
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* return replaceOperation("result/shipping_methods/shipping_method_one/amount", 6);
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
declare const replaceOperation: <TValue = unknown>(path: string, value: TValue, instance?: string) => ReplaceOperation<TValue>;
|
|
134
|
+
/**
|
|
135
|
+
* Creates a remove operation response
|
|
136
|
+
* Causes Commerce to remove a value or node in triggered event arguments by the provided path.
|
|
137
|
+
*
|
|
138
|
+
* @param path - Path at which the value should be removed
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* return removeOperation("result/key2");
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
declare const removeOperation: (path: string) => RemoveOperation;
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region source/responses/presets.d.ts
|
|
148
|
+
/**
|
|
149
|
+
* Creates an HTTP 200 OK response with webhook operation(s)
|
|
150
|
+
* Webhook-optimized version of ok() that automatically wraps operations in the response body.
|
|
151
|
+
*
|
|
152
|
+
* This function shadows the core library's ok() to provide a cleaner API for webhook actions.
|
|
153
|
+
* Instead of `ok({ body: operation })`, you can simply use `ok(operation)`.
|
|
154
|
+
*
|
|
155
|
+
* @template TValue - The type of the value for add/replace operations (defaults to unknown)
|
|
156
|
+
* @param operations - Single webhook operation or array of operations
|
|
157
|
+
* @returns Success response with operations in body
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* import { ok, successOperation } from "@adobe/aio-commerce-lib-webhooks/responses";
|
|
162
|
+
*
|
|
163
|
+
* // Single operation
|
|
164
|
+
* return ok(successOperation());
|
|
165
|
+
*
|
|
166
|
+
* // Array of operations
|
|
167
|
+
* return ok([
|
|
168
|
+
* addOperation("result", data),
|
|
169
|
+
* removeOperation("result/old_field")
|
|
170
|
+
* ]);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
declare function ok<TValue = unknown>(operations: WebhookOperationResponse<TValue> | WebhookOperationResponse[]): SuccessResponse;
|
|
174
|
+
//#endregion
|
|
175
|
+
export { type AddOperation, type ExceptionOperation, type RemoveOperation, type ReplaceOperation, type SuccessOperation, type WebhookOperationResponse, addOperation, exceptionOperation, ok, removeOperation, replaceOperation, successOperation };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
*
|
|
4
|
+
* Copyright 2026 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 { ok as ok$1 } from "@adobe/aio-commerce-lib-core/responses";
|
|
16
|
+
|
|
17
|
+
//#region source/responses/operations/presets.ts
|
|
18
|
+
/**
|
|
19
|
+
* Creates a success operation response
|
|
20
|
+
* The process that triggered the original event continues without any changes.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* return successOperation();
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
const successOperation = () => ({ op: "success" });
|
|
28
|
+
/**
|
|
29
|
+
* Creates an exception operation response with a message
|
|
30
|
+
* Causes Commerce to terminate the process that triggered the original event.
|
|
31
|
+
*
|
|
32
|
+
* @param message - Exception message
|
|
33
|
+
* @param exceptionClass - Optional exception class name
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* return exceptionOperation("The product cannot be added to the cart because it is out of stock");
|
|
38
|
+
*
|
|
39
|
+
* return exceptionOperation(
|
|
40
|
+
* "Custom error occurred",
|
|
41
|
+
* "Path\\To\\Exception\\Class"
|
|
42
|
+
* );
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
const exceptionOperation = (message, exceptionClass) => {
|
|
46
|
+
return {
|
|
47
|
+
op: "exception",
|
|
48
|
+
...message && { message },
|
|
49
|
+
...exceptionClass && { class: exceptionClass }
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Creates an add operation response
|
|
54
|
+
* Causes Commerce to add the provided value to the provided path in the triggered event arguments.
|
|
55
|
+
*
|
|
56
|
+
* @template TValue - The type of the value to be added
|
|
57
|
+
* @param path - Path at which the value should be added
|
|
58
|
+
* @param value - Value to be added
|
|
59
|
+
* @param instance - Optional DataObject class name
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* return addOperation(
|
|
64
|
+
* "result",
|
|
65
|
+
* { data: { amount: "5", carrier_code: "newshipmethod" } },
|
|
66
|
+
* "Magento\\Quote\\Api\\Data\\ShippingMethodInterface"
|
|
67
|
+
* );
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
const addOperation = (path, value, instance) => ({
|
|
71
|
+
op: "add",
|
|
72
|
+
path,
|
|
73
|
+
value,
|
|
74
|
+
...instance && { instance }
|
|
75
|
+
});
|
|
76
|
+
/**
|
|
77
|
+
* Creates a replace operation response
|
|
78
|
+
* Causes Commerce to replace a value in triggered event arguments for the provided path.
|
|
79
|
+
*
|
|
80
|
+
* @template TValue - The type of the replacement value
|
|
81
|
+
* @param path - Path at which the value should be replaced
|
|
82
|
+
* @param value - Replacement value
|
|
83
|
+
* @param instance - Optional DataObject class name
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* return replaceOperation("result/shipping_methods/shipping_method_one/amount", 6);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
const replaceOperation = (path, value, instance) => ({
|
|
91
|
+
op: "replace",
|
|
92
|
+
path,
|
|
93
|
+
value,
|
|
94
|
+
...instance && { instance }
|
|
95
|
+
});
|
|
96
|
+
/**
|
|
97
|
+
* Creates a remove operation response
|
|
98
|
+
* Causes Commerce to remove a value or node in triggered event arguments by the provided path.
|
|
99
|
+
*
|
|
100
|
+
* @param path - Path at which the value should be removed
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* return removeOperation("result/key2");
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
const removeOperation = (path) => ({
|
|
108
|
+
op: "remove",
|
|
109
|
+
path
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region source/responses/presets.ts
|
|
114
|
+
/**
|
|
115
|
+
* Creates an HTTP 200 OK response with webhook operation(s)
|
|
116
|
+
* Webhook-optimized version of ok() that automatically wraps operations in the response body.
|
|
117
|
+
*
|
|
118
|
+
* This function shadows the core library's ok() to provide a cleaner API for webhook actions.
|
|
119
|
+
* Instead of `ok({ body: operation })`, you can simply use `ok(operation)`.
|
|
120
|
+
*
|
|
121
|
+
* @template TValue - The type of the value for add/replace operations (defaults to unknown)
|
|
122
|
+
* @param operations - Single webhook operation or array of operations
|
|
123
|
+
* @returns Success response with operations in body
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* import { ok, successOperation } from "@adobe/aio-commerce-lib-webhooks/responses";
|
|
128
|
+
*
|
|
129
|
+
* // Single operation
|
|
130
|
+
* return ok(successOperation());
|
|
131
|
+
*
|
|
132
|
+
* // Array of operations
|
|
133
|
+
* return ok([
|
|
134
|
+
* addOperation("result", data),
|
|
135
|
+
* removeOperation("result/old_field")
|
|
136
|
+
* ]);
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
function ok(operations) {
|
|
140
|
+
return ok$1({ body: operations });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
export { addOperation, exceptionOperation, ok, removeOperation, replaceOperation, successOperation };
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@adobe/aio-commerce-lib-webhooks",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"author": "Adobe Inc.",
|
|
5
|
+
"version": "0.1.0",
|
|
6
|
+
"private": false,
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20 <=24"
|
|
9
|
+
},
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"description": "A library to interact with the Adobe Commerce Webhooks API",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"aio",
|
|
14
|
+
"commerce",
|
|
15
|
+
"adobe-commerce",
|
|
16
|
+
"adobe-commerce-webhooks",
|
|
17
|
+
"aio-commerce-sdk"
|
|
18
|
+
],
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/adobe/aio-commerce-sdk/issues"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/adobe/aio-commerce-sdk.git",
|
|
25
|
+
"directory": "packages/aio-commerce-lib-webhooks"
|
|
26
|
+
},
|
|
27
|
+
"exports": {
|
|
28
|
+
"./api": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/es/api/index.d.mts",
|
|
31
|
+
"default": "./dist/es/api/index.mjs"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/cjs/api/index.d.cts",
|
|
35
|
+
"default": "./dist/cjs/api/index.cjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"./responses": {
|
|
39
|
+
"import": {
|
|
40
|
+
"types": "./dist/es/responses/index.d.mts",
|
|
41
|
+
"default": "./dist/es/responses/index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"require": {
|
|
44
|
+
"types": "./dist/cjs/responses/index.d.cts",
|
|
45
|
+
"default": "./dist/cjs/responses/index.cjs"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"./package.json": "./package.json"
|
|
49
|
+
},
|
|
50
|
+
"imports": {
|
|
51
|
+
"#*": "./source/*.ts",
|
|
52
|
+
"#test/*": "./test/*.ts"
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"dist",
|
|
56
|
+
"package.json",
|
|
57
|
+
"CHANGELOG.md",
|
|
58
|
+
"README.md"
|
|
59
|
+
],
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"ky": "^1.9.0",
|
|
62
|
+
"valibot": "^1.1.0",
|
|
63
|
+
"@adobe/aio-commerce-lib-api": "1.0.1",
|
|
64
|
+
"@adobe/aio-commerce-lib-core": "1.0.0"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"typescript": "^5.9.3",
|
|
68
|
+
"msw": "^2.11.1",
|
|
69
|
+
"@aio-commerce-sdk/config-tsdown": "1.0.1",
|
|
70
|
+
"@aio-commerce-sdk/config-typescript": "1.0.0",
|
|
71
|
+
"@aio-commerce-sdk/config-vitest": "1.0.0",
|
|
72
|
+
"@aio-commerce-sdk/scripting-utils": "0.3.0",
|
|
73
|
+
"@aio-commerce-sdk/scripts": "0.1.0"
|
|
74
|
+
},
|
|
75
|
+
"sideEffects": false,
|
|
76
|
+
"scripts": {
|
|
77
|
+
"build": "tsdown",
|
|
78
|
+
"pack": "pnpm pack",
|
|
79
|
+
"publint": "publint",
|
|
80
|
+
"docs": "typedoc && prettier --write '**/*.md'",
|
|
81
|
+
"assist": "biome check --formatter-enabled=false --linter-enabled=false --assist-enabled=true --no-errors-on-unmatched",
|
|
82
|
+
"assist:apply": "biome check --write --formatter-enabled=false --linter-enabled=false --assist-enabled=true --no-errors-on-unmatched",
|
|
83
|
+
"check:ci": "biome ci --formatter-enabled=true --linter-enabled=true --assist-enabled=true --no-errors-on-unmatched",
|
|
84
|
+
"code:fix": "pnpm run lint:fix && pnpm run assist:apply && pnpm run format && pnpm run format:markdown",
|
|
85
|
+
"format": "biome format --write --no-errors-on-unmatched",
|
|
86
|
+
"format:markdown": "prettier --no-error-on-unmatched-pattern --write '**/*.md' \"!**/{CODE_OF_CONDUCT.md,COPYRIGHT,LICENSE,SECURITY.md,CONTRIBUTING.md}\"",
|
|
87
|
+
"format:check": "biome format --no-errors-on-unmatched",
|
|
88
|
+
"lint": "biome lint --no-errors-on-unmatched",
|
|
89
|
+
"lint:fix": "biome lint --write --no-errors-on-unmatched",
|
|
90
|
+
"typecheck": "tsc --noEmit && echo '✅ No type errors found.'",
|
|
91
|
+
"test": "vitest run --coverage",
|
|
92
|
+
"test:ui": "vitest --ui --coverage",
|
|
93
|
+
"test:watch": "vitest --watch --coverage"
|
|
94
|
+
}
|
|
95
|
+
}
|