goods-exporter 0.5.1 → 0.5.3

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/README.md CHANGED
@@ -17,6 +17,7 @@ export tasks with ease.
17
17
  - Easily integrate into your JavaScript projects.
18
18
  - Compatible with Node.js version 16 and above.
19
19
  - Comprehensive TypeScript type definitions included.
20
+ - Supports streams.
20
21
 
21
22
  ## Supported formats
22
23
 
@@ -41,7 +42,8 @@ yarn add goods-exporter
41
42
  ## Quick start
42
43
 
43
44
  ```typescript
44
- import { GoodsExporter, Product, Category, Formatters } from '../src'
45
+ import { GoodsExporter, Product, Category, Formatters } from '../src';
46
+ import { PassThrough } from "stream";
45
47
 
46
48
  // Create an instance of the GoodsExporter class.
47
49
  const exporter = new GoodsExporter()
@@ -50,7 +52,19 @@ const products: Product[] = [] // Put your products;
50
52
  const categories: Category[] = [{ id: 1, name: 'Обувь' }]
51
53
 
52
54
  // Call the data export method.
53
- exporter.export(products, categories)
55
+ const stream = new PassThrough();
56
+ exporter.setExporter(() => stream);
57
+ exporter.setFormatter(new Formatters.YMLFormatter());
58
+ exporter.setTransformers([
59
+ (products) => {
60
+ return products.map((product) => ({
61
+ ...product,
62
+ price: product.price + 10000,
63
+ images: product.images?.map((image) => image.replace("image", "pic")),
64
+ }));
65
+ },
66
+ ]);
67
+ await exporter.export(products, categories);
54
68
  ```
55
69
 
56
70
  ## Example
@@ -80,16 +94,8 @@ exporter.setFormatter(new Formatters.YMLFormatter()) // or your own Formatter;
80
94
  exporter.setTransformers(transformers);
81
95
 
82
96
  // Set an exporter that saves the data to the "output.yml" file.
83
- exporter.setExporter((data: Buffer) => {
84
- fs.writeFileSync("output.yml", data); // Write data to the "output.yml" file.
85
- return data; // Return the data (you can return any type).
86
- });
87
-
88
- // Call the data export method specifying the data type (Buffer) and expect the result as a promise.
89
- exporter.export<Buffer>(products, categories)
90
- .then(data => {
91
- // Here, you can add additional handling for the export result if needed.
92
- });
97
+ exporter.setExporter(fs.createWriteStream("output.yml"));
98
+ await exporter.export(products, categories);
93
99
  ```
94
100
 
95
101
  # Supported by [PoizonAPI](https://t.me/PoizonAPI)
package/dist/bundle.d.ts CHANGED
@@ -87,6 +87,12 @@ interface Product {
87
87
  * Пример: 01.01.2000
88
88
  */
89
89
  saleDate?: string;
90
+ /**
91
+ * **Вендор в магазине**
92
+ *
93
+ * Содержит номер вендора, а не ее название.
94
+ */
95
+ vendorId?: number;
90
96
  /**
91
97
  * **Категория в магазине**
92
98
  *
@@ -622,6 +628,12 @@ declare class JSONFormatter implements FormatterAbstract {
622
628
  format(products: Product[], categories?: Category[], brands?: Brand[], _?: FormatterOptions): Promise<Stream>;
623
629
  }
624
630
 
631
+ declare class SimpleJSONFormatter implements FormatterAbstract {
632
+ formatterName: string;
633
+ fileExtension: Extension;
634
+ format(products: Product[], categories?: Category[], brands?: Brand[], _?: FormatterOptions): Promise<Stream>;
635
+ }
636
+
625
637
  declare class TgShopFormatter implements FormatterAbstract {
626
638
  formatterName: string;
627
639
  fileExtension: Extension;
@@ -640,7 +652,7 @@ declare class YMLFormatter implements FormatterAbstract {
640
652
  format(products: Product[], categories?: Category[], brands?: Brand[], options?: FormatterOptions): Promise<Stream>;
641
653
  private getBrands;
642
654
  private getCategories;
643
- private getOffers;
655
+ private getOffer;
644
656
  }
645
657
 
646
658
  declare const Formatters: {
@@ -651,6 +663,7 @@ declare const Formatters: {
651
663
  TgShopFormatter: typeof TgShopFormatter;
652
664
  ExcelFormatter: typeof ExcelFormatter;
653
665
  JSONFormatter: typeof JSONFormatter;
666
+ SimpleJSONFormatter: typeof SimpleJSONFormatter;
654
667
  };
655
668
 
656
669
  type Transformer = (products: Product[]) => Product[] | Promise<Product[]>;
@@ -1,25 +1,25 @@
1
1
  'use strict';
2
2
 
3
- var stream = require('stream');
4
- var exceljs = require('exceljs');
3
+ var stream$3 = require('stream');
4
+ var pkg = require('exceljs');
5
5
  var jsonStreamStringify = require('json-stream-stringify');
6
6
  var fastXmlParser = require('fast-xml-parser');
7
7
  var xml = require('xml');
8
8
  var fs = require('fs');
9
9
 
10
- var __defProp$8 = Object.defineProperty;
11
- var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
- var __publicField$8 = (obj, key, value) => {
13
- __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
10
+ var __defProp$9 = Object.defineProperty;
11
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __publicField$9 = (obj, key, value) => {
13
+ __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
14
14
  return value;
15
15
  };
16
16
  class CSVStream {
17
17
  constructor({ delimiter, lineSeparator, emptyFieldValue }) {
18
- __publicField$8(this, "stream", new stream.PassThrough());
19
- __publicField$8(this, "delimiter", ";");
20
- __publicField$8(this, "lineSeparator", "\n");
21
- __publicField$8(this, "emptyFieldValue", "");
22
- __publicField$8(this, "columns", /* @__PURE__ */ new Set());
18
+ __publicField$9(this, "stream", new stream$3.PassThrough());
19
+ __publicField$9(this, "delimiter", ";");
20
+ __publicField$9(this, "lineSeparator", "\n");
21
+ __publicField$9(this, "emptyFieldValue", "");
22
+ __publicField$9(this, "columns", /* @__PURE__ */ new Set());
23
23
  if (delimiter !== void 0)
24
24
  this.delimiter = delimiter;
25
25
  if (lineSeparator !== void 0)
@@ -55,16 +55,16 @@ var Extension = /* @__PURE__ */ ((Extension2) => {
55
55
  return Extension2;
56
56
  })(Extension || {});
57
57
 
58
- var __defProp$7 = Object.defineProperty;
59
- var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
60
- var __publicField$7 = (obj, key, value) => {
61
- __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
58
+ var __defProp$8 = Object.defineProperty;
59
+ var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
60
+ var __publicField$8 = (obj, key, value) => {
61
+ __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
62
62
  return value;
63
63
  };
64
64
  class CSVFormatter {
65
65
  constructor() {
66
- __publicField$7(this, "formatterName", "CSV");
67
- __publicField$7(this, "fileExtension", Extension.CSV);
66
+ __publicField$8(this, "formatterName", "CSV");
67
+ __publicField$8(this, "fileExtension", Extension.CSV);
68
68
  }
69
69
  async format(products, categories, _, __) {
70
70
  const mappedCategories = {};
@@ -127,16 +127,17 @@ class CSVFormatter {
127
127
  }
128
128
  }
129
129
 
130
- var __defProp$6 = Object.defineProperty;
131
- var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
132
- var __publicField$6 = (obj, key, value) => {
133
- __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
130
+ var __defProp$7 = Object.defineProperty;
131
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
132
+ var __publicField$7 = (obj, key, value) => {
133
+ __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
134
134
  return value;
135
135
  };
136
+ const { stream: stream$2 } = pkg;
136
137
  class ExcelFormatter {
137
138
  constructor() {
138
- __publicField$6(this, "formatterName", "Excel");
139
- __publicField$6(this, "fileExtension", Extension.XLSX);
139
+ __publicField$7(this, "formatterName", "Excel");
140
+ __publicField$7(this, "fileExtension", Extension.XLSX);
140
141
  }
141
142
  async format(products, categories, _, __) {
142
143
  const mappedCategories = {};
@@ -173,7 +174,7 @@ class ExcelFormatter {
173
174
  columns.add(key);
174
175
  });
175
176
  });
176
- const workbook = new exceljs.stream.xlsx.WorkbookWriter({});
177
+ const workbook = new stream$2.xlsx.WorkbookWriter({});
177
178
  const worksheet = workbook.addWorksheet("products");
178
179
  worksheet.columns = Array.from(columns).map((column) => ({
179
180
  key: column,
@@ -201,16 +202,17 @@ class ExcelFormatter {
201
202
  }
202
203
  }
203
204
 
204
- var __defProp$5 = Object.defineProperty;
205
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
206
- var __publicField$5 = (obj, key, value) => {
207
- __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
205
+ var __defProp$6 = Object.defineProperty;
206
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
207
+ var __publicField$6 = (obj, key, value) => {
208
+ __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
208
209
  return value;
209
210
  };
211
+ const { stream: stream$1 } = pkg;
210
212
  class InsalesFormatter {
211
213
  constructor() {
212
- __publicField$5(this, "formatterName", "Insales");
213
- __publicField$5(this, "fileExtension", Extension.XLSX);
214
+ __publicField$6(this, "formatterName", "Insales");
215
+ __publicField$6(this, "fileExtension", Extension.XLSX);
214
216
  }
215
217
  async format(products, categories, _, __) {
216
218
  const mappedCategories = {};
@@ -251,7 +253,7 @@ class InsalesFormatter {
251
253
  });
252
254
  return categories2;
253
255
  };
254
- const workbook = new exceljs.stream.xlsx.WorkbookWriter({});
256
+ const workbook = new stream$1.xlsx.WorkbookWriter({});
255
257
  const worksheet = workbook.addWorksheet("products");
256
258
  const columns = /* @__PURE__ */ new Set([
257
259
  "\u0412\u043D\u0435\u0448\u043D\u0438\u0439 ID",
@@ -339,22 +341,59 @@ class InsalesFormatter {
339
341
  }
340
342
  }
341
343
 
344
+ var __defProp$5 = Object.defineProperty;
345
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
346
+ var __publicField$5 = (obj, key, value) => {
347
+ __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
348
+ return value;
349
+ };
350
+ class JSONFormatter {
351
+ constructor() {
352
+ __publicField$5(this, "formatterName", "JSON");
353
+ __publicField$5(this, "fileExtension", Extension.JSON);
354
+ }
355
+ async format(products, categories, brands, _) {
356
+ return new jsonStreamStringify.JsonStreamStringify({
357
+ categories,
358
+ brands,
359
+ products
360
+ });
361
+ }
362
+ }
363
+
342
364
  var __defProp$4 = Object.defineProperty;
343
365
  var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
344
366
  var __publicField$4 = (obj, key, value) => {
345
367
  __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
346
368
  return value;
347
369
  };
348
- class JSONFormatter {
370
+ class SimpleJSONFormatter {
349
371
  constructor() {
350
372
  __publicField$4(this, "formatterName", "JSON");
351
373
  __publicField$4(this, "fileExtension", Extension.JSON);
352
374
  }
353
375
  async format(products, categories, brands, _) {
376
+ const groupedProduct = /* @__PURE__ */ new Map();
377
+ products.forEach((product) => {
378
+ if (product.parentId !== void 0)
379
+ return;
380
+ groupedProduct.set(product.variantId, {
381
+ ...product,
382
+ children: []
383
+ });
384
+ });
385
+ products.forEach((product) => {
386
+ if (product.parentId === void 0)
387
+ return;
388
+ const parent = groupedProduct.get(product.parentId);
389
+ if (!parent)
390
+ return;
391
+ parent.children.push(product);
392
+ });
354
393
  return new jsonStreamStringify.JsonStreamStringify({
355
394
  categories,
356
395
  brands,
357
- products
396
+ products: Array.from(groupedProduct.values())
358
397
  });
359
398
  }
360
399
  }
@@ -365,6 +404,7 @@ var __publicField$3 = (obj, key, value) => {
365
404
  __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
366
405
  return value;
367
406
  };
407
+ const { stream } = pkg;
368
408
  class TgShopFormatter {
369
409
  constructor() {
370
410
  __publicField$3(this, "formatterName", "TgShop");
@@ -388,7 +428,7 @@ class TgShopFormatter {
388
428
  size: getParameter(product, "size")?.value,
389
429
  priority: void 0
390
430
  });
391
- const workbook = new exceljs.stream.xlsx.WorkbookWriter({});
431
+ const workbook = new stream.xlsx.WorkbookWriter({});
392
432
  const categoryWorksheet = workbook.addWorksheet("categories");
393
433
  const productsWorksheet = workbook.addWorksheet("offers");
394
434
  categoryWorksheet.columns = [
@@ -505,7 +545,7 @@ class YMLFormatter {
505
545
  __publicField$1(this, "fileExtension", Extension.YML);
506
546
  }
507
547
  async format(products, categories, brands, options) {
508
- const result = new stream.PassThrough();
548
+ const result = new stream$3.PassThrough();
509
549
  const builder = new fastXmlParser.XMLBuilder({
510
550
  ignoreAttributes: false,
511
551
  cdataPropName: "__cdata"
@@ -513,13 +553,14 @@ class YMLFormatter {
513
553
  const ymlCatalog = xml.element({
514
554
  _attr: { date: (/* @__PURE__ */ new Date()).toISOString().replace(/.\d+Z/, "") }
515
555
  });
516
- const stream$1 = xml(
556
+ const stream = xml(
517
557
  { yml_catalog: ymlCatalog },
518
558
  {
519
559
  stream: true,
520
560
  declaration: { standalone: "yes", encoding: "UTF-8" }
521
561
  }
522
562
  );
563
+ stream.pipe(result, { end: false });
523
564
  const shop = xml.element();
524
565
  const streamShop = xml({ shop }, { stream: true });
525
566
  shop.push({ name: options?.shopName });
@@ -528,12 +569,13 @@ class YMLFormatter {
528
569
  shop.push({ brands: this.getBrands(brands) });
529
570
  streamShop.pipe(result, { end: false });
530
571
  const offers = xml.element();
531
- const streamOffers = xml({ offers }, { stream: true });
532
- streamOffers.pipe(result, { end: false });
572
+ const streamOffersTag = xml({ offers }, { stream: true });
573
+ streamOffersTag.pipe(result, { end: false });
574
+ const streamOffers = new stream$3.PassThrough();
533
575
  products.forEach((product) => {
534
- result.write(builder.build({ offer: this.getOffers(product) }));
576
+ streamOffers.write(builder.build({ offer: this.getOffer(product) }));
535
577
  });
536
- stream$1.pipe(result, { end: false });
578
+ streamOffers.pipe(result, { end: false });
537
579
  offers.close();
538
580
  shop.close();
539
581
  ymlCatalog.close();
@@ -555,7 +597,7 @@ class YMLFormatter {
555
597
  ]
556
598
  }));
557
599
  }
558
- getOffers(product) {
600
+ getOffer(product) {
559
601
  const result = {
560
602
  "@_id": product.variantId,
561
603
  name: product.title,
@@ -566,6 +608,7 @@ class YMLFormatter {
566
608
  cofinance_price: product.cofinancePrice,
567
609
  currencyId: product.currency,
568
610
  categoryId: product.categoryId,
611
+ vendorId: product.vendorId,
569
612
  vendor: product.vendor,
570
613
  vendorCode: product.vendorCode,
571
614
  picture: product.images,
@@ -639,7 +682,8 @@ const Formatters = {
639
682
  YMLFormatter,
640
683
  TgShopFormatter,
641
684
  ExcelFormatter,
642
- JSONFormatter
685
+ JSONFormatter,
686
+ SimpleJSONFormatter
643
687
  };
644
688
 
645
689
  var __defProp = Object.defineProperty;