@gradientedge/commercetools-utils 4.11.1 → 4.12.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.
Files changed (139) hide show
  1. package/dist/ge-commercetools-utils-browser.cjs.js +2 -0
  2. package/dist/ge-commercetools-utils-browser.cjs.js.map +1 -0
  3. package/dist/ge-commercetools-utils-browser.esm.js +2 -0
  4. package/dist/ge-commercetools-utils-browser.esm.js.map +1 -0
  5. package/dist/ge-commercetools-utils-node.cjs.js +2 -0
  6. package/dist/ge-commercetools-utils-node.cjs.js.map +1 -0
  7. package/dist/ge-commercetools-utils-node.esm.js +1 -0
  8. package/dist/typings/api/CommercetoolsApi.d.ts +436 -0
  9. package/{build → dist/typings}/api/index.d.ts +0 -1
  10. package/dist/typings/api/types.d.ts +12 -0
  11. package/dist/typings/auth/CommercetoolsAuth.d.ts +22 -0
  12. package/dist/typings/auth/CommercetoolsAuthApi.d.ts +21 -0
  13. package/dist/typings/auth/CommercetoolsGrant.d.ts +13 -0
  14. package/{build → dist/typings}/auth/constants.d.ts +0 -4
  15. package/dist/typings/auth/index.d.ts +3 -0
  16. package/dist/typings/auth/scopes.d.ts +2 -0
  17. package/dist/typings/auth/types.d.ts +43 -0
  18. package/{build → dist/typings}/auth/utils.d.ts +0 -1
  19. package/dist/typings/constants.d.ts +1 -0
  20. package/dist/typings/error/CommercetoolsError.d.ts +16 -0
  21. package/{build → dist/typings}/error/index.d.ts +0 -1
  22. package/dist/typings/index.d.ts +6 -0
  23. package/dist/typings/models/cart-discount.d.ts +196 -0
  24. package/dist/typings/models/cart.d.ts +597 -0
  25. package/dist/typings/models/category.d.ts +166 -0
  26. package/dist/typings/models/channel.d.ts +103 -0
  27. package/dist/typings/models/common.d.ts +232 -0
  28. package/dist/typings/models/custom-object.d.ts +30 -0
  29. package/dist/typings/models/customer-group.d.ts +58 -0
  30. package/dist/typings/models/customer.d.ts +276 -0
  31. package/dist/typings/models/discount-code.d.ts +116 -0
  32. package/dist/typings/models/error.d.ts +412 -0
  33. package/dist/typings/models/extension.d.ts +78 -0
  34. package/dist/typings/models/graph-ql.d.ts +21 -0
  35. package/dist/typings/models/index.d.ts +33 -0
  36. package/dist/typings/models/inventory.d.ts +82 -0
  37. package/dist/typings/models/me.d.ts +481 -0
  38. package/dist/typings/models/message.d.ts +1823 -0
  39. package/dist/typings/models/order-edit.d.ts +565 -0
  40. package/dist/typings/models/order.d.ts +558 -0
  41. package/dist/typings/models/payment.d.ts +208 -0
  42. package/dist/typings/models/product-discount.d.ts +123 -0
  43. package/dist/typings/models/product-selection.d.ts +106 -0
  44. package/dist/typings/models/product-type.d.ts +216 -0
  45. package/dist/typings/models/product.d.ts +517 -0
  46. package/dist/typings/models/project.d.ts +108 -0
  47. package/dist/typings/models/review.d.ts +115 -0
  48. package/dist/typings/models/scalar-types.d.ts +4 -0
  49. package/dist/typings/models/shipping-method.d.ts +160 -0
  50. package/dist/typings/models/shopping-list.d.ts +217 -0
  51. package/dist/typings/models/state.d.ts +86 -0
  52. package/dist/typings/models/store.d.ts +121 -0
  53. package/dist/typings/models/subscription.d.ts +148 -0
  54. package/dist/typings/models/tax-category.d.ts +87 -0
  55. package/dist/typings/models/type.d.ts +182 -0
  56. package/dist/typings/models/zone.d.ts +65 -0
  57. package/dist/typings/types.d.ts +21 -0
  58. package/dist/typings/utils/format.d.ts +1 -0
  59. package/{build → dist/typings}/utils/index.d.ts +0 -1
  60. package/dist/typings/utils/mask.d.ts +6 -0
  61. package/dist/typings/utils/useragent.d.ts +1 -0
  62. package/package.json +40 -18
  63. package/.husky/pre-commit +0 -4
  64. package/.nvmrc +0 -1
  65. package/build/api/CommercetoolsApi.d.ts +0 -1096
  66. package/build/api/CommercetoolsApi.d.ts.map +0 -1
  67. package/build/api/CommercetoolsApi.js +0 -1419
  68. package/build/api/CommercetoolsApi.js.map +0 -1
  69. package/build/api/index.d.ts.map +0 -1
  70. package/build/api/index.js +0 -15
  71. package/build/api/index.js.map +0 -1
  72. package/build/api/types.d.ts +0 -33
  73. package/build/api/types.d.ts.map +0 -1
  74. package/build/api/types.js +0 -3
  75. package/build/api/types.js.map +0 -1
  76. package/build/auth/CommercetoolsAuth.d.ts +0 -182
  77. package/build/auth/CommercetoolsAuth.d.ts.map +0 -1
  78. package/build/auth/CommercetoolsAuth.js +0 -326
  79. package/build/auth/CommercetoolsAuth.js.map +0 -1
  80. package/build/auth/CommercetoolsAuthApi.d.ts +0 -74
  81. package/build/auth/CommercetoolsAuthApi.d.ts.map +0 -1
  82. package/build/auth/CommercetoolsAuthApi.js +0 -249
  83. package/build/auth/CommercetoolsAuthApi.js.map +0 -1
  84. package/build/auth/CommercetoolsGrant.d.ts +0 -66
  85. package/build/auth/CommercetoolsGrant.d.ts.map +0 -1
  86. package/build/auth/CommercetoolsGrant.js +0 -82
  87. package/build/auth/CommercetoolsGrant.js.map +0 -1
  88. package/build/auth/constants.d.ts.map +0 -1
  89. package/build/auth/constants.js +0 -32
  90. package/build/auth/constants.js.map +0 -1
  91. package/build/auth/index.d.ts +0 -4
  92. package/build/auth/index.d.ts.map +0 -1
  93. package/build/auth/index.js +0 -18
  94. package/build/auth/index.js.map +0 -1
  95. package/build/auth/scopes.d.ts +0 -11
  96. package/build/auth/scopes.d.ts.map +0 -1
  97. package/build/auth/scopes.js +0 -30
  98. package/build/auth/scopes.js.map +0 -1
  99. package/build/auth/types.d.ts +0 -120
  100. package/build/auth/types.d.ts.map +0 -1
  101. package/build/auth/types.js +0 -28
  102. package/build/auth/types.js.map +0 -1
  103. package/build/auth/utils.d.ts.map +0 -1
  104. package/build/auth/utils.js +0 -8
  105. package/build/auth/utils.js.map +0 -1
  106. package/build/constants.d.ts +0 -6
  107. package/build/constants.d.ts.map +0 -1
  108. package/build/constants.js +0 -9
  109. package/build/constants.js.map +0 -1
  110. package/build/error/CommercetoolsError.d.ts +0 -38
  111. package/build/error/CommercetoolsError.d.ts.map +0 -1
  112. package/build/error/CommercetoolsError.js +0 -117
  113. package/build/error/CommercetoolsError.js.map +0 -1
  114. package/build/error/index.d.ts.map +0 -1
  115. package/build/error/index.js +0 -14
  116. package/build/error/index.js.map +0 -1
  117. package/build/index.d.ts +0 -7
  118. package/build/index.d.ts.map +0 -1
  119. package/build/index.js +0 -21
  120. package/build/index.js.map +0 -1
  121. package/build/types.d.ts +0 -44
  122. package/build/types.d.ts.map +0 -1
  123. package/build/types.js +0 -16
  124. package/build/types.js.map +0 -1
  125. package/build/utils/format.d.ts +0 -11
  126. package/build/utils/format.d.ts.map +0 -1
  127. package/build/utils/format.js +0 -48
  128. package/build/utils/format.js.map +0 -1
  129. package/build/utils/index.d.ts.map +0 -1
  130. package/build/utils/index.js +0 -16
  131. package/build/utils/index.js.map +0 -1
  132. package/build/utils/mask.d.ts +0 -28
  133. package/build/utils/mask.d.ts.map +0 -1
  134. package/build/utils/mask.js +0 -56
  135. package/build/utils/mask.js.map +0 -1
  136. package/build/utils/useragent.d.ts +0 -10
  137. package/build/utils/useragent.d.ts.map +0 -1
  138. package/build/utils/useragent.js +0 -20
  139. package/build/utils/useragent.js.map +0 -1
@@ -1,1419 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __importDefault = (this && this.__importDefault) || function (mod) {
50
- return (mod && mod.__esModule) ? mod : { "default": mod };
51
- };
52
- Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.CommercetoolsApi = void 0;
54
- var https_1 = require("https");
55
- var axios_1 = __importDefault(require("axios"));
56
- var qs_1 = __importDefault(require("qs"));
57
- var __1 = require("../");
58
- var error_1 = require("../error");
59
- var constants_1 = require("../auth/constants");
60
- var constants_2 = require("../constants");
61
- var utils_1 = require("../utils");
62
- /**
63
- * Default retry configuration - equates to no retying at all
64
- */
65
- var DEFAULT_RETRY_CONFIG = {
66
- maxRetries: 0,
67
- delayMs: 0,
68
- };
69
- /**
70
- * List of status codes which are allowed to retry
71
- */
72
- var RETRYABLE_STATUS_CODES = [500, 501, 502, 503, 504];
73
- /**
74
- * The config options passed in to the {@see HttpsAgent.Agent} used
75
- * with the axios instance that we create.
76
- */
77
- var DEFAULT_HTTPS_AGENT_CONFIG = {
78
- keepAlive: true,
79
- maxSockets: 32,
80
- maxFreeSockets: 10,
81
- timeout: 60000,
82
- };
83
- /**
84
- * A collection of convenience methods for interacting with the
85
- * commercetools API.
86
- */
87
- var CommercetoolsApi = /** @class */ (function () {
88
- function CommercetoolsApi(config) {
89
- CommercetoolsApi.validateConfig(config);
90
- this.config = config;
91
- this.auth = new __1.CommercetoolsAuth(config);
92
- this.endpoints = constants_1.REGION_URLS[this.config.region];
93
- this.userAgent = (0, utils_1.buildUserAgent)(this.config.systemIdentifier);
94
- this.axios = this.createAxiosInstance();
95
- this.retry = config.retry || DEFAULT_RETRY_CONFIG;
96
- }
97
- /**
98
- * Define the base axios instance that forms the foundation
99
- * of all axios calls made by the {@see request} method.
100
- */
101
- CommercetoolsApi.prototype.createAxiosInstance = function () {
102
- var agent;
103
- if (this.config.httpsAgent) {
104
- agent = this.config.httpsAgent;
105
- }
106
- else {
107
- agent = new https_1.Agent(DEFAULT_HTTPS_AGENT_CONFIG);
108
- }
109
- var instance = axios_1.default.create({
110
- timeout: this.config.timeoutMs || constants_2.DEFAULT_REQUEST_TIMEOUT_MS,
111
- paramsSerializer: function (params) {
112
- return qs_1.default.stringify(params, { arrayFormat: 'repeat' });
113
- },
114
- httpsAgent: agent,
115
- });
116
- instance.defaults.headers.common['User-Agent'] = this.userAgent;
117
- return instance;
118
- };
119
- /**
120
- * Get a store given it's id
121
- * https://docs.commercetools.com/api/projects/stores#get-a-store-by-id
122
- */
123
- CommercetoolsApi.prototype.getStoreById = function (options) {
124
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/stores/".concat(options.id), method: 'GET' }));
125
- };
126
- /**
127
- * Get a store given it's key
128
- * https://docs.commercetools.com/api/projects/stores#get-a-store-by-key
129
- */
130
- CommercetoolsApi.prototype.getStoreByKey = function (options) {
131
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/stores/key=".concat(options.key), method: 'GET' }));
132
- };
133
- /**
134
- * Query stores
135
- * https://docs.commercetools.com/api/projects/stores#get-a-store-by-key
136
- */
137
- CommercetoolsApi.prototype.queryStores = function (options) {
138
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: '/stores', method: 'GET' }));
139
- };
140
- /**
141
- * Create a store:
142
- * https://docs.commercetools.com/api/projects/stores#create-a-store
143
- */
144
- CommercetoolsApi.prototype.createStore = function (options) {
145
- return this.request({
146
- path: "/stores",
147
- method: 'POST',
148
- data: options.data,
149
- });
150
- };
151
- /**
152
- * Update a store by id:
153
- * https://docs.commercetools.com/api/projects/stores#update-store-by-id
154
- */
155
- CommercetoolsApi.prototype.updateStoreById = function (options) {
156
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/stores/".concat(options.id), method: 'POST', data: options.data }));
157
- };
158
- /**
159
- * Update a store by key:
160
- * https://docs.commercetools.com/api/projects/stores#update-store-by-key
161
- */
162
- CommercetoolsApi.prototype.updateStoreByKey = function (options) {
163
- return this.request({
164
- path: "/stores/key=".concat(options.key),
165
- method: 'POST',
166
- data: options.data,
167
- });
168
- };
169
- /**
170
- * Delete a store by id:
171
- * https://docs.commercetools.com/api/projects/stores#delete-store-by-id
172
- */
173
- CommercetoolsApi.prototype.deleteStoreById = function (options) {
174
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/stores/".concat(options.id), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }));
175
- };
176
- /**
177
- * Delete a store by key:
178
- * https://docs.commercetools.com/api/projects/stores#delete-store-by-key
179
- */
180
- CommercetoolsApi.prototype.deleteStoreByKey = function (options) {
181
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/stores/key=".concat(options.key), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }));
182
- };
183
- /**
184
- * Get an individual category by id:
185
- * https://docs.commercetools.com/api/projects/categories#get-category-by-id
186
- */
187
- CommercetoolsApi.prototype.getCategoryById = function (options) {
188
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories/".concat(options.id), method: 'GET' }));
189
- };
190
- /**
191
- * Get an individual category by key:
192
- * https://docs.commercetools.com/api/projects/categories#get-category-by-key
193
- */
194
- CommercetoolsApi.prototype.getCategoryByKey = function (options) {
195
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories/key=".concat(options.key), method: 'GET' }));
196
- };
197
- /**
198
- * Query channels:
199
- * https://docs.commercetools.com/api/projects/channels#query-channels
200
- */
201
- CommercetoolsApi.prototype.queryChannels = function (options) {
202
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/channels", method: 'GET' }));
203
- };
204
- /**
205
- * Import an order.
206
- * https://docs.commercetools.com/api/projects/orders-import#orderimportdraft
207
- */
208
- CommercetoolsApi.prototype.importOrder = function (options) {
209
- return __awaiter(this, void 0, void 0, function () {
210
- return __generator(this, function (_a) {
211
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: '/orders/import', method: 'POST', data: options.data }))];
212
- });
213
- });
214
- };
215
- /**
216
- * Get channel by id:
217
- * https://docs.commercetools.com/api/projects/channels#get-channel-by-id
218
- */
219
- CommercetoolsApi.prototype.getChannelById = function (options) {
220
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/channels/".concat(options.id), method: 'GET' }));
221
- };
222
- /**
223
- * Get an category by id or key. Either the id or the key must be provided.
224
- */
225
- CommercetoolsApi.prototype.getCategory = function (options) {
226
- if (!options.id && !options.key) {
227
- throw new error_1.CommercetoolsError('Either an id, key or slug must be provided');
228
- }
229
- if (options.id) {
230
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories/".concat(options.id), method: 'GET' }));
231
- }
232
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories/key=".concat(options.key), method: 'GET' }));
233
- };
234
- /**
235
- * Get a category projection by slug and locale
236
- * Queries the categories for the given slug + locale using:
237
- * https://docs.commercetools.com/api/projects/categories#query-categories
238
- */
239
- CommercetoolsApi.prototype.getCategoryBySlug = function (options) {
240
- return __awaiter(this, void 0, void 0, function () {
241
- var languageCodes, data;
242
- return __generator(this, function (_a) {
243
- switch (_a.label) {
244
- case 0:
245
- if (!options.languageCode && !options.languageCodes) {
246
- throw new error_1.CommercetoolsError('Either the `languageCode` or `languageCodes` property must be provided');
247
- }
248
- languageCodes = [];
249
- if (options.languageCodes) {
250
- languageCodes.push.apply(languageCodes, options.languageCodes);
251
- }
252
- else if (options.languageCode) {
253
- languageCodes.push(options.languageCode);
254
- }
255
- return [4 /*yield*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(__assign(__assign({}, options), { params: __assign(__assign({}, options === null || options === void 0 ? void 0 : options.params), { where: "slug(".concat(languageCodes.map(function (code) { return "".concat(code, "=\"").concat(options.slug, "\""); }).join(' or '), ")") }) }))), { path: "/categories", method: 'GET' }))];
256
- case 1:
257
- data = _a.sent();
258
- if (!data.count) {
259
- throw new error_1.CommercetoolsError("No category found with slug [".concat(options.slug, "] and language code [").concat(options.languageCode, "]"), { options: options }, 404);
260
- }
261
- return [2 /*return*/, data.results[0]];
262
- }
263
- });
264
- });
265
- };
266
- /**
267
- * Query categories
268
- * https://docs.commercetools.com/api/projects/categories#query-categories
269
- */
270
- CommercetoolsApi.prototype.queryCategories = function (options) {
271
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories", method: 'GET' }));
272
- };
273
- /**
274
- * Get the parent categories of a given category. This method returns an array
275
- * of {@see Category} items representing the ancestry of categories for the given
276
- * category id. The list is ordered from top down. In other words, the root
277
- * category is always the first item in the list.
278
- */
279
- CommercetoolsApi.prototype.getCategoryParents = function (options) {
280
- return __awaiter(this, void 0, void 0, function () {
281
- var category, ancestors;
282
- return __generator(this, function (_a) {
283
- switch (_a.label) {
284
- case 0: return [4 /*yield*/, this.getCategory(__assign(__assign({}, options), { params: __assign(__assign({}, options.params), { expand: 'ancestors[*]' }) }))];
285
- case 1:
286
- category = _a.sent();
287
- ancestors = category.ancestors.map(function (ref) { return ref.obj; });
288
- ancestors.push(__assign(__assign({}, category), { ancestors: category.ancestors.map(function (ancestor) { return ({ id: ancestor.id, typeId: ancestor.typeId }); }) }));
289
- return [2 /*return*/, ancestors];
290
- }
291
- });
292
- });
293
- };
294
- /**
295
- * Get an individual product by id:
296
- * https://docs.commercetools.com/api/projects/products#get-product-by-id
297
- */
298
- CommercetoolsApi.prototype.getProductById = function (options) {
299
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/".concat(options.id), method: 'GET' }));
300
- };
301
- /**
302
- * Get an individual product by key:
303
- * https://docs.commercetools.com/api/projects/products#get-product-by-key
304
- */
305
- CommercetoolsApi.prototype.getProductByKey = function (options) {
306
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/key=".concat(options.key), method: 'GET' }));
307
- };
308
- /**
309
- * Get a product projection by id
310
- * https://docs.commercetools.com/api/projects/productProjections#get-productprojection-by-id
311
- */
312
- CommercetoolsApi.prototype.getProductProjectionById = function (options) {
313
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/product-projections/".concat(options.id), options.storeKey), method: 'GET' }));
314
- };
315
- /**
316
- * Get a product projection by key
317
- * https://docs.commercetools.com/api/projects/productProjections#get-productprojection-by-key
318
- */
319
- CommercetoolsApi.prototype.getProductProjectionByKey = function (options) {
320
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/product-projections/key=".concat(options.key), options.storeKey), method: 'GET' }));
321
- };
322
- /**
323
- * Get a product projection by searching the slug with a given locale or array of locales
324
- *
325
- * Utilises the product projection query endpoint:
326
- * https://docs.commercetools.com/api/projects/productProjections#query-productprojections
327
- *
328
- * You must pass either the {@see options.languageCode} or {@see options.languageCodes}
329
- * property in the {@see options} parameter. If both are provided, only the {@see options.languageCodes}
330
- * is actually used.
331
- */
332
- CommercetoolsApi.prototype.getProductProjectionBySlug = function (options) {
333
- return __awaiter(this, void 0, void 0, function () {
334
- var languageCodes, data;
335
- return __generator(this, function (_a) {
336
- switch (_a.label) {
337
- case 0:
338
- if (!options.languageCode && !options.languageCodes) {
339
- throw new error_1.CommercetoolsError('Either the `languageCode` or `languageCodes` property must be provided');
340
- }
341
- languageCodes = [];
342
- if (options.languageCodes) {
343
- languageCodes.push.apply(languageCodes, options.languageCodes);
344
- }
345
- else if (options.languageCode) {
346
- languageCodes.push(options.languageCode);
347
- }
348
- return [4 /*yield*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(__assign(__assign({}, options), { params: __assign(__assign({}, options === null || options === void 0 ? void 0 : options.params), { where: "slug(".concat(languageCodes.map(function (code) { return "".concat(code, "=\"").concat(options.slug, "\""); }).join(' or '), ")") }) }))), { path: "/product-projections", method: 'GET' }))];
349
- case 1:
350
- data = _a.sent();
351
- if (!data.count) {
352
- throw new error_1.CommercetoolsError("No product projection found with slug [".concat(options.slug, "] and language code [").concat(options.languageCode, "]"), { options: options }, 404);
353
- }
354
- return [2 /*return*/, data.results[0]];
355
- }
356
- });
357
- });
358
- };
359
- /**
360
- * Query product projections
361
- * https://docs.commercetools.com/api/projects/productProjections#query-productprojections
362
- */
363
- CommercetoolsApi.prototype.queryProductProjections = function (options) {
364
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-projections", method: 'GET' }));
365
- };
366
- /**
367
- * Search product projections
368
- * https://docs.commercetools.com/api/projects/products-search#search-productprojections
369
- */
370
- CommercetoolsApi.prototype.searchProductProjections = function (options) {
371
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-projections/search", method: 'GET' }));
372
- };
373
- /**
374
- * Get a product selection by id
375
- * https://docs.commercetools.com/api/projects/product-selections#get-product-selection
376
- */
377
- CommercetoolsApi.prototype.getProductSelectionById = function (options) {
378
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/product-selections/".concat(options.id), options.storeKey), method: 'GET' }));
379
- };
380
- /**
381
- * Get a product selection by key
382
- * https://docs.commercetools.com/api/projects/product-selections#get-product-selection-by-key
383
- */
384
- CommercetoolsApi.prototype.getProductSelectionByKey = function (options) {
385
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/product-selections/key=".concat(options.key), options.storeKey), method: 'GET' }));
386
- };
387
- /**
388
- * Query product selections
389
- * https://docs.commercetools.com/api/projects/product-selections#query-product-selections
390
- */
391
- CommercetoolsApi.prototype.queryProductSelections = function (options) {
392
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-selections", method: 'GET' }));
393
- };
394
- /**
395
- * Create a product selection
396
- * https://docs.commercetools.com/api/projects/product-selections#create-product-selection
397
- */
398
- CommercetoolsApi.prototype.createProductSelection = function (options) {
399
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-selections", method: 'POST', data: options.data }));
400
- };
401
- /**
402
- * Update a product selection by key
403
- * https://docs.commercetools.com/api/projects/product-selections#update-product-selection-by-key
404
- */
405
- CommercetoolsApi.prototype.updateProductSelectionByKey = function (options) {
406
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-selections/key=".concat(options.key), method: 'POST', data: { version: options.version, actions: options.actions } }));
407
- };
408
- /**
409
- * Delete a product selection by key
410
- * https://docs.commercetools.com/api/projects/product-selections#delete-product-selection-by-key
411
- */
412
- CommercetoolsApi.prototype.deleteProductSelectionByKey = function (options) {
413
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-selections/key=".concat(options.key, "&version=").concat(options.version), method: 'DELETE' }));
414
- };
415
- /**
416
- * Query the products available in a store through active Product Selections
417
- * https://docs.commercetools.com/api/projects/product-selections#query-products-available-in-a-store-through-active-product-selections
418
- */
419
- CommercetoolsApi.prototype.queryProductsInStore = function (options) {
420
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/in-store/key=".concat(options.storeKey, "/product-selection-assignments"), method: "GET" }));
421
- };
422
- /**
423
- * Get a cart by id
424
- * https://docs.commercetools.com/api/projects/carts#update-a-cart-by-id
425
- */
426
- CommercetoolsApi.prototype.getCartById = function (options) {
427
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/carts/".concat(options.id), options.storeKey), method: 'GET' }));
428
- };
429
- /**
430
- * Query carts
431
- * https://docs.commercetools.com/api/projects/carts#query-carts-1
432
- */
433
- CommercetoolsApi.prototype.queryCarts = function (options) {
434
- return __awaiter(this, void 0, void 0, function () {
435
- return __generator(this, function (_a) {
436
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/carts", options === null || options === void 0 ? void 0 : options.storeKey), method: 'GET' }))];
437
- });
438
- });
439
- };
440
- /**
441
- * Query my carts
442
- * https://docs.commercetools.com/api/projects/me-carts#query-carts-1
443
- */
444
- CommercetoolsApi.prototype.queryMyCarts = function (options) {
445
- return __awaiter(this, void 0, void 0, function () {
446
- return __generator(this, function (_a) {
447
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/carts", options.storeKey), method: 'GET', accessToken: options.accessToken }))];
448
- });
449
- });
450
- };
451
- /**
452
- * Create a new cart:
453
- * https://docs.commercetools.com/api/projects/carts#create-a-cart-1
454
- */
455
- CommercetoolsApi.prototype.createCart = function (options) {
456
- return __awaiter(this, void 0, void 0, function () {
457
- return __generator(this, function (_a) {
458
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/carts", options.storeKey), method: 'POST', data: options.data }))];
459
- });
460
- });
461
- };
462
- /**
463
- * Update a cart by id
464
- * https://docs.commercetools.com/api/projects/carts#update-a-cart-by-id
465
- */
466
- CommercetoolsApi.prototype.updateCartById = function (options) {
467
- return __awaiter(this, void 0, void 0, function () {
468
- return __generator(this, function (_a) {
469
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/carts/".concat(options.id), options.storeKey), method: 'POST', data: {
470
- version: options.version,
471
- actions: options.actions,
472
- } }))];
473
- });
474
- });
475
- };
476
- /**
477
- * Get the active cart. Requires a logged in or anonymous customer access token:
478
- * https://docs.commercetools.com/api/projects/me-carts#get-active-cart
479
- */
480
- CommercetoolsApi.prototype.getMyActiveCart = function (options) {
481
- return __awaiter(this, void 0, void 0, function () {
482
- return __generator(this, function (_a) {
483
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/active-cart", options.storeKey), method: 'GET', accessToken: options.accessToken }))];
484
- });
485
- });
486
- };
487
- /**
488
- * Get my cart. Requires a logged in or anonymous customer access token:
489
- * https://docs.commercetools.com/api/projects/me-carts#get-cart-by-id
490
- */
491
- CommercetoolsApi.prototype.getMyCartById = function (options) {
492
- return __awaiter(this, void 0, void 0, function () {
493
- return __generator(this, function (_a) {
494
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/carts/".concat(options.cartId), options.storeKey), method: 'GET', accessToken: options.accessToken }))];
495
- });
496
- });
497
- };
498
- /**
499
- * Create a new cart for the customer associated with the given `accessToken` parameter:
500
- * https://docs.commercetools.com/api/projects/me-carts#create-a-cart-1
501
- */
502
- CommercetoolsApi.prototype.createMyCart = function (options) {
503
- return __awaiter(this, void 0, void 0, function () {
504
- return __generator(this, function (_a) {
505
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/carts", options.storeKey), method: 'POST', data: options.data, accessToken: options.accessToken }))];
506
- });
507
- });
508
- };
509
- /**
510
- * Delete the active cart This method uses {@see getMyActiveCart} to first
511
- * get the active cart, in order to find the cart id and version:
512
- * https://docs.commercetools.com/api/projects/me-carts#delete-a-cart
513
- */
514
- CommercetoolsApi.prototype.deleteMyActiveCart = function (options) {
515
- return __awaiter(this, void 0, void 0, function () {
516
- var cart;
517
- return __generator(this, function (_a) {
518
- switch (_a.label) {
519
- case 0: return [4 /*yield*/, this.getMyActiveCart(options)];
520
- case 1:
521
- cart = _a.sent();
522
- return [4 /*yield*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/carts/".concat(cart.id), options.storeKey), method: 'DELETE', params: __assign(__assign({}, options.params), { version: cart.version }), accessToken: options.accessToken }))];
523
- case 2: return [2 /*return*/, _a.sent()];
524
- }
525
- });
526
- });
527
- };
528
- /**
529
- * Delete my cart by id
530
- * https://docs.commercetools.com/api/projects/me-carts#delete-a-cart
531
- */
532
- CommercetoolsApi.prototype.deleteMyCartById = function (options) {
533
- return __awaiter(this, void 0, void 0, function () {
534
- var cart;
535
- return __generator(this, function (_a) {
536
- switch (_a.label) {
537
- case 0: return [4 /*yield*/, this.getMyCartById(options)];
538
- case 1:
539
- cart = _a.sent();
540
- return [4 /*yield*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/carts/".concat(options.cartId), options.storeKey), method: 'DELETE', params: __assign(__assign({}, options.params), { version: cart.version }), accessToken: options.accessToken }))];
541
- case 2: return [2 /*return*/, _a.sent()];
542
- }
543
- });
544
- });
545
- };
546
- /**
547
- * Update a customer's cart with the given actions. Note that we automatically
548
- * retrieve the customer's active cart using the given access token:
549
- * https://docs.commercetools.com/api/projects/me-carts#update-cart
550
- * https://docs.commercetools.com/api/projects/me-carts#update-actions
551
- */
552
- CommercetoolsApi.prototype.updateMyActiveCart = function (options) {
553
- return __awaiter(this, void 0, void 0, function () {
554
- var cart;
555
- return __generator(this, function (_a) {
556
- switch (_a.label) {
557
- case 0: return [4 /*yield*/, this.getMyActiveCart(options)];
558
- case 1:
559
- cart = _a.sent();
560
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/carts/".concat(cart.id), options.storeKey), method: 'POST', data: {
561
- version: cart.version,
562
- actions: options.actions,
563
- }, accessToken: options.accessToken }))];
564
- }
565
- });
566
- });
567
- };
568
- /**
569
- * Create an order from the given cart id. The cart id and version are automatically
570
- * retrieved by looking up the customers active cart:
571
- * https://docs.commercetools.com/api/projects/me-orders#create-order-from-a-cart
572
- */
573
- CommercetoolsApi.prototype.createMyOrderFromActiveCart = function (options) {
574
- return __awaiter(this, void 0, void 0, function () {
575
- var cart;
576
- return __generator(this, function (_a) {
577
- switch (_a.label) {
578
- case 0: return [4 /*yield*/, this.getMyActiveCart(options)];
579
- case 1:
580
- cart = _a.sent();
581
- return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore('/me/orders', options.storeKey), method: 'POST', data: {
582
- version: cart.version,
583
- id: cart.id,
584
- }, accessToken: options.accessToken }))];
585
- }
586
- });
587
- });
588
- };
589
- /**
590
- * Delete a cart by the given id:
591
- * https://docs.commercetools.com/api/projects/carts#delete-a-cart-by-id
592
- */
593
- CommercetoolsApi.prototype.deleteCartById = function (options) {
594
- return __awaiter(this, void 0, void 0, function () {
595
- return __generator(this, function (_a) {
596
- switch (_a.label) {
597
- case 0: return [4 /*yield*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/carts/".concat(options.id), options.storeKey), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }))];
598
- case 1: return [2 /*return*/, _a.sent()];
599
- }
600
- });
601
- });
602
- };
603
- /**
604
- * Create a payment object:
605
- * https://docs.commercetools.com/api/projects/payments#create-a-payment
606
- */
607
- CommercetoolsApi.prototype.createPayment = function (options) {
608
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: '/payments', method: 'POST', data: options.data }));
609
- };
610
- /**
611
- * Delete a payment object by id:
612
- * https://docs.commercetools.com/api/projects/payments#delete-payment-by-id
613
- */
614
- CommercetoolsApi.prototype.deletePaymentById = function (options) {
615
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/payments/".concat(options.id), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }));
616
- };
617
- /**
618
- * Update a payment object by id:
619
- * https://docs.commercetools.com/api/projects/payments#update-payment-by-id
620
- */
621
- CommercetoolsApi.prototype.updatePaymentById = function (options) {
622
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/payments/".concat(options.id), method: 'POST', data: options.data }));
623
- };
624
- /**
625
- * Get a payment object by id:
626
- * https://docs.commercetools.com/api/projects/payments#get-payment-by-id
627
- */
628
- CommercetoolsApi.prototype.getPaymentById = function (options) {
629
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/payments/".concat(options.id), method: 'GET' }));
630
- };
631
- /**
632
- * Query payment objects:
633
- * https://docs.commercetools.com/api/projects/payments#query-payments
634
- */
635
- CommercetoolsApi.prototype.queryPayments = function (options) {
636
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: '/payments', method: 'GET' }));
637
- };
638
- /**
639
- * Create a payment object using the customer's access token:
640
- * https://docs.commercetools.com/api/projects/me-payments#create-mypayment
641
- */
642
- CommercetoolsApi.prototype.createMyPayment = function (options) {
643
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: '/me/payments', method: 'POST', data: options.data, accessToken: options.accessToken }));
644
- };
645
- /**
646
- * Update a payment object using the customer's access token:
647
- * https://docs.commercetools.com/api/projects/me-payments#update-mypayment
648
- */
649
- CommercetoolsApi.prototype.updateMyPaymentById = function (options) {
650
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/me/payments/".concat(options.id), method: 'POST', data: options.data, accessToken: options.accessToken }));
651
- };
652
- /**
653
- * Get a payment object by id using the customer's access token:
654
- * https://docs.commercetools.com/api/projects/me-payments#get-mypayment-by-id
655
- */
656
- CommercetoolsApi.prototype.getMyPaymentById = function (options) {
657
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/me/payments/".concat(options.id), method: 'GET', accessToken: options.accessToken }));
658
- };
659
- /**
660
- * Query payment objects using the customer's access token:
661
- * https://docs.commercetools.com/api/projects/me-payments#query-mypayments
662
- */
663
- CommercetoolsApi.prototype.queryMyPayments = function (options) {
664
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: '/me/payments', method: 'GET', accessToken: options.accessToken }));
665
- };
666
- /**
667
- * Get an order by id using the customer's access token:
668
- * https://docs.commercetools.com/api/projects/me-orders#get-order-by-id
669
- */
670
- CommercetoolsApi.prototype.getMyOrderById = function (options) {
671
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/orders/".concat(options.id), options.storeKey), method: 'GET', accessToken: options.accessToken }));
672
- };
673
- /**
674
- * Query my orders:
675
- * https://docs.commercetools.com/api/projects/me-orders#query-orders-1
676
- */
677
- CommercetoolsApi.prototype.queryMyOrders = function (options) {
678
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/orders", options.storeKey), method: 'GET', accessToken: options.accessToken }));
679
- };
680
- /**
681
- * Update an order by id:
682
- * https://docs.commercetools.com/api/projects/orders#update-order-by-id
683
- */
684
- CommercetoolsApi.prototype.updateOrderById = function (options) {
685
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/orders/".concat(options.id), options.storeKey), method: 'POST', data: options.data }));
686
- };
687
- /**
688
- * Update an order by order number:
689
- * https://docs.commercetools.com/api/projects/orders#update-order-by-ordernumber
690
- */
691
- CommercetoolsApi.prototype.updateOrderByOrderNumber = function (options) {
692
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/orders/order-number=".concat(options.orderNumber), options.storeKey), method: 'POST', data: options.data }));
693
- };
694
- /**
695
- * Get an order by id:
696
- * https://docs.commercetools.com/api/projects/orders#get-order-by-id
697
- */
698
- CommercetoolsApi.prototype.getOrderById = function (options) {
699
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/orders/".concat(options.id), options.storeKey), method: 'GET' }));
700
- };
701
- /**
702
- * Get an order by order number:
703
- * https://docs.commercetools.com/api/projects/orders#get-order-by-ordernumber
704
- */
705
- CommercetoolsApi.prototype.getOrderByOrderNumber = function (options) {
706
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/orders/order-number=".concat(options.orderNumber), options.storeKey), method: 'GET' }));
707
- };
708
- /**
709
- * Delete an order by id:
710
- * https://docs.commercetools.com/api/projects/orders#delete-order-by-id
711
- */
712
- CommercetoolsApi.prototype.deleteOrderById = function (options) {
713
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/orders/".concat(options.id), options.storeKey), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version, dataErasure: options.dataErasure }) }));
714
- };
715
- /**
716
- * Delete an order by order number:
717
- * https://docs.commercetools.com/api/projects/orders#delete-order-by-ordernumber
718
- */
719
- CommercetoolsApi.prototype.deleteOrderByOrderNumber = function (options) {
720
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/orders/order-number=".concat(options.orderNo), options.storeKey), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version, dataErasure: options.dataErasure }) }));
721
- };
722
- /**
723
- * Query orders:
724
- * https://docs.commercetools.com/api/projects/orders#query-orders-1
725
- */
726
- CommercetoolsApi.prototype.queryOrders = function (options) {
727
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/orders", options.storeKey), method: 'GET' }));
728
- };
729
- /**
730
- * Create a product:
731
- * https://docs.commercetools.com/api/projects/products#create-a-product
732
- */
733
- CommercetoolsApi.prototype.createProduct = function (options) {
734
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products", method: 'POST', data: options.data }));
735
- };
736
- /**
737
- * Update a product by key:
738
- * https://docs.commercetools.com/api/projects/products#update-product-by-key
739
- */
740
- CommercetoolsApi.prototype.updateProductByKey = function (options) {
741
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/key=".concat(options.key), method: 'POST', data: options.data }));
742
- };
743
- /**
744
- * Update a product by id:
745
- * https://docs.commercetools.com/api/projects/products#update-product-by-id
746
- */
747
- CommercetoolsApi.prototype.updateProductById = function (options) {
748
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/".concat(options.id), method: 'POST', data: options.data }));
749
- };
750
- /**
751
- * Delete a product by id:
752
- * https://docs.commercetools.com/api/projects/products#delete-product-by-id
753
- *
754
- * @param {object} options Request options
755
- * @param {boolean} options.unpublish If true, the product will be unpublished before being deleted
756
- */
757
- CommercetoolsApi.prototype.deleteProductById = function (options) {
758
- return __awaiter(this, void 0, void 0, function () {
759
- var version, product;
760
- return __generator(this, function (_a) {
761
- switch (_a.label) {
762
- case 0:
763
- version = options.version;
764
- if (!options.unpublish) return [3 /*break*/, 2];
765
- return [4 /*yield*/, this.unpublishProductById({
766
- id: options.id,
767
- version: options.version,
768
- })];
769
- case 1:
770
- product = _a.sent();
771
- version = product.version;
772
- _a.label = 2;
773
- case 2: return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/".concat(options.id), method: 'DELETE', params: __assign(__assign({}, options.params), { version: version }) }))];
774
- }
775
- });
776
- });
777
- };
778
- /**
779
- * Delete a product by key:
780
- * https://docs.commercetools.com/api/projects/products#delete-product-by-key
781
- *
782
- * @param {object} options Request options
783
- * @param {boolean} options.unpublish If true, the product will be unpublished before being deleted
784
- */
785
- CommercetoolsApi.prototype.deleteProductByKey = function (options) {
786
- return __awaiter(this, void 0, void 0, function () {
787
- var version, product;
788
- return __generator(this, function (_a) {
789
- switch (_a.label) {
790
- case 0:
791
- version = options.version;
792
- if (!options.unpublish) return [3 /*break*/, 2];
793
- return [4 /*yield*/, this.unpublishProductByKey({
794
- key: options.key,
795
- version: options.version,
796
- })];
797
- case 1:
798
- product = _a.sent();
799
- version = product.version;
800
- _a.label = 2;
801
- case 2: return [2 /*return*/, this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/key=".concat(options.key), method: 'DELETE', params: __assign(__assign({}, options.params), { version: version }) }))];
802
- }
803
- });
804
- });
805
- };
806
- /**
807
- * Unpublish a product by key
808
- *
809
- * Issues an 'unpublish' action for the given product:
810
- * https://docs.commercetools.com/api/projects/products#unpublish
811
- */
812
- CommercetoolsApi.prototype.unpublishProductByKey = function (options) {
813
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/key=".concat(options.key), method: 'POST', params: options.params, data: {
814
- version: options.version,
815
- actions: [{ action: 'unpublish' }],
816
- } }));
817
- };
818
- /**
819
- * Unpublish a product by id
820
- *
821
- * Issues an 'unpublish' action for the given product:
822
- * https://docs.commercetools.com/api/projects/products#unpublish
823
- */
824
- CommercetoolsApi.prototype.unpublishProductById = function (options) {
825
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/products/".concat(options.id), method: 'POST', params: options.params, data: {
826
- version: options.version,
827
- actions: [{ action: 'unpublish' }],
828
- } }));
829
- };
830
- /**
831
- * Create a category:
832
- * https://docs.commercetools.com/api/projects/categories#create-a-category
833
- */
834
- CommercetoolsApi.prototype.createCategory = function (options) {
835
- return this.request({
836
- path: "/categories",
837
- method: 'POST',
838
- data: options.data,
839
- });
840
- };
841
- /**
842
- * Update a category by key:
843
- * https://docs.commercetools.com/api/projects/categories#update-category-by-key
844
- */
845
- CommercetoolsApi.prototype.updateCategoryByKey = function (options) {
846
- return this.request({
847
- path: "/categories/key=".concat(options.key),
848
- method: 'POST',
849
- data: options.data,
850
- });
851
- };
852
- /**
853
- * Update a category by id:
854
- * https://docs.commercetools.com/api/projects/categories#update-category-by-id
855
- */
856
- CommercetoolsApi.prototype.updateCategoryById = function (options) {
857
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories/".concat(options.id), method: 'POST', data: options.data }));
858
- };
859
- /**
860
- * Delete a category by id:
861
- * https://docs.commercetools.com/api/projects/categories#delete-category-by-id
862
- */
863
- CommercetoolsApi.prototype.deleteCategoryById = function (options) {
864
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories/".concat(options.id), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }));
865
- };
866
- /**
867
- * Delete a category by key:
868
- * https://docs.commercetools.com/api/projects/categories#delete-category-by-key
869
- */
870
- CommercetoolsApi.prototype.deleteCategoryByKey = function (options) {
871
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/categories/key=".concat(options.key), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }));
872
- };
873
- /**
874
- * Create a customer account:
875
- * https://docs.commercetools.com/api/projects/customers#create-customer-sign-up
876
- */
877
- CommercetoolsApi.prototype.createAccount = function (options) {
878
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers", options.storeKey), method: 'POST', data: options.data }));
879
- };
880
- /**
881
- * Delete a customer account by id
882
- * https://docs.commercetools.com/api/projects/customers#delete-customer-by-id
883
- */
884
- CommercetoolsApi.prototype.deleteCustomerById = function (options) {
885
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers/".concat(options.id), options.storeKey), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version, dataErasure: options.dataErasure }) }));
886
- };
887
- /**
888
- * Delete a customer account by key
889
- * https://docs.commercetools.com/api/projects/customers#delete-customer-by-key
890
- */
891
- CommercetoolsApi.prototype.deleteCustomerByKey = function (options) {
892
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers/key=".concat(options.key), options.storeKey), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version, dataErasure: options.dataErasure }) }));
893
- };
894
- /**
895
- * Create a customer account given an (optional) anonymous access token:
896
- * https://docs.commercetools.com/api/projects/me-profile#create-customer-sign-up
897
- */
898
- CommercetoolsApi.prototype.createMyAccount = function (options) {
899
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/signup", options.storeKey), method: 'POST', data: options.data, accessToken: options.accessToken }));
900
- };
901
- /**
902
- * Login to customer's account given an (optional) anonymous access token:
903
- * https://docs.commercetools.com/api/projects/me-profile#authenticate-customer-sign-in
904
- */
905
- CommercetoolsApi.prototype.loginMyAccount = function (options) {
906
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/login", options.storeKey), method: 'POST', data: options.data, accessToken: options.accessToken }));
907
- };
908
- /**
909
- * Login to customer's account:
910
- * https://docs.commercetools.com/api/projects/customers#authenticate-customer-sign-in
911
- */
912
- CommercetoolsApi.prototype.login = function (options) {
913
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/login", options.storeKey), method: 'POST', data: options.data }));
914
- };
915
- /**
916
- * Get a customer's account/profile:
917
- * https://docs.commercetools.com/api/projects/me-profile#get-customer
918
- */
919
- CommercetoolsApi.prototype.getMyAccount = function (options) {
920
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me", options.storeKey), method: 'GET', accessToken: options.accessToken }));
921
- };
922
- /**
923
- * Update a customer's account/profile:
924
- * https://docs.commercetools.com/api/projects/me-profile#update-customer
925
- */
926
- CommercetoolsApi.prototype.updateMyAccount = function (options) {
927
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me", options.storeKey), method: 'POST', accessToken: options.accessToken, data: options.data }));
928
- };
929
- /**
930
- * Change the customer's password:
931
- * https://docs.commercetools.com/api/projects/me-profile#change-customers-password
932
- */
933
- CommercetoolsApi.prototype.changeMyPassword = function (options) {
934
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/password", options.storeKey), method: 'POST', accessToken: options.accessToken, data: options.data }));
935
- };
936
- /**
937
- * Reset the customer's password:
938
- * https://docs.commercetools.com/api/projects/me-profile#reset-customers-password
939
- */
940
- CommercetoolsApi.prototype.resetMyPassword = function (options) {
941
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/me/password/reset", options.storeKey), method: 'POST', accessToken: options.accessToken, data: options.data }));
942
- };
943
- /**
944
- * Get a password reset token
945
- * https://docs.commercetools.com/api/projects/customers#create-a-token-for-resetting-the-customers-password
946
- */
947
- CommercetoolsApi.prototype.getPasswordResetToken = function (options) {
948
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers/password-token", options.storeKey), method: 'POST', data: options.data }));
949
- };
950
- /**
951
- * Get a customer by id:
952
- * https://docs.commercetools.com/api/projects/customers#get-customer-by-id
953
- */
954
- CommercetoolsApi.prototype.getCustomerById = function (options) {
955
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers/".concat(options.id), options.storeKey), method: 'GET' }));
956
- };
957
- /**
958
- * Get a customer by key:
959
- * https://docs.commercetools.com/api/projects/customers#get-customer-by-key
960
- */
961
- CommercetoolsApi.prototype.getCustomerByKey = function (options) {
962
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers/key=".concat(options.key), options.storeKey), method: 'GET' }));
963
- };
964
- /**
965
- * Get a customer by password token:
966
- * https://docs.commercetools.com/api/projects/customers#get-customer-by-password-token
967
- */
968
- CommercetoolsApi.prototype.getCustomerByPasswordToken = function (options) {
969
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers/password-token=".concat(options.token), options.storeKey), method: 'GET' }));
970
- };
971
- /**
972
- * Query customers:
973
- * https://docs.commercetools.com/api/projects/customers#query-customers
974
- */
975
- CommercetoolsApi.prototype.queryCustomers = function (options) {
976
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: this.applyStore("/customers", options.storeKey), method: 'GET' }));
977
- };
978
- /**
979
- * Update a customer by id:
980
- * https://docs.commercetools.com/api/projects/customers#update-customer-by-id
981
- */
982
- CommercetoolsApi.prototype.updateCustomerById = function (options) {
983
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customers/".concat(options.id), method: 'POST', data: options.data }));
984
- };
985
- /**
986
- * Update a customer by key:
987
- * https://docs.commercetools.com/api/projects/customers#update-customer-by-key
988
- */
989
- CommercetoolsApi.prototype.updateCustomerByKey = function (options) {
990
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customers/key=".concat(options.key), method: 'POST', data: options.data }));
991
- };
992
- /**
993
- * Get a product type by id:
994
- * https://docs.commercetools.com/api/projects/productTypes#get-a-producttype-by-id
995
- */
996
- CommercetoolsApi.prototype.getProductTypeById = function (options) {
997
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-types/".concat(options.id), method: 'GET' }));
998
- };
999
- /**
1000
- * Get a product type by key:
1001
- * https://docs.commercetools.com/api/projects/productTypes#get-a-producttype-by-key
1002
- */
1003
- CommercetoolsApi.prototype.getProductTypeByKey = function (options) {
1004
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/product-types/key=".concat(options.key), method: 'GET' }));
1005
- };
1006
- /**
1007
- * Get a type by id:
1008
- * https://docs.commercetools.com/api/projects/types#get-type-by-id
1009
- */
1010
- CommercetoolsApi.prototype.getTypeById = function (options) {
1011
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/types/".concat(options.id), method: 'GET' }));
1012
- };
1013
- /**
1014
- * Get a type by key:
1015
- * https://docs.commercetools.com/api/projects/types#get-type-by-key
1016
- */
1017
- CommercetoolsApi.prototype.getTypeByKey = function (options) {
1018
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/types/key=".concat(options.key), method: 'GET' }));
1019
- };
1020
- /**
1021
- * Get a discount code by id:
1022
- * https://docs.commercetools.com/api/projects/discountCodes#get-discountcode-by-id
1023
- */
1024
- CommercetoolsApi.prototype.getDiscountCodeById = function (options) {
1025
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/discount-codes/".concat(options.id), method: 'GET' }));
1026
- };
1027
- /**
1028
- * Get a cart discount code by id:
1029
- * https://docs.commercetools.com/api/projects/cartDiscounts#get-cartdiscount-by-id
1030
- */
1031
- CommercetoolsApi.prototype.getCartDiscountById = function (options) {
1032
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/cart-discounts/".concat(options.id), method: 'GET' }));
1033
- };
1034
- /**
1035
- * Get a cart discount by key:
1036
- * https://docs.commercetools.com/api/projects/cartDiscounts#get-cartdiscount-by-key
1037
- */
1038
- CommercetoolsApi.prototype.getCartDiscountByKey = function (options) {
1039
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/cart-discounts/key=".concat(options.key), method: 'GET' }));
1040
- };
1041
- /**
1042
- * Query cart discounts:
1043
- * https://docs.commercetools.com/api/projects/cartDiscounts#query-cartdiscounts
1044
- */
1045
- CommercetoolsApi.prototype.queryCartDiscounts = function (options) {
1046
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/cart-discounts", method: 'GET' }));
1047
- };
1048
- /**
1049
- * Get a custom object
1050
- * https://docs.commercetools.com/api/projects/custom-objects#get-customobject-by-container-and-key
1051
- */
1052
- CommercetoolsApi.prototype.getCustomObject = function (options) {
1053
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/custom-objects/".concat(options.container, "/").concat(options.key), method: 'GET' }));
1054
- };
1055
- /**
1056
- * Get a custom object by id
1057
- * https://docs.commercetools.com/api/projects/custom-objects#get-customobject
1058
- */
1059
- CommercetoolsApi.prototype.getCustomObjectById = function (options) {
1060
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/custom-objects/".concat(options.id), method: 'GET' }));
1061
- };
1062
- /**
1063
- * Save a custom object
1064
- * https://docs.commercetools.com/api/projects/custom-objects#create-or-update-a-customobject
1065
- */
1066
- CommercetoolsApi.prototype.saveCustomObject = function (options) {
1067
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/custom-objects", method: 'POST', data: options.data }));
1068
- };
1069
- /**
1070
- * Delete a custom object
1071
- * https://docs.commercetools.com/api/projects/custom-objects#delete-customobject-by-container-and-key
1072
- */
1073
- CommercetoolsApi.prototype.deleteCustomObject = function (options) {
1074
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/custom-objects/".concat(options.container, "/").concat(options.key), method: 'DELETE' }));
1075
- };
1076
- /**
1077
- * Get a shipping method by id:
1078
- * https://docs.commercetools.com/api/projects/shippingMethods#get-shippingmethod-by-id
1079
- */
1080
- CommercetoolsApi.prototype.getShippingMethodById = function (options) {
1081
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/shipping-methods/".concat(options.id), method: 'GET' }));
1082
- };
1083
- /**
1084
- * Get a shipping method by key:
1085
- * https://docs.commercetools.com/api/projects/shippingMethods#get-shippingmethod-by-key
1086
- */
1087
- CommercetoolsApi.prototype.getShippingMethodByKey = function (options) {
1088
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/shipping-methods/key=".concat(options.key), method: 'GET' }));
1089
- };
1090
- /**
1091
- * Get shipping method by location:
1092
- * https://docs.commercetools.com/api/projects/shippingMethods#get-shippingmethods-for-a-location
1093
- */
1094
- CommercetoolsApi.prototype.getShippingMethodsForLocation = function (options) {
1095
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(__assign(__assign({}, options), { params: __assign(__assign({}, options === null || options === void 0 ? void 0 : options.params), { country: options.country, state: options.state, currency: options.currency }) }))), { path: "/shipping-methods/matching-location", method: 'GET', accessToken: options.accessToken }));
1096
- };
1097
- /**
1098
- * Get shipping methods applicable to a given cart id:
1099
- * https://docs.commercetools.com/api/projects/shippingMethods#get-shippingmethods-for-a-cart
1100
- */
1101
- CommercetoolsApi.prototype.getShippingMethodsForCart = function (options) {
1102
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(__assign(__assign({}, options), { params: __assign(__assign({}, options === null || options === void 0 ? void 0 : options.params), { cartId: options.cartId }) }))), { path: "/shipping-methods/matching-cart", method: 'GET', accessToken: options.accessToken }));
1103
- };
1104
- /**
1105
- * Query shipping methods:
1106
- * https://docs.commercetools.com/api/projects/shippingMethods#query-shippingmethods
1107
- */
1108
- CommercetoolsApi.prototype.queryShippingMethods = function (options) {
1109
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/shipping-methods", method: 'GET' }));
1110
- };
1111
- /**
1112
- * Get an individual customer group by id:
1113
- * https://docs.commercetools.com/api/projects/customerGroups#get-customergroup-by-id
1114
- */
1115
- CommercetoolsApi.prototype.getCustomerGroupById = function (options) {
1116
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customer-groups/".concat(options.id), method: 'GET' }));
1117
- };
1118
- /**
1119
- * Get an individual customer group by key:
1120
- * https://docs.commercetools.com/api/projects/customerGroups#get-customergroup-by-key
1121
- */
1122
- CommercetoolsApi.prototype.getCustomerGroupByKey = function (options) {
1123
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customer-groups/key=".concat(options.key), method: 'GET' }));
1124
- };
1125
- /**
1126
- * Query customer groups:
1127
- * https://docs.commercetools.com/api/projects/customerGroups#query-customergroups
1128
- */
1129
- CommercetoolsApi.prototype.queryCustomerGroups = function (options) {
1130
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customer-groups", method: 'GET' }));
1131
- };
1132
- /**
1133
- * Create a customer group:
1134
- * https://docs.commercetools.com/api/projects/customerGroups#create-a-customergroup
1135
- */
1136
- CommercetoolsApi.prototype.createCustomerGroup = function (options) {
1137
- return this.request({
1138
- path: "/customer-groups",
1139
- method: 'POST',
1140
- data: options.data,
1141
- });
1142
- };
1143
- /**
1144
- * Update a customer group by key:
1145
- * https://docs.commercetools.com/api/projects/customerGroups#update-customergroup-by-key
1146
- */
1147
- CommercetoolsApi.prototype.updateCustomerGroupByKey = function (options) {
1148
- return this.request({
1149
- path: "/customer-groups/key=".concat(options.key),
1150
- method: 'POST',
1151
- data: options.data,
1152
- });
1153
- };
1154
- /**
1155
- * Update a customer group by id:
1156
- * https://docs.commercetools.com/api/projects/customerGroups#update-customergroup-by-id
1157
- */
1158
- CommercetoolsApi.prototype.updateCustomerGroupById = function (options) {
1159
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customer-groups/".concat(options.id), method: 'POST', data: options.data }));
1160
- };
1161
- /**
1162
- * Delete a customer group by id:
1163
- * https://docs.commercetools.com/api/projects/customerGroups#delete-customergroup-by-id
1164
- */
1165
- CommercetoolsApi.prototype.deleteCustomerGroupById = function (options) {
1166
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customer-groups/".concat(options.id), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }));
1167
- };
1168
- /**
1169
- * Delete a customer group by key:
1170
- * https://docs.commercetools.com/api/projects/customerGroups#delete-customergroup-by-key
1171
- */
1172
- CommercetoolsApi.prototype.deleteCustomerGroupByKey = function (options) {
1173
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/customer-groups/key=".concat(options.key), method: 'DELETE', params: __assign(__assign({}, options.params), { version: options.version }) }));
1174
- };
1175
- /**
1176
- * Execute a GraphQL statement:
1177
- * https://docs.commercetools.com/api/graphql
1178
- */
1179
- CommercetoolsApi.prototype.graphql = function (options) {
1180
- return this.request(__assign(__assign({}, this.extractCommonRequestOptions(options)), { path: "/graphql", method: 'POST', data: options.data, accessToken: options.accessToken }));
1181
- };
1182
- /**
1183
- * Make the request to the commercetools REST API.
1184
- */
1185
- CommercetoolsApi.prototype.request = function (options) {
1186
- return __awaiter(this, void 0, void 0, function () {
1187
- var requestConfig, retryConfig, retryCount, lastError, _loop_1, this_1, state_1;
1188
- return __generator(this, function (_a) {
1189
- switch (_a.label) {
1190
- case 0: return [4 /*yield*/, this.getRequestOptions(options)];
1191
- case 1:
1192
- requestConfig = _a.sent();
1193
- retryConfig = this.getRetryConfig(options.retry);
1194
- retryCount = 0;
1195
- _loop_1 = function () {
1196
- var delay_1, response, error_2;
1197
- return __generator(this, function (_b) {
1198
- switch (_b.label) {
1199
- case 0:
1200
- if (!(retryCount > 0)) return [3 /*break*/, 2];
1201
- delay_1 = this_1.calculateDelay(retryCount, retryConfig);
1202
- return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, delay_1); })];
1203
- case 1:
1204
- _b.sent();
1205
- _b.label = 2;
1206
- case 2:
1207
- _b.trys.push([2, 4, , 5]);
1208
- return [4 /*yield*/, this_1.axios(requestConfig)];
1209
- case 3:
1210
- response = _b.sent();
1211
- return [2 /*return*/, { value: response.data }];
1212
- case 4:
1213
- error_2 = _b.sent();
1214
- if (this_1.isRetryableError(error_2)) {
1215
- lastError = error_2;
1216
- }
1217
- else {
1218
- throw this_1.transformError(error_2);
1219
- }
1220
- return [3 /*break*/, 5];
1221
- case 5:
1222
- retryCount++;
1223
- return [2 /*return*/];
1224
- }
1225
- });
1226
- };
1227
- this_1 = this;
1228
- _a.label = 2;
1229
- case 2: return [5 /*yield**/, _loop_1()];
1230
- case 3:
1231
- state_1 = _a.sent();
1232
- if (typeof state_1 === "object")
1233
- return [2 /*return*/, state_1.value];
1234
- _a.label = 4;
1235
- case 4:
1236
- if (retryCount <= retryConfig.maxRetries) return [3 /*break*/, 2];
1237
- _a.label = 5;
1238
- case 5: throw this.transformError(lastError);
1239
- }
1240
- });
1241
- });
1242
- };
1243
- /**
1244
- * Get the {@see CommercetoolsRetryConfig} for this request.
1245
- * Uses the class instance's retry config and merges and additional
1246
- * config passed in via the request options.
1247
- */
1248
- CommercetoolsApi.prototype.getRetryConfig = function (methodRetryConfig) {
1249
- return methodRetryConfig || this.retry;
1250
- };
1251
- /**
1252
- * Generate request options. These are then fed in to axios when
1253
- * making the request to commercetools.
1254
- */
1255
- CommercetoolsApi.prototype.getRequestOptions = function (options) {
1256
- return __awaiter(this, void 0, void 0, function () {
1257
- var accessToken, url, opts, grant, headers;
1258
- return __generator(this, function (_a) {
1259
- switch (_a.label) {
1260
- case 0:
1261
- accessToken = options.accessToken;
1262
- url = "".concat(this.endpoints.api, "/").concat(this.config.projectKey).concat(options.path);
1263
- opts = __assign({}, options);
1264
- opts.path && delete opts.path;
1265
- opts.accessToken && delete opts.accessToken;
1266
- if (!!accessToken) return [3 /*break*/, 2];
1267
- return [4 /*yield*/, this.auth.getClientGrant()];
1268
- case 1:
1269
- grant = _a.sent();
1270
- accessToken = grant.accessToken;
1271
- _a.label = 2;
1272
- case 2:
1273
- headers = __assign(__assign(__assign({}, this.axios.defaults.headers), { Authorization: "Bearer ".concat(accessToken) }), opts.headers);
1274
- if (typeof options.correlationId === 'string' && options.correlationId !== '') {
1275
- headers['X-Correlation-ID'] = options.correlationId;
1276
- delete options.correlationId;
1277
- }
1278
- return [2 /*return*/, __assign(__assign({}, opts), { url: url, headers: headers })];
1279
- }
1280
- });
1281
- });
1282
- };
1283
- /**
1284
- * Calculate how long to delay before running the request.
1285
- * For each retry attempt, we increase the time that we delay for.
1286
- */
1287
- CommercetoolsApi.prototype.calculateDelay = function (retryCount, retryConfig) {
1288
- if (!retryConfig || retryCount === 0) {
1289
- return 0;
1290
- }
1291
- // `retryCount` will be at least 1 at this point
1292
- var exponentialDelay = retryConfig.delayMs * Math.pow(2, (retryCount - 1));
1293
- if (retryConfig.jitter) {
1294
- // A 'full' jitter calculation usually just calculates the delay value
1295
- // by selecting a random value between zero and the standard exponential
1296
- // delay value (calculated above). This next line increases the exponential
1297
- // delay by a factor that reduces for each further request. This ensures
1298
- // more variance on earlier requests.
1299
- var increasedDelay = exponentialDelay * (1 + 1 / (retryCount + 1));
1300
- return Math.floor(Math.random() * increasedDelay);
1301
- }
1302
- else {
1303
- // Assuming a `delayMs` of 500, you'd get the following responses for
1304
- // the given `retryCount` value: 1 = 500, 2 = 1000, 3 = 2000, 4 = 4000
1305
- return exponentialDelay;
1306
- }
1307
- };
1308
- /**
1309
- * Determine whether the given error means we should allow the request
1310
- * to be retried (assuming retry config is provided).
1311
- */
1312
- CommercetoolsApi.prototype.isRetryableError = function (error) {
1313
- // If the error isn't an axios error, then something serious
1314
- // went wrong. Probably a coding error in this package. We should
1315
- // never really hit this scenario.
1316
- if (!error.isAxiosError) {
1317
- return true;
1318
- }
1319
- // If axios makes a request successfully, the `request` property will
1320
- // be defined. Equally, if it received a response, the `response` property
1321
- // will be defined. If either is not defined then we assume there was
1322
- // a serious connectivity issue and allow the request to be retried.
1323
- if (!error.request || !error.response) {
1324
- return true;
1325
- }
1326
- // Finally we only allow requests to be retried if the status code
1327
- // returned is in the given list
1328
- // @TODO - commercetools returns a 502 error when an extension returns
1329
- // update actions that are not actionable by commercetools:
1330
- // https://docs.commercetools.com/api/errors#502-extension-update-actions-failed
1331
- // In this scenario, we should probably not retry the request as
1332
- // this may result in successful update actions being re-run
1333
- return RETRYABLE_STATUS_CODES.includes(error.response.status);
1334
- };
1335
- /**
1336
- * Type-guard against any additional unexpected properties being passed in.
1337
- */
1338
- CommercetoolsApi.prototype.extractCommonRequestOptions = function (options) {
1339
- if (!options) {
1340
- return {};
1341
- }
1342
- return {
1343
- correlationId: options.correlationId,
1344
- params: options.params,
1345
- retry: options.retry,
1346
- };
1347
- };
1348
- /**
1349
- * Applies the store key to a given path
1350
- */
1351
- CommercetoolsApi.prototype.applyStore = function (path, storeKey) {
1352
- if (typeof storeKey === 'string' && storeKey !== '') {
1353
- return "/in-store/key=".concat(storeKey).concat(path);
1354
- }
1355
- else if (this.config.storeKey) {
1356
- return "/in-store/key=".concat(this.config.storeKey).concat(path);
1357
- }
1358
- return path;
1359
- };
1360
- /**
1361
- * Transform an unknown error in to a {@see CommercetoolsError}
1362
- * if the error we receive is from axios.
1363
- */
1364
- CommercetoolsApi.prototype.transformError = function (lastError) {
1365
- if (lastError.isAxiosError) {
1366
- return error_1.CommercetoolsError.fromAxiosError(lastError);
1367
- }
1368
- return lastError;
1369
- };
1370
- /**
1371
- * Ensure that all required properties on the {@see CommercetoolsApiConfig}
1372
- * object have been populated. These are currently:
1373
- *
1374
- * projectKey: string
1375
- * clientId: string
1376
- * clientSecret: string
1377
- * region: Region
1378
- * clientScopes: string[]
1379
- */
1380
- CommercetoolsApi.validateConfig = function (config) {
1381
- var errors = [];
1382
- if (!config) {
1383
- errors.push('The config object missing or empty');
1384
- }
1385
- else {
1386
- if (!config.projectKey) {
1387
- errors.push('The `projectKey` property is empty');
1388
- }
1389
- else if (typeof config.projectKey !== 'string') {
1390
- errors.push('The `projectKey` property must be a string');
1391
- }
1392
- if (!config.clientId) {
1393
- errors.push('The `clientId` property is empty');
1394
- }
1395
- else if (typeof config.clientId !== 'string') {
1396
- errors.push('The `clientId` property must be a string');
1397
- }
1398
- if (!config.clientSecret) {
1399
- errors.push('The `clientSecret` property is empty');
1400
- }
1401
- else if (typeof config.clientSecret !== 'string') {
1402
- errors.push('The `clientSecret` property must be a string');
1403
- }
1404
- if (!Array.isArray(config.clientScopes)) {
1405
- errors.push('The `clientScopes` property must be an array');
1406
- }
1407
- else if (config.clientScopes.length === 0) {
1408
- errors.push('The `clientScopes` property must have at least 1 scope defined');
1409
- }
1410
- }
1411
- if (errors.length) {
1412
- throw new error_1.CommercetoolsError('The configuration object passed in to the `CommercetoolsApi` constructor is not valid: \n' +
1413
- errors.map(function (error) { return "\u2022 ".concat(error); }).join('\n'));
1414
- }
1415
- };
1416
- return CommercetoolsApi;
1417
- }());
1418
- exports.CommercetoolsApi = CommercetoolsApi;
1419
- //# sourceMappingURL=CommercetoolsApi.js.map