Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ fileignoreconfig:
checksum: a467e56edcb43858512c47bd82c76dbf8799d57837f03c247e2cebe27ca5eaa8
- filename: src/core/lib/utils.js
checksum: 7ae53c3be5cdcd1468d66577c9450adc53e9c6aaeaeabc4275e87a47aa709850
- filename: src/core/messages.js
checksum: 7da74b6911bb30b238459646c84e5094393a0067757de45c2361c49dcfa28719
version: ""
4 changes: 3 additions & 1 deletion src/core/cache-provider/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import localstorage from './localstorage';
import MESSAGES from '../messages';

const CacheProvider = {};

CacheProvider.providers = function (provider) {
if (provider) {
return localstorage;
} else {
console.error('Kindly provide valid provider.');
console.error(MESSAGES.CACHE_PROVIDER_MISSING);
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/core/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Request from './request';
import Result from '../modules/result';
import MESSAGES from '../messages';

/**
* @method addSpread
Expand Down Expand Up @@ -28,7 +29,7 @@
this._query[type].BASE = query;
return this;
} else {
console.error('Kindly provide valid parameters');
console.error(MESSAGES.TRANSFORM_INVALID_SINGLE_PARAM);
}
break;
case 2:
Expand All @@ -38,11 +39,11 @@
this._query[type][arguments[0]] = query;
return this;
} else {
console.error('Kindly provide valid parameters');
console.error(MESSAGES.TRANSFORM_INVALID_DOUBLE_PARAM);
}
break;
default:
console.error('Kindly provide valid parameters');
console.error(MESSAGES.TRANSFORM_INVALID_PARAM_COUNT);
}
};
}
Expand All @@ -56,7 +57,7 @@
__typeof = 'array';
}
} else {
_typeof = __typeof;

Check warning on line 60 in src/core/lib/utils.js

View workflow job for this annotation

GitHub Actions / lint

'_typeof' is assigned a value but never used
}
return __typeof;
}
Expand Down
34 changes: 34 additions & 0 deletions src/core/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Centralized error and informational messages for Contentstack JavaScript SDK
* @module messages
*/

const MESSAGES = {
// Cache Provider Messages
CACHE_PROVIDER_MISSING: 'Missing cache provider. Provide a valid provider and try again.',
CACHE_POLICY_INVALID: 'Invalid cache policy. Provide a valid policy value and try again.',

// Stack Initialization Messages
STACK_INVALID_PARAMS_OBJECT: 'Invalid parameters. The specified API Key, Delivery Token, or Environment Name is invalid.',
STACK_INVALID_PARAMS_STRING: 'Invalid string parameters. Provide valid API Key, Delivery Token, and Environment Name.',
STACK_INVALID_PARAMS_GENERIC: 'Invalid parameters. Provide valid parameters to initialize the Contentstack javascript-SDK Stack.',
STACK_OBSOLETE_FUNCTION: "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!",

// Entry Messages
ENTRY_UID_REQUIRED: "Entry UID required. Provide an entry UID. e.g. .Entry('entry_uid')",
ENTRY_INCLUDE_OWNER_DEPRECATED: 'The includeOwner function is deprecated. This functionality is no longer supported. Please remove this method from your code.',
ENTRY_INVALID_ARGUMENT: 'Invalid argument. Argument should be a String or an Array.',
ENTRY_LANGUAGE_INVALID: 'Invalid language code. Argument should be a String.',
ENTRY_ADD_QUERY_INVALID: 'Invalid query parameters. First argument should be a String.',
ENTRY_ADD_PARAM_INVALID: 'Invalid parameters. Both key and value should be strings.',

// Query/Transform Messages (only/except)
TRANSFORM_INVALID_SINGLE_PARAM: 'Invalid parameters. Expected a string or an array of field names.',
TRANSFORM_INVALID_DOUBLE_PARAM: 'Invalid parameters. Expected first parameter as a string (reference field UID) and second parameter as a string or an array of field names.',
TRANSFORM_INVALID_PARAM_COUNT: 'Invalid parameters. Provide either one parameter (field name or array) or two parameters (reference field UID and field name or array).',

// Request/Error Messages
REQUEST_ERROR_OCCURRED: (error) => `An error occurred: ${error}`
};

export default MESSAGES;
15 changes: 8 additions & 7 deletions src/core/modules/entry.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Utils from '../lib/utils';
import MESSAGES from '../messages';

/**
* @class
Expand Down Expand Up @@ -83,7 +84,7 @@ export default class Entry {
this.queryCachePolicy = policy;
}
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide the valid policy');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.CACHE_POLICY_INVALID);
}
return this;
}
Expand Down Expand Up @@ -135,7 +136,7 @@ export default class Entry {
}
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Argument should be a String or an Array.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_INVALID_ARGUMENT);
}
}

Expand All @@ -160,7 +161,7 @@ export default class Entry {
this._query.locale = language_code;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Argument should be a String.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_LANGUAGE_INVALID);
}
}

Expand All @@ -179,7 +180,7 @@ export default class Entry {
this._query[key] = value;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'First argument should be a String.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_ADD_QUERY_INVALID);
}
}

Expand Down Expand Up @@ -294,7 +295,7 @@ export default class Entry {
* @instance
*/
includeOwner () {
console.warn('The includeOwner function is deprecated.');
console.warn(MESSAGES.ENTRY_INCLUDE_OWNER_DEPRECATED);
this._query.include_owner = true;
return this;
}
Expand Down Expand Up @@ -338,7 +339,7 @@ export default class Entry {
this._query[key] = value;
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid parameters.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_ADD_PARAM_INVALID);
}
}

Expand Down Expand Up @@ -391,7 +392,7 @@ export default class Entry {
const options = Utils.mergeDeep(this.fetchOptions, fetchOptions);
return Utils.sendRequest(Utils.mergeDeep({}, this), options);
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', "Kindly provide an entry uid. e.g. .Entry('asset_uid')");
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.ENTRY_UID_REQUIRED);
}
}
}
27 changes: 14 additions & 13 deletions src/core/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Query from './modules/query';
import Taxonomy from './modules/taxonomy';
import Request from './lib/request';
import CacheProvider from './cache-provider/index';
import MESSAGES from './messages';
const errorRetry = [408, 429];

/**
Expand Down Expand Up @@ -57,11 +58,11 @@ export default class Stack {
debug: false,
logHandler: (level, data) => {
if (level === 'error' && data) {
console.error(`[error] ${data}`);
console.error(`Error: ${data}`);
} else if (level === 'warning' && data) {
console.warn(`[warning] ${data}`);
console.warn(`Warning: ${data}`);
} else if (level === 'info' && data) {
console.info(`[info] ${data}`);
console.info(`Info: ${data}`);
}
}
};
Expand Down Expand Up @@ -118,10 +119,10 @@ export default class Stack {
this.environment = stack_arguments[0].environment;
return this;
} else {
if (this.fetchOptions.debug) 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', MESSAGES.STACK_INVALID_PARAMS_OBJECT);
}
case 3:
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 (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION);
if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') {
this.headers = {
api_key: stack_arguments[0],
Expand All @@ -130,18 +131,18 @@ export default class Stack {
this.environment = stack_arguments[2];
return this;
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING);
}
case 4:
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 (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION);
if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') {
this.headers = {
api_key: stack_arguments[0],
access_token: stack_arguments[1]
};
this.environment = stack_arguments[2];
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING);
}
if (stack_arguments[3]) {
if (typeof stack_arguments[3] === 'string' && stack_arguments[3] !== undefined && stack_arguments[3] !== 'us') {
Expand All @@ -152,15 +153,15 @@ export default class Stack {
}
return this;
case 5:
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 (this.fetchOptions.debug) this.fetchOptions.logHandler('warning', MESSAGES.STACK_OBSOLETE_FUNCTION);
if (typeof stack_arguments[0] === 'string' && typeof stack_arguments[1] === 'string' && typeof stack_arguments[2] === 'string') {
this.headers = {
api_key: stack_arguments[0],
access_token: stack_arguments[1]
};
this.environment = stack_arguments[2];
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid string parameters.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_STRING);
}

if (stack_arguments[3]) {
Expand All @@ -175,7 +176,7 @@ export default class Stack {
}
return this;
default:
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide valid parameters to initialize the Contentstack javascript-SDK Stack.');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.STACK_INVALID_PARAMS_GENERIC);
}
}

Expand Down Expand Up @@ -240,7 +241,7 @@ export default class Stack {
this.queryCachePolicy = policy;
}
} else {
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', 'Kindly provide the valid policy');
if (this.fetchOptions.debug) this.fetchOptions.logHandler('error', MESSAGES.CACHE_POLICY_INVALID);
}
return this;
}
Expand Down Expand Up @@ -426,7 +427,7 @@ export default class Stack {
* .then(function(result) {
* // 'result' is a single contentType information.
* }).catch((error) => {
* console.log(error)
* console.error(MESSAGES.REQUEST_ERROR_OCCURRED(error.message || error))
* });
* @returns {promise}
* @instance
Expand Down
Loading