@phystack/products 4.4.29

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 (44) hide show
  1. package/CHANGELOG.md +2736 -0
  2. package/dist/api.d.ts +11 -0
  3. package/dist/api.js +49 -0
  4. package/dist/helpers.d.ts +2 -0
  5. package/dist/helpers.js +11 -0
  6. package/dist/index.d.ts +7 -0
  7. package/dist/index.js +29 -0
  8. package/dist/index.test.d.ts +1 -0
  9. package/dist/index.test.js +1423 -0
  10. package/dist/services/grid-product-service-admin.d.ts +23 -0
  11. package/dist/services/grid-product-service-admin.js +120 -0
  12. package/dist/services/grid-product-service-client.d.ts +80 -0
  13. package/dist/services/grid-product-service-client.js +633 -0
  14. package/dist/services/grid-product-service-interface.d.ts +47 -0
  15. package/dist/services/grid-product-service-interface.js +2 -0
  16. package/dist/services/http-service.d.ts +14 -0
  17. package/dist/services/http-service.js +44 -0
  18. package/dist/types/grid-product.d.ts +458 -0
  19. package/dist/types/grid-product.js +38 -0
  20. package/dist/types/iso-currency-codes.d.ts +182 -0
  21. package/dist/types/iso-currency-codes.js +186 -0
  22. package/dist/types/iso-language-ids.d.ts +170 -0
  23. package/dist/types/iso-language-ids.js +174 -0
  24. package/dist/types/parameters.d.ts +242 -0
  25. package/dist/types/parameters.js +99 -0
  26. package/dist/utils.d.ts +27 -0
  27. package/dist/utils.js +187 -0
  28. package/jest.config.js +10 -0
  29. package/jest.setup.js +1 -0
  30. package/package.json +31 -0
  31. package/src/api.ts +47 -0
  32. package/src/helpers.ts +7 -0
  33. package/src/index.test.ts +1526 -0
  34. package/src/index.ts +8 -0
  35. package/src/services/grid-product-service-admin.ts +123 -0
  36. package/src/services/grid-product-service-client.ts +995 -0
  37. package/src/services/grid-product-service-interface.ts +105 -0
  38. package/src/services/http-service.ts +50 -0
  39. package/src/types/grid-product.ts +548 -0
  40. package/src/types/iso-currency-codes.ts +182 -0
  41. package/src/types/iso-language-ids.ts +170 -0
  42. package/src/types/parameters.ts +231 -0
  43. package/src/utils.ts +325 -0
  44. package/tsconfig.json +20 -0
package/src/utils.ts ADDED
@@ -0,0 +1,325 @@
1
+ import {
2
+ GridProduct,
3
+ ProductConsumerStorageInstruction,
4
+ ProductInternalName,
5
+ ProductShortDescription,
6
+ ProductStorageInstructions,
7
+ ProductShippingInstruction,
8
+ ProductName,
9
+ ProductDescription,
10
+ VariantProductName,
11
+ Variant,
12
+ ProductBrand,
13
+ ProductStatus,
14
+ ProductFeature,
15
+ ProductPriceList,
16
+ ProductLabel,
17
+ ProductType,
18
+ ProductTypeTitle,
19
+ } from './types/grid-product';
20
+ import { IsoLanguageIds } from './types/iso-language-ids';
21
+ // @ts-ignore
22
+ import tds from '@ombori/epc-ean';
23
+
24
+ type DataMap<T> = {
25
+ [key in IsoLanguageIds]?: T;
26
+ };
27
+
28
+ interface IsoLanguageData {
29
+ isoLanguageId?: string;
30
+ }
31
+
32
+ // Returns default "en" related language as fallback if isoLanguageId not found in products data
33
+ const getLocalizedValue = <T extends IsoLanguageData>({
34
+ data = [],
35
+ isoLanguageId = IsoLanguageIds.en_US,
36
+ }: {
37
+ data?: T[];
38
+ isoLanguageId?: IsoLanguageIds;
39
+ }): T | undefined => {
40
+ const dataMap = data.reduce<DataMap<T>>((accumulator, dataItem): DataMap<T> => {
41
+ const newAccumulator = {
42
+ ...accumulator,
43
+ [dataItem.isoLanguageId as string]: dataItem,
44
+ };
45
+
46
+ return newAccumulator;
47
+ }, {});
48
+
49
+ let result = dataMap[isoLanguageId];
50
+
51
+ if (!result) {
52
+ const defaultLanguage = Object.keys(dataMap).find((key) => key.startsWith('en'));
53
+ if (defaultLanguage) {
54
+ result = dataMap[defaultLanguage as keyof DataMap<T>];
55
+ }
56
+ }
57
+
58
+ return result;
59
+ };
60
+
61
+ // Returns default "en" related language as fallback if isoLanguageId not found in products data
62
+ const getLocalizedValues = <T extends IsoLanguageData>({
63
+ data = [],
64
+ isoLanguageId = IsoLanguageIds.en_US,
65
+ }: {
66
+ data?: T[];
67
+ isoLanguageId?: IsoLanguageIds;
68
+ }): T[] | undefined => {
69
+ let result = data.filter((dataItem) => dataItem.isoLanguageId === isoLanguageId);
70
+
71
+ if (result.length === 0) {
72
+ result = data.filter(
73
+ (dataItem) => dataItem.isoLanguageId && dataItem.isoLanguageId.startsWith('en'),
74
+ );
75
+ }
76
+
77
+ if (result.length === 0) {
78
+ return undefined;
79
+ }
80
+
81
+ return result;
82
+ };
83
+
84
+ export const getProductShortDescription = (
85
+ product: Partial<GridProduct>,
86
+ isoLanguageId?: IsoLanguageIds,
87
+ ): ProductShortDescription | undefined => {
88
+ const result = getLocalizedValue<ProductShortDescription>({
89
+ data: product.productShortDescription,
90
+ isoLanguageId,
91
+ });
92
+
93
+ return result;
94
+ };
95
+
96
+ export const getProductInternalName = (
97
+ product: Partial<GridProduct>,
98
+ isoLanguageId?: IsoLanguageIds,
99
+ ): ProductInternalName | undefined => {
100
+ const result = getLocalizedValue<ProductInternalName>({
101
+ data: product.productInternalName,
102
+ isoLanguageId,
103
+ });
104
+
105
+ return result;
106
+ };
107
+
108
+ export const getProductStorageInstructions = (
109
+ product: Partial<GridProduct>,
110
+ isoLanguageId?: IsoLanguageIds,
111
+ ): ProductStorageInstructions | undefined => {
112
+ const result = getLocalizedValue<ProductStorageInstructions>({
113
+ data: product.storageInstructions,
114
+ isoLanguageId,
115
+ });
116
+
117
+ return result;
118
+ };
119
+
120
+ export const getProductConsumerStorageInstruction = (
121
+ product: Partial<GridProduct>,
122
+ isoLanguageId?: IsoLanguageIds,
123
+ ): ProductConsumerStorageInstruction | undefined => {
124
+ const result = getLocalizedValue<ProductConsumerStorageInstruction>({
125
+ data: product.consumerStorageInstruction,
126
+ isoLanguageId,
127
+ });
128
+
129
+ return result;
130
+ };
131
+
132
+ export const getProductShippingInstruction = (
133
+ product: Partial<GridProduct>,
134
+ isoLanguageId?: IsoLanguageIds,
135
+ ): ProductShippingInstruction | undefined => {
136
+ const result = getLocalizedValue<ProductShippingInstruction>({
137
+ data: product.productShippingInstruction,
138
+ isoLanguageId,
139
+ });
140
+
141
+ return result;
142
+ };
143
+
144
+ export const getProductName = (
145
+ product: Partial<GridProduct>,
146
+ isoLanguageId?: IsoLanguageIds,
147
+ ): ProductName | undefined => {
148
+ const result = getLocalizedValue<ProductName>({
149
+ data: product.productName,
150
+ isoLanguageId,
151
+ });
152
+
153
+ return result;
154
+ };
155
+
156
+ export const getProductDescription = (
157
+ product: Partial<GridProduct>,
158
+ isoLanguageId?: IsoLanguageIds,
159
+ ): ProductDescription | undefined => {
160
+ const result = getLocalizedValue<ProductDescription>({
161
+ data: product.productDescription,
162
+ isoLanguageId,
163
+ });
164
+
165
+ return result;
166
+ };
167
+
168
+ export const getVariantProductName = (
169
+ variant: Variant,
170
+ isoLanguageId?: IsoLanguageIds,
171
+ ): VariantProductName | undefined => {
172
+ const result = getLocalizedValue<VariantProductName>({
173
+ data: variant.productName,
174
+ isoLanguageId,
175
+ });
176
+
177
+ return result;
178
+ };
179
+
180
+ export const getProductBrand = (
181
+ product: Partial<GridProduct>,
182
+ isoLanguageId?: IsoLanguageIds,
183
+ ): ProductBrand | undefined => {
184
+ const result = getLocalizedValue<ProductBrand>({
185
+ data: product.brand,
186
+ isoLanguageId,
187
+ });
188
+
189
+ return result;
190
+ };
191
+
192
+ export const getProductStatus = (
193
+ product: Partial<GridProduct>,
194
+ ): ProductStatus[] | undefined => {
195
+ return product.productStatus;
196
+ };
197
+
198
+ export const getProductFeature = (
199
+ product: Partial<GridProduct>,
200
+ isoLanguageId?: IsoLanguageIds,
201
+ ): ProductFeature[] | undefined => {
202
+ const result = getLocalizedValues<ProductFeature>({
203
+ data: product.productFeature,
204
+ isoLanguageId,
205
+ });
206
+
207
+ return result;
208
+ };
209
+
210
+ export const getProductPriceList = (
211
+ product: Partial<GridProduct>,
212
+ ): ProductPriceList[] | undefined => {
213
+ return product.productPriceList;
214
+ };
215
+
216
+ export interface PriceData {
217
+ label: string;
218
+ currency: string;
219
+ price: number | undefined;
220
+ }
221
+
222
+ export const getProductPrice = (
223
+ productPriceList: ProductPriceList,
224
+ isoLanguageId: IsoLanguageIds,
225
+ ): PriceData => {
226
+ const label = new Intl.NumberFormat(isoLanguageId, {
227
+ style: 'currency',
228
+ currency: productPriceList.isoCurrencyCode,
229
+ }).format(productPriceList.listPrice);
230
+
231
+ const priceData = {
232
+ label,
233
+ currency: productPriceList.isoCurrencyCode,
234
+ price: productPriceList.listPrice,
235
+ };
236
+
237
+ return priceData;
238
+ };
239
+
240
+ export const getProductLabel = (
241
+ product: Partial<GridProduct>,
242
+ isoLanguageId?: IsoLanguageIds,
243
+ ): ProductLabel[] | undefined => {
244
+ const result = getLocalizedValues<ProductLabel>({
245
+ data: product.productLabel,
246
+ isoLanguageId,
247
+ });
248
+
249
+ return result;
250
+ };
251
+
252
+ export const getProductTypeTitle = (
253
+ productType: ProductType,
254
+ isoLanguageId?: IsoLanguageIds,
255
+ ): ProductTypeTitle | undefined => {
256
+ const result = getLocalizedValue<ProductTypeTitle>({
257
+ data: productType.title,
258
+ isoLanguageId,
259
+ });
260
+
261
+ return result;
262
+ };
263
+
264
+ export const getProductTypeById = (
265
+ productTypes: ProductType[],
266
+ productTypeId: string,
267
+ ): ProductType[] => {
268
+ const productTypeList = productTypes.filter(
269
+ (productType) => productType.productTypeId === productTypeId,
270
+ );
271
+
272
+ return productTypeList;
273
+ };
274
+
275
+ export const getProductTypeChildren = (
276
+ productTypes: ProductType[],
277
+ productTypeId: string,
278
+ ): ProductType[] => {
279
+ const children = productTypes.filter(
280
+ (productType) =>
281
+ productType.parentId === productTypeId &&
282
+ productType.productTypeId !== productTypeId,
283
+ );
284
+
285
+ return children;
286
+ };
287
+
288
+ export const getProductTypeChildrenAndSubchildrenIds = (
289
+ productTypes: ProductType[],
290
+ productTypeId: string,
291
+ ): string[] => {
292
+ let result: string[] = [];
293
+ const traverseAllChildren = (id: string) => {
294
+ const currProductTypeChildren = getProductTypeChildren(productTypes, id).map(
295
+ (productType) => productType.productTypeId,
296
+ );
297
+
298
+ if (currProductTypeChildren.length > 0) {
299
+ result = result.concat([...currProductTypeChildren]);
300
+ currProductTypeChildren.forEach(
301
+ (child) => child !== id && traverseAllChildren(child),
302
+ );
303
+ }
304
+ };
305
+
306
+ traverseAllChildren(productTypeId);
307
+ return result;
308
+ };
309
+
310
+ export const isLeafProductType = (
311
+ productTypes: ProductType[],
312
+ productTypeId: string,
313
+ ): boolean => {
314
+ const children = productTypes.filter(
315
+ (productType) => productType.parentId === productTypeId,
316
+ );
317
+
318
+ return children.length === 0;
319
+ };
320
+
321
+ export const getProductEanByEpc = (epc: string) => {
322
+ const data = tds.valueOf(epc);
323
+ const barcode = data.toBarcode() as string;
324
+ return barcode;
325
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "include": ["src/**/*"],
3
+ "compilerOptions": {
4
+ "target": "es6",
5
+ "allowJs": true,
6
+ "skipLibCheck": true,
7
+ "esModuleInterop": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "strict": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "module": "commonjs",
12
+ "moduleResolution": "node",
13
+ "resolveJsonModule": true,
14
+ "isolatedModules": false,
15
+ "baseUrl": "src",
16
+ "declaration": true,
17
+ "emitDeclarationOnly": false,
18
+ "outDir": "dist"
19
+ }
20
+ }