@websolutespa/bom-core 0.4.1 → 0.5.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,11 +1,18 @@
1
1
  # @websolutespa/bom-core
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Modified: JsonService, qs.
8
+ - Added: asEquatable, cloneItem, typeGuards.
9
+
3
10
  ## 0.4.1
4
11
 
5
12
  ### Patch Changes
6
13
 
7
14
  - Added: IAnchor, babelCase.
8
- - Modified: IApplication, IPageRelation.
15
+ - Modified: IApplication, IPageRelation.
9
16
 
10
17
  ## 0.4.0
11
18
 
package/dist/index.d.ts CHANGED
@@ -183,23 +183,25 @@ type IRoute = {
183
183
  isDefault?: boolean;
184
184
  locale: string;
185
185
  market: string;
186
+ media?: IMedia;
186
187
  order?: number;
187
188
  page: IEquatable;
188
189
  schema: string;
189
190
  template?: string;
190
191
  title: string;
191
- media?: IMedia;
192
- useSplat?: boolean;
193
192
  updatedAt?: Date;
193
+ useSplat?: boolean;
194
194
  };
195
195
  type IRouteCanonical = IRoute & {
196
196
  alternates: IRoute[];
197
197
  };
198
198
  type IRouteLink = {
199
+ category: IEquatable;
199
200
  href: string;
200
201
  id: IEquatable;
201
202
  items?: IRouteLink[];
202
203
  media?: IMedia;
204
+ schema: string;
203
205
  title: string;
204
206
  };
205
207
  type IRouteParams = Record<string, any> & {
@@ -229,6 +231,10 @@ type IPageRelation<T extends ICategorized = ICategorized> = {
229
231
  relationTo: string;
230
232
  value: IPageRelationResult<T>;
231
233
  };
234
+ type IPageUnresolvedRelation = {
235
+ relationTo: string;
236
+ value: IEquatable | IEquatable[];
237
+ };
232
238
  type IPageResult<T extends ICategorized = ICategorized> = IPage & T;
233
239
  type PageProps<T extends ICategorized = ICategorized> = {
234
240
  layout: ILayout;
@@ -241,6 +247,7 @@ type PageProps<T extends ICategorized = ICategorized> = {
241
247
 
242
248
  type IMenuBase = {
243
249
  abstract?: ILocalizable;
250
+ extra?: ILocalizable;
244
251
  customClass?: string;
245
252
  id: IEquatable;
246
253
  items?: IMenuItem[];
@@ -286,6 +293,7 @@ type IMenu = {
286
293
  */
287
294
  type IUndecoratedMenuBase = {
288
295
  abstract?: ILocalizable;
296
+ extra?: ILocalizable;
289
297
  customClass?: string;
290
298
  id?: string;
291
299
  items: IUndecoratedMenuItem[];
@@ -337,6 +345,13 @@ type ILayout = {
337
345
  tree?: IRouteLink;
338
346
  };
339
347
 
348
+ type IUndecoratedNav<T> = T & {
349
+ nav?: IUndecoratedMenuPage;
350
+ };
351
+ type IDecoratedNav<T> = T & {
352
+ nav?: IMenuPage;
353
+ };
354
+
340
355
  type Pagination = {
341
356
  hasNextPage: boolean;
342
357
  hasPrevPage: boolean;
@@ -485,7 +500,8 @@ declare class JsonService<T extends IEntity = IEntity> implements IQuerable<T> {
485
500
  create<B extends T = T>(payload: Omit<B, 'id'>): Promise<B>;
486
501
  update<B extends T = T>(payload: Partial<B>): Promise<B>;
487
502
  delete<B extends T = T>(id: IEquatable): Promise<B | undefined>;
488
- protected decorator_<B extends T = T>(item: B, params?: QueryParams): B;
503
+ protected decorateOne_<B extends T = T>(item: B, params?: QueryParams): Promise<B>;
504
+ protected decorateMany_<B extends T = T>(items: B[], params?: QueryParams): Promise<B[]>;
489
505
  protected newUUID_(): number;
490
506
  protected evaluateValue_(value: unknown): unknown;
491
507
  protected existsClause_(value: unknown, exists?: boolean): boolean;
@@ -505,13 +521,12 @@ declare function isLocalizedString(value: any): value is ILocalizedString;
505
521
  declare function localizedToString(json: ILocalizedString, locale?: string, defaultLocale?: string): string;
506
522
  declare function localize(value: ILocalizable, locale?: string, defaultLocale?: string): string;
507
523
  declare function localizeValue(value: any, locale?: string, defaultLocale?: string): any;
508
- declare function localizeItem(item: any, locale?: string, defaultLocale?: string): any;
524
+ declare function localizeItem<T = any>(item: T, locale?: string, defaultLocale?: string): T;
509
525
 
510
526
  declare function qsSerialize(queryParams?: QueryParams): string;
511
- declare function asEquatable(value: string): IEquatable;
512
- declare function deserializeValue(value: string): string | boolean | number;
513
- declare function deserializeKey(value: string): string | number;
527
+ declare function qsSerializeRaw(queryParams?: {}): string;
514
528
  declare function qsDeserialize(query?: string, deserializedParams?: {}): QueryParams;
529
+ declare function qsDeserializeRaw(query?: string, deserializedParams?: {}): {};
515
530
 
516
531
  declare function collectRoutes(store: IMemoryStore<ICategorized>, categories: ICategory[], markets: IMarket[], locales: ILocale[]): IRoute[];
517
532
  declare function eachMarketLocale(markets: IMarket[], locales: ILocale[], callback: (market: IMarket, locale: ILocale, markets: IMarket[], locales: ILocale[]) => void): void;
@@ -525,6 +540,8 @@ declare function getRouteHref(segments: ICategory[], market: IMarket, locale: IL
525
540
 
526
541
  declare function textToSlug(title: string): string;
527
542
 
543
+ declare function asEquatable(value: string): IEquatable;
544
+
528
545
  /**
529
546
  * PascalCase
530
547
  * camelCase
@@ -543,6 +560,8 @@ declare function toCharCase(unknownCase: string, char?: string): string;
543
560
  declare function toKebabCase(unknownCase: string): string;
544
561
  declare function toSnakeCase(unknownCase: string): string;
545
562
 
563
+ declare function cloneItem<T = unknown>(item: T): T;
564
+
546
565
  type ListItem<T> = {
547
566
  [K in keyof T]: {
548
567
  readonly key: string;
@@ -556,6 +575,17 @@ type ListItem<T> = {
556
575
  */
557
576
  declare function enumToList<T extends Record<string, number | string>>(enumeration: T, separatorRegex?: RegExp): ListItem<T>[];
558
577
 
578
+ declare function isArray<T = unknown>(item: unknown): item is Array<T>;
579
+ declare function isObject<T = Record<string, unknown>>(item: unknown): item is T;
580
+ declare function isNotNullString(item: unknown): item is string;
581
+ declare function isEquatable(item: unknown): item is IEquatable;
582
+ declare function isEntity(item: unknown): item is IEntity;
583
+ declare function isUnresolvedRelation(item: unknown): item is IPageUnresolvedRelation;
584
+ declare function isRelation(item: unknown): item is IPageRelation;
585
+ declare function isUndecoratedMenuItem(item: unknown): item is IUndecoratedMenuItem;
586
+ declare function isUndecoratedNav<T>(item: unknown): item is IUndecoratedNav<T>;
587
+ declare function isUndecoratedMenuPage(item: unknown): item is IUndecoratedMenuPage;
588
+
559
589
  declare function getIsDevelopment(): boolean;
560
590
  declare function getIsProduction(): boolean;
561
591
  declare const isDevelopment: boolean;
@@ -568,4 +598,4 @@ declare function asServerProps(props: any): any;
568
598
  declare function awaitAll(array: any, callback: (item: any, index: number) => Promise<any>): Promise<any[]>;
569
599
  declare function merge(target: any, source: any): any;
570
600
 
571
- export { FetchError, FetchRequestOptions, FetchService, FindParams, FindWhereParams, IAnchor, 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, IPageRelationResult, 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, pascalCaseToCamelCase, pascalCaseToCharCase, pascalCaseToKebabCase, pascalCaseToSnakeCase, qsDeserialize, qsSerialize, textToSlug, toCamelCase, toCharCase, toDisplayName, toFindParams, toKebabCase, toPascalCase, toPluralName, toSnakeCase, toTitleCase, toValidCollectionName, unknownCaseToPascalCase, withSchema };
601
+ export { FetchError, FetchRequestOptions, FetchService, FindParams, FindWhereParams, IAnchor, ICategorized, ICategory, ICategoryItem, IClassName, IClassNameCollection, IClassNameValue, ICollectionDescription, IConfig, IContextBaseParams, IContextParams, IDecoratedNav, 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, IPageRelationResult, IPageResult, IPageUnresolvedRelation, 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, IUndecoratedNav, 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, cloneItem, collectRoutes, consoleWarn, eachCategorized, eachMarketLocale, enumToList, getCategorySegments, getCategorySegmentsByCategory, getClassNames, getCollectionDescription, getEnv, getIsDevelopment, getIsProduction, getParentCategoriesById, getRootCategory, getRouteHref, httpDelete, httpFetch, httpGet, httpPatch, httpPost, httpPut, isArray, isBrowser, isCategory, isDevelopment, isEntity, isEquatable, isLocalizedString, isNotNullString, isObject, isProduction, isRelation, isStage, isUndecoratedMenuItem, isUndecoratedMenuPage, isUndecoratedNav, isUnresolvedRelation, isValidCollectionDescription, localize, localizeItem, localizeValue, localizedToString, merge, pascalCaseToCamelCase, pascalCaseToCharCase, pascalCaseToKebabCase, pascalCaseToSnakeCase, qsDeserialize, qsDeserializeRaw, qsSerialize, qsSerializeRaw, textToSlug, toCamelCase, toCharCase, toDisplayName, toFindParams, toKebabCase, toPascalCase, toPluralName, toSnakeCase, toTitleCase, toValidCollectionName, unknownCaseToPascalCase, withSchema };
package/dist/index.js CHANGED
@@ -38,10 +38,9 @@ __export(src_exports, {
38
38
  asServerProps: () => asServerProps,
39
39
  awaitAll: () => awaitAll,
40
40
  camelToSnakeCase: () => camelToSnakeCase,
41
+ cloneItem: () => cloneItem,
41
42
  collectRoutes: () => collectRoutes,
42
43
  consoleWarn: () => consoleWarn,
43
- deserializeKey: () => deserializeKey,
44
- deserializeValue: () => deserializeValue,
45
44
  eachCategorized: () => eachCategorized,
46
45
  eachMarketLocale: () => eachMarketLocale,
47
46
  enumToList: () => enumToList,
@@ -61,12 +60,22 @@ __export(src_exports, {
61
60
  httpPatch: () => httpPatch,
62
61
  httpPost: () => httpPost,
63
62
  httpPut: () => httpPut,
63
+ isArray: () => isArray,
64
64
  isBrowser: () => isBrowser,
65
65
  isCategory: () => isCategory,
66
66
  isDevelopment: () => isDevelopment,
67
+ isEntity: () => isEntity,
68
+ isEquatable: () => isEquatable,
67
69
  isLocalizedString: () => isLocalizedString,
70
+ isNotNullString: () => isNotNullString,
71
+ isObject: () => isObject,
68
72
  isProduction: () => isProduction,
73
+ isRelation: () => isRelation,
69
74
  isStage: () => isStage,
75
+ isUndecoratedMenuItem: () => isUndecoratedMenuItem,
76
+ isUndecoratedMenuPage: () => isUndecoratedMenuPage,
77
+ isUndecoratedNav: () => isUndecoratedNav,
78
+ isUnresolvedRelation: () => isUnresolvedRelation,
70
79
  isValidCollectionDescription: () => isValidCollectionDescription,
71
80
  localize: () => localize,
72
81
  localizeItem: () => localizeItem,
@@ -78,7 +87,9 @@ __export(src_exports, {
78
87
  pascalCaseToKebabCase: () => pascalCaseToKebabCase,
79
88
  pascalCaseToSnakeCase: () => pascalCaseToSnakeCase,
80
89
  qsDeserialize: () => qsDeserialize,
90
+ qsDeserializeRaw: () => qsDeserializeRaw,
81
91
  qsSerialize: () => qsSerialize,
92
+ qsSerializeRaw: () => qsSerializeRaw,
82
93
  textToSlug: () => textToSlug,
83
94
  toCamelCase: () => toCamelCase,
84
95
  toCharCase: () => toCharCase,
@@ -261,6 +272,14 @@ function consoleWarn(message, component) {
261
272
  console.warn(log);
262
273
  }
263
274
 
275
+ // src/utils/asEquatable.ts
276
+ function asEquatable(value) {
277
+ if (String(parseInt(value)) === value) {
278
+ return Number(value);
279
+ }
280
+ return value;
281
+ }
282
+
264
283
  // src/utils/babelCase.ts
265
284
  function unknownCaseToPascalCase(unknownCase) {
266
285
  const pascalCase = unknownCase.replace(
@@ -311,6 +330,52 @@ function toSnakeCase(unknownCase) {
311
330
  return snakeCase;
312
331
  }
313
332
 
333
+ // src/utils/typeGuards.ts
334
+ function isArray(item) {
335
+ return Array.isArray(item);
336
+ }
337
+ function isObject(item) {
338
+ return item != null && typeof item === "object" && !isArray(item);
339
+ }
340
+ function isNotNullString(item) {
341
+ return typeof item === "string" && Boolean(item);
342
+ }
343
+ function isEquatable(item) {
344
+ return typeof item === "number" || isNotNullString(item);
345
+ }
346
+ function isEntity(item) {
347
+ return isObject(item) && isEquatable(item.id);
348
+ }
349
+ function isUnresolvedRelation(item) {
350
+ return isObject(item) && isNotNullString(item.relationTo) && (isEquatable(item.value) || isArray(item.value));
351
+ }
352
+ function isRelation(item) {
353
+ return isObject(item) && isNotNullString(item.relationTo) && isEntity(item.value);
354
+ }
355
+ function isUndecoratedMenuItem(item) {
356
+ return isObject(item) && (item.type === "category" && isEquatable(item.category) || item.type === "page" && isEquatable(item.page.value));
357
+ }
358
+ function isUndecoratedNav(item) {
359
+ return isObject(item) && isUndecoratedMenuPage(item.nav);
360
+ }
361
+ function isUndecoratedMenuPage(item) {
362
+ return isObject(item) && item.type === "page" && isNotNullString(item.page.relationTo) && isEquatable(item.page.value);
363
+ }
364
+
365
+ // src/utils/cloneItem.ts
366
+ function cloneItem(item) {
367
+ if (isArray(item)) {
368
+ item = item.map((x) => cloneItem(x));
369
+ } else if (isObject(item)) {
370
+ item = { ...item };
371
+ const object = item;
372
+ Object.keys(object).forEach((key) => {
373
+ object[key] = cloneItem(object[key]);
374
+ });
375
+ }
376
+ return item;
377
+ }
378
+
314
379
  // src/utils/enumToList.ts
315
380
  function enumToList(enumeration, separatorRegex = /_/g) {
316
381
  const array = Object.keys(enumeration);
@@ -435,7 +500,7 @@ var JsonService = class {
435
500
  params.where.id = params.where.id || { equals: id };
436
501
  items = this.whereItems_(items, params.where);
437
502
  if (items.length > 0) {
438
- return this.decorator_(items[0], params);
503
+ return await this.decorateOne_(items[0], params);
439
504
  } else {
440
505
  console.log("JsonService.findOne.notfound", id, params);
441
506
  return void 0;
@@ -450,7 +515,7 @@ var JsonService = class {
450
515
  if (params.sort) {
451
516
  items = this.sortCollection_(items, params.sort);
452
517
  }
453
- return items.map((x) => this.decorator_(x, params));
518
+ return await this.decorateMany_(items, params);
454
519
  }
455
520
  async findPaged(params = {}) {
456
521
  params.pagination = true;
@@ -462,7 +527,7 @@ var JsonService = class {
462
527
  items = this.sortCollection_(items, params.sort);
463
528
  }
464
529
  const pagination = this.paginatedItems_(items, params.page, params.limit);
465
- pagination.docs = pagination.docs.map((x) => this.decorator_(x, params));
530
+ pagination.docs = await this.decorateMany_(pagination.docs, params);
466
531
  return pagination;
467
532
  }
468
533
  async create(payload) {
@@ -499,9 +564,12 @@ var JsonService = class {
499
564
  }
500
565
  return void 0;
501
566
  }
502
- decorator_(item, params = {}) {
567
+ async decorateOne_(item, params = {}) {
503
568
  return item;
504
569
  }
570
+ async decorateMany_(items, params = {}) {
571
+ return items;
572
+ }
505
573
  newUUID_() {
506
574
  return (/* @__PURE__ */ new Date()).getTime();
507
575
  }
@@ -543,24 +611,6 @@ var JsonService = class {
543
611
  }
544
612
  return true;
545
613
  }
546
- // old where
547
- /*
548
- protected where_(items: any[], params: FindParams): any[] {
549
- const where = params.where;
550
- if (where) {
551
- const keys = Object.keys(where);
552
- items = items.filter(x => {
553
- return keys.reduce<boolean>((p, c) => {
554
- const whereValue = where[c];
555
- const shouldMatch = Array.isArray(whereValue) ? whereValue : [whereValue];
556
- // console.log('key', c, 'value', x[c], 'shouldMatch', shouldMatch);
557
- return p && (shouldMatch.indexOf(x[c]) !== -1);
558
- }, true);
559
- });
560
- }
561
- return items;
562
- }
563
- */
564
614
  whereItem_(item, where) {
565
615
  let has = true;
566
616
  if (typeof where === "object") {
@@ -688,60 +738,62 @@ function localizeValue(value, locale = "en", defaultLocale = "en") {
688
738
  }
689
739
  }
690
740
  function localizeItem(item, locale = "en", defaultLocale = "en") {
691
- if (Array.isArray(item)) {
741
+ if (isArray(item)) {
692
742
  item = item.map((x) => localizeValue(x, locale, defaultLocale));
693
- } else if (typeof item === "object") {
743
+ } else if (isObject(item)) {
694
744
  item = { ...item };
695
- Object.keys(item).forEach((key) => {
696
- item[key] = localizeValue(item[key], locale, defaultLocale);
745
+ const object = item;
746
+ Object.keys(object).forEach((key) => {
747
+ object[key] = localizeValue(object[key], locale, defaultLocale);
697
748
  });
698
749
  }
699
750
  return item;
700
751
  }
701
752
 
702
753
  // src/qs/qs.ts
703
- function serializeKey(key, parentKey = "") {
754
+ function qsSerializeKey(key, parentKey = "") {
704
755
  return parentKey ? `${parentKey}[${key}]` : String(key);
705
756
  }
706
- function serializeParam(key, value, serializedParams = [], parentKey = "") {
757
+ function qsSerializeParam(key, value, serializedParams = [], parentKey = "") {
707
758
  if (value != null) {
708
759
  if (typeof value === "object") {
709
- serializedParams = serializeParams(value, serializedParams, serializeKey(key, parentKey));
760
+ serializedParams = qsSerializeParams(value, serializedParams, qsSerializeKey(key, parentKey));
710
761
  } else {
711
762
  serializedParams.push({
712
- key: serializeKey(key, parentKey),
763
+ key: qsSerializeKey(key, parentKey),
713
764
  value
714
765
  });
715
766
  }
716
767
  }
717
768
  return serializedParams;
718
769
  }
719
- function serializeParams(params, serializedParams = [], parentKey = "") {
770
+ function qsSerializeParams(params, serializedParams = [], parentKey = "") {
720
771
  if (Array.isArray(params)) {
721
772
  params.forEach((value, key) => {
722
- serializedParams = serializeParam(key, value, serializedParams, parentKey);
773
+ serializedParams = qsSerializeParam(key, value, serializedParams, parentKey);
723
774
  });
724
775
  } else {
725
776
  Object.entries(params).forEach(([key, value]) => {
726
- serializedParams = serializeParam(key, value, serializedParams, parentKey);
777
+ serializedParams = qsSerializeParam(key, value, serializedParams, parentKey);
727
778
  });
728
779
  }
729
780
  return serializedParams;
730
781
  }
731
782
  function qsSerialize(queryParams = {}) {
732
- const params = serializeParams(queryParams);
783
+ const params = qsSerializeParams(queryParams);
733
784
  if (params.length) {
734
785
  return params.map((x) => `${x.key}=${x.value}`).join("&");
735
786
  }
736
787
  return "";
737
788
  }
738
- function asEquatable(value) {
739
- if (String(parseInt(value)) === value) {
740
- return Number(value);
789
+ function qsSerializeRaw(queryParams = {}) {
790
+ const params = qsSerializeParams(queryParams);
791
+ if (params.length) {
792
+ return params.map((x) => `${x.key}=${x.value}`).join("&");
741
793
  }
742
- return value;
794
+ return "";
743
795
  }
744
- function deserializeValue(value) {
796
+ function qsDeserializeValue(value) {
745
797
  if (String(parseInt(value)) === value) {
746
798
  return Number(value);
747
799
  } else if (String(value) === "true") {
@@ -751,14 +803,14 @@ function deserializeValue(value) {
751
803
  }
752
804
  return value;
753
805
  }
754
- function deserializeKey(value) {
806
+ function qsDeserializeKey(value) {
755
807
  if (String(parseInt(value)) === value) {
756
808
  return Number(value);
757
809
  }
758
810
  return value;
759
811
  }
760
- function deserializeKeys(key) {
761
- const keys = key.replace(/[\]]/g, "").split("[").map((x) => deserializeKey(x));
812
+ function qsDeserializeKeys(key) {
813
+ const keys = key.replace(/[\]]/g, "").split("[").map((x) => qsDeserializeKey(x));
762
814
  return keys;
763
815
  }
764
816
  function getNextKeyItem(key) {
@@ -768,13 +820,13 @@ function getNextKeyItem(key) {
768
820
  return {};
769
821
  }
770
822
  }
771
- function deserializeParam(key, value, deserializedParams = {}) {
772
- const keys = deserializeKeys(key);
823
+ function qsDeserializeParam(key, value, deserializedParams = {}) {
824
+ const keys = qsDeserializeKeys(key);
773
825
  let target = deserializedParams;
774
826
  while (keys.length) {
775
827
  const key2 = keys.shift();
776
828
  if (keys.length === 0) {
777
- target[key2] = deserializeValue(value);
829
+ target[key2] = qsDeserializeValue(value);
778
830
  } else {
779
831
  const next = target[key2] ? target[key2] : target[key2] = getNextKeyItem(keys[0]);
780
832
  target = next;
@@ -782,8 +834,8 @@ function deserializeParam(key, value, deserializedParams = {}) {
782
834
  }
783
835
  return deserializedParams;
784
836
  }
785
- function deserializeParams(params, deserializedParams = {}) {
786
- params.forEach((x) => deserializeParam(x[0], x[1], deserializedParams));
837
+ function qsDeserializeParams(params, deserializedParams = {}) {
838
+ params.forEach((x) => qsDeserializeParam(x[0], x[1], deserializedParams));
787
839
  return deserializedParams;
788
840
  }
789
841
  function qsDeserialize(query = "", deserializedParams = {}) {
@@ -793,7 +845,19 @@ function qsDeserialize(query = "", deserializedParams = {}) {
793
845
  if (query !== "") {
794
846
  const params = query.split("&").map((x) => x.split("="));
795
847
  if (params.length) {
796
- return deserializeParams(params, deserializedParams);
848
+ return qsDeserializeParams(params, deserializedParams);
849
+ }
850
+ }
851
+ return deserializedParams;
852
+ }
853
+ function qsDeserializeRaw(query = "", deserializedParams = {}) {
854
+ if (query.indexOf("?") === 0) {
855
+ query = query.substring(1);
856
+ }
857
+ if (query !== "") {
858
+ const params = query.split("&").map((x) => x.split("="));
859
+ if (params.length) {
860
+ return qsDeserializeParams(params, deserializedParams);
797
861
  }
798
862
  }
799
863
  return deserializedParams;
@@ -811,18 +875,19 @@ function collectRoutes(store, categories, markets, locales) {
811
875
  const title = localize(item.title, locale.id);
812
876
  const id = getRouteHref(segments, market, locale, markets2, locales2);
813
877
  const route = {
878
+ category: item.category,
814
879
  id,
815
- market: market.id,
880
+ isDefault: item.isDefault,
816
881
  locale: locale.id,
817
- category: item.category,
882
+ market: market.id,
883
+ media: item.media,
884
+ order: item.order,
818
885
  page: item.id,
819
886
  schema: key,
820
887
  template: item.template,
821
888
  title,
822
- isDefault: item.isDefault,
823
- order: item.order,
824
- useSplat: item.useSplat,
825
- updatedAt: item.updatedAt
889
+ updatedAt: item.updatedAt,
890
+ useSplat: item.useSplat
826
891
  };
827
892
  routes.push(route);
828
893
  }
@@ -948,10 +1013,9 @@ var RedirectReason = /* @__PURE__ */ ((RedirectReason2) => {
948
1013
  asServerProps,
949
1014
  awaitAll,
950
1015
  camelToSnakeCase,
1016
+ cloneItem,
951
1017
  collectRoutes,
952
1018
  consoleWarn,
953
- deserializeKey,
954
- deserializeValue,
955
1019
  eachCategorized,
956
1020
  eachMarketLocale,
957
1021
  enumToList,
@@ -971,12 +1035,22 @@ var RedirectReason = /* @__PURE__ */ ((RedirectReason2) => {
971
1035
  httpPatch,
972
1036
  httpPost,
973
1037
  httpPut,
1038
+ isArray,
974
1039
  isBrowser,
975
1040
  isCategory,
976
1041
  isDevelopment,
1042
+ isEntity,
1043
+ isEquatable,
977
1044
  isLocalizedString,
1045
+ isNotNullString,
1046
+ isObject,
978
1047
  isProduction,
1048
+ isRelation,
979
1049
  isStage,
1050
+ isUndecoratedMenuItem,
1051
+ isUndecoratedMenuPage,
1052
+ isUndecoratedNav,
1053
+ isUnresolvedRelation,
980
1054
  isValidCollectionDescription,
981
1055
  localize,
982
1056
  localizeItem,
@@ -988,7 +1062,9 @@ var RedirectReason = /* @__PURE__ */ ((RedirectReason2) => {
988
1062
  pascalCaseToKebabCase,
989
1063
  pascalCaseToSnakeCase,
990
1064
  qsDeserialize,
1065
+ qsDeserializeRaw,
991
1066
  qsSerialize,
1067
+ qsSerializeRaw,
992
1068
  textToSlug,
993
1069
  toCamelCase,
994
1070
  toCharCase,
package/dist/index.mjs CHANGED
@@ -164,6 +164,14 @@ function consoleWarn(message, component) {
164
164
  console.warn(log);
165
165
  }
166
166
 
167
+ // src/utils/asEquatable.ts
168
+ function asEquatable(value) {
169
+ if (String(parseInt(value)) === value) {
170
+ return Number(value);
171
+ }
172
+ return value;
173
+ }
174
+
167
175
  // src/utils/babelCase.ts
168
176
  function unknownCaseToPascalCase(unknownCase) {
169
177
  const pascalCase = unknownCase.replace(
@@ -214,6 +222,52 @@ function toSnakeCase(unknownCase) {
214
222
  return snakeCase;
215
223
  }
216
224
 
225
+ // src/utils/typeGuards.ts
226
+ function isArray(item) {
227
+ return Array.isArray(item);
228
+ }
229
+ function isObject(item) {
230
+ return item != null && typeof item === "object" && !isArray(item);
231
+ }
232
+ function isNotNullString(item) {
233
+ return typeof item === "string" && Boolean(item);
234
+ }
235
+ function isEquatable(item) {
236
+ return typeof item === "number" || isNotNullString(item);
237
+ }
238
+ function isEntity(item) {
239
+ return isObject(item) && isEquatable(item.id);
240
+ }
241
+ function isUnresolvedRelation(item) {
242
+ return isObject(item) && isNotNullString(item.relationTo) && (isEquatable(item.value) || isArray(item.value));
243
+ }
244
+ function isRelation(item) {
245
+ return isObject(item) && isNotNullString(item.relationTo) && isEntity(item.value);
246
+ }
247
+ function isUndecoratedMenuItem(item) {
248
+ return isObject(item) && (item.type === "category" && isEquatable(item.category) || item.type === "page" && isEquatable(item.page.value));
249
+ }
250
+ function isUndecoratedNav(item) {
251
+ return isObject(item) && isUndecoratedMenuPage(item.nav);
252
+ }
253
+ function isUndecoratedMenuPage(item) {
254
+ return isObject(item) && item.type === "page" && isNotNullString(item.page.relationTo) && isEquatable(item.page.value);
255
+ }
256
+
257
+ // src/utils/cloneItem.ts
258
+ function cloneItem(item) {
259
+ if (isArray(item)) {
260
+ item = item.map((x) => cloneItem(x));
261
+ } else if (isObject(item)) {
262
+ item = { ...item };
263
+ const object = item;
264
+ Object.keys(object).forEach((key) => {
265
+ object[key] = cloneItem(object[key]);
266
+ });
267
+ }
268
+ return item;
269
+ }
270
+
217
271
  // src/utils/enumToList.ts
218
272
  function enumToList(enumeration, separatorRegex = /_/g) {
219
273
  const array = Object.keys(enumeration);
@@ -338,7 +392,7 @@ var JsonService = class {
338
392
  params.where.id = params.where.id || { equals: id };
339
393
  items = this.whereItems_(items, params.where);
340
394
  if (items.length > 0) {
341
- return this.decorator_(items[0], params);
395
+ return await this.decorateOne_(items[0], params);
342
396
  } else {
343
397
  console.log("JsonService.findOne.notfound", id, params);
344
398
  return void 0;
@@ -353,7 +407,7 @@ var JsonService = class {
353
407
  if (params.sort) {
354
408
  items = this.sortCollection_(items, params.sort);
355
409
  }
356
- return items.map((x) => this.decorator_(x, params));
410
+ return await this.decorateMany_(items, params);
357
411
  }
358
412
  async findPaged(params = {}) {
359
413
  params.pagination = true;
@@ -365,7 +419,7 @@ var JsonService = class {
365
419
  items = this.sortCollection_(items, params.sort);
366
420
  }
367
421
  const pagination = this.paginatedItems_(items, params.page, params.limit);
368
- pagination.docs = pagination.docs.map((x) => this.decorator_(x, params));
422
+ pagination.docs = await this.decorateMany_(pagination.docs, params);
369
423
  return pagination;
370
424
  }
371
425
  async create(payload) {
@@ -402,9 +456,12 @@ var JsonService = class {
402
456
  }
403
457
  return void 0;
404
458
  }
405
- decorator_(item, params = {}) {
459
+ async decorateOne_(item, params = {}) {
406
460
  return item;
407
461
  }
462
+ async decorateMany_(items, params = {}) {
463
+ return items;
464
+ }
408
465
  newUUID_() {
409
466
  return (/* @__PURE__ */ new Date()).getTime();
410
467
  }
@@ -446,24 +503,6 @@ var JsonService = class {
446
503
  }
447
504
  return true;
448
505
  }
449
- // old where
450
- /*
451
- protected where_(items: any[], params: FindParams): any[] {
452
- const where = params.where;
453
- if (where) {
454
- const keys = Object.keys(where);
455
- items = items.filter(x => {
456
- return keys.reduce<boolean>((p, c) => {
457
- const whereValue = where[c];
458
- const shouldMatch = Array.isArray(whereValue) ? whereValue : [whereValue];
459
- // console.log('key', c, 'value', x[c], 'shouldMatch', shouldMatch);
460
- return p && (shouldMatch.indexOf(x[c]) !== -1);
461
- }, true);
462
- });
463
- }
464
- return items;
465
- }
466
- */
467
506
  whereItem_(item, where) {
468
507
  let has = true;
469
508
  if (typeof where === "object") {
@@ -591,60 +630,62 @@ function localizeValue(value, locale = "en", defaultLocale = "en") {
591
630
  }
592
631
  }
593
632
  function localizeItem(item, locale = "en", defaultLocale = "en") {
594
- if (Array.isArray(item)) {
633
+ if (isArray(item)) {
595
634
  item = item.map((x) => localizeValue(x, locale, defaultLocale));
596
- } else if (typeof item === "object") {
635
+ } else if (isObject(item)) {
597
636
  item = { ...item };
598
- Object.keys(item).forEach((key) => {
599
- item[key] = localizeValue(item[key], locale, defaultLocale);
637
+ const object = item;
638
+ Object.keys(object).forEach((key) => {
639
+ object[key] = localizeValue(object[key], locale, defaultLocale);
600
640
  });
601
641
  }
602
642
  return item;
603
643
  }
604
644
 
605
645
  // src/qs/qs.ts
606
- function serializeKey(key, parentKey = "") {
646
+ function qsSerializeKey(key, parentKey = "") {
607
647
  return parentKey ? `${parentKey}[${key}]` : String(key);
608
648
  }
609
- function serializeParam(key, value, serializedParams = [], parentKey = "") {
649
+ function qsSerializeParam(key, value, serializedParams = [], parentKey = "") {
610
650
  if (value != null) {
611
651
  if (typeof value === "object") {
612
- serializedParams = serializeParams(value, serializedParams, serializeKey(key, parentKey));
652
+ serializedParams = qsSerializeParams(value, serializedParams, qsSerializeKey(key, parentKey));
613
653
  } else {
614
654
  serializedParams.push({
615
- key: serializeKey(key, parentKey),
655
+ key: qsSerializeKey(key, parentKey),
616
656
  value
617
657
  });
618
658
  }
619
659
  }
620
660
  return serializedParams;
621
661
  }
622
- function serializeParams(params, serializedParams = [], parentKey = "") {
662
+ function qsSerializeParams(params, serializedParams = [], parentKey = "") {
623
663
  if (Array.isArray(params)) {
624
664
  params.forEach((value, key) => {
625
- serializedParams = serializeParam(key, value, serializedParams, parentKey);
665
+ serializedParams = qsSerializeParam(key, value, serializedParams, parentKey);
626
666
  });
627
667
  } else {
628
668
  Object.entries(params).forEach(([key, value]) => {
629
- serializedParams = serializeParam(key, value, serializedParams, parentKey);
669
+ serializedParams = qsSerializeParam(key, value, serializedParams, parentKey);
630
670
  });
631
671
  }
632
672
  return serializedParams;
633
673
  }
634
674
  function qsSerialize(queryParams = {}) {
635
- const params = serializeParams(queryParams);
675
+ const params = qsSerializeParams(queryParams);
636
676
  if (params.length) {
637
677
  return params.map((x) => `${x.key}=${x.value}`).join("&");
638
678
  }
639
679
  return "";
640
680
  }
641
- function asEquatable(value) {
642
- if (String(parseInt(value)) === value) {
643
- return Number(value);
681
+ function qsSerializeRaw(queryParams = {}) {
682
+ const params = qsSerializeParams(queryParams);
683
+ if (params.length) {
684
+ return params.map((x) => `${x.key}=${x.value}`).join("&");
644
685
  }
645
- return value;
686
+ return "";
646
687
  }
647
- function deserializeValue(value) {
688
+ function qsDeserializeValue(value) {
648
689
  if (String(parseInt(value)) === value) {
649
690
  return Number(value);
650
691
  } else if (String(value) === "true") {
@@ -654,14 +695,14 @@ function deserializeValue(value) {
654
695
  }
655
696
  return value;
656
697
  }
657
- function deserializeKey(value) {
698
+ function qsDeserializeKey(value) {
658
699
  if (String(parseInt(value)) === value) {
659
700
  return Number(value);
660
701
  }
661
702
  return value;
662
703
  }
663
- function deserializeKeys(key) {
664
- const keys = key.replace(/[\]]/g, "").split("[").map((x) => deserializeKey(x));
704
+ function qsDeserializeKeys(key) {
705
+ const keys = key.replace(/[\]]/g, "").split("[").map((x) => qsDeserializeKey(x));
665
706
  return keys;
666
707
  }
667
708
  function getNextKeyItem(key) {
@@ -671,13 +712,13 @@ function getNextKeyItem(key) {
671
712
  return {};
672
713
  }
673
714
  }
674
- function deserializeParam(key, value, deserializedParams = {}) {
675
- const keys = deserializeKeys(key);
715
+ function qsDeserializeParam(key, value, deserializedParams = {}) {
716
+ const keys = qsDeserializeKeys(key);
676
717
  let target = deserializedParams;
677
718
  while (keys.length) {
678
719
  const key2 = keys.shift();
679
720
  if (keys.length === 0) {
680
- target[key2] = deserializeValue(value);
721
+ target[key2] = qsDeserializeValue(value);
681
722
  } else {
682
723
  const next = target[key2] ? target[key2] : target[key2] = getNextKeyItem(keys[0]);
683
724
  target = next;
@@ -685,8 +726,8 @@ function deserializeParam(key, value, deserializedParams = {}) {
685
726
  }
686
727
  return deserializedParams;
687
728
  }
688
- function deserializeParams(params, deserializedParams = {}) {
689
- params.forEach((x) => deserializeParam(x[0], x[1], deserializedParams));
729
+ function qsDeserializeParams(params, deserializedParams = {}) {
730
+ params.forEach((x) => qsDeserializeParam(x[0], x[1], deserializedParams));
690
731
  return deserializedParams;
691
732
  }
692
733
  function qsDeserialize(query = "", deserializedParams = {}) {
@@ -696,7 +737,19 @@ function qsDeserialize(query = "", deserializedParams = {}) {
696
737
  if (query !== "") {
697
738
  const params = query.split("&").map((x) => x.split("="));
698
739
  if (params.length) {
699
- return deserializeParams(params, deserializedParams);
740
+ return qsDeserializeParams(params, deserializedParams);
741
+ }
742
+ }
743
+ return deserializedParams;
744
+ }
745
+ function qsDeserializeRaw(query = "", deserializedParams = {}) {
746
+ if (query.indexOf("?") === 0) {
747
+ query = query.substring(1);
748
+ }
749
+ if (query !== "") {
750
+ const params = query.split("&").map((x) => x.split("="));
751
+ if (params.length) {
752
+ return qsDeserializeParams(params, deserializedParams);
700
753
  }
701
754
  }
702
755
  return deserializedParams;
@@ -714,18 +767,19 @@ function collectRoutes(store, categories, markets, locales) {
714
767
  const title = localize(item.title, locale.id);
715
768
  const id = getRouteHref(segments, market, locale, markets2, locales2);
716
769
  const route = {
770
+ category: item.category,
717
771
  id,
718
- market: market.id,
772
+ isDefault: item.isDefault,
719
773
  locale: locale.id,
720
- category: item.category,
774
+ market: market.id,
775
+ media: item.media,
776
+ order: item.order,
721
777
  page: item.id,
722
778
  schema: key,
723
779
  template: item.template,
724
780
  title,
725
- isDefault: item.isDefault,
726
- order: item.order,
727
- useSplat: item.useSplat,
728
- updatedAt: item.updatedAt
781
+ updatedAt: item.updatedAt,
782
+ useSplat: item.useSplat
729
783
  };
730
784
  routes.push(route);
731
785
  }
@@ -850,10 +904,9 @@ export {
850
904
  asServerProps,
851
905
  awaitAll,
852
906
  camelToSnakeCase,
907
+ cloneItem,
853
908
  collectRoutes,
854
909
  consoleWarn,
855
- deserializeKey,
856
- deserializeValue,
857
910
  eachCategorized,
858
911
  eachMarketLocale,
859
912
  enumToList,
@@ -873,12 +926,22 @@ export {
873
926
  httpPatch,
874
927
  httpPost,
875
928
  httpPut,
929
+ isArray,
876
930
  isBrowser,
877
931
  isCategory,
878
932
  isDevelopment,
933
+ isEntity,
934
+ isEquatable,
879
935
  isLocalizedString,
936
+ isNotNullString,
937
+ isObject,
880
938
  isProduction,
939
+ isRelation,
881
940
  isStage,
941
+ isUndecoratedMenuItem,
942
+ isUndecoratedMenuPage,
943
+ isUndecoratedNav,
944
+ isUnresolvedRelation,
882
945
  isValidCollectionDescription,
883
946
  localize,
884
947
  localizeItem,
@@ -890,7 +953,9 @@ export {
890
953
  pascalCaseToKebabCase,
891
954
  pascalCaseToSnakeCase,
892
955
  qsDeserialize,
956
+ qsDeserializeRaw,
893
957
  qsSerialize,
958
+ qsSerializeRaw,
894
959
  textToSlug,
895
960
  toCamelCase,
896
961
  toCharCase,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websolutespa/bom-core",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Core module of the BOM Repository",
5
5
  "keywords": [
6
6
  "bom",