@constructor-io/constructorio-node 4.2.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-node",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "description": "Constructor.io Node.js client",
5
5
  "main": "src/constructorio.js",
6
6
  "scripts": {
@@ -34,14 +34,14 @@ function createCatalogUrl(path, options, additionalQueryParams = {}, apiVersion
34
34
  // Convert a read stream to buffer
35
35
  function convertToBuffer(stream) {
36
36
  return new Promise((resolve, reject) => {
37
- let buffer = '';
37
+ const chunks = [];
38
38
 
39
39
  stream.on('data', (chunk) => {
40
- buffer += chunk;
40
+ chunks.push(chunk);
41
41
  });
42
42
 
43
43
  stream.on('end', () => {
44
- resolve(buffer);
44
+ resolve(Buffer.concat(chunks));
45
45
  });
46
46
 
47
47
  stream.on('error', (err) => {
@@ -118,6 +118,37 @@ async function createQueryParamsAndFormData(parameters) {
118
118
  return { queryParams, formData };
119
119
  }
120
120
 
121
+ async function addTarArchiveToFormData(parameters, formData, operation, apiKey) {
122
+ try {
123
+ const { section } = parameters;
124
+ let { tarArchive } = parameters;
125
+
126
+ // Convert tarArchive to buffer if passed as stream
127
+ if (tarArchive instanceof fs.ReadStream || tarArchive instanceof Duplex) {
128
+ tarArchive = await convertToBuffer(tarArchive);
129
+ }
130
+
131
+ // Pull tarArchive from parameters
132
+ if (tarArchive && formData && operation && apiKey && section) {
133
+ // Convert timestamp to YYYY-MM-DD-HH-MM-SS format
134
+ const formattedDateTime = new Date()
135
+ .toISOString()
136
+ .replace('T', '-')
137
+ .replace(/:/g, '-')
138
+ .slice(0, 19);
139
+ const filename = `${apiKey}_${section}_${operation}_${formattedDateTime}.tar.gz`;
140
+
141
+ formData.append(filename, tarArchive, {
142
+ filename,
143
+ });
144
+ }
145
+ } catch (error) {
146
+ throw new Error(error);
147
+ }
148
+
149
+ return formData;
150
+ }
151
+
121
152
  /**
122
153
  * Interface to catalog related API calls
123
154
  *
@@ -2207,6 +2238,155 @@ class Catalog {
2207
2238
  }
2208
2239
  }
2209
2240
 
2241
+ /**
2242
+ * Send full catalog tar archive to replace the current catalog
2243
+ *
2244
+ * @function replaceCatalogUsingTarArchive
2245
+ * @param {object} parameters - Additional parameters for catalog details
2246
+ * @param {string} parameters.section - The section to update
2247
+ * @param {string} [parameters.notification_email] - An email address to receive an email notification if the task fails
2248
+ * @param {boolean} [parameters.force=false] - Process the catalog even if it will invalidate a large number of existing items
2249
+ * @param {file} [parameters.tarArchive] - The tar file that includes csv files
2250
+ * @param {object} [networkParameters] - Parameters relevant to the network request
2251
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
2252
+ * @returns {Promise}
2253
+ * @see https://docs.constructor.io/rest_api/full_catalog
2254
+ * @example
2255
+ * constructorio.catalog.replaceCatalogUsingTarArchive({
2256
+ * section: 'Products',
2257
+ * notification_email: 'notifications@example.com',
2258
+ * tarArchive: tarArchiveBufferOrStream,
2259
+ * });
2260
+ */
2261
+ async replaceCatalogUsingTarArchive(parameters = {}, networkParameters = {}) {
2262
+ try {
2263
+ const fetch = (this.options && this.options.fetch) || nodeFetch;
2264
+ const apiKey = this.options && this.options.apiKey;
2265
+ const controller = new AbortController();
2266
+ const { signal } = controller;
2267
+ const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
2268
+ const formDataWithTarArchive = await addTarArchiveToFormData(parameters, formData, 'sync', apiKey);
2269
+ const requestUrl = createCatalogUrl('catalog', this.options, queryParams);
2270
+ // Handle network timeout if specified
2271
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
2272
+
2273
+ const response = await fetch(requestUrl, {
2274
+ method: 'PUT',
2275
+ body: formDataWithTarArchive,
2276
+ headers: helpers.createAuthHeader(this.options),
2277
+ signal,
2278
+ });
2279
+
2280
+ if (response.ok) {
2281
+ return Promise.resolve(response.json());
2282
+ }
2283
+
2284
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
2285
+ } catch (error) {
2286
+ return Promise.reject(error);
2287
+ }
2288
+ }
2289
+
2290
+ /**
2291
+ * Send delta catalog tar archive to update the current catalog
2292
+ *
2293
+ * @function updateCatalogUsingTarArchive
2294
+ * @param {object} parameters - Additional parameters for catalog details
2295
+ * @param {string} parameters.section - The section to update
2296
+ * @param {string} [parameters.notification_email] - An email address to receive an email notification if the task fails
2297
+ * @param {boolean} [parameters.force=false] - Process the catalog even if it will invalidate a large number of existing items
2298
+ * @param {file} [parameters.tarArchive] - The tar file that includes csv files
2299
+ * @param {object} [networkParameters] - Parameters relevant to the network request
2300
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
2301
+ * @returns {Promise}
2302
+ * @see https://docs.constructor.io/rest_api/full_catalog
2303
+ * @example
2304
+ * constructorio.catalog.updateCatalogUsingTarArchive({
2305
+ * section: 'Products',
2306
+ * notification_email: 'notifications@example.com',
2307
+ * tarArchive: tarArchiveBufferOrStream,
2308
+ * });
2309
+ */
2310
+ async updateCatalogUsingTarArchive(parameters = {}, networkParameters = {}) {
2311
+ try {
2312
+ const fetch = (this.options && this.options.fetch) || nodeFetch;
2313
+ const apiKey = this.options && this.options.apiKey;
2314
+ const controller = new AbortController();
2315
+ const { signal } = controller;
2316
+ const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
2317
+ const formDataWithTarArchive = await addTarArchiveToFormData(parameters, formData, 'delta', apiKey);
2318
+ const requestUrl = createCatalogUrl('catalog', this.options, queryParams);
2319
+
2320
+ // Handle network timeout if specified
2321
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
2322
+
2323
+ const response = await fetch(requestUrl, {
2324
+ method: 'PATCH',
2325
+ body: formDataWithTarArchive,
2326
+ headers: helpers.createAuthHeader(this.options),
2327
+ signal,
2328
+ });
2329
+
2330
+ if (response.ok) {
2331
+ return Promise.resolve(response.json());
2332
+ }
2333
+
2334
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
2335
+ } catch (error) {
2336
+ return Promise.reject(error);
2337
+ }
2338
+ }
2339
+
2340
+ /**
2341
+ * Send patch delta tar archive to patch the current catalog
2342
+ *
2343
+ * @function patchCatalogUsingTarArchive
2344
+ * @param {object} parameters - Additional parameters for catalog details
2345
+ * @param {string} parameters.section - The section to update
2346
+ * @param {string} [parameters.notification_email] - An email address to receive an email notification if the task fails
2347
+ * @param {boolean} [parameters.force=false] - Process the catalog even if it will invalidate a large number of existing items
2348
+ * @param {file} [parameters.tarArchive] - The tar file that includes csv files
2349
+ * @param {object} [networkParameters] - Parameters relevant to the network request
2350
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
2351
+ * @returns {Promise}
2352
+ * @see https://docs.constructor.io/rest_api/full_catalog
2353
+ * @example
2354
+ * constructorio.catalog.patchCatalogUsingTarArchive({
2355
+ * section: 'Products',
2356
+ * notification_email: 'notifications@example.com',
2357
+ * tarArchive: tarArchiveBufferOrStream,
2358
+ * });
2359
+ */
2360
+ async patchCatalogUsingTarArchive(parameters = {}, networkParameters = {}) {
2361
+ try {
2362
+ const fetch = (this.options && this.options.fetch) || nodeFetch;
2363
+ const apiKey = this.options && this.options.apiKey;
2364
+ const controller = new AbortController();
2365
+ const { signal } = controller;
2366
+ const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
2367
+ const formDataWithTarArchive = await addTarArchiveToFormData(parameters, formData, 'delta', apiKey);
2368
+ const requestUrl = createCatalogUrl('catalog', this.options, { ...queryParams, patch_delta: true });
2369
+
2370
+ // Handle network timeout if specified
2371
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
2372
+
2373
+ const response = await fetch(requestUrl, {
2374
+ method: 'PATCH',
2375
+ body: formDataWithTarArchive,
2376
+ headers: helpers.createAuthHeader(this.options),
2377
+ signal,
2378
+ });
2379
+
2380
+ if (response.ok) {
2381
+ return Promise.resolve(response.json());
2382
+ }
2383
+
2384
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
2385
+ } catch (error) {
2386
+ return Promise.reject(error);
2387
+ }
2388
+ }
2389
+
2210
2390
  /**
2211
2391
  * Create a facet configuration
2212
2392
  *
package/src/.DS_Store DELETED
Binary file