@commercelayer/organization-config 1.4.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Commerce Layer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # organization-config
2
+
3
+ Organization config utils for extracting config by market.
4
+
5
+ ## How to use
6
+
7
+ ```shell
8
+ pnpm add @commercelayer/organization-config
9
+ ```
10
+
11
+ you can use the function `getConfig` to manipulate the configuration settings of your organization.
12
+
13
+ ```javascript
14
+ getConfig({
15
+ jsonConfig: {},
16
+ market: 'market:id:hashid',
17
+ params: { lang: 'en', orderId: 'your-order-id' }
18
+ })
19
+
20
+ ```
21
+
22
+ The function is going to manipulate the organization configuration, extract the `mfe` key and merge `default` configuration with overrides for the market used.
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";var u=Object.create;var o=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var d=Object.getPrototypeOf,y=Object.prototype.hasOwnProperty;var k=(e,n)=>{for(var t in n)o(e,t,{get:n[t],enumerable:!0})},g=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of p(n))!y.call(e,i)&&i!==t&&o(e,i,{get:()=>n[i],enumerable:!(r=C(n,i))||r.enumerable});return e};var m=(e,n,t)=>(t=e!=null?u(d(e)):{},g(n||!e||!e.__esModule?o(t,"default",{value:e,enumerable:!0}):t,e)),_=e=>g(o({},"__esModule",{value:!0}),e);var h={};k(h,{getConfig:()=>N});module.exports=_(h);var f=m(require("lodash/merge"),1);function N({jsonConfig:e,market:n,params:t}){if(e?.mfe==null)return console.warn("No configuration found."),null;let r=e?.mfe?.default,i=n!=null?e?.mfe[n]:{};function a(s){let c=JSON.stringify(s).replace(/:lang/g,t?.lang??":lang").replace(/:access_token/g,t?.accessToken??":access_token").replace(/:order_id/g,t?.orderId??":order_id");return JSON.parse(c)}let l=(0,f.default)(JSON.parse(JSON.stringify(r)),i);return a(l)}0&&(module.exports={getConfig});
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import merge from 'lodash/merge'\n\nexport type NullableType<T> = T | null | undefined\n\ninterface ConfigParams {\n /**\n * language used to replace `:lang` placeholder\n */\n lang?: NullableType<string>\n /**\n * accessToken used to replace `:access_token` placeholder\n */\n accessToken?: NullableType<string>\n /**\n * order id used to replace `:order_id` placeholder\n */\n orderId?: NullableType<string>\n}\n\ninterface LinkConfig {\n cart?: string\n checkout?: string\n my_account?: string\n identity?: string\n}\n\ninterface Country {\n value: string\n label: string\n}\n\ntype StateConfig = Record<string, Country[]>\n\ninterface CheckoutConfig {\n thankyou_page?: string\n billing_countries?: Country[]\n shipping_countries?: Country[]\n billing_states?: StateConfig[]\n shipping_states?: StateConfig[]\n default_country?: string\n}\n\ninterface UrlsConfig {\n privacy: string\n terms: string\n}\n\nexport interface DefaultConfig {\n links?: LinkConfig\n checkout?: CheckoutConfig\n urls?: UrlsConfig\n}\n\nexport interface Configs {\n default: DefaultConfig\n [key: string]: Partial<DefaultConfig>\n}\n\ninterface GetConfigProps {\n /**\n * `config` attribute of the organization\n */\n jsonConfig?: { mfe?: Configs }\n /**\n * `market:id:hashid` used to override default configuration\n */\n market?: string\n /**\n * `params` used to replace url placeholders\n */\n params?: ConfigParams\n}\n\n/**\n * The function is going to manipulate the organization configuration,\n * extract the `mfe` key and merge `default` configuration with overrides\n * for the market used.\n */\nexport function getConfig({\n jsonConfig,\n market,\n params\n}: GetConfigProps): DefaultConfig | null {\n if (jsonConfig?.mfe == null) {\n console.warn('No configuration found.')\n return null\n }\n\n const defaultConfig = jsonConfig?.mfe?.default\n const overrideConfig = market != null ? jsonConfig?.mfe[market] : {}\n\n // Replace placeholders in all string values within the object\n function replacePlaceholders(config: DefaultConfig): DefaultConfig {\n const replacedConfig = JSON.stringify(config)\n .replace(/:lang/g, params?.lang ?? ':lang')\n .replace(/:access_token/g, params?.accessToken ?? ':access_token')\n .replace(/:order_id/g, params?.orderId ?? ':order_id')\n\n return JSON.parse(replacedConfig)\n }\n\n const mergedConfig: DefaultConfig = merge(\n JSON.parse(JSON.stringify(defaultConfig)),\n overrideConfig\n )\n return replacePlaceholders(mergedConfig)\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,eAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAkB,6BA8EX,SAASF,EAAU,CACxB,WAAAG,EACA,OAAAC,EACA,OAAAC,CACF,EAAyC,CACvC,GAAIF,GAAY,KAAO,KACrB,eAAQ,KAAK,yBAAyB,EAC/B,KAGT,IAAMG,EAAgBH,GAAY,KAAK,QACjCI,EAAiBH,GAAU,KAAOD,GAAY,IAAIC,CAAM,EAAI,CAAC,EAGnE,SAASI,EAAoBC,EAAsC,CACjE,IAAMC,EAAiB,KAAK,UAAUD,CAAM,EACzC,QAAQ,SAAUJ,GAAQ,MAAQ,OAAO,EACzC,QAAQ,iBAAkBA,GAAQ,aAAe,eAAe,EAChE,QAAQ,aAAcA,GAAQ,SAAW,WAAW,EAEvD,OAAO,KAAK,MAAMK,CAAc,CAClC,CAEA,IAAMC,KAA8B,EAAAC,SAClC,KAAK,MAAM,KAAK,UAAUN,CAAa,CAAC,EACxCC,CACF,EACA,OAAOC,EAAoBG,CAAY,CACzC","names":["src_exports","__export","getConfig","__toCommonJS","import_merge","jsonConfig","market","params","defaultConfig","overrideConfig","replacePlaceholders","config","replacedConfig","mergedConfig","merge"]}
@@ -0,0 +1,71 @@
1
+ type NullableType<T> = T | null | undefined;
2
+ interface ConfigParams {
3
+ /**
4
+ * language used to replace `:lang` placeholder
5
+ */
6
+ lang?: NullableType<string>;
7
+ /**
8
+ * accessToken used to replace `:access_token` placeholder
9
+ */
10
+ accessToken?: NullableType<string>;
11
+ /**
12
+ * order id used to replace `:order_id` placeholder
13
+ */
14
+ orderId?: NullableType<string>;
15
+ }
16
+ interface LinkConfig {
17
+ cart?: string;
18
+ checkout?: string;
19
+ my_account?: string;
20
+ identity?: string;
21
+ }
22
+ interface Country {
23
+ value: string;
24
+ label: string;
25
+ }
26
+ type StateConfig = Record<string, Country[]>;
27
+ interface CheckoutConfig {
28
+ thankyou_page?: string;
29
+ billing_countries?: Country[];
30
+ shipping_countries?: Country[];
31
+ billing_states?: StateConfig[];
32
+ shipping_states?: StateConfig[];
33
+ default_country?: string;
34
+ }
35
+ interface UrlsConfig {
36
+ privacy: string;
37
+ terms: string;
38
+ }
39
+ interface DefaultConfig {
40
+ links?: LinkConfig;
41
+ checkout?: CheckoutConfig;
42
+ urls?: UrlsConfig;
43
+ }
44
+ interface Configs {
45
+ default: DefaultConfig;
46
+ [key: string]: Partial<DefaultConfig>;
47
+ }
48
+ interface GetConfigProps {
49
+ /**
50
+ * `config` attribute of the organization
51
+ */
52
+ jsonConfig?: {
53
+ mfe?: Configs;
54
+ };
55
+ /**
56
+ * `market:id:hashid` used to override default configuration
57
+ */
58
+ market?: string;
59
+ /**
60
+ * `params` used to replace url placeholders
61
+ */
62
+ params?: ConfigParams;
63
+ }
64
+ /**
65
+ * The function is going to manipulate the organization configuration,
66
+ * extract the `mfe` key and merge `default` configuration with overrides
67
+ * for the market used.
68
+ */
69
+ declare function getConfig({ jsonConfig, market, params }: GetConfigProps): DefaultConfig | null;
70
+
71
+ export { type Configs, type DefaultConfig, type NullableType, getConfig };
@@ -0,0 +1,71 @@
1
+ type NullableType<T> = T | null | undefined;
2
+ interface ConfigParams {
3
+ /**
4
+ * language used to replace `:lang` placeholder
5
+ */
6
+ lang?: NullableType<string>;
7
+ /**
8
+ * accessToken used to replace `:access_token` placeholder
9
+ */
10
+ accessToken?: NullableType<string>;
11
+ /**
12
+ * order id used to replace `:order_id` placeholder
13
+ */
14
+ orderId?: NullableType<string>;
15
+ }
16
+ interface LinkConfig {
17
+ cart?: string;
18
+ checkout?: string;
19
+ my_account?: string;
20
+ identity?: string;
21
+ }
22
+ interface Country {
23
+ value: string;
24
+ label: string;
25
+ }
26
+ type StateConfig = Record<string, Country[]>;
27
+ interface CheckoutConfig {
28
+ thankyou_page?: string;
29
+ billing_countries?: Country[];
30
+ shipping_countries?: Country[];
31
+ billing_states?: StateConfig[];
32
+ shipping_states?: StateConfig[];
33
+ default_country?: string;
34
+ }
35
+ interface UrlsConfig {
36
+ privacy: string;
37
+ terms: string;
38
+ }
39
+ interface DefaultConfig {
40
+ links?: LinkConfig;
41
+ checkout?: CheckoutConfig;
42
+ urls?: UrlsConfig;
43
+ }
44
+ interface Configs {
45
+ default: DefaultConfig;
46
+ [key: string]: Partial<DefaultConfig>;
47
+ }
48
+ interface GetConfigProps {
49
+ /**
50
+ * `config` attribute of the organization
51
+ */
52
+ jsonConfig?: {
53
+ mfe?: Configs;
54
+ };
55
+ /**
56
+ * `market:id:hashid` used to override default configuration
57
+ */
58
+ market?: string;
59
+ /**
60
+ * `params` used to replace url placeholders
61
+ */
62
+ params?: ConfigParams;
63
+ }
64
+ /**
65
+ * The function is going to manipulate the organization configuration,
66
+ * extract the `mfe` key and merge `default` configuration with overrides
67
+ * for the market used.
68
+ */
69
+ declare function getConfig({ jsonConfig, market, params }: GetConfigProps): DefaultConfig | null;
70
+
71
+ export { type Configs, type DefaultConfig, type NullableType, getConfig };
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import l from"lodash/merge";function c({jsonConfig:e,market:t,params:n}){if(e?.mfe==null)return console.warn("No configuration found."),null;let i=e?.mfe?.default,r=t!=null?e?.mfe[t]:{};function o(f){let a=JSON.stringify(f).replace(/:lang/g,n?.lang??":lang").replace(/:access_token/g,n?.accessToken??":access_token").replace(/:order_id/g,n?.orderId??":order_id");return JSON.parse(a)}let g=l(JSON.parse(JSON.stringify(i)),r);return o(g)}export{c as getConfig};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import merge from 'lodash/merge'\n\nexport type NullableType<T> = T | null | undefined\n\ninterface ConfigParams {\n /**\n * language used to replace `:lang` placeholder\n */\n lang?: NullableType<string>\n /**\n * accessToken used to replace `:access_token` placeholder\n */\n accessToken?: NullableType<string>\n /**\n * order id used to replace `:order_id` placeholder\n */\n orderId?: NullableType<string>\n}\n\ninterface LinkConfig {\n cart?: string\n checkout?: string\n my_account?: string\n identity?: string\n}\n\ninterface Country {\n value: string\n label: string\n}\n\ntype StateConfig = Record<string, Country[]>\n\ninterface CheckoutConfig {\n thankyou_page?: string\n billing_countries?: Country[]\n shipping_countries?: Country[]\n billing_states?: StateConfig[]\n shipping_states?: StateConfig[]\n default_country?: string\n}\n\ninterface UrlsConfig {\n privacy: string\n terms: string\n}\n\nexport interface DefaultConfig {\n links?: LinkConfig\n checkout?: CheckoutConfig\n urls?: UrlsConfig\n}\n\nexport interface Configs {\n default: DefaultConfig\n [key: string]: Partial<DefaultConfig>\n}\n\ninterface GetConfigProps {\n /**\n * `config` attribute of the organization\n */\n jsonConfig?: { mfe?: Configs }\n /**\n * `market:id:hashid` used to override default configuration\n */\n market?: string\n /**\n * `params` used to replace url placeholders\n */\n params?: ConfigParams\n}\n\n/**\n * The function is going to manipulate the organization configuration,\n * extract the `mfe` key and merge `default` configuration with overrides\n * for the market used.\n */\nexport function getConfig({\n jsonConfig,\n market,\n params\n}: GetConfigProps): DefaultConfig | null {\n if (jsonConfig?.mfe == null) {\n console.warn('No configuration found.')\n return null\n }\n\n const defaultConfig = jsonConfig?.mfe?.default\n const overrideConfig = market != null ? jsonConfig?.mfe[market] : {}\n\n // Replace placeholders in all string values within the object\n function replacePlaceholders(config: DefaultConfig): DefaultConfig {\n const replacedConfig = JSON.stringify(config)\n .replace(/:lang/g, params?.lang ?? ':lang')\n .replace(/:access_token/g, params?.accessToken ?? ':access_token')\n .replace(/:order_id/g, params?.orderId ?? ':order_id')\n\n return JSON.parse(replacedConfig)\n }\n\n const mergedConfig: DefaultConfig = merge(\n JSON.parse(JSON.stringify(defaultConfig)),\n overrideConfig\n )\n return replacePlaceholders(mergedConfig)\n}\n"],"mappings":"AAAA,OAAOA,MAAW,eA8EX,SAASC,EAAU,CACxB,WAAAC,EACA,OAAAC,EACA,OAAAC,CACF,EAAyC,CACvC,GAAIF,GAAY,KAAO,KACrB,eAAQ,KAAK,yBAAyB,EAC/B,KAGT,IAAMG,EAAgBH,GAAY,KAAK,QACjCI,EAAiBH,GAAU,KAAOD,GAAY,IAAIC,CAAM,EAAI,CAAC,EAGnE,SAASI,EAAoBC,EAAsC,CACjE,IAAMC,EAAiB,KAAK,UAAUD,CAAM,EACzC,QAAQ,SAAUJ,GAAQ,MAAQ,OAAO,EACzC,QAAQ,iBAAkBA,GAAQ,aAAe,eAAe,EAChE,QAAQ,aAAcA,GAAQ,SAAW,WAAW,EAEvD,OAAO,KAAK,MAAMK,CAAc,CAClC,CAEA,IAAMC,EAA8BV,EAClC,KAAK,MAAM,KAAK,UAAUK,CAAa,CAAC,EACxCC,CACF,EACA,OAAOC,EAAoBG,CAAY,CACzC","names":["merge","getConfig","jsonConfig","market","params","defaultConfig","overrideConfig","replacePlaceholders","config","replacedConfig","mergedConfig"]}
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@commercelayer/organization-config",
3
+ "version": "1.4.0",
4
+ "description": "Organization config utils for extracting config by market",
5
+ "author": {
6
+ "name": "Matteo Alessani",
7
+ "email": "matteo@commercelayer.io"
8
+ },
9
+ "license": "MIT",
10
+ "type": "module",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "module": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/index.js",
19
+ "require": "./dist/index.cjs"
20
+ }
21
+ },
22
+ "engines": {
23
+ "node": ">=18",
24
+ "pnpm": ">=7"
25
+ },
26
+ "keywords": [
27
+ "react",
28
+ "commercelayer"
29
+ ],
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/commercelayer/common-utils.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/commercelayer/common-utils/issues"
36
+ },
37
+ "devDependencies": {
38
+ "@commercelayer/eslint-config-ts": "^1.4.0",
39
+ "@types/lodash": "^4.17.0",
40
+ "eslint": "^8.57.0",
41
+ "tsup": "^8.0.2",
42
+ "typescript": "^5.4.2",
43
+ "vitest": "^1.4.0"
44
+ },
45
+ "peerDependencies": {
46
+ "eslint": ">=8.0",
47
+ "typescript": ">=5.0"
48
+ },
49
+ "dependencies": {
50
+ "lodash": "^4.17.21"
51
+ },
52
+ "scripts": {
53
+ "lint": "eslint src --ext .ts,.tsx",
54
+ "lint:fix": "eslint src --ext .ts,.tsx --fix",
55
+ "test": "pnpm run lint && vitest run --silent",
56
+ "build": "tsup"
57
+ }
58
+ }