@unchainedshop/utils 2.14.2 → 3.0.0-alpha3

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 (76) hide show
  1. package/lib/build-obfuscated-fields-filter.d.ts.map +1 -1
  2. package/lib/build-obfuscated-fields-filter.js +2 -3
  3. package/lib/build-obfuscated-fields-filter.js.map +1 -1
  4. package/lib/calculation.d.ts +1 -1
  5. package/lib/calculation.d.ts.map +1 -1
  6. package/lib/director/BaseAdapter.d.ts +10 -1
  7. package/lib/director/BaseAdapter.d.ts.map +1 -1
  8. package/lib/director/BaseAdapter.js.map +1 -1
  9. package/lib/director/BaseDirector.d.ts +9 -1
  10. package/lib/director/BaseDirector.d.ts.map +1 -1
  11. package/lib/director/BaseDirector.js.map +1 -1
  12. package/lib/director/BaseDiscountAdapter.d.ts +36 -1
  13. package/lib/director/BaseDiscountAdapter.d.ts.map +1 -1
  14. package/lib/director/BaseDiscountAdapter.js.map +1 -1
  15. package/lib/director/BaseDiscountDirector.d.ts +11 -1
  16. package/lib/director/BaseDiscountDirector.d.ts.map +1 -1
  17. package/lib/director/BaseDiscountDirector.js.map +1 -1
  18. package/lib/director/BasePricingAdapter.d.ts +45 -2
  19. package/lib/director/BasePricingAdapter.d.ts.map +1 -1
  20. package/lib/director/BasePricingAdapter.js.map +1 -1
  21. package/lib/director/BasePricingDirector.d.ts +14 -1
  22. package/lib/director/BasePricingDirector.d.ts.map +1 -1
  23. package/lib/director/BasePricingDirector.js.map +1 -1
  24. package/lib/director/BasePricingSheet.d.ts +45 -1
  25. package/lib/director/BasePricingSheet.d.ts.map +1 -1
  26. package/lib/director/BasePricingSheet.js.map +1 -1
  27. package/lib/locale-helpers.d.ts +6 -10
  28. package/lib/locale-helpers.d.ts.map +1 -1
  29. package/lib/locale-helpers.js +8 -19
  30. package/lib/locale-helpers.js.map +1 -1
  31. package/lib/object-invert.d.ts +1 -1
  32. package/lib/object-invert.d.ts.map +1 -1
  33. package/lib/object-invert.js.map +1 -1
  34. package/lib/utils-index.d.ts +17 -8
  35. package/lib/utils-index.d.ts.map +1 -1
  36. package/lib/utils-index.js +12 -69
  37. package/lib/utils-index.js.map +1 -1
  38. package/package.json +5 -8
  39. package/src/build-obfuscated-fields-filter.ts +2 -3
  40. package/src/calculation.ts +1 -1
  41. package/src/director/BaseAdapter.ts +13 -1
  42. package/src/director/BaseDirector.ts +8 -1
  43. package/src/director/BaseDiscountAdapter.ts +32 -1
  44. package/src/director/BaseDiscountDirector.ts +15 -2
  45. package/src/director/BasePricingAdapter.ts +59 -11
  46. package/src/director/BasePricingDirector.ts +36 -9
  47. package/src/director/BasePricingSheet.ts +45 -5
  48. package/src/locale-helpers.ts +19 -30
  49. package/src/object-invert.ts +1 -1
  50. package/src/utils-index.test.ts +0 -2
  51. package/src/utils-index.ts +18 -68
  52. package/lib/schemas/AddressSchema.d.ts +0 -3
  53. package/lib/schemas/AddressSchema.d.ts.map +0 -1
  54. package/lib/schemas/AddressSchema.js +0 -13
  55. package/lib/schemas/AddressSchema.js.map +0 -1
  56. package/lib/schemas/ContactSchema.d.ts +0 -3
  57. package/lib/schemas/ContactSchema.d.ts.map +0 -1
  58. package/lib/schemas/ContactSchema.js +0 -6
  59. package/lib/schemas/ContactSchema.js.map +0 -1
  60. package/lib/schemas/UsersSchema.d.ts +0 -5
  61. package/lib/schemas/UsersSchema.d.ts.map +0 -1
  62. package/lib/schemas/UsersSchema.js +0 -52
  63. package/lib/schemas/UsersSchema.js.map +0 -1
  64. package/lib/schemas/commonSchemaFields.d.ts +0 -35
  65. package/lib/schemas/commonSchemaFields.d.ts.map +0 -1
  66. package/lib/schemas/commonSchemaFields.js +0 -28
  67. package/lib/schemas/commonSchemaFields.js.map +0 -1
  68. package/lib/schemas/index.d.ts +0 -43
  69. package/lib/schemas/index.d.ts.map +0 -1
  70. package/lib/schemas/index.js +0 -14
  71. package/lib/schemas/index.js.map +0 -1
  72. package/src/schemas/AddressSchema.ts +0 -16
  73. package/src/schemas/ContactSchema.ts +0 -9
  74. package/src/schemas/UsersSchema.ts +0 -67
  75. package/src/schemas/commonSchemaFields.ts +0 -29
  76. package/src/schemas/index.ts +0 -18
@@ -1 +1 @@
1
- {"version":3,"file":"utils-index.js","sourceRoot":"","sources":["../src/utils-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC;AAEhD,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,OAAO,IAAI,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAE7F;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAE1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4DE"}
1
+ {"version":3,"file":"utils-index.js","sourceRoot":"","sources":["../src/utils-index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAC;AAEhD,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,OAAO,IAAI,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAE7F;;GAEG;AAEH,MAAM,CAAN,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,4BAAW,CAAA;IACX,8BAAa,CAAA;AACf,CAAC,EAHW,aAAa,KAAb,aAAa,QAGxB;AASD;;GAEG;AAEH,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAE3C,cAAc,kCAAkC,CAAC;AACjD,cAAc,mCAAmC,CAAC;AAClD,cAAc,gCAAgC,CAAC;AAE/C,cAAc,mCAAmC,CAAC;AAClD,cAAc,oCAAoC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/utils",
3
- "version": "2.14.2",
3
+ "version": "3.0.0-alpha3",
4
4
  "main": "lib/utils-index.js",
5
5
  "exports": {
6
6
  ".": "./lib/utils-index.js",
@@ -30,16 +30,13 @@
30
30
  },
31
31
  "homepage": "https://github.com/unchainedshop/unchained#readme",
32
32
  "dependencies": {
33
- "@unchainedshop/logger": "^2.14.2",
34
- "abort-controller": "3.0.0",
33
+ "@unchainedshop/logger": "^3.0.0-alpha3",
35
34
  "hashids": "^2.3.0",
36
- "locale": "0.1.0",
37
- "simpl-schema": "^3.4.6"
35
+ "resolve-accept-language": "^3.1.7"
38
36
  },
39
37
  "devDependencies": {
40
- "@types/node": "^20.16.5",
41
- "@unchainedshop/types": "^2.14.2",
38
+ "@types/node": "^22.5.5",
42
39
  "jest": "^29.7.0",
43
- "typescript": "^5.5.4"
40
+ "typescript": "^5.6.2"
44
41
  }
45
42
  }
@@ -1,8 +1,7 @@
1
1
  const defaultObfuscatedFields = [
2
2
  'password',
3
- 'plainPassword',
4
- 'newPlainPassword',
5
- 'oldPlainPassword',
3
+ 'newPassword',
4
+ 'oldPassword',
6
5
  'authorization',
7
6
  'secret',
8
7
  'accesskey',
@@ -1,4 +1,4 @@
1
- import { IBasePricingSheet, PricingCalculation } from '@unchainedshop/types/pricing.js';
1
+ import { IBasePricingSheet, PricingCalculation } from './director/BasePricingSheet.js';
2
2
 
3
3
  export const roundToNext = (value: number, precision: number) =>
4
4
  Math.ceil(value / precision) * precision;
@@ -1,6 +1,18 @@
1
- import { IBaseAdapter } from '@unchainedshop/types/common.js';
2
1
  import { log, LogLevel } from '@unchainedshop/logger';
3
2
 
3
+ export interface IBaseAdapter {
4
+ key: string;
5
+ label: string;
6
+ version: string;
7
+ log: (
8
+ message: string,
9
+ options?: {
10
+ level?: LogLevel;
11
+ [x: string]: any;
12
+ },
13
+ ) => void;
14
+ }
15
+
4
16
  export const BaseAdapter: Omit<IBaseAdapter, 'key' | 'label' | 'version'> = {
5
17
  log(message: string, { level = LogLevel.Debug, ...options } = {}) {
6
18
  return log(message, { level, ...options });
@@ -1,5 +1,12 @@
1
- import { IBaseAdapter, IBaseDirector } from '@unchainedshop/types/common.js';
2
1
  import { log } from '@unchainedshop/logger';
2
+ import { IBaseAdapter } from './BaseAdapter.js';
3
+
4
+ export interface IBaseDirector<Adapter extends IBaseAdapter> {
5
+ getAdapters: (options?: { adapterFilter?: (adapter: Adapter) => boolean }) => Array<Adapter>;
6
+ getAdapter: (key: string) => Adapter;
7
+ registerAdapter: (A: Adapter) => void;
8
+ unregisterAdapter: (key: string) => boolean;
9
+ }
3
10
 
4
11
  export const BaseDirector = <AdapterType extends IBaseAdapter>(
5
12
  directorName: string,
@@ -1,6 +1,37 @@
1
1
  import { log, LogLevel } from '@unchainedshop/logger';
2
+ import { UnchainedCore } from '@unchainedshop/core';
3
+ import { IBaseAdapter } from './BaseAdapter.js';
4
+ import { IPricingSheet, PricingCalculation } from './BasePricingSheet.js';
2
5
 
3
- import { IDiscountAdapter } from '@unchainedshop/types/discount.js';
6
+ export interface DiscountContext {
7
+ code?: string;
8
+ orderDiscount?: { _id?: string; orderId: string };
9
+ order?: { _id?: string; userId: string };
10
+ }
11
+
12
+ export type IDiscountAdapter<DiscountConfiguration> = IBaseAdapter & {
13
+ orderIndex: number;
14
+
15
+ isManualAdditionAllowed: (code: string) => Promise<boolean>;
16
+ isManualRemovalAllowed: () => Promise<boolean>;
17
+
18
+ actions: (params: {
19
+ context: DiscountContext & UnchainedCore;
20
+ }) => Promise<DiscountAdapterActions<DiscountConfiguration>>;
21
+ };
22
+
23
+ export interface DiscountAdapterActions<DiscountConfiguration> {
24
+ isValidForSystemTriggering: () => Promise<boolean>;
25
+ isValidForCodeTriggering: (params: { code: string }) => Promise<boolean>;
26
+
27
+ discountForPricingAdapterKey: (params: {
28
+ pricingAdapterKey: string;
29
+ calculationSheet: IPricingSheet<PricingCalculation>;
30
+ }) => DiscountConfiguration;
31
+
32
+ reserve: (params: { code: string }) => Promise<any>;
33
+ release: () => Promise<void>;
34
+ }
4
35
 
5
36
  export const BaseDiscountAdapter: Omit<IDiscountAdapter<unknown>, 'key' | 'label' | 'version'> = {
6
37
  orderIndex: 0,
@@ -1,6 +1,19 @@
1
- import { IDiscountAdapter, IDiscountDirector } from '@unchainedshop/types/discount.js';
2
1
  import { log } from '@unchainedshop/logger';
3
- import { BaseDirector } from './BaseDirector.js';
2
+ import { BaseDirector, IBaseDirector } from './BaseDirector.js';
3
+ import { DiscountContext, IDiscountAdapter } from './BaseDiscountAdapter.js';
4
+ import { UnchainedCore } from '@unchainedshop/core';
5
+
6
+ export type IDiscountDirector<DiscountConfiguration> = IBaseDirector<
7
+ IDiscountAdapter<DiscountConfiguration>
8
+ > & {
9
+ actions: (
10
+ discountContext: DiscountContext,
11
+ unchainedAPI: UnchainedCore,
12
+ ) => Promise<{
13
+ resolveDiscountKeyFromStaticCode: (params: { code: string }) => Promise<string | null>;
14
+ findSystemDiscounts: () => Promise<Array<string>>;
15
+ }>;
16
+ };
4
17
 
5
18
  export const BaseDiscountDirector = <DiscountConfigurationType>(
6
19
  directorName: string,
@@ -1,16 +1,64 @@
1
- import {
2
- BasePricingAdapterContext,
3
- IPricingSheet,
4
- IPricingAdapter,
5
- PricingCalculation,
6
- IPricingAdapterActions,
7
- } from '@unchainedshop/types/pricing.js';
1
+ import { UnchainedCore } from '@unchainedshop/core';
2
+ import { User } from '@unchainedshop/core-users';
8
3
  import { log, LogLevel } from '@unchainedshop/logger';
4
+ import { IPricingSheet, PricingCalculation } from './BasePricingSheet.js';
5
+ import { IBaseAdapter } from './BaseAdapter.js';
6
+ import { Discount } from './BasePricingDirector.js';
7
+
8
+ type Order = { _id?: string };
9
+ type OrderDiscount = { _id?: string };
10
+
11
+ interface IDiscountableItem {
12
+ _id?: string;
13
+ orderId?: string;
14
+ }
15
+
16
+ export interface BasePricingAdapterContext extends UnchainedCore {
17
+ order?: Order;
18
+ user: User;
19
+ discounts: Array<OrderDiscount>;
20
+ }
21
+
22
+ export type BasePricingContext =
23
+ | {
24
+ order?: Order;
25
+ user?: User;
26
+ discounts?: Array<OrderDiscount>;
27
+ }
28
+ | {
29
+ item: IDiscountableItem;
30
+ };
31
+
32
+ export interface IPricingAdapterActions<
33
+ Calculation extends PricingCalculation,
34
+ PricingAdapterContext extends BasePricingAdapterContext,
35
+ > {
36
+ calculate: () => Promise<Array<Calculation>>;
37
+ getCalculation: () => Array<Calculation>;
38
+ getContext: () => PricingAdapterContext;
39
+ }
40
+
41
+ export type IPricingAdapter<
42
+ PricingAdapterContext extends BasePricingAdapterContext,
43
+ Calculation extends PricingCalculation,
44
+ Sheet extends IPricingSheet<Calculation>,
45
+ DiscountConfiguration = unknown,
46
+ > = IBaseAdapter & {
47
+ orderIndex: number;
48
+
49
+ isActivatedFor: (context: PricingAdapterContext) => boolean;
50
+
51
+ actions: (params: {
52
+ context: PricingAdapterContext;
53
+ calculationSheet: Sheet;
54
+ discounts: Array<Discount<DiscountConfiguration>>;
55
+ }) => IPricingAdapterActions<Calculation, PricingAdapterContext> & { resultSheet: () => Sheet };
56
+ };
9
57
 
10
58
  export const BasePricingAdapter = <
11
- Context extends BasePricingAdapterContext,
59
+ PricingAdapterContext extends BasePricingAdapterContext,
12
60
  Calculation extends PricingCalculation,
13
- >(): IPricingAdapter<Context, Calculation, IPricingSheet<Calculation>> => ({
61
+ >(): IPricingAdapter<PricingAdapterContext, Calculation, IPricingSheet<Calculation>> => ({
14
62
  key: '',
15
63
  label: '',
16
64
  version: '',
@@ -22,7 +70,7 @@ export const BasePricingAdapter = <
22
70
 
23
71
  actions: (params) => {
24
72
  const calculation = [];
25
- const actions: IPricingAdapterActions<Calculation, Context> = {
73
+ const actions: IPricingAdapterActions<Calculation, PricingAdapterContext> = {
26
74
  calculate: async () => {
27
75
  return [];
28
76
  },
@@ -30,7 +78,7 @@ export const BasePricingAdapter = <
30
78
  getContext: () => params.context,
31
79
  };
32
80
 
33
- return actions as IPricingAdapterActions<Calculation, Context> & {
81
+ return actions as IPricingAdapterActions<Calculation, PricingAdapterContext> & {
34
82
  resultSheet: () => IPricingSheet<Calculation>;
35
83
  };
36
84
  },
@@ -1,15 +1,42 @@
1
- import { Discount } from '@unchainedshop/types/discount.js';
1
+ import { log, LogLevel } from '@unchainedshop/logger';
2
+ import { BaseDirector, IBaseDirector } from './BaseDirector.js';
3
+ import { UnchainedCore } from '@unchainedshop/core';
2
4
  import {
3
5
  BasePricingAdapterContext,
4
6
  BasePricingContext,
5
- IPricingDirector,
6
7
  IPricingAdapter,
7
- IPricingSheet,
8
- PricingCalculation,
9
8
  IPricingAdapterActions,
10
- } from '@unchainedshop/types/pricing.js';
11
- import { log, LogLevel } from '@unchainedshop/logger';
12
- import { BaseDirector } from './BaseDirector.js';
9
+ } from './BasePricingAdapter.js';
10
+ import { IPricingSheet, PricingCalculation } from './BasePricingSheet.js';
11
+ export interface Discount<DiscountConfiguration> {
12
+ discountId: string;
13
+ configuration: DiscountConfiguration;
14
+ }
15
+
16
+ export type IPricingDirector<
17
+ PricingContext extends BasePricingContext,
18
+ Calculation extends PricingCalculation,
19
+ PricingAdapterContext extends BasePricingAdapterContext,
20
+ PricingAdapterSheet extends IPricingSheet<Calculation>,
21
+ Adapter extends IPricingAdapter<PricingAdapterContext, Calculation, PricingAdapterSheet>,
22
+ > = IBaseDirector<Adapter> & {
23
+ buildPricingContext: (
24
+ context: PricingContext,
25
+ unchainedAPI: UnchainedCore,
26
+ ) => Promise<PricingAdapterContext>;
27
+ actions: (
28
+ pricingContext: PricingContext,
29
+ unchainedAPI: UnchainedCore,
30
+ buildPricingContext?: (
31
+ pricingCtx: PricingContext,
32
+ _unchainedAPI: UnchainedCore,
33
+ ) => Promise<PricingAdapterContext>,
34
+ ) => Promise<
35
+ IPricingAdapterActions<Calculation, PricingAdapterContext> & {
36
+ calculationSheet: () => PricingAdapterSheet;
37
+ }
38
+ >;
39
+ };
13
40
 
14
41
  export const BasePricingDirector = <
15
42
  DirectorContext extends BasePricingContext,
@@ -61,10 +88,10 @@ export const BasePricingDirector = <
61
88
  context.discounts.map(async (discount) => ({
62
89
  discountId: discount._id,
63
90
  configuration: await context.modules.orders.discounts.configurationForPricingAdapterKey(
64
- discount,
91
+ discount as any,
65
92
  Adapter.key,
66
93
  this.calculationSheet(),
67
- context,
94
+ context as any,
68
95
  ),
69
96
  })),
70
97
  );
@@ -1,8 +1,48 @@
1
- import {
2
- PricingCalculation,
3
- IBasePricingSheet,
4
- PricingSheetParams,
5
- } from '@unchainedshop/types/pricing.js';
1
+ export interface PricingCalculation {
2
+ category: string;
3
+ amount: number;
4
+ baseCategory?: string;
5
+ meta?: any;
6
+ }
7
+ export interface PricingDiscount {
8
+ discountId: string;
9
+ amount: number;
10
+ currency: string;
11
+ }
12
+
13
+ export interface IPricingSheet<Calculation extends PricingCalculation>
14
+ extends IBasePricingSheet<Calculation> {
15
+ discountPrices: (discountId?: string) => Array<PricingDiscount>;
16
+ addDiscount: (params: { amount: number; discountId: string; meta?: any }) => void;
17
+ }
18
+
19
+ export interface IBasePricingSheet<Calculation extends PricingCalculation> {
20
+ calculation: Array<Calculation>;
21
+ currency?: string;
22
+ quantity?: number;
23
+
24
+ getRawPricingSheet: () => Array<Calculation>;
25
+ filterBy: (filter?: Partial<Calculation>) => Array<Calculation>;
26
+ isValid: () => boolean;
27
+
28
+ gross: () => number;
29
+ net: () => number;
30
+ sum: (filter?: Partial<Calculation>) => number;
31
+ total: (params?: { category?: string; discountId?: string; useNetPrice?: boolean }) => {
32
+ amount: number;
33
+ currency: string;
34
+ };
35
+
36
+ taxSum: (filter?: Partial<Calculation>) => number;
37
+
38
+ resetCalculation: (sheetToInvert: IBasePricingSheet<Calculation>) => Array<Calculation>;
39
+ }
40
+
41
+ export interface PricingSheetParams<Calculation extends PricingCalculation> {
42
+ calculation?: Array<Calculation>;
43
+ currency?: string;
44
+ quantity?: number;
45
+ }
6
46
 
7
47
  export const BasePricingSheet = <Calculation extends PricingCalculation>(
8
48
  params: PricingSheetParams<Calculation>,
@@ -1,26 +1,29 @@
1
- import { Currency } from '@unchainedshop/types/currencies.js';
2
- import type { Locale as LocaleType, Locales as LocalesType } from 'locale';
3
- import localePkg from 'locale';
4
-
5
- const { Locales, Locale } = localePkg;
1
+ import { Country } from '@unchainedshop/core-countries';
2
+ import { Currency } from '@unchainedshop/core-currencies';
3
+ import { resolveAcceptLanguage } from 'resolve-accept-language';
6
4
 
7
5
  const { UNCHAINED_LANG = 'de', UNCHAINED_COUNTRY = 'CH', UNCHAINED_CURRENCY = 'CHF' } = process.env;
8
6
 
9
- export const systemLocale = new Locale(`${UNCHAINED_LANG}-${UNCHAINED_COUNTRY}`);
7
+ export const systemLocale = new Intl.Locale(`${UNCHAINED_LANG}-${UNCHAINED_COUNTRY}`);
10
8
 
11
9
  export const resolveBestSupported = (
12
10
  acceptLanguage: string,
13
- supportedLocales: LocalesType,
14
- ): LocaleType => {
15
- const acceptLocale = new Locales(acceptLanguage);
16
- const bestLocale = acceptLocale.best(supportedLocales);
17
- if (!bestLocale) return systemLocale;
18
- return bestLocale;
11
+ supportedLocales: Array<string>,
12
+ fallbackLocale: string,
13
+ ): Intl.Locale => {
14
+ const { match } = resolveAcceptLanguage(acceptLanguage || '', supportedLocales, fallbackLocale, {
15
+ returnMatchType: true,
16
+ });
17
+ return new Intl.Locale(match);
19
18
  };
20
19
 
21
- export const resolveBestCountry = (localeCountry, shopCountry, countries) => {
20
+ export const resolveBestCountry = (
21
+ localeCountry: string,
22
+ shopCountry: string,
23
+ countries: Array<Country>,
24
+ ) => {
22
25
  if (shopCountry) {
23
- const resolvedCountry = countries.reduce((lastResolved, country) => {
26
+ const resolvedCountry = countries.reduce<string>((lastResolved, country) => {
24
27
  if (shopCountry === country.isoCode) {
25
28
  return country.isoCode;
26
29
  }
@@ -30,10 +33,10 @@ export const resolveBestCountry = (localeCountry, shopCountry, countries) => {
30
33
  return resolvedCountry;
31
34
  }
32
35
  }
33
- return localeCountry || systemLocale.country;
36
+ return localeCountry || systemLocale.region;
34
37
  };
35
38
 
36
- export const resolveBestCurrency = (localeCurrency, currencies: Array<Currency>) => {
39
+ export const resolveBestCurrency = (localeCurrency: string, currencies: Array<Currency>) => {
37
40
  if (localeCurrency) {
38
41
  const resolvedCurrency = currencies.find((currency) => currency.isoCode === localeCurrency);
39
42
  if (resolvedCurrency) {
@@ -48,17 +51,3 @@ export const resolveBestCurrency = (localeCurrency, currencies: Array<Currency>)
48
51
 
49
52
  return currencies?.[0]?.isoCode || UNCHAINED_CURRENCY;
50
53
  };
51
-
52
- export const resolveUserRemoteAddress = (req) => {
53
- const remoteAddress =
54
- req.headers['x-real-ip'] ||
55
- req.headers['x-forwarded-for'] ||
56
- req.connection?.remoteAddress ||
57
- req.socket?.remoteAddress ||
58
- req.connection?.socket?.remoteAddress;
59
-
60
- const remotePort =
61
- req.connection?.remotePort || req.socket?.remotePort || req.connection?.socket?.remotePort;
62
-
63
- return { remoteAddress, remotePort };
64
- };
@@ -1,4 +1,4 @@
1
- const invertMapObject = (theMapObject) =>
1
+ const invertMapObject = (theMapObject: Record<string, string>): Record<string, string> =>
2
2
  Object.keys(theMapObject).reduce((invertedObj, key) => {
3
3
  const newObj = invertedObj;
4
4
  newObj[theMapObject[key]] = key;
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  resolveBestCountry,
3
3
  resolveBestSupported,
4
- resolveUserRemoteAddress,
5
4
  slugify,
6
5
  systemLocale,
7
6
  } from '@unchainedshop/utils';
@@ -13,7 +12,6 @@ describe('Utils', () => {
13
12
  expect(systemLocale).toBeDefined();
14
13
  expect(resolveBestCountry).toBeInstanceOf(Function)
15
14
  expect(resolveBestSupported).toBeInstanceOf(Function)
16
- expect(resolveUserRemoteAddress).toBeInstanceOf(Function)
17
15
  });
18
16
 
19
17
  describe('generateHashId', () => {
@@ -13,78 +13,28 @@ export { default as buildObfuscatedFieldsFilter } from './build-obfuscated-field
13
13
  * Schemas
14
14
  */
15
15
 
16
- export { Schemas } from './schemas/index.js';
16
+ export enum SortDirection {
17
+ ASC = 'ASC',
18
+ DESC = 'DESC',
19
+ }
20
+
21
+ export type SortOption = {
22
+ key: string;
23
+ value: SortDirection;
24
+ };
17
25
 
26
+ export type NodeOrTree<T> = string | Tree<T>; // eslint-disable-line
27
+ export type Tree<T> = Array<NodeOrTree<T>>;
18
28
  /*
19
29
  * Director
20
30
  */
21
31
 
22
- export { BaseAdapter } from './director/BaseAdapter.js';
23
- export { BaseDirector } from './director/BaseDirector.js';
24
- export { BasePricingAdapter } from './director/BasePricingAdapter.js';
25
- export { BasePricingDirector } from './director/BasePricingDirector.js';
26
- export { BasePricingSheet } from './director/BasePricingSheet.js';
27
- export { BaseDiscountAdapter } from './director/BaseDiscountAdapter.js';
28
- export { BaseDiscountDirector } from './director/BaseDiscountDirector.js';
29
-
30
- /*
31
-
32
- function resolveBestCountry(
33
- contextCountry: string,
34
- headerCountry: string | string[],
35
- countries: Array<Country>,
36
- ): string;
37
- function resolveUserRemoteAddress(req: IncomingMessage): {
38
- remoteAddress: string;
39
- remotePort: string;
40
- };
41
-
42
- function objectInvert(obj: Record<string, string>): Record<string, string>;
43
-
44
- const systemLocale: Locale;
45
-
46
- const Schemas: {
47
- timestampFields: TimestampFields;
48
- User: SimpleSchema;
49
- Address: SimpleSchema;
50
- Contact: SimpleSchema;
51
- };
52
-
53
- // Director
54
- const BaseAdapter: IBaseAdapter;
55
- const BaseDirector: <Adapter extends IBaseAdapter>(
56
- directorName: string,
57
- options?: {
58
- adapterSortKey?: string;
59
- adapterKeyField?: string;
60
- },
61
- ) => IBaseDirector<Adapter>;
62
-
63
- const BaseDiscountAdapter: Omit<IDiscountAdapter, 'key' | 'label' | 'version'>;
64
- const BaseDiscountDirector: (directorName: string) => IDiscountDirector;
65
-
66
- const BasePricingAdapter: <
67
- AdapterContext extends BasePricingAdapterContext,
68
- Calculation extends PricingCalculation,
69
- >() => IPricingAdapter<AdapterContext, Calculation, IPricingSheet<Calculation>>;
70
-
71
- const BasePricingDirector: <
72
- PricingContext extends BasePricingContext,
73
- AdapterPricingContext extends BasePricingAdapterContext,
74
- Calculation extends PricingCalculation,
75
- Adapter extends IPricingAdapter<AdapterPricingContext, Calculation, IPricingSheet<Calculation>>,
76
- >(
77
- directorName: string,
78
- ) => IPricingDirector<
79
- PricingContext,
80
- Calculation,
81
- AdapterPricingContext,
82
- IPricingSheet<Calculation>,
83
- Adapter
84
- >;
32
+ export * from './director/BaseAdapter.js';
33
+ export * from './director/BaseDirector.js';
85
34
 
86
- const BasePricingSheet: <Calculation extends PricingCalculation>(
87
- params: PricingSheetParams<Calculation>,
88
- ) => IBasePricingSheet<Calculation>;
35
+ export * from './director/BasePricingAdapter.js';
36
+ export * from './director/BasePricingDirector.js';
37
+ export * from './director/BasePricingSheet.js';
89
38
 
90
- */
39
+ export * from './director/BaseDiscountAdapter.js';
40
+ export * from './director/BaseDiscountDirector.js';
@@ -1,3 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export declare const AddressSchema: SimpleSchema;
3
- //# sourceMappingURL=AddressSchema.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AddressSchema.d.ts","sourceRoot":"","sources":["../../src/schemas/AddressSchema.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,eAAO,MAAM,aAAa,cAazB,CAAC"}
@@ -1,13 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export const AddressSchema = new SimpleSchema({
3
- firstName: String,
4
- lastName: String,
5
- company: String,
6
- addressLine: String,
7
- addressLine2: String,
8
- city: String,
9
- postalCode: String,
10
- regionCode: String,
11
- countryCode: String,
12
- }, { requiredByDefault: false });
13
- //# sourceMappingURL=AddressSchema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AddressSchema.js","sourceRoot":"","sources":["../../src/schemas/AddressSchema.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,YAAY,CAC3C;IACE,SAAS,EAAE,MAAM;IACjB,QAAQ,EAAE,MAAM;IAChB,OAAO,EAAE,MAAM;IACf,WAAW,EAAE,MAAM;IACnB,YAAY,EAAE,MAAM;IACpB,IAAI,EAAE,MAAM;IACZ,UAAU,EAAE,MAAM;IAClB,UAAU,EAAE,MAAM;IAClB,WAAW,EAAE,MAAM;CACpB,EACD,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAC7B,CAAC"}
@@ -1,3 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export declare const ContactSchema: SimpleSchema;
3
- //# sourceMappingURL=ContactSchema.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ContactSchema.d.ts","sourceRoot":"","sources":["../../src/schemas/ContactSchema.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,eAAO,MAAM,aAAa,cAMzB,CAAC"}
@@ -1,6 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export const ContactSchema = new SimpleSchema({
3
- telNumber: String,
4
- emailAddress: String,
5
- }, { requiredByDefault: false });
6
- //# sourceMappingURL=ContactSchema.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ContactSchema.js","sourceRoot":"","sources":["../../src/schemas/ContactSchema.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,YAAY,CAC3C;IACE,SAAS,EAAE,MAAM;IACjB,YAAY,EAAE,MAAM;CACrB,EACD,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAC7B,CAAC"}
@@ -1,5 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export declare const LastLoginSchema: SimpleSchema;
3
- export declare const LastContactSchema: SimpleSchema;
4
- export declare const UserSchema: SimpleSchema;
5
- //# sourceMappingURL=UsersSchema.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"UsersSchema.d.ts","sourceRoot":"","sources":["../../src/schemas/UsersSchema.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,cAAc,CAAC;AAexC,eAAO,MAAM,eAAe,cAU3B,CAAC;AAEF,eAAO,MAAM,iBAAiB,cAM7B,CAAC;AAEF,eAAO,MAAM,UAAU,cA+BtB,CAAC"}