@commercetools/sdk-client-v2 1.0.2 → 1.3.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 CHANGED
@@ -1,5 +1,33 @@
1
1
  # @commercetools/sdk-client-v2
2
2
 
3
+ ## 1.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#241](https://github.com/commercetools/commercetools-sdk-typescript/pull/241) [`85f5be3`](https://github.com/commercetools/commercetools-sdk-typescript/commit/85f5be349a9b0fa46539259981bfd8d5fc2ffdc6) Thanks [@ajimae](https://github.com/ajimae)! - Releasing the TS SDK with the following changelogs
8
+
9
+ - added functionalities to extend client user agent
10
+ - custom field added to OrderFromCardDraft
11
+
12
+ ## 1.2.0
13
+
14
+ ### Minor Changes
15
+
16
+ - [#211](https://github.com/commercetools/commercetools-sdk-typescript/pull/211) [`f3c1e3e`](https://github.com/commercetools/commercetools-sdk-typescript/commit/f3c1e3ea0ca000b309eca1de6163c3ad065d526f) Thanks [@jherey](https://github.com/jherey)! - - Change Import Summaries `processingState` to `processingstate`.
17
+ - Add `sort` to `ByProjectKeyShippingMethodsMatchingLocationRequestBuilder`.
18
+ - New `MyCustomerResetPassword` model added to `ByProjectKeyMePasswordResetRequestBuilder` class.
19
+ - Other changes are detailed here: https://github.com/commercetools/commercetools-sdk-typescript/pull/192/files.
20
+
21
+ ## 1.1.0
22
+
23
+ ### Minor Changes
24
+
25
+ - [#188](https://github.com/commercetools/commercetools-sdk-typescript/pull/188) [`4c2d9b6`](https://github.com/commercetools/commercetools-sdk-typescript/commit/4c2d9b64b204200dffbeb18130239138dd2ad7d3) Thanks [@ajimae](https://github.com/ajimae)! - February package release
26
+
27
+ ### Patch Changes
28
+
29
+ - [#149](https://github.com/commercetools/commercetools-sdk-typescript/pull/149) [`08caea9`](https://github.com/commercetools/commercetools-sdk-typescript/commit/08caea93560c01e2158f018538b7a2b9f4be39c1) Thanks [@renovate](https://github.com/apps/renovate)! - fix(deps): update all dependencies
30
+
3
31
  ## 1.0.2
4
32
 
5
33
  ### Patch Changes
package/README.md CHANGED
@@ -50,7 +50,7 @@ const authMiddlewareOptions = {
50
50
  clientId: 'mc-client-id',
51
51
  clientSecret: 'mc-client-secrets',
52
52
  },
53
- oauthUri: 'https://auth.europe-west1.gcp.commercetools.com',
53
+ oauthUri: '/oauth/token', // - optional: custom oauthUri
54
54
  scopes: [`manage_project:${projectKey}`],
55
55
  fetch,
56
56
  }
@@ -39,13 +39,104 @@ function validate(funcName, request, options = {
39
39
  if (!options.allowedMethods.includes(request.method)) throw new Error(`The "${funcName}" Request object requires a valid method. See https://commercetools.github.io/nodejs/sdk/Glossary.html#clientrequest`);
40
40
  }
41
41
 
42
+ let _options;
43
+
42
44
  function compose(...funcs) {
43
45
  funcs = funcs.filter(func => typeof func === 'function');
44
46
  if (funcs.length === 1) return funcs[0];
45
47
  return funcs.reduce((a, b) => (...args) => a(b(...args)));
46
48
  }
47
49
 
50
+ function process$1(request, fn, processOpt) {
51
+ validate('process', request, {
52
+ allowedMethods: ['GET']
53
+ });
54
+ if (typeof fn !== 'function') throw new Error('The "process" function accepts a "Function" as a second argument that returns a Promise. See https://commercetools.github.io/nodejs/sdk/api/sdkClient.html#processrequest-processfn-options'); // Set default process options
55
+
56
+ const opt = {
57
+ total: Number.POSITIVE_INFINITY,
58
+ accumulate: true,
59
+ ...processOpt
60
+ };
61
+ return new Promise((resolve, reject) => {
62
+ let _path,
63
+ _queryString = '';
64
+
65
+ if (request && request.uri) {
66
+ const [path, queryString] = request.uri.split('?');
67
+ _path = path;
68
+ _queryString = queryString;
69
+ }
70
+
71
+ const requestQuery = { ...qs__default["default"].parse(_queryString)
72
+ };
73
+ const query = {
74
+ // defaults
75
+ limit: 20,
76
+ // merge given query params
77
+ ...requestQuery
78
+ };
79
+ let hasFirstPageBeenProcessed = false;
80
+ let itemsToGet = opt.total;
81
+
82
+ const processPage = async (lastId, acc = []) => {
83
+ // Use the lesser value between limit and itemsToGet in query
84
+ const limit = query.limit < itemsToGet ? query.limit : itemsToGet;
85
+ const originalQueryString = qs__default["default"].stringify({ ...query,
86
+ limit
87
+ });
88
+ const enhancedQuery = {
89
+ sort: 'id asc',
90
+ withTotal: false,
91
+ ...(lastId ? {
92
+ where: `id > "${lastId}"`
93
+ } : {})
94
+ };
95
+ const enhancedQueryString = qs__default["default"].stringify(enhancedQuery);
96
+ const enhancedRequest = { ...request,
97
+ uri: `${_path}?${enhancedQueryString}&${originalQueryString}`
98
+ };
99
+
100
+ try {
101
+ const payload = await createClient(_options).execute(enhancedRequest);
102
+ const {
103
+ results,
104
+ count: resultsLength
105
+ } = payload.body;
106
+
107
+ if (!resultsLength && hasFirstPageBeenProcessed) {
108
+ return resolve(acc || []);
109
+ }
110
+
111
+ const result = await Promise.resolve(fn(payload));
112
+ let accumulated;
113
+ hasFirstPageBeenProcessed = true;
114
+ if (opt.accumulate) accumulated = acc.concat(result || []);
115
+ itemsToGet -= resultsLength; // If there are no more items to get, it means the total number
116
+ // of items in the original request have been fetched so we
117
+ // resolve the promise.
118
+ // Also, if we get less results in a page then the limit set it
119
+ // means that there are no more pages and that we can finally
120
+ // resolve the promise.
121
+
122
+ if (resultsLength < query.limit || !itemsToGet) {
123
+ return resolve(accumulated || []);
124
+ }
125
+
126
+ const last = results[resultsLength - 1];
127
+ const newLastId = last && last.id;
128
+ processPage(newLastId, accumulated);
129
+ } catch (error) {
130
+ reject(error);
131
+ }
132
+ }; // Start iterating through pages
133
+
134
+
135
+ processPage();
136
+ });
137
+ }
48
138
  function createClient(options) {
139
+ _options = options;
49
140
  if (!options) throw new Error('Missing required options');
50
141
  if (options.middlewares && !Array.isArray(options.middlewares)) throw new Error('Middlewares should be an array');
51
142
  if (!options.middlewares || !Array.isArray(options.middlewares) || !options.middlewares.length) throw new Error('You need to provide at least one middleware');
@@ -53,6 +144,8 @@ function createClient(options) {
53
144
  /**
54
145
  * Given a request object,
55
146
  */
147
+ process: process$1,
148
+
56
149
  execute(request) {
57
150
  validate('exec', request);
58
151
  return new Promise((resolve, reject) => {
@@ -81,95 +174,6 @@ function createClient(options) {
81
174
  error: undefined
82
175
  });
83
176
  });
84
- },
85
-
86
- process(request, fn, processOpt) {
87
- validate('process', request, {
88
- allowedMethods: ['GET']
89
- });
90
- if (typeof fn !== 'function') throw new Error('The "process" function accepts a "Function" as a second argument that returns a Promise. See https://commercetools.github.io/nodejs/sdk/api/sdkClient.html#processrequest-processfn-options'); // Set default process options
91
-
92
- const opt = {
93
- total: Number.POSITIVE_INFINITY,
94
- accumulate: true,
95
- ...processOpt
96
- };
97
- return new Promise((resolve, reject) => {
98
- let _path,
99
- _queryString = '';
100
-
101
- if (request && request.uri) {
102
- const [path, queryString] = request.uri.split('?');
103
- _path = path;
104
- _queryString = queryString;
105
- }
106
-
107
- const requestQuery = { ...qs__default["default"].parse(_queryString)
108
- };
109
- const query = {
110
- // defaults
111
- limit: 20,
112
- // merge given query params
113
- ...requestQuery
114
- };
115
- let hasFirstPageBeenProcessed = false;
116
- let itemsToGet = opt.total;
117
-
118
- const processPage = async (lastId, acc = []) => {
119
- // Use the lesser value between limit and itemsToGet in query
120
- const limit = query.limit < itemsToGet ? query.limit : itemsToGet;
121
- const originalQueryString = qs__default["default"].stringify({ ...query,
122
- limit
123
- });
124
- const enhancedQuery = {
125
- sort: 'id asc',
126
- withTotal: false,
127
- ...(lastId ? {
128
- where: `id > "${lastId}"`
129
- } : {})
130
- };
131
- const enhancedQueryString = qs__default["default"].stringify(enhancedQuery);
132
- const enhancedRequest = { ...request,
133
- uri: `${_path}?${enhancedQueryString}&${originalQueryString}`
134
- };
135
-
136
- try {
137
- const payload = await this.execute(enhancedRequest);
138
- const {
139
- results,
140
- count: resultsLength
141
- } = payload.body;
142
-
143
- if (!resultsLength && hasFirstPageBeenProcessed) {
144
- return resolve(acc || []);
145
- }
146
-
147
- const result = await Promise.resolve(fn(payload));
148
- let accumulated;
149
- hasFirstPageBeenProcessed = true;
150
- if (opt.accumulate) accumulated = acc.concat(result || []);
151
- itemsToGet -= resultsLength; // If there are no more items to get, it means the total number
152
- // of items in the original request have been fetched so we
153
- // resolve the promise.
154
- // Also, if we get less results in a page then the limit set it
155
- // means that there are no more pages and that we can finally
156
- // resolve the promise.
157
-
158
- if (resultsLength < query.limit || !itemsToGet) {
159
- return resolve(accumulated || []);
160
- }
161
-
162
- const last = results[resultsLength - 1];
163
- const newLastId = last && last.id;
164
- processPage(newLastId, accumulated);
165
- } catch (error) {
166
- reject(error);
167
- }
168
- }; // Start iterating through pages
169
-
170
-
171
- processPage();
172
- });
173
177
  }
174
178
 
175
179
  };
@@ -749,7 +753,8 @@ function createHttpMiddleware({
749
753
  maxRetries = 10,
750
754
  backoff = true,
751
755
  retryDelay = 200,
752
- maxDelay = Infinity
756
+ maxDelay = Infinity,
757
+ retryCodes = [503]
753
758
  } = {},
754
759
  fetch: fetcher,
755
760
  getAbortController
@@ -767,20 +772,29 @@ function createHttpMiddleware({
767
772
  fetchFunction = fetch;
768
773
  }
769
774
 
775
+ if (!Array.isArray(retryCodes)) {
776
+ throw new Error('`retryCodes` option must be an array of retry status (error) codes.');
777
+ }
778
+
770
779
  return next => (request, response) => {
771
780
  let abortController;
772
781
  if (timeout || getAbortController) abortController = (getAbortController ? getAbortController() : null) || new AbortController();
773
782
  const url = host.replace(/\/$/, '') + request.uri;
774
- const body = typeof request.body === 'string' || Buffer.isBuffer(request.body) ? request.body : // NOTE: `stringify` of `null` gives the String('null')
775
- JSON.stringify(request.body || undefined);
776
783
  const requestHeader = { ...request.headers
777
- };
784
+ }; // Unset the content-type header if explicitly asked to (passing `null` as value).
778
785
 
779
- if (!Object.prototype.hasOwnProperty.call(requestHeader, 'Content-Type')) {
780
- requestHeader['Content-Type'] = 'application/json';
786
+ if (requestHeader['Content-Type'] === null) {
787
+ delete requestHeader['Content-Type'];
781
788
  }
782
789
 
783
- if (body) {
790
+ if (!Object.prototype.hasOwnProperty.call(requestHeader, 'Content-Type') || !Object.prototype.hasOwnProperty.call(requestHeader, 'content-type')) {
791
+ requestHeader['Content-Type'] = 'application/json';
792
+ } // Ensure body is a string if content type is application/json
793
+
794
+
795
+ const body = requestHeader['Content-Type'] === 'application/json' && typeof request.body === 'string' || Buffer.isBuffer(request.body) ? request.body : JSON.stringify(request.body || undefined);
796
+
797
+ if (body && (typeof body === 'string' || Buffer.isBuffer(body))) {
784
798
  requestHeader['Content-Length'] = Buffer.byteLength(body).toString();
785
799
  }
786
800
 
@@ -849,15 +863,25 @@ function createHttpMiddleware({
849
863
  next(request, parsedResponse);
850
864
  });
851
865
  return;
852
- }
853
-
854
- if (res.status === 503 && enableRetry) if (retryCount < maxRetries) {
855
- setTimeout(executeFetch, calcDelayDuration(retryCount, retryDelay, maxRetries, backoff, maxDelay));
856
- retryCount += 1;
857
- return;
858
- } // Server responded with an error. Try to parse it as JSON, then
866
+ } // if (res.status === 503 && enableRetry)
867
+ // if (retryCount < maxRetries) {
868
+ // setTimeout(
869
+ // executeFetch,
870
+ // calcDelayDuration(
871
+ // retryCount,
872
+ // retryDelay,
873
+ // maxRetries,
874
+ // backoff,
875
+ // maxDelay
876
+ // )
877
+ // )
878
+ // retryCount += 1
879
+ // return
880
+ // }
881
+ // Server responded with an error. Try to parse it as JSON, then
859
882
  // return a proper error type with all necessary meta information.
860
883
 
884
+
861
885
  res.text().then(text => {
862
886
  // Try to parse the error response as JSON
863
887
  let parsed;
@@ -881,6 +905,15 @@ function createHttpMiddleware({
881
905
  body: parsed
882
906
  })
883
907
  });
908
+
909
+ if (enableRetry && (retryCodes.indexOf(error.statusCode) !== -1 || (retryCodes === null || retryCodes === void 0 ? void 0 : retryCodes.indexOf(error.message)) !== -1)) {
910
+ if (retryCount < maxRetries) {
911
+ setTimeout(executeFetch, calcDelayDuration(retryCount, retryDelay, maxRetries, backoff, maxDelay));
912
+ retryCount += 1;
913
+ return;
914
+ }
915
+ }
916
+
884
917
  maskAuthData(error.originalRequest, maskSensitiveHeaderData); // Let the final resolver to reject the promise
885
918
 
886
919
  const parsedResponse = { ...response,
@@ -982,7 +1015,7 @@ function createQueueMiddleware({
982
1015
 
983
1016
  var packageJson = {
984
1017
  name: "@commercetools/sdk-client-v2",
985
- version: "1.0.2",
1018
+ version: "1.3.0",
986
1019
  description: "commercetools TypeScript SDK client.",
987
1020
  keywords: [
988
1021
  "commercetools",
@@ -1029,10 +1062,10 @@ var packageJson = {
1029
1062
  devDependencies: {
1030
1063
  "abort-controller": "3.0.0",
1031
1064
  "common-tags": "1.8.2",
1032
- dotenv: "10.0.0",
1033
- jest: "27.3.1",
1065
+ dotenv: "16.0.0",
1066
+ jest: "27.4.7",
1034
1067
  nock: "12.0.3",
1035
- "organize-imports-cli": "0.8.0"
1068
+ "organize-imports-cli": "0.9.0"
1036
1069
  },
1037
1070
  scripts: {
1038
1071
  organize_imports: "find src -type f -name '*.ts' | xargs organize-imports-cli",
@@ -1073,12 +1106,14 @@ function createUserAgent(options) {
1073
1106
  let contactInfo = null;
1074
1107
  if (options.contactUrl && !options.contactEmail) contactInfo = `(+${options.contactUrl})`;else if (!options.contactUrl && options.contactEmail) contactInfo = `(+${options.contactEmail})`;else if (options.contactUrl && options.contactEmail) contactInfo = `(+${options.contactUrl}; +${options.contactEmail})`; // System info
1075
1108
 
1076
- const systemInfo = getSystemInfo();
1077
- return [baseInfo, systemInfo, libraryInfo, contactInfo].filter(Boolean).join(' ');
1109
+ const systemInfo = getSystemInfo(); // customName
1110
+
1111
+ const customAgent = options.customAgent || '';
1112
+ return [baseInfo, systemInfo, libraryInfo, contactInfo, customAgent].filter(Boolean).join(' ');
1078
1113
  }
1079
1114
 
1080
- function createUserAgentMiddleware() {
1081
- const userAgent = createUserAgent({
1115
+ function createUserAgentMiddleware(options) {
1116
+ const userAgent = createUserAgent({ ...options,
1082
1117
  name: `commercetools-sdk-javascript-v2/${packageJson.version}`
1083
1118
  });
1084
1119
  return next => (request, response) => {
@@ -1219,8 +1254,8 @@ class ClientBuilder {
1219
1254
  return this;
1220
1255
  }
1221
1256
 
1222
- withUserAgentMiddleware() {
1223
- this.userAgentMiddleware = createUserAgentMiddleware();
1257
+ withUserAgentMiddleware(options) {
1258
+ this.userAgentMiddleware = createUserAgentMiddleware(options);
1224
1259
  return this;
1225
1260
  }
1226
1261
 
@@ -1261,6 +1296,7 @@ class ClientBuilder {
1261
1296
  }
1262
1297
 
1263
1298
  exports.ClientBuilder = ClientBuilder;
1299
+ exports.Process = process$1;
1264
1300
  exports.createAuthForAnonymousSessionFlow = createAuthMiddlewareForAnonymousSessionFlow$1;
1265
1301
  exports.createAuthForClientCredentialsFlow = createAuthMiddlewareForClientCredentialsFlow$1;
1266
1302
  exports.createAuthForPasswordFlow = createAuthMiddlewareForPasswordFlow$1;