UNPKG

coloquent-test2

Version:

Library for retrieving model objects from a JSON-API, with a fluent syntax inspired by Laravel Eloquent.

258 lines 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Builder_1 = require("./Builder"); var Map_1 = require("./util/Map"); var PaginationStrategy_1 = require("./PaginationStrategy"); var php_date_formatter_1 = require("php-date-formatter"); var SaveResponse_1 = require("./response/SaveResponse"); var ToManyRelation_1 = require("./relation/ToManyRelation"); var ToOneRelation_1 = require("./relation/ToOneRelation"); var Reflection_1 = require("./util/Reflection"); var AxiosHttpClient_1 = require("./httpclient/axios/AxiosHttpClient"); var Model = /** @class */ (function () { function Model() { this.type = typeof this; this.relations = new Map_1.Map(); this.attributes = new Map_1.Map(); this.readOnlyAttributes = []; this.httpClient = new AxiosHttpClient_1.AxiosHttpClient(); this.dates = {}; this.initHttpClient(); } Model.prototype.initHttpClient = function () { this.httpClient.setBaseUrl(this.getJsonApiBaseUrl()); }; Model.get = function (page) { if (page === void 0) { page = null; } return new Builder_1.Builder(this) .get(page); }; Model.first = function () { return new Builder_1.Builder(this) .first(); }; Model.find = function (id) { return new Builder_1.Builder(this) .find(id); }; Model.with = function (attribute) { return new Builder_1.Builder(this) .with(attribute); }; Model.where = function (attribute, value) { return new Builder_1.Builder(this) .where(attribute, value); }; Model.orderBy = function (attribute, direction) { if (direction === void 0) { direction = null; } return new Builder_1.Builder(this) .orderBy(attribute, direction); }; Model.option = function (queryParameter, value) { return new Builder_1.Builder(this) .option(queryParameter, value); }; Model.prototype.save = function () { var _this = this; var attributes = {}; for (var key in this.attributes.toArray()) { if (this.readOnlyAttributes.indexOf(key) == -1) { attributes[key] = this.attributes.get(key); } } var payload = { data: { type: this.getJsonApiType(), attributes: attributes } }; if (this.id !== null) { payload['data']['id'] = this.id; return this.httpClient .patch(this.getJsonApiType() + '/' + this.id, payload) .then(function (response) { _this.setApiId(response.getData().data.id); return new SaveResponse_1.SaveResponse(response, _this.constructor, response.getData()); }, function (response) { throw new Error(response.message); }); } else { return this.httpClient .post(this.getJsonApiType(), payload) .then(function (response) { _this.setApiId(response.getData().data.id); return new SaveResponse_1.SaveResponse(response, _this.constructor, response.getData()); }, function (response) { throw new Error(response.message); }); } }; Model.prototype.create = function () { var _this = this; var attributes = {}; for (var key in this.attributes.toArray()) { if (this.readOnlyAttributes.indexOf(key) == -1) { attributes[key] = this.attributes.get(key); } } var payload = { data: { type: this.getJsonApiType(), attributes: attributes } }; if (this.id !== null) { payload['data']['id'] = this.id; } return this.httpClient .post(this.getJsonApiType(), payload) .then(function (response) { _this.setApiId(response.getData().data.id); return new SaveResponse_1.SaveResponse(response, _this.constructor, response.getData()); }, function (response) { throw new Error(response.message); }); }; Model.prototype.delete = function () { if (this.id === null) { throw new Error('Cannot delete a model with no ID.'); } return this.httpClient .delete(this.getJsonApiType() + '/' + this.id) .then(function () { }); }; /** * Allows you to get the current HTTP client (AxiosHttpClient by default), e.g. to alter its configuration. * @returns {HttpClient} */ Model.prototype.getHttpClient = function () { return this.httpClient; }; /** * Allows you to use any HTTP client library, as long as you write a wrapper for it that implements the interfaces * HttpClient, HttpClientPromise and HttpClientResponse. * @param httpClient */ Model.prototype.setHttpClient = function (httpClient) { this.httpClient = httpClient; }; Model.prototype.getJsonApiType = function () { return this.jsonApiType; }; Model.prototype.populateFromJsonApiDoc = function (jsonApiDoc) { this.id = jsonApiDoc.id; for (var key in jsonApiDoc.attributes) { this.setAttribute(key, jsonApiDoc.attributes[key]); } }; Model.getPageSize = function () { return this.pageSize; }; Model.getPaginationStrategy = function () { return this.paginationStrategy; }; Model.getPaginationPageNumberParamName = function () { return this.paginationPageNumberParamName; }; Model.getPaginationPageSizeParamName = function () { return this.paginationPageSizeParamName; }; Model.getPaginationOffsetParamName = function () { return this.paginationOffsetParamName; }; Model.getPaginationLimitParamName = function () { return this.paginationLimitParName; }; Model.prototype.getRelation = function (relationName) { return this.relations.get(relationName); }; Model.prototype.setRelation = function (relationName, value) { this.relations.set(relationName, value); }; Model.prototype.getAttributes = function () { return this.attributes.toArray(); }; Model.prototype.getAttribute = function (attributeName) { if (this.isDateAttribute(attributeName)) { return this.getAttributeAsDate(attributeName); } return this.attributes.get(attributeName); }; Model.prototype.getAttributeAsDate = function (attributeName) { if (!Date.parse(this.attributes.get(attributeName))) { throw new Error("Attribute " + attributeName + " cannot be cast to type Date"); } return new Date(this.attributes.get(attributeName)); }; Model.prototype.isDateAttribute = function (attributeName) { return this.dates.hasOwnProperty(attributeName); }; Model.prototype.setAttribute = function (attributeName, value) { if (this.isDateAttribute(attributeName)) { if (!Date.parse(value)) { throw new Error(value + " cannot be cast to type Date"); } value = Model.getDateFormatter().parseDate(value, this.dates[attributeName]); } this.attributes.set(attributeName, value); }; /** * We use a single instance of DateFormatter, which is stored as a static property on Model, to minimize the number * of times we need to instantiate the DateFormatter class. By using this getter a DateFormatter is instantiated * only when it is used at least once. * * @returns DateFormatter */ Model.getDateFormatter = function () { if (!Model.dateFormatter) { Model.dateFormatter = new php_date_formatter_1.default(); } return Model.dateFormatter; }; Model.prototype.getApiId = function () { return this.id; }; Model.prototype.setApiId = function (id) { this.id = id; }; Model.prototype.hasMany = function (relatedType, relationName) { if (typeof relationName === 'undefined') { relationName = Reflection_1.Reflection.getNameOfNthMethodOffStackTrace(new Error(), 2); } return new ToManyRelation_1.ToManyRelation(relatedType, this, relationName); }; Model.prototype.hasOne = function (relatedType, relationName) { if (typeof relationName === 'undefined') { relationName = Reflection_1.Reflection.getNameOfNthMethodOffStackTrace(new Error(), 2); } return new ToOneRelation_1.ToOneRelation(relatedType, this, relationName); }; /** * @type {number} the page size */ Model.pageSize = 50; /** * @type {PaginationStrategy} the pagination strategy */ Model.paginationStrategy = PaginationStrategy_1.PaginationStrategy.OffsetBased; /** * @type {string} The number query parameter name. By default: 'page[number]' */ Model.paginationPageNumberParamName = 'page[number]'; /** * @type {string} The size query parameter name. By default: 'page[size]' */ Model.paginationPageSizeParamName = 'page[size]'; /** * @type {string} The offset query parameter name. By default: 'page[offset]' */ Model.paginationOffsetParamName = 'page[offset]'; /** * @type {string} The limit query parameter name. By default: 'page[limit]' */ Model.paginationLimitParName = 'page[limit]'; return Model; }()); exports.Model = Model; //# sourceMappingURL=Model.js.map