diff --git a/package-lock.json b/package-lock.json index 563c29c..6f8cf42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.5.0", + "version": "1.5.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.5.0", + "version": "1.5.1", "license": "MIT", "dependencies": { "json-mask": "2.0.0", diff --git a/package.json b/package.json index 2feb157..982bfcd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/datasync-filesystem-sdk", - "version": "1.5.0", + "version": "1.5.1", "description": "JavaScript filesystem SDK to query data synced via @contentstack/datasync-content-store-filesystem", "main": "dist/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index 1e4c8c4..437a3cb 100755 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { merge } from 'lodash' import { defaultConfig } from './config' import { Stack } from './stack' +export { ERROR_MESSAGES, WARNING_MESSAGES } from './messages' interface IUserConfig { contentStore?: { diff --git a/src/messages.ts b/src/messages.ts new file mode 100644 index 0000000..b5d64f6 --- /dev/null +++ b/src/messages.ts @@ -0,0 +1,149 @@ +/*! + * Contentstack DataSync Filesystem SDK. + * Centralized error and warning messages + * Copyright (c) Contentstack LLC + * MIT Licensed + */ + +/** + * Centralized error and warning messages for the SDK + * This file contains all user-facing messages to ensure consistency and ease of maintenance + */ +export const ERROR_MESSAGES = { + /** + * Error when a key does not exist on a data object + * @param key - The key that was not found + * @param data - The data object that was searched + */ + KEY_NOT_FOUND: (key: string, data: any) => + `The key '${key}' does not exist on: ${JSON.stringify(data)}`, + + /** + * Error for invalid parameters in comparison operators + */ + INVALID_COMPARISON_PARAMS: (type: string) => + `Kindly provide valid parameters for ${type}`, + + /** + * Error for invalid parameters in contained operators + */ + INVALID_CONTAINED_PARAMS: (bool: boolean) => + `Kindly provide valid parameters for ${bool}`, + + /** + * Error for invalid parameters in exists operators + */ + INVALID_EXISTS_PARAMS: (bool: boolean) => + `Kindly provide valid parameters for ${bool}`, + + /** + * Error for invalid parameters in sort methods + */ + INVALID_SORT_PARAMS: (type: string) => + `Kindly provide valid parameters for sort-${type}`, + + /** + * Error when argument should be a number + */ + ARGUMENT_SHOULD_BE_NUMBER: () => + 'Argument should be a number.', + + /** + * Error when content type uid is not provided + */ + CONTENT_TYPE_UID_REQUIRED: () => + 'Kindly provide a uid for .contentType()', + + /** + * Error when contentType() is not called before entries() + */ + CONTENT_TYPE_REQUIRED_BEFORE_ENTRIES: () => + 'Please call .contentType() before calling .entries()!', + + /** + * Error when content type is not found at path + */ + CONTENT_TYPE_NOT_FOUND: (contentTypeUid: string, filePath: string) => + `Queried content type ${contentTypeUid} was not found at ${filePath}!`, + + /** + * Error for invalid parameters in equalTo() + */ + INVALID_EQUAL_TO_PARAMS: () => + 'Kindly provide valid parameters for .equalTo()!', + + /** + * Error for invalid parameters in where() + */ + INVALID_WHERE_PARAMS: () => + 'Kindly provide a valid field and expr/fn value for \'.where()\'', + + /** + * Error for invalid parameters in query() + */ + INVALID_QUERY_PARAMS: () => + 'Kindly provide valid parameters for \'.query()\'', + + /** + * Error for invalid parameters in tags() + */ + INVALID_TAGS_PARAMS: () => + 'Kindly provide valid parameters for \'.tags()\'', + + /** + * Error when language code is invalid + */ + INVALID_LANGUAGE_CODE: (languageCode: any) => + `${languageCode} should be of type string and non-empty!`, + + /** + * Error for invalid parameters in include() + */ + INVALID_INCLUDE_PARAMS: () => + 'Kindly pass \'string\' OR \'array\' fields for .include()!', + + /** + * Error for invalid parameters in regex() + */ + INVALID_REGEX_PARAMS: () => + 'Kindly provide valid parameters for .regex()!', + + /** + * Error for invalid parameters in only() + */ + INVALID_ONLY_PARAMS: () => + 'Kindly provide valid parameters for .only()!', + + /** + * Error for invalid parameters in except() + */ + INVALID_EXCEPT_PARAMS: () => + 'Kindly provide valid parameters for .except()!', + + /** + * Error for invalid parameters in queryReferences() + */ + INVALID_QUERY_REFERENCES_PARAMS: () => + 'Kindly valid parameters for \'.queryReferences()\'!', + + /** + * Error for invalid parameters in referenceDepth() + */ + INVALID_REFERENCE_DEPTH_PARAMS: () => + 'Kindly valid parameters for \'.referenceDepth()\'!', +} + +export const WARNING_MESSAGES = { + /** + * Warning for slow .includeReferences() query + */ + INCLUDE_REFERENCES_SLOW: () => + '.includeReferences(...)', + + /** + * Warning when increasing reference depth beyond default + */ + REFERENCE_DEPTH_PERFORMANCE: (referenceDepth: number) => + `Increasing reference depth beyond ${referenceDepth} may impact performance.`, +} + diff --git a/src/stack.ts b/src/stack.ts index 2abef91..4d2c48e 100755 --- a/src/stack.ts +++ b/src/stack.ts @@ -26,6 +26,7 @@ import { getEntriesPath, segregateQueries, } from './utils' +import { WARNING_MESSAGES } from './messages' interface IShelf { path: string, @@ -847,7 +848,7 @@ export class Stack { * @returns {this} - Returns `stack's` instance */ public includeReferences(depth?: number) { - console.warn('.includeReferences() is a relatively slow query..!') + console.warn(WARNING_MESSAGES.INCLUDE_REFERENCES_SLOW()) this.q.includeAllReferences = true if (typeof depth === 'number') { this.q.referenceDepth = depth @@ -1069,7 +1070,7 @@ export class Stack { this.q.referenceDepth = depth if (depth > this.contentStore.referenceDepth) { - console.warn(`Increasing reference depth above ${this.contentStore.referenceDepth} may degrade performance!`) + console.warn(WARNING_MESSAGES.REFERENCE_DEPTH_PERFORMANCE(this.contentStore.referenceDepth)) } return this diff --git a/src/utils.ts b/src/utils.ts index a9160a1..41a9d69 100755 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,7 +19,8 @@ import { existsSync } from './fs' import { getConfig, } from './index' -import { sync } from 'mkdirp'; +import { sync } from 'mkdirp' +import { ERROR_MESSAGES } from './messages'; const localePaths = Object.create(null) export const difference = (obj, baseObj) => { @@ -52,7 +53,7 @@ const buildPath = (pattern, data) => { if (data[k]) { pathKeys.push(data[k]) } else { - throw new TypeError(`The key ${k} did not exist on ${JSON.stringify(data)}`) + throw new TypeError(ERROR_MESSAGES.KEY_NOT_FOUND(k, data)) } } else { pathKeys.push(patternKeys[i]) diff --git a/typings/index.d.ts b/typings/index.d.ts index 530f237..60776be 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -5,6 +5,7 @@ * MIT Licensed */ import { Stack } from './stack'; +export { ERROR_MESSAGES, WARNING_MESSAGES } from './messages'; interface IUserConfig { contentStore?: { baseDir?: string; @@ -53,4 +54,3 @@ export declare class Contentstack { */ static Stack(stackArguments: any): Stack; } -export {}; diff --git a/typings/messages.d.ts b/typings/messages.d.ts new file mode 100644 index 0000000..224b986 --- /dev/null +++ b/typings/messages.d.ts @@ -0,0 +1,104 @@ +/*! + * Contentstack DataSync Filesystem SDK. + * Centralized error and warning messages + * Copyright (c) Contentstack LLC + * MIT Licensed + */ +/** + * Centralized error and warning messages for the SDK + * This file contains all user-facing messages to ensure consistency and ease of maintenance + */ +export declare const ERROR_MESSAGES: { + /** + * Error when a key does not exist on a data object + * @param key - The key that was not found + * @param data - The data object that was searched + */ + KEY_NOT_FOUND: (key: string, data: any) => string; + /** + * Error for invalid parameters in comparison operators + */ + INVALID_COMPARISON_PARAMS: (type: string) => string; + /** + * Error for invalid parameters in contained operators + */ + INVALID_CONTAINED_PARAMS: (bool: boolean) => string; + /** + * Error for invalid parameters in exists operators + */ + INVALID_EXISTS_PARAMS: (bool: boolean) => string; + /** + * Error for invalid parameters in sort methods + */ + INVALID_SORT_PARAMS: (type: string) => string; + /** + * Error when argument should be a number + */ + ARGUMENT_SHOULD_BE_NUMBER: () => string; + /** + * Error when content type uid is not provided + */ + CONTENT_TYPE_UID_REQUIRED: () => string; + /** + * Error when contentType() is not called before entries() + */ + CONTENT_TYPE_REQUIRED_BEFORE_ENTRIES: () => string; + /** + * Error when content type is not found at path + */ + CONTENT_TYPE_NOT_FOUND: (contentTypeUid: string, filePath: string) => string; + /** + * Error for invalid parameters in equalTo() + */ + INVALID_EQUAL_TO_PARAMS: () => string; + /** + * Error for invalid parameters in where() + */ + INVALID_WHERE_PARAMS: () => string; + /** + * Error for invalid parameters in query() + */ + INVALID_QUERY_PARAMS: () => string; + /** + * Error for invalid parameters in tags() + */ + INVALID_TAGS_PARAMS: () => string; + /** + * Error when language code is invalid + */ + INVALID_LANGUAGE_CODE: (languageCode: any) => string; + /** + * Error for invalid parameters in include() + */ + INVALID_INCLUDE_PARAMS: () => string; + /** + * Error for invalid parameters in regex() + */ + INVALID_REGEX_PARAMS: () => string; + /** + * Error for invalid parameters in only() + */ + INVALID_ONLY_PARAMS: () => string; + /** + * Error for invalid parameters in except() + */ + INVALID_EXCEPT_PARAMS: () => string; + /** + * Error for invalid parameters in queryReferences() + */ + INVALID_QUERY_REFERENCES_PARAMS: () => string; + /** + * Error for invalid parameters in referenceDepth() + */ + INVALID_REFERENCE_DEPTH_PARAMS: () => string; +}; +export declare const WARNING_MESSAGES: { + /** + * Warning for slow .includeReferences() query + */ + INCLUDE_REFERENCES_SLOW: () => string; + /** + * Warning when increasing reference depth beyond default + */ + REFERENCE_DEPTH_PERFORMANCE: (referenceDepth: number) => string; +};