@websolutespa/bom-core 0.2.0 → 0.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,32 +1,44 @@
1
- # @websolutespa/bom-core
2
-
3
- ## 0.2.0
4
-
5
- ### Minor Changes
6
-
7
- - Modified routes, added redirects.
8
-
9
- ## 0.1.2
10
-
11
- ### Patch Changes
12
-
13
- - Modified: MockMenuService.
14
-
15
- ## 0.1.1
16
-
17
- ### Patch Changes
18
-
19
- - Modified: ILocalizable.
20
-
21
- ## 0.1.0
22
-
23
- ### Minor Changes
24
-
25
- - Added: splat routes.
26
- - Added: variants.
27
-
28
- ## 0.0.1
29
-
30
- ### Patch Changes
31
-
32
- - Added: first release.
1
+ # @websolutespa/bom-core
2
+
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Modified: HttpService.
8
+
9
+ ## 0.2.1
10
+
11
+ ### Patch Changes
12
+
13
+ - Added: env ROOT_CATEGORY.
14
+
15
+ ## 0.2.0
16
+
17
+ ### Minor Changes
18
+
19
+ - Modified routes, added redirects.
20
+
21
+ ## 0.1.2
22
+
23
+ ### Patch Changes
24
+
25
+ - Modified: MockMenuService.
26
+
27
+ ## 0.1.1
28
+
29
+ ### Patch Changes
30
+
31
+ - Modified: ILocalizable.
32
+
33
+ ## 0.1.0
34
+
35
+ ### Minor Changes
36
+
37
+ - Added: splat routes.
38
+ - Added: variants.
39
+
40
+ ## 0.0.1
41
+
42
+ ### Patch Changes
43
+
44
+ - Added: first release.
package/dist/index.d.ts CHANGED
@@ -138,7 +138,8 @@ type FetchRequestOptions = {
138
138
  keepalive?: boolean;
139
139
  signal?: AbortSignal;
140
140
  };
141
- type FetchService<T = any> = (url: string, options?: FetchRequestOptions, serviceOptions?: any) => Promise<T>;
141
+ type FetchService<T = unknown> = (url: string, options?: FetchRequestOptions, serviceOptions?: any) => Promise<T>;
142
+ type FetchError = any;
142
143
 
143
144
  type ILabel = IEntity & {
144
145
  id: IEquatable;
@@ -444,6 +445,7 @@ type ITemplate = {
444
445
 
445
446
  declare function isCategory(value?: ICategory | IEquatable): value is ICategory;
446
447
  declare function asCategoryId(value?: ICategory | IEquatable): string | undefined;
448
+ declare function getRootCategory(categories: ICategory[]): ICategory | undefined;
447
449
  declare function getParentCategoriesById(id: string | undefined, categories: ICategory[]): ICategory[];
448
450
  declare function getCategorySegments(item: ICategorized, categories: ICategory[]): ICategory[];
449
451
  declare function getCategorySegmentsByCategory(item: ICategory, categories: ICategory[]): ICategory[];
@@ -519,16 +521,29 @@ declare function getRouteHref(segments: ICategory[], market: IMarket, locale: IL
519
521
 
520
522
  declare function textToSlug(title: string): string;
521
523
 
524
+ type ListItem<T> = {
525
+ [K in keyof T]: {
526
+ readonly key: string;
527
+ readonly value: T[K];
528
+ };
529
+ }[keyof T];
530
+ /**
531
+ * Helper to produce an array of enum descriptors.
532
+ * @param enumeration Enumeration object.
533
+ * @param separatorRegex Regex that would catch the separator in your enum key.
534
+ */
535
+ declare function enumToList<T extends Record<string, number | string>>(enumeration: T, separatorRegex?: RegExp): ListItem<T>[];
536
+
522
537
  declare function getIsDevelopment(): boolean;
523
538
  declare function getIsProduction(): boolean;
524
539
  declare const isDevelopment: boolean;
525
540
  declare const isProduction: boolean;
526
541
  declare const isStage: boolean;
527
542
  declare const isBrowser: boolean;
543
+ declare function getEnv(): Record<string, Json>;
528
544
  declare function withSchema<Component extends {}, Schema>(component: Component, schema: Schema): Component & Schema;
529
545
  declare function asServerProps(props: any): any;
530
546
  declare function awaitAll(array: any, callback: (item: any, index: number) => Promise<any>): Promise<any[]>;
531
- declare function merge(target: any, source: any): any;
532
- declare function deepCopy<T>(source: T): T;
547
+ declare function merge(target: any, source: any): any;
533
548
 
534
- export { FetchRequestOptions, FetchService, FindParams, FindWhereParams, ICategorized, ICategory, ICategoryItem, IClassName, IClassNameCollection, IClassNameValue, ICollectionDescription, IConfig, IContextBaseParams, IContextParams, IEntity, IEquatable, ILabel, ILayout, ILocale, ILocalizable, ILocalized, ILocalizedString, IMarket, IMedia, IMediaType, IMemoryStore, IMenu, IMenuBase, IMenuCategory, IMenuCustom, IMenuGroup, IMenuItem, IMenuLink, IMenuPage, IMenuRoute, INamedEntity, IOption, IOverride, IPackages, IPage, IPageRelation, IPageResult, IPartial, IPrettify, IQuerable, IRedirect, IRoute, IRouteCanonical, IRouteLink, IRouteParams, ISchema, ISerializedCollection, ISerializedStore, IServerSideContext, IServerSideSplatContext, IStaticContext, IStore, ITemplate, ITitledEntity, IUndecoratedMenu, IUndecoratedMenuBase, IUndecoratedMenuCategory, IUndecoratedMenuCustom, IUndecoratedMenuGroup, IUndecoratedMenuItem, IUndecoratedMenuLink, IUndecoratedMenuPage, Json, JsonService, LiteralUnion, NextApiRequestCookies, Operator, PageProps, Pagination, PreviewData, QueryParams, RedirectReason, RouteStrategy, SchemaType, ServerSidePropsContext, StateValue, StaticPropsContext, ValueOf, Where, WhereAndOr, WhereBase, WhereField, asCategoryId, asEquatable, asServerProps, awaitAll, camelToSnakeCase, collectRoutes, consoleWarn, deepCopy, deserializeKey, deserializeValue, eachCategorized, eachMarketLocale, getCategorySegments, getCategorySegmentsByCategory, getClassNames, getCollectionDescription, getIsDevelopment, getIsProduction, getParentCategoriesById, getRouteHref, httpDelete, httpFetch, httpGet, httpPatch, httpPost, httpPut, isBrowser, isCategory, isDevelopment, isLocalizedString, isProduction, isStage, isValidCollectionDescription, localize, localizeItem, localizeValue, localizedToString, merge, qsDeserialize, qsSerialize, textToSlug, toDisplayName, toFindParams, toPluralName, toTitleCase, toValidCollectionName, withSchema };
549
+ export { FetchError, FetchRequestOptions, FetchService, FindParams, FindWhereParams, ICategorized, ICategory, ICategoryItem, IClassName, IClassNameCollection, IClassNameValue, ICollectionDescription, IConfig, IContextBaseParams, IContextParams, IEntity, IEquatable, ILabel, ILayout, ILocale, ILocalizable, ILocalized, ILocalizedString, IMarket, IMedia, IMediaType, IMemoryStore, IMenu, IMenuBase, IMenuCategory, IMenuCustom, IMenuGroup, IMenuItem, IMenuLink, IMenuPage, IMenuRoute, INamedEntity, IOption, IOverride, IPackages, IPage, IPageRelation, IPageResult, IPartial, IPrettify, IQuerable, IRedirect, IRoute, IRouteCanonical, IRouteLink, IRouteParams, ISchema, ISerializedCollection, ISerializedStore, IServerSideContext, IServerSideSplatContext, IStaticContext, IStore, ITemplate, ITitledEntity, IUndecoratedMenu, IUndecoratedMenuBase, IUndecoratedMenuCategory, IUndecoratedMenuCustom, IUndecoratedMenuGroup, IUndecoratedMenuItem, IUndecoratedMenuLink, IUndecoratedMenuPage, Json, JsonService, LiteralUnion, NextApiRequestCookies, Operator, PageProps, Pagination, PreviewData, QueryParams, RedirectReason, RouteStrategy, SchemaType, ServerSidePropsContext, StateValue, StaticPropsContext, ValueOf, Where, WhereAndOr, WhereBase, WhereField, asCategoryId, asEquatable, asServerProps, awaitAll, camelToSnakeCase, collectRoutes, consoleWarn, deserializeKey, deserializeValue, eachCategorized, eachMarketLocale, enumToList, getCategorySegments, getCategorySegmentsByCategory, getClassNames, getCollectionDescription, getEnv, getIsDevelopment, getIsProduction, getParentCategoriesById, getRootCategory, getRouteHref, httpDelete, httpFetch, httpGet, httpPatch, httpPost, httpPut, isBrowser, isCategory, isDevelopment, isLocalizedString, isProduction, isStage, isValidCollectionDescription, localize, localizeItem, localizeValue, localizedToString, merge, qsDeserialize, qsSerialize, textToSlug, toDisplayName, toFindParams, toPluralName, toTitleCase, toValidCollectionName, withSchema };
package/dist/index.js CHANGED
@@ -40,18 +40,20 @@ __export(src_exports, {
40
40
  camelToSnakeCase: () => camelToSnakeCase,
41
41
  collectRoutes: () => collectRoutes,
42
42
  consoleWarn: () => consoleWarn,
43
- deepCopy: () => deepCopy,
44
43
  deserializeKey: () => deserializeKey,
45
44
  deserializeValue: () => deserializeValue,
46
45
  eachCategorized: () => eachCategorized,
47
46
  eachMarketLocale: () => eachMarketLocale,
47
+ enumToList: () => enumToList,
48
48
  getCategorySegments: () => getCategorySegments,
49
49
  getCategorySegmentsByCategory: () => getCategorySegmentsByCategory,
50
50
  getClassNames: () => getClassNames,
51
51
  getCollectionDescription: () => getCollectionDescription,
52
+ getEnv: () => getEnv,
52
53
  getIsDevelopment: () => getIsDevelopment,
53
54
  getIsProduction: () => getIsProduction,
54
55
  getParentCategoriesById: () => getParentCategoriesById,
56
+ getRootCategory: () => getRootCategory,
55
57
  getRouteHref: () => getRouteHref,
56
58
  httpDelete: () => httpDelete,
57
59
  httpFetch: () => httpFetch,
@@ -90,6 +92,15 @@ function isCategory(value) {
90
92
  function asCategoryId(value) {
91
93
  return isCategory(value) ? value.id : value;
92
94
  }
95
+ var ROOT_CATEGORY = process.env.ROOT_CATEGORY || "";
96
+ function getRootCategory(categories) {
97
+ const rootCategory = ROOT_CATEGORY && categories.find((x) => x.id == ROOT_CATEGORY);
98
+ if (rootCategory) {
99
+ return rootCategory;
100
+ } else {
101
+ return categories.find((x) => x.category == null);
102
+ }
103
+ }
93
104
  function getParentCategoriesById(id, categories) {
94
105
  const routeCategories = [];
95
106
  const segments = [];
@@ -239,6 +250,15 @@ function consoleWarn(message, component) {
239
250
  console.warn(log);
240
251
  }
241
252
 
253
+ // src/utils/enumToList.ts
254
+ function enumToList(enumeration, separatorRegex = /_/g) {
255
+ const array = Object.keys(enumeration);
256
+ return array.filter((key) => isNaN(Number(key))).filter((key) => typeof enumeration[key] === "number" || typeof enumeration[key] === "string").map((key) => ({
257
+ key: String(key).replace(separatorRegex, " "),
258
+ value: enumeration[key]
259
+ }));
260
+ }
261
+
242
262
  // src/utils/index.ts
243
263
  function getIsDevelopment() {
244
264
  return process && (!process.env.NODE_ENV || process.env.NODE_ENV === "development");
@@ -250,6 +270,14 @@ var isDevelopment = getIsDevelopment();
250
270
  var isProduction = getIsProduction();
251
271
  var isStage = !isProduction && !isDevelopment;
252
272
  var isBrowser = typeof window !== "undefined";
273
+ function getEnv() {
274
+ const env = {};
275
+ const keys = Object.keys(process.env).sort();
276
+ keys.forEach((key) => {
277
+ env[key] = process.env[key];
278
+ });
279
+ return env;
280
+ }
253
281
  function withSchema(component, schema) {
254
282
  return Object.assign(component, schema);
255
283
  }
@@ -283,19 +311,6 @@ function merge(target, source) {
283
311
  }
284
312
  return target;
285
313
  }
286
- function deepCopy(source) {
287
- if (Array.isArray(source)) {
288
- return source.map((x) => deepCopy(x));
289
- } else if (source && typeof source === "object") {
290
- const copy = {};
291
- Object.keys(source).forEach((key) => {
292
- copy[key] = deepCopy(source[key]);
293
- });
294
- return copy;
295
- } else {
296
- return source;
297
- }
298
- }
299
314
 
300
315
  // src/http/http.service.ts
301
316
  var defaultOptions = {
@@ -312,13 +327,22 @@ var defaultOptions = {
312
327
  // body: JSON.stringify(data) // body data type must match 'Content-Type' header
313
328
  };
314
329
  async function httpFetch(url, options = {}, serviceOptions) {
315
- const httpOptions = merge({ ...defaultOptions }, options);
316
- const httpResponse = await fetch(url, httpOptions);
317
- if (!httpResponse.ok) {
318
- throw httpResponse;
330
+ try {
331
+ const httpOptions = merge({ ...defaultOptions }, options);
332
+ const httpResponse = await fetch(url, httpOptions);
333
+ if (!httpResponse.ok) {
334
+ throw httpResponse;
335
+ }
336
+ const response = await httpResponse.json();
337
+ return response;
338
+ } catch (error) {
339
+ if (typeof error === "object" && error.status) {
340
+ console.log("httpFetch.error", options.method, url, error.status, error.statusText);
341
+ } else {
342
+ console.log("httpFetch.error", options.method, url, error);
343
+ }
344
+ throw error;
319
345
  }
320
- const response = await httpResponse.json();
321
- return response;
322
346
  }
323
347
  async function httpGet(url, options = {}) {
324
348
  return await httpFetch(url, { ...options, method: "GET" });
@@ -708,7 +732,7 @@ function qsDeserialize(query = "", deserializedParams = {}) {
708
732
  function collectRoutes(store, categories, markets, locales) {
709
733
  const routes = [];
710
734
  if (locales.length > 0 && markets.length > 0) {
711
- const firstTopCategory = categories.find((x) => x.category === void 0);
735
+ const rootCategory = getRootCategory(categories);
712
736
  eachCategorized(store, (item, key) => {
713
737
  const segments = getCategorySegments(item, categories);
714
738
  eachMarketLocale(markets, locales, (market, locale, markets2, locales2) => {
@@ -732,7 +756,7 @@ function collectRoutes(store, categories, markets, locales) {
732
756
  routes.push(route);
733
757
  }
734
758
  });
735
- if (firstTopCategory && item.category === firstTopCategory.id) {
759
+ if (rootCategory && item.category === rootCategory.id) {
736
760
  const defaultLocale = locales.find((x) => x.isDefault) || locales[0];
737
761
  const defaultMarket = markets.find((x) => x.isDefault) || markets[0];
738
762
  const defaultMarketLocale = defaultMarket && defaultMarket.defaultLanguage ? defaultMarket.defaultLanguage : defaultLocale.id;
@@ -855,18 +879,20 @@ var RedirectReason = /* @__PURE__ */ ((RedirectReason2) => {
855
879
  camelToSnakeCase,
856
880
  collectRoutes,
857
881
  consoleWarn,
858
- deepCopy,
859
882
  deserializeKey,
860
883
  deserializeValue,
861
884
  eachCategorized,
862
885
  eachMarketLocale,
886
+ enumToList,
863
887
  getCategorySegments,
864
888
  getCategorySegmentsByCategory,
865
889
  getClassNames,
866
890
  getCollectionDescription,
891
+ getEnv,
867
892
  getIsDevelopment,
868
893
  getIsProduction,
869
894
  getParentCategoriesById,
895
+ getRootCategory,
870
896
  getRouteHref,
871
897
  httpDelete,
872
898
  httpFetch,
package/dist/index.mjs CHANGED
@@ -5,6 +5,15 @@ function isCategory(value) {
5
5
  function asCategoryId(value) {
6
6
  return isCategory(value) ? value.id : value;
7
7
  }
8
+ var ROOT_CATEGORY = process.env.ROOT_CATEGORY || "";
9
+ function getRootCategory(categories) {
10
+ const rootCategory = ROOT_CATEGORY && categories.find((x) => x.id == ROOT_CATEGORY);
11
+ if (rootCategory) {
12
+ return rootCategory;
13
+ } else {
14
+ return categories.find((x) => x.category == null);
15
+ }
16
+ }
8
17
  function getParentCategoriesById(id, categories) {
9
18
  const routeCategories = [];
10
19
  const segments = [];
@@ -154,6 +163,15 @@ function consoleWarn(message, component) {
154
163
  console.warn(log);
155
164
  }
156
165
 
166
+ // src/utils/enumToList.ts
167
+ function enumToList(enumeration, separatorRegex = /_/g) {
168
+ const array = Object.keys(enumeration);
169
+ return array.filter((key) => isNaN(Number(key))).filter((key) => typeof enumeration[key] === "number" || typeof enumeration[key] === "string").map((key) => ({
170
+ key: String(key).replace(separatorRegex, " "),
171
+ value: enumeration[key]
172
+ }));
173
+ }
174
+
157
175
  // src/utils/index.ts
158
176
  function getIsDevelopment() {
159
177
  return process && (!process.env.NODE_ENV || process.env.NODE_ENV === "development");
@@ -165,6 +183,14 @@ var isDevelopment = getIsDevelopment();
165
183
  var isProduction = getIsProduction();
166
184
  var isStage = !isProduction && !isDevelopment;
167
185
  var isBrowser = typeof window !== "undefined";
186
+ function getEnv() {
187
+ const env = {};
188
+ const keys = Object.keys(process.env).sort();
189
+ keys.forEach((key) => {
190
+ env[key] = process.env[key];
191
+ });
192
+ return env;
193
+ }
168
194
  function withSchema(component, schema) {
169
195
  return Object.assign(component, schema);
170
196
  }
@@ -198,19 +224,6 @@ function merge(target, source) {
198
224
  }
199
225
  return target;
200
226
  }
201
- function deepCopy(source) {
202
- if (Array.isArray(source)) {
203
- return source.map((x) => deepCopy(x));
204
- } else if (source && typeof source === "object") {
205
- const copy = {};
206
- Object.keys(source).forEach((key) => {
207
- copy[key] = deepCopy(source[key]);
208
- });
209
- return copy;
210
- } else {
211
- return source;
212
- }
213
- }
214
227
 
215
228
  // src/http/http.service.ts
216
229
  var defaultOptions = {
@@ -227,13 +240,22 @@ var defaultOptions = {
227
240
  // body: JSON.stringify(data) // body data type must match 'Content-Type' header
228
241
  };
229
242
  async function httpFetch(url, options = {}, serviceOptions) {
230
- const httpOptions = merge({ ...defaultOptions }, options);
231
- const httpResponse = await fetch(url, httpOptions);
232
- if (!httpResponse.ok) {
233
- throw httpResponse;
243
+ try {
244
+ const httpOptions = merge({ ...defaultOptions }, options);
245
+ const httpResponse = await fetch(url, httpOptions);
246
+ if (!httpResponse.ok) {
247
+ throw httpResponse;
248
+ }
249
+ const response = await httpResponse.json();
250
+ return response;
251
+ } catch (error) {
252
+ if (typeof error === "object" && error.status) {
253
+ console.log("httpFetch.error", options.method, url, error.status, error.statusText);
254
+ } else {
255
+ console.log("httpFetch.error", options.method, url, error);
256
+ }
257
+ throw error;
234
258
  }
235
- const response = await httpResponse.json();
236
- return response;
237
259
  }
238
260
  async function httpGet(url, options = {}) {
239
261
  return await httpFetch(url, { ...options, method: "GET" });
@@ -623,7 +645,7 @@ function qsDeserialize(query = "", deserializedParams = {}) {
623
645
  function collectRoutes(store, categories, markets, locales) {
624
646
  const routes = [];
625
647
  if (locales.length > 0 && markets.length > 0) {
626
- const firstTopCategory = categories.find((x) => x.category === void 0);
648
+ const rootCategory = getRootCategory(categories);
627
649
  eachCategorized(store, (item, key) => {
628
650
  const segments = getCategorySegments(item, categories);
629
651
  eachMarketLocale(markets, locales, (market, locale, markets2, locales2) => {
@@ -647,7 +669,7 @@ function collectRoutes(store, categories, markets, locales) {
647
669
  routes.push(route);
648
670
  }
649
671
  });
650
- if (firstTopCategory && item.category === firstTopCategory.id) {
672
+ if (rootCategory && item.category === rootCategory.id) {
651
673
  const defaultLocale = locales.find((x) => x.isDefault) || locales[0];
652
674
  const defaultMarket = markets.find((x) => x.isDefault) || markets[0];
653
675
  const defaultMarketLocale = defaultMarket && defaultMarket.defaultLanguage ? defaultMarket.defaultLanguage : defaultLocale.id;
@@ -769,18 +791,20 @@ export {
769
791
  camelToSnakeCase,
770
792
  collectRoutes,
771
793
  consoleWarn,
772
- deepCopy,
773
794
  deserializeKey,
774
795
  deserializeValue,
775
796
  eachCategorized,
776
797
  eachMarketLocale,
798
+ enumToList,
777
799
  getCategorySegments,
778
800
  getCategorySegmentsByCategory,
779
801
  getClassNames,
780
802
  getCollectionDescription,
803
+ getEnv,
781
804
  getIsDevelopment,
782
805
  getIsProduction,
783
806
  getParentCategoriesById,
807
+ getRootCategory,
784
808
  getRouteHref,
785
809
  httpDelete,
786
810
  httpFetch,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websolutespa/bom-core",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Core module of the BOM Repository",
5
5
  "keywords": [
6
6
  "bom",