@commercelayer/cli-core 4.12.0 → 4.12.1

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/lib/cjs/raw.js CHANGED
@@ -1 +1,49 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Operation=exports.rawRequest=exports.readDataFile=void 0;const axios_1=__importDefault(require("axios")),fs_1=require("fs"),_1=require("."),rawRequest=async(e,t,r)=>await axios_1.default.request({method:e.operation,baseURL:e.baseUrl,url:`/api/${e.resource}`+(r?`/${r}`:""),headers:{"Content-Type":"application/vnd.api+json",Accept:"application/vnd.api+json",Authorization:`Bearer ${e.accessToken}`},data:t}).then(a=>a.data);exports.rawRequest=rawRequest;const readDataFile=e=>{let t,r;try{t=(0,fs_1.readFileSync)(e,"utf-8")}catch{throw new Error(`Unable to find or open the data file ${_1.clColor.msg.error(e)}`)}try{r=JSON.parse(t)}catch{throw new Error(`Unable to parse the data file ${_1.clColor.msg.error(e)}: invalid JSON format`)}return r.data?r:{data:r}};exports.readDataFile=readDataFile;var Operation;(function(e){e.Create="POST",e.Update="PATCH"})(Operation||(exports.Operation=Operation={}));
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Operation = exports.rawRequest = exports.readDataFile = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const fs_1 = require("fs");
9
+ const _1 = require(".");
10
+ const rawRequest = async (config, data, id) => {
11
+ return await axios_1.default.request({
12
+ method: config.operation,
13
+ baseURL: config.baseUrl,
14
+ url: `/api/${config.resource}` + (id ? `/${id}` : ''),
15
+ headers: {
16
+ 'Content-Type': 'application/vnd.api+json',
17
+ Accept: 'application/vnd.api+json',
18
+ Authorization: `Bearer ${config.accessToken}`,
19
+ },
20
+ data,
21
+ }).then(res => {
22
+ return res.data;
23
+ });
24
+ };
25
+ exports.rawRequest = rawRequest;
26
+ const readDataFile = (file) => {
27
+ let dataFile;
28
+ let dataJson;
29
+ try {
30
+ dataFile = (0, fs_1.readFileSync)(file, 'utf-8');
31
+ }
32
+ catch (error) {
33
+ throw new Error(`Unable to find or open the data file ${_1.clColor.msg.error(file)}`);
34
+ }
35
+ try {
36
+ dataJson = JSON.parse(dataFile);
37
+ }
38
+ catch (error) {
39
+ throw new Error(`Unable to parse the data file ${_1.clColor.msg.error(file)}: invalid JSON format`);
40
+ }
41
+ const data = dataJson.data ? dataJson : { data: dataJson };
42
+ return data;
43
+ };
44
+ exports.readDataFile = readDataFile;
45
+ var Operation;
46
+ (function (Operation) {
47
+ Operation["Create"] = "POST";
48
+ Operation["Update"] = "PATCH";
49
+ })(Operation || (exports.Operation = Operation = {}));
package/lib/cjs/schema.js CHANGED
@@ -1 +1,20 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.download=void 0;const axios_1=__importDefault(require("axios")),config_1=__importDefault(require("./config")),downloadSchema=async e=>{const a=`https://data.${config_1.default.api.default_app_domain}/schemas/`,t=`openapi${e?"_"+e.replace(/\./g,"-"):""}.json`,s=a+t;return(await axios_1.default.get(s)).data};exports.download=downloadSchema;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.download = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ const config_1 = __importDefault(require("./config"));
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ const downloadSchema = async (version) => {
11
+ const domain = config_1.default.api.default_app_domain;
12
+ const baseUrl = `https://data.${domain}/schemas/`;
13
+ const schemaVersion = version ? ('_' + version.replace(/\./g, '-')) : '';
14
+ const fileName = `openapi${schemaVersion}.json`;
15
+ const schemaUrl = baseUrl + fileName;
16
+ const schemaFile = await axios_1.default.get(schemaUrl);
17
+ const schemaData = schemaFile.data;
18
+ return schemaData;
19
+ };
20
+ exports.download = downloadSchema;
package/lib/cjs/style.js CHANGED
@@ -1 +1,39 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.highlight=exports.warning=exports.success=exports.error=exports.flag=exports.command=exports.id=exports.slug=exports.magenta=exports.cyan=exports.blue=exports.yellow=exports.green=exports.red=void 0;const chalk_1=__importDefault(require("chalk")),red=e=>chalk_1.default.red(e);exports.red=red;const green=e=>chalk_1.default.green(e);exports.green=green;const yellow=e=>chalk_1.default.yellow(e);exports.yellow=yellow;const blue=e=>chalk_1.default.blue(e);exports.blue=blue;const cyan=e=>chalk_1.default.cyan(e);exports.cyan=cyan;const magenta=e=>chalk_1.default.magenta(e);exports.magenta=magenta;const slug=e=>chalk_1.default.yellowBright(e);exports.slug=slug;const id=e=>chalk_1.default.magenta.bold(e);exports.id=id;const command=e=>chalk_1.default.cyan.italic(e);exports.command=command;const flag=e=>chalk_1.default.magenta.italic(e);exports.flag=flag;const error=e=>chalk_1.default.red(e);exports.error=error;const success=e=>chalk_1.default.green(e);exports.success=success;const warning=e=>chalk_1.default.yellow(e);exports.warning=warning;const highlight=e=>chalk_1.default.yellowBright(e);exports.highlight=highlight;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.highlight = exports.warning = exports.success = exports.error = exports.flag = exports.command = exports.id = exports.slug = exports.magenta = exports.cyan = exports.blue = exports.yellow = exports.green = exports.red = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ // Colors
9
+ const red = (txt) => { return chalk_1.default.red(txt); };
10
+ exports.red = red;
11
+ const green = (txt) => { return chalk_1.default.green(txt); };
12
+ exports.green = green;
13
+ const yellow = (txt) => { return chalk_1.default.yellow(txt); };
14
+ exports.yellow = yellow;
15
+ const blue = (txt) => { return chalk_1.default.blue(txt); };
16
+ exports.blue = blue;
17
+ const cyan = (txt) => { return chalk_1.default.cyan(txt); };
18
+ exports.cyan = cyan;
19
+ const magenta = (txt) => { return chalk_1.default.magenta(txt); };
20
+ exports.magenta = magenta;
21
+ // API
22
+ const slug = (txt) => { return chalk_1.default.yellowBright(txt); };
23
+ exports.slug = slug;
24
+ const id = (txt) => { return chalk_1.default.magenta.bold(txt); };
25
+ exports.id = id;
26
+ // CLI
27
+ const command = (txt) => { return chalk_1.default.cyan.italic(txt); };
28
+ exports.command = command;
29
+ const flag = (txt) => { return chalk_1.default.magenta.italic(txt); };
30
+ exports.flag = flag;
31
+ // Messages
32
+ const error = (txt) => { return chalk_1.default.red(txt); };
33
+ exports.error = error;
34
+ const success = (txt) => { return chalk_1.default.green(txt); };
35
+ exports.success = success;
36
+ const warning = (txt) => { return chalk_1.default.yellow(txt); };
37
+ exports.warning = warning;
38
+ const highlight = (txt) => { return chalk_1.default.yellowBright(txt); };
39
+ exports.highlight = highlight;
package/lib/cjs/symbol.js CHANGED
@@ -1 +1,28 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.symbols=void 0;const symbols={check:{small:"\u2714",bkgGreen:"\u2705",squareRoot:"\u221A"},cross:{small:"\u2718",red:"\u274C"},clock:{stopwatch:"\u23F1"},arrow:{down:"\u2193"},selection:{fisheye:"\u25C9"}};exports.symbols=symbols,symbols.check.heavy=symbols.check.small,symbols.check.whiteHeavy=symbols.check.bkgGreen,symbols.cross.heavyBallot=symbols.cross.small;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.symbols = void 0;
4
+ const symbols = {
5
+ check: {
6
+ small: '\u2714', // ✔ (heavy)
7
+ bkgGreen: '\u2705', // ✅ (whiteHeavy)
8
+ squareRoot: '\u221A' // √
9
+ },
10
+ cross: {
11
+ small: '\u2718', // ✘ (heavyBallot)
12
+ red: '\u274C' // ❌ (crossMark)
13
+ },
14
+ clock: {
15
+ stopwatch: '\u23F1' // ⏱
16
+ },
17
+ arrow: {
18
+ down: '\u2193' // ↓ (downArrow)
19
+ },
20
+ selection: {
21
+ fisheye: '\u25C9' // ◉ (fishEye)
22
+ }
23
+ };
24
+ exports.symbols = symbols;
25
+ // ALIASES
26
+ symbols.check.heavy = symbols.check.small;
27
+ symbols.check.whiteHeavy = symbols.check.bkgGreen;
28
+ symbols.cross.heavyBallot = symbols.cross.small;
package/lib/cjs/text.js CHANGED
@@ -1 +1,23 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.symbols=exports.camelize=exports.singularize=exports.pluralize=exports.capitalize=void 0;const inflector_1=__importDefault(require("./inflector")),capitalize=e=>{if(!e)return e;let t=e.toLowerCase();return t=t.substring(0,1).toUpperCase()+t.substring(1),t};exports.capitalize=capitalize;const pluralize=inflector_1.default.pluralize;exports.pluralize=pluralize;const singularize=inflector_1.default.singularize;exports.singularize=singularize;const camelize=inflector_1.default.camelize;exports.camelize=camelize;var symbol_1=require("./symbol");Object.defineProperty(exports,"symbols",{enumerable:!0,get:function(){return symbol_1.symbols}});
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.symbols = exports.camelize = exports.singularize = exports.pluralize = exports.capitalize = void 0;
7
+ const inflector_1 = __importDefault(require("./inflector"));
8
+ const capitalize = (str) => {
9
+ if (!str)
10
+ return str;
11
+ let s = str.toLowerCase();
12
+ s = s.substring(0, 1).toUpperCase() + s.substring(1);
13
+ return s;
14
+ };
15
+ exports.capitalize = capitalize;
16
+ const pluralize = inflector_1.default.pluralize;
17
+ exports.pluralize = pluralize;
18
+ const singularize = inflector_1.default.singularize;
19
+ exports.singularize = singularize;
20
+ const camelize = inflector_1.default.camelize;
21
+ exports.camelize = camelize;
22
+ var symbol_1 = require("./symbol");
23
+ Object.defineProperty(exports, "symbols", { enumerable: true, get: function () { return symbol_1.symbols; } });
package/lib/cjs/token.js CHANGED
@@ -1 +1,177 @@
1
- "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,n,o){o===void 0&&(o=n);var r=Object.getOwnPropertyDescriptor(t,n);(!r||("get"in r?!t.__esModule:r.writable||r.configurable))&&(r={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,o,r)}:function(e,t,n,o){o===void 0&&(o=n),e[o]=t[n]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(e!=null)for(var n in e)n!=="default"&&Object.prototype.hasOwnProperty.call(e,n)&&__createBinding(t,e,n);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.getTokenEnvironment=exports.isAccessTokenExpiring=exports.revokeAccessToken=exports.getAccessToken=exports.generateAccessToken=exports.decodeAccessToken=void 0;const jsonwebtoken_1=__importDefault(require("jsonwebtoken")),config_1=__importDefault(require("./config")),https_1=__importDefault(require("https")),application_1=require("./application"),util_1=require("./util"),api_1=require("./api"),errors_1=require("@oclif/core/lib/errors"),js_auth_1=__importStar(require("@commercelayer/js-auth")),decodeAccessToken=e=>{const t=jsonwebtoken_1.default.decode(e);if(t===null)throw new Error("Error decoding access token");return t};exports.decodeAccessToken=decodeAccessToken;const generateAccessToken=(e,t,n)=>{const r=Object.assign(Object.assign({},e),{exp:Math.floor(Date.now()/1e3)+n*60,rand:Math.random()}),i=config_1.default.api.token_encoding_algorithm,c=jsonwebtoken_1.default.sign(r,t,{algorithm:i,noTimestamp:!0}),a=jsonwebtoken_1.default.verify(c,t,{algorithms:[i]});return{accessToken:c,info:a,expMinutes:n}};exports.generateAccessToken=generateAccessToken;const getAccessToken=async e=>{let t;if((0,application_1.isProvisioningApp)(e))t=await getAccessTokenProvisioning(e);else{const n=e.scope?Array.isArray(e.scope)?e.scope.map(r=>r.trim()).join(","):e.scope:"",o={clientId:e.clientId,clientSecret:e.clientSecret,slug:e.slug,domain:e.domain,scope:n};e.email&&e.password?(o.username=e.email,o.password=e.password,t=await(0,js_auth_1.default)("password",o)):t=await(0,js_auth_1.default)("client_credentials",o)}if(t){if(t.error)throw new Error(`Unable to get access token: ${t.error}`)}else throw new Error("Unable to get access token");return t};exports.getAccessToken=getAccessToken;const getAccessTokenProvisioning=async e=>{const t={clientId:e.clientId,clientSecret:e.clientSecret,domain:e.domain};return js_auth_1.provisioning.authentication(t)},revokeAccessToken=async(e,t,n)=>{const o=Array.isArray(e.scope)?e.scope.join(";"):e.scope,r=JSON.stringify({grant_type:"client_credentials",client_id:e.clientId,client_secret:e.clientSecret,scope:o,token:t}),i=(0,application_1.isProvisioningApp)(e),c=i?"application/vnd.api+json":"application/json",a=i?"auth":e.slug||"",p={hostname:(0,api_1.baseURL)(a,e.domain).replace("https://","").replace("http://",""),port:443,path:"/oauth/revoke",method:"POST",headers:{"Content-Type":c,"Content-Length":r.length}};n&&n.log(p),n&&n.log(r);let l=!1;try{const s=https_1.default.request(p);s.on("error",d=>{throw l=!0,new errors_1.CLIError(d.message||"Error revoking access token")}),s.write(r),s.end()}catch(s){throw l=!0,n&&n.log(s.message),s instanceof errors_1.CLIError?s:new errors_1.CLIError(s.message||"Error revoking access token")}await(0,util_1.sleep)(300),!l&&n&&n.log("Access token revoked")};exports.revokeAccessToken=revokeAccessToken;const isAccessTokenExpiring=e=>{const n=Math.floor(Date.now()/1e3),o=Math.floor(new Date(e.expires).getTime()/1e3);return n>=o-30};exports.isAccessTokenExpiring=isAccessTokenExpiring;const getTokenEnvironment=e=>(typeof e=="string"?decodeAccessToken(e):e).test?"test":"live";exports.getTokenEnvironment=getTokenEnvironment;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getTokenEnvironment = exports.isAccessTokenExpiring = exports.revokeAccessToken = exports.getAccessToken = exports.generateAccessToken = exports.decodeAccessToken = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const config_1 = __importDefault(require("./config"));
9
+ const https_1 = __importDefault(require("https"));
10
+ const application_1 = require("./application");
11
+ const util_1 = require("./util");
12
+ const api_1 = require("./api");
13
+ const errors_1 = require("@oclif/core/lib/errors");
14
+ const js_auth_1 = require("@commercelayer/js-auth");
15
+ /** Decode a Commerce Layer access token */
16
+ const decodeAccessToken = (accessToken) => {
17
+ const info = jsonwebtoken_1.default.decode(accessToken);
18
+ if (info === null)
19
+ throw new Error('Error decoding access token');
20
+ return info;
21
+ };
22
+ exports.decodeAccessToken = decodeAccessToken;
23
+ /** generate a custom access token */
24
+ const generateAccessToken = (token, sharedSecret, minutes) => {
25
+ const tokenData = token;
26
+ const payload = Object.assign(Object.assign({}, tokenData), { exp: Math.floor(Date.now() / 1000) + (minutes * 60), rand: Math.random() });
27
+ const algo = config_1.default.api.token_encoding_algorithm;
28
+ const accessToken = jsonwebtoken_1.default.sign(payload, sharedSecret, { algorithm: algo, noTimestamp: true });
29
+ const info = jsonwebtoken_1.default.verify(accessToken, sharedSecret, { algorithms: [algo] });
30
+ return {
31
+ accessToken,
32
+ info: info,
33
+ expMinutes: minutes,
34
+ };
35
+ };
36
+ exports.generateAccessToken = generateAccessToken;
37
+ const getAccessToken = async (auth) => {
38
+ let accessToken;
39
+ if ((0, application_1.isProvisioningApp)(auth))
40
+ accessToken = await getAccessTokenProvisioning(auth);
41
+ else {
42
+ const scope = auth.scope ? (Array.isArray(auth.scope) ? auth.scope.map(s => s.trim()).join(',') : auth.scope) : '';
43
+ const credentials = {
44
+ clientId: auth.clientId,
45
+ clientSecret: auth.clientSecret,
46
+ slug: auth.slug,
47
+ domain: auth.domain,
48
+ scope
49
+ };
50
+ if (auth.email && auth.password) {
51
+ credentials.username = auth.email;
52
+ credentials.password = auth.password;
53
+ accessToken = await js_auth_1.core.authentication('password', credentials);
54
+ }
55
+ else
56
+ accessToken = await js_auth_1.core.authentication('client_credentials', credentials);
57
+ }
58
+ if (!accessToken)
59
+ throw new Error('Unable to get access token');
60
+ else if (accessToken.error)
61
+ throw new Error(`Unable to get access token: ${accessToken.error}`);
62
+ return accessToken;
63
+ };
64
+ exports.getAccessToken = getAccessToken;
65
+ const getAccessTokenProvisioning = async (auth) => {
66
+ const credentials = {
67
+ clientId: auth.clientId,
68
+ clientSecret: auth.clientSecret,
69
+ domain: auth.domain
70
+ };
71
+ return js_auth_1.provisioning.authentication(credentials);
72
+ };
73
+ const revokeAccessToken = async (app, token, logger) => {
74
+ /*
75
+ return axios
76
+ .post(`${app.baseUrl}/oauth/revoke`, {
77
+ grant_type: 'client_credentials',
78
+ client_id: app.clientId,
79
+ client_secret: app.clientSecret,
80
+ token,
81
+ })
82
+ */
83
+ const scope = Array.isArray(app.scope) ? app.scope.join(';') : app.scope;
84
+ const data = JSON.stringify({
85
+ grant_type: 'client_credentials',
86
+ client_id: app.clientId,
87
+ client_secret: app.clientSecret,
88
+ scope,
89
+ token,
90
+ });
91
+ const provisioning = (0, application_1.isProvisioningApp)(app);
92
+ const contentType = provisioning ? 'application/vnd.api+json' : 'application/json';
93
+ const slug = provisioning ? 'auth' : app.slug || '';
94
+ const options = {
95
+ hostname: (0, api_1.baseURL)(slug, app.domain).replace('https://', '').replace('http://', ''),
96
+ port: 443,
97
+ path: '/oauth/revoke',
98
+ method: 'POST',
99
+ headers: {
100
+ 'Content-Type': contentType,
101
+ 'Content-Length': data.length,
102
+ },
103
+ };
104
+ if (logger)
105
+ logger.log(options);
106
+ if (logger)
107
+ logger.log(data);
108
+ let err = false;
109
+ try {
110
+ const req = https_1.default.request(options /* , res => {
111
+ console.log(`statusCode: ${res.statusCode}`)
112
+
113
+ res.on('data', d => {
114
+ process.stdout.write(d)
115
+ })
116
+ } */);
117
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
118
+ req.on('error', error => {
119
+ err = true;
120
+ throw new errors_1.CLIError(error.message || 'Error revoking access token');
121
+ });
122
+ req.write(data);
123
+ req.end();
124
+ }
125
+ catch (error) {
126
+ err = true;
127
+ if (logger)
128
+ logger.log(error.message);
129
+ if (error instanceof errors_1.CLIError)
130
+ throw error;
131
+ else
132
+ throw new errors_1.CLIError(error.message || 'Error revoking access token');
133
+ }
134
+ await (0, util_1.sleep)(300);
135
+ if (!err && logger)
136
+ logger.log('Access token revoked');
137
+ };
138
+ exports.revokeAccessToken = revokeAccessToken;
139
+ /*
140
+ const revokeAccessTokenAxios = async (app: AppAuth, token: string, logger?: { log: (msg: any) => void }): Promise<void> => {
141
+
142
+ const scope = Array.isArray(app.scope) ? app.scope.join(';') : app.scope
143
+
144
+ const data = {
145
+ grant_type: 'client_credentials',
146
+ client_id: app.clientId,
147
+ client_secret: app.clientSecret,
148
+ scope,
149
+ token,
150
+ }
151
+ if (logger) logger.log(data)
152
+
153
+ try {
154
+ await axios.post(`${baseURL(app.slug, app.domain)}/oauth/revoke`, data, { headers: { 'Content-Type': 'application/json' }})
155
+ } catch (error) {
156
+ if (logger) logger.log((error as Error).message)
157
+ if (error instanceof CLIError) throw error
158
+ else throw new CLIError((error as Error).message || 'Error revoking access token')
159
+ }
160
+
161
+ await sleep(300)
162
+ if (logger) logger.log('Access token revoked')
163
+
164
+ }
165
+ */
166
+ const isAccessTokenExpiring = (tokenData) => {
167
+ const safetyInterval = 30; // secs
168
+ const now = Math.floor(Date.now() / 1000); // secs
169
+ const expiration = Math.floor(new Date(tokenData.expires).getTime() / 1000); // secs
170
+ return (now >= (expiration - safetyInterval));
171
+ };
172
+ exports.isAccessTokenExpiring = isAccessTokenExpiring;
173
+ const getTokenEnvironment = (token) => {
174
+ const decodedToken = ((typeof token === 'string') ? decodeAccessToken(token) : token);
175
+ return decodedToken.test ? 'test' : 'live';
176
+ };
177
+ exports.getTokenEnvironment = getTokenEnvironment;
package/lib/cjs/update.js CHANGED
@@ -1,4 +1,22 @@
1
- "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkUpdate=void 0;const chalk_1=__importDefault(require("chalk")),update_notifier_cjs_1=__importDefault(require("update-notifier-cjs")),UPDATE_CHECK_INTERVAL_HOURS=1,checkUpdate=e=>{const t=(0,update_notifier_cjs_1.default)({pkg:e,updateCheckInterval:36e5*UPDATE_CHECK_INTERVAL_HOURS});if(t.update){const a="commercelayer plugins:update";t.notify({isGlobal:!1,message:`-= ${chalk_1.default.bgWhite.black.bold(` ${e.description} `)} =-
2
-
3
- New version available: ${chalk_1.default.dim("{currentVersion}")} -> ${chalk_1.default.green("{latestVersion}")}
4
- Run ${chalk_1.default.cyanBright(a)} to update`})}};exports.checkUpdate=checkUpdate;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.checkUpdate = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ // import { resolve, join } from 'path'
9
+ const update_notifier_cjs_1 = __importDefault(require("update-notifier-cjs"));
10
+ const UPDATE_CHECK_INTERVAL_HOURS = 1;
11
+ const checkUpdate = (pkg) => {
12
+ const notifier = (0, update_notifier_cjs_1.default)({ pkg, updateCheckInterval: 1000 * 60 * 60 * UPDATE_CHECK_INTERVAL_HOURS });
13
+ if (notifier.update) {
14
+ const pluginMode = true; // resolve(__dirname).includes(join('@commercelayer', 'cli', 'node_modules', pkg.name))
15
+ const updateCommand = pluginMode ? 'commercelayer plugins:update' : '{updateCommand}';
16
+ notifier.notify({
17
+ isGlobal: !pluginMode,
18
+ message: `-= ${chalk_1.default.bgWhite.black.bold(` ${pkg.description} `)} =-\n\nNew version available: ${chalk_1.default.dim('{currentVersion}')} -> ${chalk_1.default.green('{latestVersion}')}\nRun ${chalk_1.default.cyanBright(updateCommand)} to update`,
19
+ });
20
+ }
21
+ };
22
+ exports.checkUpdate = checkUpdate;
package/lib/cjs/util.js CHANGED
@@ -1,2 +1,59 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.userAgent=exports.generateGroupUID=exports.specialFolder=exports.log=exports.resetConsole=exports.sleep=void 0;const util_1=require("util"),path_1=require("path"),os_1=require("os"),fs_1=require("fs"),sleep=async e=>new Promise(t=>setTimeout(t,e));exports.sleep=sleep;const resetConsole=()=>{const e="\x1B[?25h",t="\x1B[?7h";process.stdout.write(`${e}${t}`)};exports.resetConsole=resetConsole;const log=(e="",...t)=>{e=typeof e=="string"?e:(0,util_1.inspect)(e),process.stdout.write((0,util_1.format)(e,...t)+`
2
- `)};exports.log=log;const specialFolder=(e,t=!1)=>{const o=["desktop","home"],r=e.toLowerCase().split(/[\\\/]/g)[0];if(o.includes(r)){let n=(0,os_1.homedir)();r==="desktop"&&(n+=`${path_1.sep}Desktop`),e=e.replace(r,n)}const s=(0,path_1.dirname)(e);return t&&!(0,fs_1.existsSync)(s)&&(0,fs_1.mkdirSync)(s,{recursive:!0}),e};exports.specialFolder=specialFolder;const generateGroupUID=()=>{const e=Math.trunc(Math.random()*46656),t=Math.trunc(Math.random()*46656),o=("000"+e.toString(36)).slice(-3),r=("000"+t.toString(36)).slice(-3);return o+r};exports.generateGroupUID=generateGroupUID;const userAgent=e=>`${e.name.replace(/@commercelayer\/cli-plugin/,"CLI")}/${e.version}`;exports.userAgent=userAgent;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.userAgent = exports.generateGroupUID = exports.specialFolder = exports.log = exports.resetConsole = exports.sleep = void 0;
4
+ const util_1 = require("util");
5
+ const path_1 = require("path");
6
+ const os_1 = require("os");
7
+ const fs_1 = require("fs");
8
+ /** Await ms milliseconds */
9
+ const sleep = async (ms) => {
10
+ return new Promise(resolve => setTimeout(resolve, ms));
11
+ };
12
+ exports.sleep = sleep;
13
+ // Reset terminal style after use of colors and text styles
14
+ const resetConsole = () => {
15
+ // Cursor
16
+ // const showCursor = '\u001B[?25l' // \x1B[?25l
17
+ const showCursor = '\u001B[?25h'; // \x1B[?25h
18
+ // Line wrap
19
+ // const lineWrap = '\u001B[?7l' // \x1B[?7l
20
+ const lineWrap = '\u001B[?7h'; // \x1B[?7h
21
+ // eslint-disable-next-line no-console
22
+ // console.log(`${showCursor}${lineWrap}`)
23
+ process.stdout.write(`${showCursor}${lineWrap}`);
24
+ };
25
+ exports.resetConsole = resetConsole;
26
+ const log = (message = '', ...args) => {
27
+ message = (typeof message === 'string') ? message : (0, util_1.inspect)(message);
28
+ process.stdout.write((0, util_1.format)(message, ...args) + '\n');
29
+ };
30
+ exports.log = log;
31
+ const specialFolder = (filePath, createIfNotExists = false) => {
32
+ const specialFolders = ['desktop', 'home'];
33
+ // Special directory (home / desktop)
34
+ // eslint-disable-next-line no-useless-escape
35
+ const root = filePath.toLowerCase().split(/[\\\/]/g)[0];
36
+ if (specialFolders.includes(root)) {
37
+ let filePrefix = (0, os_1.homedir)();
38
+ if (root === 'desktop')
39
+ filePrefix += `${path_1.sep}Desktop`;
40
+ filePath = filePath.replace(root, filePrefix);
41
+ }
42
+ const fileDir = (0, path_1.dirname)(filePath);
43
+ if (createIfNotExists && !(0, fs_1.existsSync)(fileDir))
44
+ (0, fs_1.mkdirSync)(fileDir, { recursive: true });
45
+ return filePath;
46
+ };
47
+ exports.specialFolder = specialFolder;
48
+ const generateGroupUID = () => {
49
+ const firstPart = Math.trunc(Math.random() * 46656);
50
+ const secondPart = Math.trunc(Math.random() * 46656);
51
+ const firstPartStr = ('000' + firstPart.toString(36)).slice(-3);
52
+ const secondPartStr = ('000' + secondPart.toString(36)).slice(-3);
53
+ return firstPartStr + secondPartStr;
54
+ };
55
+ exports.generateGroupUID = generateGroupUID;
56
+ const userAgent = (config) => {
57
+ return `${config.name.replace(/@commercelayer\/cli-plugin/, 'CLI')}/${config.version}`;
58
+ };
59
+ exports.userAgent = userAgent;
package/lib/esm/api.js CHANGED
@@ -1 +1,93 @@
1
- import s from"./config";import{rawRequest as _,readDataFile as c,Operation as p}from"./raw";import{denormalize as d}from"./jsonapi";const q=(e,r,u)=>`https://${(u?"provisioning":e||"").toLowerCase()}.${r||s.api.default_domain}`,f=e=>{if(e)return e.substring(e.indexOf(".")+1)},g=e=>e===!0||e==="live"?"live":"test",b=e=>e.replace(/_/g," ");export{q as baseURL,f as extractDomain,g as execMode,b as humanizeResource};const y=["bundles","imports","markets","prices","price_lists","promotions","external_promotions","fixed_amount_promotions","fixed_price_promotions","free_gift_promotions","free_shipping_promotions","percentage_discount_promotions","skus","sku_options","stock_items","stock_locations"];export const isResourceCacheable=(e,r)=>y.includes(e||"")&&(r||"get").toLowerCase()==="get",liveEnvironment=e=>e==="live",requestRateLimitDelay=e=>{const r=e?.environment||"test",u=e?.parallelRequests||1,a=isResourceCacheable(e?.resourceType,e?.method);let i=a?s.api.requests_max_num_burst_cacheable:s.api.requests_max_num_burst,n=a?s.api.requests_max_num_avg_cacheable:s.api.requests_max_num_avg;r!=="live"&&(i=a?s.api.requests_max_num_burst_test_cacheable:s.api.requests_max_num_burst_test,n=a?s.api.requests_max_num_avg_test_cacheable:s.api.requests_max_num_avg_test);const v=s.api.requests_max_secs_burst/i,x=s.api.requests_max_secs_avg/n,l=u*v,m=u*x,o=e?.totalRequests;let t=0;return o?o>i&&(o>n?t=m:t=l):t=Math.max(l,m),t=t*1e3,e?.minimumDelay&&(t=Math.max(e.minimumDelay,t)),e?.securityDelay&&(t+=e.securityDelay),t=Math.ceil(t),t};export{_ as requestRaw,c as requestDataFile,p as Operation};export{d as responseDenormalize};export const request={raw:_,readDataFile:c,rateLimitDelay:requestRateLimitDelay},response={denormalize:d};
1
+ import config from './config';
2
+ import { rawRequest, readDataFile, Operation } from './raw';
3
+ import { denormalize } from './jsonapi';
4
+ /** Build base URL */
5
+ const baseURL = (slug, domain, provisioning) => {
6
+ const subdomain = provisioning ? 'provisioning' : (slug || '');
7
+ return `https://${subdomain.toLowerCase()}.${domain || config.api.default_domain}`;
8
+ };
9
+ /** Extract domain name from URL */
10
+ const extractDomain = (baseUrl) => {
11
+ if (!baseUrl)
12
+ return undefined;
13
+ return baseUrl.substring(baseUrl.indexOf('.') + 1);
14
+ };
15
+ /** Decode API execution mode */
16
+ const execMode = (liveFlag) => {
17
+ return ((liveFlag === true) || (liveFlag === 'live')) ? 'live' : 'test';
18
+ };
19
+ const humanizeResource = (type) => {
20
+ return type.replace(/_/g, ' ');
21
+ };
22
+ export { baseURL, extractDomain, execMode, humanizeResource };
23
+ const CACHEABLE_RESOURCES = [
24
+ 'bundles',
25
+ 'imports',
26
+ 'markets',
27
+ 'prices',
28
+ 'price_lists',
29
+ 'promotions',
30
+ 'external_promotions',
31
+ 'fixed_amount_promotions',
32
+ 'fixed_price_promotions',
33
+ 'free_gift_promotions',
34
+ 'free_shipping_promotions',
35
+ 'percentage_discount_promotions',
36
+ 'skus',
37
+ 'sku_options',
38
+ 'stock_items',
39
+ 'stock_locations'
40
+ ];
41
+ export const isResourceCacheable = (resource, method) => {
42
+ return CACHEABLE_RESOURCES.includes(resource || '') && ((method || 'get').toLowerCase() === 'get');
43
+ };
44
+ export const liveEnvironment = (env) => {
45
+ return (env === 'live');
46
+ };
47
+ export const requestRateLimitDelay = (options) => {
48
+ const env = (options === null || options === void 0 ? void 0 : options.environment) || 'test';
49
+ const parallelRequests = (options === null || options === void 0 ? void 0 : options.parallelRequests) || 1;
50
+ const resourceCacheable = isResourceCacheable(options === null || options === void 0 ? void 0 : options.resourceType, options === null || options === void 0 ? void 0 : options.method);
51
+ let requestsMaxNumBurst = resourceCacheable ? config.api.requests_max_num_burst_cacheable : config.api.requests_max_num_burst;
52
+ let requestsMaxNumAvg = resourceCacheable ? config.api.requests_max_num_avg_cacheable : config.api.requests_max_num_avg;
53
+ // Test env allows half number of requests than live
54
+ if (env !== 'live') {
55
+ requestsMaxNumBurst = resourceCacheable ? config.api.requests_max_num_burst_test_cacheable : config.api.requests_max_num_burst_test;
56
+ requestsMaxNumAvg = resourceCacheable ? config.api.requests_max_num_avg_test_cacheable : config.api.requests_max_num_avg_test;
57
+ }
58
+ const unitDelayBurst = config.api.requests_max_secs_burst / requestsMaxNumBurst;
59
+ const unitDelayAvg = config.api.requests_max_secs_avg / requestsMaxNumAvg;
60
+ const delayBurst = parallelRequests * unitDelayBurst;
61
+ const delayAvg = parallelRequests * unitDelayAvg;
62
+ // If the total number of requests is known the delay can be optimized
63
+ const totalRequests = options === null || options === void 0 ? void 0 : options.totalRequests;
64
+ let delay = 0;
65
+ if (totalRequests) {
66
+ if (totalRequests > requestsMaxNumBurst) {
67
+ if (totalRequests > requestsMaxNumAvg)
68
+ delay = delayAvg;
69
+ else
70
+ delay = delayBurst;
71
+ }
72
+ }
73
+ else
74
+ delay = Math.max(delayBurst, delayAvg);
75
+ // Msec delay
76
+ delay = delay * 1000;
77
+ if (options === null || options === void 0 ? void 0 : options.minimumDelay)
78
+ delay = Math.max(options.minimumDelay, delay);
79
+ if (options === null || options === void 0 ? void 0 : options.securityDelay)
80
+ delay += options.securityDelay;
81
+ delay = Math.ceil(delay);
82
+ return delay;
83
+ };
84
+ export { rawRequest as requestRaw, readDataFile as requestDataFile, Operation };
85
+ export { denormalize as responseDenormalize };
86
+ export const request = {
87
+ raw: rawRequest,
88
+ readDataFile,
89
+ rateLimitDelay: requestRateLimitDelay
90
+ };
91
+ export const response = {
92
+ denormalize
93
+ };
@@ -1 +1,26 @@
1
- import t from"./config";const s=()=>Date.now().toString(36),c=n=>n.key!==void 0&&n.key!=="",u=(n,e)=>{const r=n!==void 0,o=e!==void 0;return!r&&!o||r&&o&&n.key===e.key},i=n=>n?Array.isArray(n)?n:[n]:[],a=n=>i(n.scope).includes(t.provisioning.scope)||n.api==="provisioning";export{s as appKey,c as appKeyValid,u as appKeyMatch,i as arrayScope,a as isProvisioningApp};
1
+ import config from "./config";
2
+ /** Build application key */
3
+ const appKey = () => {
4
+ return Date.now().toString(36);
5
+ };
6
+ /** Check application key */
7
+ const appKeyValid = (appKey) => {
8
+ return (appKey.key !== undefined) && (appKey.key !== '');
9
+ };
10
+ /** Check if two application keys are equal */
11
+ const appKeyMatch = (app1, app2) => {
12
+ const a1 = app1 !== undefined;
13
+ const a2 = app2 !== undefined;
14
+ return (!a1 && !a2) || (a1 && a2 && (app1.key === app2.key));
15
+ };
16
+ const arrayScope = (scope) => {
17
+ if (!scope)
18
+ return [];
19
+ else
20
+ return Array.isArray(scope) ? scope : [scope];
21
+ };
22
+ const isProvisioningApp = (app) => {
23
+ const scope = arrayScope(app.scope);
24
+ return scope.includes(config.provisioning.scope) || (app.api === 'provisioning');
25
+ };
26
+ export { appKey, appKeyValid, appKeyMatch, arrayScope, isProvisioningApp };