diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index a7544553..4f827b78 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -26,7 +26,7 @@ jobs: - uses: actions/setup-node@v3 with: node-version: '12.x' - registry-url: 'https://registry.npmjs.org' + registry-url: 'https://npm.pkg.github.com' scope: '@contentstack' - run: npm ci - run: npm publish diff --git a/.talismanrc b/.talismanrc index 045ea59c..d8a90027 100644 --- a/.talismanrc +++ b/.talismanrc @@ -1,4 +1,4 @@ threshold: medium fileignoreconfig: - filename: package-lock.json - checksum: be538ba2f4b106e5de0a55a3137271f659901e287ec09e8ae93351543242712e \ No newline at end of file + checksum: 1e966081577191b52e4ac7fa113c9563d5e120043a3e399dec06e3943c4d81bc \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 7e14be97..0f95d3b5 100644 --- a/index.d.ts +++ b/index.d.ts @@ -52,7 +52,7 @@ export interface Config { region?: Region; branch?: string; live_preview?: LivePreview; - fetchOptions?: object; + fetchOptions?: FetchOptions; } // Stack Config export interface StackConfig { @@ -79,13 +79,28 @@ export interface LivePreviewQuery { content_type_uid: string } +export interface RetryDelayOption { + base?: number + customBackoff?: (retryCount: number, error: Error) => number +} +export interface FetchOptions { + [propName: string]: any + debug?: boolean + timeout?: number + retryLimit?: number + retryDelay?: number + retryCondition?: (error: any) => boolean + logHandler?: (level: string, data: any) => void + retryDelayOptions?: RetryDelayOption +} + // Stack export class Stack { constructor(config: Config); /** * @deprecated since version 3.15.0 */ - constructor(api_key: string, delivery_token: string, environment_name: string, region?: Region, fetchOptions?: any, live_preview?: LivePreview); + constructor(api_key: string, delivery_token: string, environment_name: string, region?: Region, fetchOptions?: FetchOptions, live_preview?: LivePreview); environment: string; cachePolicy: CachePolicy; @@ -116,7 +131,7 @@ export function Stack(config: Config): Stack; /** * @deprecated since version 3.15.0 */ -export function Stack(api_key: string, access_token: string, environment_name: string, region?: string, fetchOptions?: object): Stack; +export function Stack(api_key: string, access_token: string, environment_name: string, region?: string, fetchOptions?: FetchOptions): Stack; export class ContentType { constructor(); diff --git a/package-lock.json b/package-lock.json index 1248b540..bc521951 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "contentstack", - "version": "3.15.4", + "version": "3.15.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3228,7 +3228,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "dev": true }, "console-browserify": { @@ -6622,6 +6622,15 @@ "strip-json-comments": "1.0.x" }, "dependencies": { + "minimatch": { + "version": "3.0.8", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.8.tgz", + "integrity": "sha512-6FsRAQsxQ61mw+qP1ZzbL9Bc78x2p5OqNgNpnoAFLTrX8n5Kxph0CsnhmKKNXTWjXqU5L0pGPR7hYk+XWZr60Q==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, "strip-json-comments": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-1.0.4.tgz", @@ -6840,9 +6849,9 @@ "dev": true }, "loader-utils": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-1.4.0.tgz", - "integrity": "sha512-qH0WSMBtn/oHuwjy/NucEgbx5dbxxnxup9s4PVXJUDHZBQY+s0NWA9rJf53RBnQZxfch7euUui7hpoAPvALZdA==", + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-1.4.2.tgz", + "integrity": "sha512-I5d00Pd/jwMD2QCduo657+YM/6L3KZu++pmX9VFncxaxvHcru9jx1lBaFft+r4Mt2jK0Yhp41XlRAihzPxHNCg==", "dev": true, "requires": { "big.js": "^5.2.2", @@ -7042,9 +7051,9 @@ "dev": true }, "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", "dev": true, "requires": { "brace-expansion": "^1.1.7" diff --git a/package.json b/package.json index 1b879d35..de73aa66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "contentstack", - "version": "3.15.4", + "version": "3.15.5", "description": "Contentstack Javascript SDK", "homepage": "https://www.contentstack.com/", "author": { @@ -11,7 +11,7 @@ "browser": "dist/web/contentstack.js", "react-native": "dist/react-native/contentstack.js", "types": "./index.d.ts", - "_id": "contentstack@3.15.4", + "_id": "contentstack@3.15.5", "scripts": { "test": "npm run test:e2e && npm run test:typescript", "test:e2e": "tape test/index.js | tap-html --out ./tap-html.html", @@ -55,11 +55,11 @@ ], "dist": { "shasum": "a328ed07240476a26b31a23261355dc929e1da63", - "tarball": "https://registry.npmjs.org/contentstack/-/contentstack-3.15.4.tgz" + "tarball": "https://registry.npmjs.org/contentstack/-/contentstack-3.15.5.tgz" }, "license": "MIT", "directories": {}, - "_resolved": "https://registry.npmjs.org/contentstack/-/contentstack-3.15.4.tgz", + "_resolved": "https://registry.npmjs.org/contentstack/-/contentstack-3.15.5.tgz", "_npmOperationalInternal": { "host": "packages-18-east.internal.npmjs.com", "tmp": "tmp/contentstack-3.15.0.tgz_1477830884275_0.9869455888401717" diff --git a/src/core/lib/request.js b/src/core/lib/request.js index ec14ec3e..bf57ee87 100755 --- a/src/core/lib/request.js +++ b/src/core/lib/request.js @@ -68,7 +68,7 @@ function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 30 function onError (error) { if (retryLimit === 0) { - fetchOptions.logHandler('error', error); + if (fetchOptions.debug) fetchOptions.logHandler('error', error); reject(error); }else { var msDelay = retryDelay @@ -90,10 +90,10 @@ function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 30 }) } } - fetchOptions.logHandler('info', { url: url, option: option}); + if (fetchOptions.debug) fetchOptions.logHandler('info', { url: url, option: option}); fetch(url, option) .then(function(response) { - fetchOptions.logHandler('info', response); + if (fetchOptions.debug) fetchOptions.logHandler('info', response); let data = response.json(); if (response.ok && response.status === 200) { resolve(data); @@ -102,16 +102,16 @@ function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 30 if (fetchOptions.retryCondition && fetchOptions.retryCondition(response)) { onError(json) } else { - fetchOptions.logHandler('error', json); + if (fetchOptions.debug) fetchOptions.logHandler('error', json); reject(json) } }).catch(() => { - fetchOptions.logHandler('error', {status: response.status, statusText: response.statusText}); + if (fetchOptions.debug) fetchOptions.logHandler('error', {status: response.status, statusText: response.statusText}); reject({status: response.status, statusText: response.statusText}) }); } }).catch((error) => { - fetchOptions.logHandler('error', error); + if (fetchOptions.debug) fetchOptions.logHandler('error', error); reject(error) }); } \ No newline at end of file diff --git a/src/core/modules/assets.js b/src/core/modules/assets.js index 74c4887f..4834037c 100755 --- a/src/core/modules/assets.js +++ b/src/core/modules/assets.js @@ -75,7 +75,7 @@ export default class Assets { this._query[key] = value; return this; } else { - this.fetchOptions.logHandler('error',"Kindly provide a valid parameters."); + if(this.fetchOptions.debug) this.fetchOptions.logHandler('error',"Kindly provide a valid parameters."); } } @@ -119,7 +119,7 @@ export default class Assets { var options = Utils.mergeDeep(this.fetchOptions, fetchOptions); return Utils.sendRequest(Utils.mergeDeep({}, this), options); } else { - fetchOptions.logHandler('error', "Kindly provide an asset uid. e.g. .Assets('asset_uid')"); + if(fetchOptions.debug) fetchOptions.logHandler('error', "Kindly provide an asset uid. e.g. .Assets('asset_uid')"); } } } \ No newline at end of file diff --git a/src/core/modules/entry.js b/src/core/modules/entry.js index 2794623b..a93e91ad 100755 --- a/src/core/modules/entry.js +++ b/src/core/modules/entry.js @@ -83,7 +83,7 @@ export default class Entry { this.queryCachePolicy = policy; } } else { - this.fetchOptions.logHandler('error', "Kindly provide the valid policy"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide the valid policy"); } return this; } @@ -135,7 +135,7 @@ export default class Entry { } return this; } else { - this.fetchOptions.logHandler('error', "Argument should be a String or an Array."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a String or an Array."); } } @@ -160,7 +160,7 @@ export default class Entry { this._query['locale'] = language_code; return this; } else { - this.fetchOptions.logHandler('error', "Argument should be a String."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a String."); } } @@ -179,7 +179,7 @@ export default class Entry { this._query[key] = value; return this; } else { - this.fetchOptions.logHandler('error', "First argument should be a String."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "First argument should be a String."); } } @@ -323,7 +323,7 @@ export default class Entry { this._query[key] = value; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } } @@ -360,7 +360,7 @@ export default class Entry { var options = Utils.mergeDeep(this.fetchOptions, fetchOptions); return Utils.sendRequest(Utils.mergeDeep({}, this), options); } else { - this.fetchOptions.logHandler('error', "Kindly provide an entry uid. e.g. .Entry('asset_uid')"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide an entry uid. e.g. .Entry('asset_uid')"); } } } \ No newline at end of file diff --git a/src/core/modules/query.js b/src/core/modules/query.js index 5704ae7d..a64e308f 100755 --- a/src/core/modules/query.js +++ b/src/core/modules/query.js @@ -9,7 +9,7 @@ const _extend = { this._query['query'][key][type] = value; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } }; }, @@ -22,7 +22,7 @@ const _extend = { this._query['query'][key][type] = this._query['query'][key][type].concat(value); return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } }; }, @@ -33,7 +33,7 @@ const _extend = { this._query['query'][key]['$exists'] = bool; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } }; }, @@ -61,7 +61,7 @@ const _extend = { this._query[type] = key; return this; } else { - this.fetchOptions.logHandler('error', "Argument should be a string."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a string."); } }; }, @@ -71,7 +71,7 @@ const _extend = { this._query[type] = value; return this; } else { - this.fetchOptions.logHandler('error', "Argument should be a number."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Argument should be a number."); } } } @@ -395,7 +395,7 @@ export default class Query extends Entry { return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } } @@ -420,7 +420,7 @@ export default class Query extends Entry { this._query['query'][key] = value; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } } @@ -476,7 +476,7 @@ export default class Query extends Entry { this._query['query'] = Utils.mergeDeep(this._query['query'], query); return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters"); } } @@ -587,7 +587,7 @@ export default class Query extends Entry { this._query['tags'] = values; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters"); } } @@ -651,7 +651,7 @@ export default class Query extends Entry { this._query[key] = value; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } } @@ -691,7 +691,7 @@ export default class Query extends Entry { if (options) this._query['query'][key]['$options'] = options; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } } @@ -717,7 +717,7 @@ export default class Query extends Entry { this._query['typeahead'] = value; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters."); } } diff --git a/src/core/stack.js b/src/core/stack.js index 4abc6a3f..cf37fc27 100755 --- a/src/core/stack.js +++ b/src/core/stack.js @@ -19,6 +19,7 @@ let errorRetry = [408, 429] * @param param.branch - Name of the branch you want to fetch data from * @param param.live_preview - Live preview configuration. * @param param.fetchOptions - Custom setting for the request. + * @param param.fetchOptions.debug - This will enable debug log. Default is false * @param param.fetchOptions.timeout - Set timeout for the request. * @param param.fetchOptions.retryLimit - The number of retries before failure. Default is 5 * @param param.fetchOptions.retryDelay - The number of ms to use for operation retries. Default is 300ms @@ -51,6 +52,7 @@ export default class Stack { } return false }, + debug: false, logHandler: (level, data) => { if (level === 'error' && data) { console.error(`[error] ${data}`) @@ -93,10 +95,10 @@ export default class Stack { this.environment = stack_arguments[0].environment; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid object parameters. The specified API Key, Delivery Token, or Environment Name is invalid."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid object parameters. The specified API Key, Delivery Token, or Environment Name is invalid."); } case 3: - this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); if (typeof stack_arguments[0] === "string" && typeof stack_arguments[1] === "string" && typeof stack_arguments[2] === "string") { this.headers = { api_key: stack_arguments[0], @@ -105,10 +107,10 @@ export default class Stack { this.environment = stack_arguments[2]; return this; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid string parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid string parameters."); } case 4: - this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); if (typeof stack_arguments[0] === "string" && typeof stack_arguments[1] === "string" && typeof stack_arguments[2] === "string") { this.headers = { api_key: stack_arguments[0], @@ -116,7 +118,7 @@ export default class Stack { }; this.environment = stack_arguments[2]; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid string parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid string parameters."); } if (stack_arguments[3]) { if(typeof stack_arguments[3] === "string" && stack_arguments[3] !== undefined && stack_arguments[3] !== "us") { @@ -127,7 +129,7 @@ export default class Stack { } return this; case 5: - this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', "WARNING! Obsolete function called. Function 'Contentstack.Stack(api_key, delivery_token, environment)' has been deprecated, please use 'Contentstack.Stack({api_key, delivery_token, environment, region, branch, fetchOptions})' function instead!"); if (typeof stack_arguments[0] === "string" && typeof stack_arguments[1] === "string" && typeof stack_arguments[2] === "string") { this.headers = { api_key: stack_arguments[0], @@ -135,7 +137,7 @@ export default class Stack { }; this.environment = stack_arguments[2]; } else { - this.fetchOptions.logHandler('error', "Kindly provide valid string parameters."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid string parameters."); } if (stack_arguments[3]) { @@ -150,7 +152,7 @@ export default class Stack { } return this; default: - this.fetchOptions.logHandler('error', "Kindly provide valid parameters to initialize the Contentstack javascript-SDK Stack."); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide valid parameters to initialize the Contentstack javascript-SDK Stack."); } } @@ -216,7 +218,7 @@ export default class Stack { this.queryCachePolicy = policy; } } else { - this.fetchOptions.logHandler('error', "Kindly provide the valid policy"); + if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide the valid policy"); } return this; }