UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

204 lines 5.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Resource = void 0; const container_1 = require("../container"); const repository_1 = require("../supports/orm/repository"); const pagination_1 = require("../pagination"); const DEFAULT_KEY = 'data'; class Resource { constructor(formatter) { this.app = container_1.Container.get('app'); this.key = DEFAULT_KEY; this.formatter = (data) => { if (data instanceof repository_1.Repository) return data.getAttributes(); return data; }; if (formatter) this.formatter = formatter; } item(data) { this.type = "item"; this.setData(data); return this; } static item(data) { return new Resource().item(data); } collection(data) { this.type = "collection"; if (data instanceof pagination_1.Paginator) { this.setData(data.getData()); this.addMeta(data.getCurrentPageKey(), data.getCurrentPage()); this.addMeta(data.getTotalKey(), data.getTotal()); this.addMeta(data.getPerPageKey(), data.getPerPage()); return this; } this.setData(data); return this; } static collection(data) { return new Resource().collection(data); } setFormatter(formatter) { this.formatter = formatter; return this; } getFormatter() { return this.formatter; } getKey() { return this.key; } setKey(val) { this.key = val; return this; } getData() { return this.data; } setData(val) { this.data = val; return this; } getMeta() { return this.meta; } setMeta(val) { this.meta = val; return this; } getMetaFormatter() { return this.metaFormatter; } setMetaFormatter(val) { this.metaFormatter = val; return this; } addMeta(name, value) { if (!this.meta) this.meta = {}; if (name && typeof name === 'object') { this.meta = Object.assign({}, this.meta, name); } else if (typeof name === 'string') { this.meta[name] = value; } return this; } withoutKey() { this.key = undefined; return this; } transformResourceMeta() { return this.useTransformer(this.metaFormatter, this.meta); } transformResourceData() { if (this.type === "item") { return this.useTransformer(this.formatter, this.data); } if (this.type === "collection") { return this.data.map((i) => this.useTransformer(this.formatter, i)); } return this.data; } useTransformer(formatter, data) { if (!data) return null; if (typeof formatter === 'string') { return this.recursiveFilter(this.useStringFormatter(formatter, data)); } if (this.isResourceFormatter(formatter)) { return this.recursiveFilter(this.useResourceFormatter(formatter, data)); } if (typeof formatter === 'function') { return this.recursiveFilter(this.useCallbackFormatter(formatter, data)); } return this.recursiveFilter(data); } isResourceFormatter(formatter) { return formatter && Reflect.getMetadata('type', formatter) === 'resource'; } useStringFormatter(formatterName, data) { const Transformer = this.app.get(formatterName); return Transformer.resolve(data); } useResourceFormatter(formatter, data) { const Transformer = this.app.get(formatter); return Transformer.resolve(data); } useCallbackFormatter(formatter, data) { return formatter(data); } serializeResourceData(isOverstore = true, hasMeta = false) { const data = this.transformResourceData(); if (this.type === "collection") { if (this.key) { return { [this.key]: data, }; } return (isOverstore || hasMeta) ? { data, } : data; } if (this.type === "item") { if (this.key) { return { [this.key]: data, }; } return data; } if (this.key) { return { [this.key]: null, }; } return null; } serializeResourceMeta() { const meta = this.transformResourceMeta(); return meta ? { meta, } : null; } transform(isOverstore = true) { const meta = this.serializeResourceMeta(); const data = this.serializeResourceData(isOverstore, !!meta) || {}; if (meta) { return Object.assign(Object.assign({}, data), meta); } return data; } recursiveFilter(data) { if (data instanceof Resource) { return data.withoutKey().transform(false); } else if (data instanceof repository_1.Repository) { return data.getAttributes(); } else if (Array.isArray(data)) { return data.map(item => this.recursiveFilter(item)); } else if (data !== null && typeof data === 'object') { const res = {}; for (const key in data) { if (Object.prototype.hasOwnProperty.call(data, key)) { const item = data[key]; res[key] = this.recursiveFilter(item); } } return res; } else { return data; } } output(isOverstore = true) { const data = this.transform(isOverstore); return data; } } exports.Resource = Resource; //# sourceMappingURL=resource.js.map