@egodigital/egoose
Advanced tools
Comparing version 1.8.1 to 1.9.0
# Change Log (@egodigital/egoose) | ||
## 1.9.0 | ||
* added `cloneObj()`, `importApiErrors()` and `importApiErrorsSync()` functions | ||
## 1.8.0 | ||
@@ -4,0 +8,0 @@ |
@@ -17,2 +17,3 @@ /** | ||
*/ | ||
/// <reference types="node" /> | ||
import { Response } from 'express'; | ||
@@ -33,2 +34,16 @@ /** | ||
/** | ||
* A possible value for 'importApiErrors()' and 'importApiErrorsSync()' functions. | ||
* | ||
* If STRING: The path to the JSON file to import. | ||
* If BUFFER: The binary content as UTF-8 JSON data. | ||
* If OBJECT or ARRAY: One or more items to import. | ||
*/ | ||
export declare type ImportApiErrorsArgument = string | Buffer | ApiErrorWithKey | ApiErrorWithKey[]; | ||
/** | ||
* An entry for an API error with a key. | ||
*/ | ||
export interface ApiErrorWithKey extends ApiError { | ||
key: string; | ||
} | ||
/** | ||
* An api result. | ||
@@ -57,2 +72,14 @@ */ | ||
/** | ||
* Imports the data for 'API_ERRORS' constant. | ||
* | ||
* @param {ImportApiErrorsArgument} errors The items to import. | ||
*/ | ||
export declare function importApiErrors(errors: ImportApiErrorsArgument): Promise<void>; | ||
/** | ||
* Imports the data for 'API_ERRORS' constant (sync). | ||
* | ||
* @param {ImportApiErrorsArgument} errors The items to import. | ||
*/ | ||
export declare function importApiErrorsSync(errors: ImportApiErrorsArgument): void; | ||
/** | ||
* Sends an API response. | ||
@@ -59,0 +86,0 @@ * |
@@ -18,4 +18,15 @@ "use strict"; | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const index_1 = require("../index"); | ||
const _ = require("lodash"); | ||
const fs_extra_1 = require("fs-extra"); | ||
const Path = require("path"); | ||
/** | ||
@@ -25,3 +36,58 @@ * Global list of API errors. | ||
exports.API_ERRORS = {}; | ||
function applyApiErrors(errors) { | ||
errors = index_1.asArray(errors); | ||
for (const ERR of errors) { | ||
const KEY = index_1.normalizeString(ERR.key); | ||
const CLONED_ERR = index_1.cloneObj(ERR); | ||
delete CLONED_ERR['key']; | ||
exports.API_ERRORS[KEY] = index_1.cloneObj(CLONED_ERR); | ||
} | ||
} | ||
/** | ||
* Imports the data for 'API_ERRORS' constant. | ||
* | ||
* @param {ImportApiErrorsArgument} errors The items to import. | ||
*/ | ||
function importApiErrors(errors) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let importedErrorList; | ||
if (_.isString(errors)) { | ||
if (!Path.isAbsolute(errors)) { | ||
errors = Path.join(process.cwd(), errors); | ||
} | ||
importedErrorList = JSON.parse((yield fs_extra_1.readFile(errors)).toString('utf8')); | ||
} | ||
else if (Buffer.isBuffer(errors)) { | ||
importedErrorList = JSON.parse(errors.toString('utf8')); | ||
} | ||
else { | ||
importedErrorList = errors; | ||
} | ||
applyApiErrors(importedErrorList); | ||
}); | ||
} | ||
exports.importApiErrors = importApiErrors; | ||
/** | ||
* Imports the data for 'API_ERRORS' constant (sync). | ||
* | ||
* @param {ImportApiErrorsArgument} errors The items to import. | ||
*/ | ||
function importApiErrorsSync(errors) { | ||
let importedErrorList; | ||
if (_.isString(errors)) { | ||
if (!Path.isAbsolute(errors)) { | ||
errors = Path.join(process.cwd(), errors); | ||
} | ||
importedErrorList = JSON.parse(fs_extra_1.readFileSync(errors).toString('utf8')); | ||
} | ||
else if (Buffer.isBuffer(errors)) { | ||
importedErrorList = JSON.parse(errors.toString('utf8')); | ||
} | ||
else { | ||
importedErrorList = errors; | ||
} | ||
applyApiErrors(importedErrorList); | ||
} | ||
exports.importApiErrorsSync = importApiErrorsSync; | ||
/** | ||
* Sends an API response. | ||
@@ -28,0 +94,0 @@ * |
@@ -36,2 +36,10 @@ /** | ||
/** | ||
* Clones an object / value. | ||
* | ||
* @param {T} obj The value to clone. | ||
* | ||
* @return {T} The cloned value. | ||
*/ | ||
export declare function cloneObj<T>(obj: T): T; | ||
/** | ||
* Compare to values for sorting. | ||
@@ -38,0 +46,0 @@ * |
@@ -45,2 +45,16 @@ "use strict"; | ||
/** | ||
* Clones an object / value. | ||
* | ||
* @param {T} obj The value to clone. | ||
* | ||
* @return {T} The cloned value. | ||
*/ | ||
function cloneObj(obj) { | ||
if (!obj) { | ||
return obj; | ||
} | ||
return JSON.parse(JSON.stringify(obj)); | ||
} | ||
exports.cloneObj = cloneObj; | ||
/** | ||
* Compare to values for sorting. | ||
@@ -47,0 +61,0 @@ * |
{ | ||
"name": "@egodigital/egoose", | ||
"version": "1.8.1", | ||
"version": "1.9.0", | ||
"description": "Helper classes and functions for Node.js 8 or later.", | ||
@@ -11,2 +11,4 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"build": "(rm -r ./lib || true) && (\"./node_modules/.bin/tsc\" && \"./node_modules/.bin/tsc\" -d)", | ||
"doc": "(rm -r ./doc || true) && (\"./node_modules/.bin/typedoc\" --out ./doc ./src)", | ||
"test": "./node_modules/.bin/mocha ./lib/test/**/*.js" | ||
@@ -46,2 +48,3 @@ }, | ||
"@types/express": "^4.16.0", | ||
"@types/fs-extra": "^5.0.4", | ||
"@types/is-stream": "^1.1.0", | ||
@@ -57,2 +60,3 @@ "@types/lodash": "^4.14.112", | ||
"express": "^4.16.3", | ||
"fs-extra": "^7.0.0", | ||
"header-case-normalizer": "^1.0.3", | ||
@@ -59,0 +63,0 @@ "is-stream": "^1.1.0", |
@@ -32,4 +32,3 @@ # egoose | ||
```bash | ||
rm -rf ./lib | ||
tsc && tsc -d | ||
npm run build | ||
``` | ||
@@ -44,3 +43,3 @@ | ||
```bash | ||
./node_modules/.bin/typedoc --out ./doc ./src/** | ||
npm run doc | ||
``` | ||
@@ -47,0 +46,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119611
38
3044
23
56
7
+ Added@types/fs-extra@^5.0.4
+ Addedfs-extra@^7.0.0
+ Added@types/fs-extra@5.1.0(transitive)
+ Addedfs-extra@7.0.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addeduniversalify@0.1.2(transitive)