diff --git a/package-lock.json b/package-lock.json index c4ca7c4..f851846 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "4.32.0", + "version": "4.33.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "4.32.0", + "version": "4.33.0", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0", diff --git a/package.json b/package.json index 6e0f7b9..5c82d1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "4.32.0", + "version": "4.33.0", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", diff --git a/src/globals/Array.ts b/src/globals/Array.ts index bb9a05d..95f2cba 100644 --- a/src/globals/Array.ts +++ b/src/globals/Array.ts @@ -13,48 +13,54 @@ export {} declare global { interface Array { - /** - * Call the toJSON method of each item - * inside the array. - */ - toAthennaJSON(): Record[] - - /** - * Call the toResource method of each item - * inside the array. - */ - toAthennaResource(criterias?: any): T[] - - /** - * Transform the array to an Athenna collection. - */ - toAthennaCollection(): Collection - } -} + athenna: { + /** + * Call the toJSON method of each item + * inside the array. + */ + toJSON(criterias?: any): Record[] -// eslint-disable-next-line no-extend-native -Array.prototype.toAthennaJSON = function () { - return this.map(model => { - if (model && model.toJSON) { - return model.toJSON() - } + /** + * Call the toResource method of each item + * inside the array. + */ + toResource(criterias?: any): T[] - return null - }).filter(Boolean) + /** + * Transform the array to an Athenna collection. + */ + toCollection(): Collection + } + } } -// eslint-disable-next-line no-extend-native -Array.prototype.toAthennaResource = function (criterias = {}) { - return this.map(model => { - if (model && model.toResource) { - return model.toResource(criterias) - } +if (!Array.prototype.athenna) { + // eslint-disable-next-line no-extend-native + Object.defineProperty(Array.prototype, 'athenna', { + get: function () { + return { + toJSON: (criterias: any = {}) => { + return this.map(model => { + if (model && model.toJSON) { + return model.toJSON(criterias) + } - return null - }).filter(Boolean) -} + return null + }).filter(Boolean) + }, + toResource: (criterias: any = {}) => { + return this.map(model => { + if (model && model.toResource) { + return model.toResource(criterias) + } -// eslint-disable-next-line no-extend-native -Array.prototype.toAthennaCollection = function () { - return new Collection(this) + return null + }).filter(Boolean) + }, + toCollection: () => { + return new Collection(this) + } + } + } + }) } diff --git a/src/globals/Error.ts b/src/globals/Error.ts index c3b2f1b..3a907fa 100644 --- a/src/globals/Error.ts +++ b/src/globals/Error.ts @@ -26,46 +26,49 @@ declare global { } } -// eslint-disable-next-line no-extend-native -Error.prototype.toAthennaException = function (options: ExceptionJson = {}) { - options.name = options.name || this.name - options.stack = options.stack || this.stack - options.message = options.message || this.message - options.code = - options.code || this.code || changeCase.constantCase(options.name) - options.otherInfos = { - ...options.otherInfos, - ...Json.omit(this, [ - 'name', - 'stack', - 'message', - 'code', - 'details', - 'errors' - ]) - } +if (!Error.prototype.toAthennaException) { + // eslint-disable-next-line no-extend-native + Error.prototype.toAthennaException = function (options: ExceptionJson = {}) { + options.name = options.name || this.name + options.stack = options.stack || this.stack + options.message = options.message || this.message + options.code = + options.code || this.code || changeCase.constantCase(options.name) + options.otherInfos = { + ...options.otherInfos, + ...Json.omit(this, [ + 'name', + 'stack', + 'message', + 'code', + 'details', + 'errors', + 'toAthennaException' + ]) + } - if (!Is.Undefined(options.details)) { - return new Exception(options) - } + if (!Is.Undefined(options.details)) { + return new Exception(options) + } - options.details = [] + options.details = [] - if (!Is.Empty(this.details)) { - if (Is.Array(this.details)) { - options.details.push(...this.details) - } else { - options.details.push(this.details) + if (!Is.Empty(this.details)) { + if (Is.Array(this.details)) { + options.details.push(...this.details) + } else { + options.details.push(this.details) + } } - } - if (!Is.Empty(this.errors)) { - if (Is.Array(this.errors)) { - options.details.push(...this.errors) - } else { - options.details.push(this.errors) + if (!Is.Empty(this.errors)) { + if (Is.Array(this.errors)) { + options.details.push(...this.errors) + } else { + options.details.push(this.errors) + } } - } - return new Exception(options) + return new Exception(options) + } } diff --git a/src/helpers/Collection.ts b/src/helpers/Collection.ts index 6a7ce0d..c1fa853 100644 --- a/src/helpers/Collection.ts +++ b/src/helpers/Collection.ts @@ -35,7 +35,7 @@ export class Collection extends CollectJS { public toJSON(): Record[] { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - return this.all().toAthennaJSON() + return this.all().athenna.toJSON() } /** @@ -44,7 +44,7 @@ export class Collection extends CollectJS { public toResource(criterias = {}): T[] { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - return this.all().toAthennaResource(criterias) + return this.all().athenna.toResource(criterias) } } diff --git a/tests/unit/CollectionTest.ts b/tests/unit/CollectionTest.ts index 12af9b2..b77415f 100644 --- a/tests/unit/CollectionTest.ts +++ b/tests/unit/CollectionTest.ts @@ -35,8 +35,8 @@ export default class CollectionTest { } ] - assert.deepEqual(models.toAthennaJSON(), [{ id: 1 }]) - assert.deepEqual(models.toAthennaCollection().toJSON(), [{ id: 1 }]) + assert.deepEqual(models.athenna.toJSON(), [{ id: 1 }]) + assert.deepEqual(models.athenna.toCollection().toJSON(), [{ id: 1 }]) assert.deepEqual(new Collection(models).toJSON(), [{ id: 1 }]) } @@ -51,8 +51,8 @@ export default class CollectionTest { { toResource: criterias => criterias } ] - assert.deepEqual(models.toAthennaResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) - assert.deepEqual(models.toAthennaCollection().toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) + assert.deepEqual(models.athenna.toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) + assert.deepEqual(models.athenna.toCollection().toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) assert.deepEqual(new Collection(models).toResource({ id: 2 }), [{ id: 1 }, { id: 2 }]) } }