json-api-models
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=function(){function t(t,e){this.store=e,this.attributes={},this.relationships={},this.meta={},this.links={},this.merge(t)}return t.prototype.identifier=function(){return{id:this.id,type:this.type}},t.prototype.merge=function(t){var e=this;if("type"in t&&(this.type=t.type),"id"in t&&(this.id=t.id),"attributes"in t&&(Object.assign(this.attributes,t.attributes),Object.keys(t.attributes).forEach((function(t){Object.getOwnPropertyDescriptor(Object.getPrototypeOf(e),t)||Object.defineProperty(e,t,{configurable:!0,get:function(){return e.attributes[t]}})}))),"relationships"in t)for(var r=function(t,r){i.relationships[t]=i.relationships[t]||{},Object.assign(i.relationships[t],r),Object.getOwnPropertyDescriptor(Object.getPrototypeOf(i),t)||Object.defineProperty(i,t,{configurable:!0,get:function(){return e.store.find(e.relationships[t].data)}})},i=this,n=0,o=Object.entries(t.relationships);n<o.length;n++){var s=o[n];r(s[0],s[1])}"links"in t&&(this.links=t.links),"meta"in t&&(this.meta=t.meta)},t}(),e=function(){function e(t){void 0===t&&(t={}),this.models=t,this.graph={}}return e.prototype.model=function(t,e){this.models[t]=e},e.prototype.find=function(t,e){var r=this;return null===t?null:Array.isArray(t)?t.map((function(t){return r.find(t)})):"object"==typeof t?this.find(t.type,t.id):this.graph[t]&&this.graph[t][e]||null},e.prototype.findAll=function(t){var e=this;return this.graph[t]?Object.keys(this.graph[t]).map((function(r){return e.graph[t][r]})):[]},e.prototype.sync=function(t){var e=this.syncResource.bind(this);return"included"in t&&t.included.map(e),Array.isArray(t.data)?t.data.map(e):e(t.data)},e.prototype.syncResource=function(t){var e=t.type,r=t.id;return this.graph[e]=this.graph[e]||{},this.graph[e][r]?this.graph[e][r].merge(t):this.graph[e][r]=this.createModel(t),this.graph[e][r]},e.prototype.createModel=function(e){return new(this.models[e.type]||this.models["*"]||t)(e,this)},e.prototype.forget=function(t){delete this.graph[t.type][t.id]},e.prototype.reset=function(){this.graph={}},e}();function r(t){return encodeURIComponent(t).replace(/[!'()*]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()}))}var i=function(){function t(t){void 0===t&&(t={}),this.query=Object.assign({},t)}return t.prototype.push=function(t,e){var r=this;return"object"==typeof t?Object.entries(t).map((function(t){return r.push.apply(r,t)})):!function(t){return"include"===t||/fields\[[^\]]+]/i.test(t)}(t)?this.query[t]=e:this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this},t.prototype.toString=function(){return Object.entries(this.query).sort((function(t,e){return t[0].localeCompare(e[0])})).map((function(t){var e=t[0],i=t[1];return r(e)+"="+r(i)})).join("&")},t}();exports.Model=t,exports.Query=i,exports.Store=e; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});class t{constructor(t,e){this.store=e,this.attributes={},this.relationships={},this.meta={},this.links={},this.casts={},this.merge(t)}getAttribute(t){const e=this.attributes[t];return this.casts[t]&&null!=e?this.casts[t](e):e}getRelationship(t){return this.store.find(this.relationships[t].data)}identifier(){return{id:this.id,type:this.type}}merge(t){if("type"in t&&(this.type=t.type),"id"in t&&(this.id=t.id),"attributes"in t&&(Object.assign(this.attributes,t.attributes),Object.keys(t.attributes).forEach((t=>{Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this),t)||Object.defineProperty(this,t,{configurable:!0,get:()=>this.getAttribute(t)})}))),"relationships"in t)for(const[e,s]of Object.entries(t.relationships))this.relationships[e]=this.relationships[e]||{},Object.assign(this.relationships[e],s),Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this),e)||Object.defineProperty(this,e,{configurable:!0,get:()=>this.getRelationship(e)});"links"in t&&(this.links=t.links),"meta"in t&&(this.meta=t.meta)}}function e(t){return encodeURIComponent(t).replace(/[!'()*]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()}))}exports.Model=t,exports.Query=class{constructor(t={}){this.query=Object.assign({},t)}push(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.push.apply(this,t))):!function(t){return"include"===t||/fields\[[^\]]+]/i.test(t)}(t)?this.query[t]=e:this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this}toString(){return Object.entries(this.query).sort(((t,e)=>t[0].localeCompare(e[0]))).map((([t,s])=>e(t)+"="+e(s))).join("&")}},exports.Store=class{constructor(t={}){this.models=t,this.graph={}}model(t,e){this.models[t]=e}find(t,e){return null===t?null:Array.isArray(t)?t.map((t=>this.find(t))):"object"==typeof t?this.find(t.type,t.id):this.graph[t]&&this.graph[t][e]||null}findAll(t){return this.graph[t]?Object.keys(this.graph[t]).map((e=>this.graph[t][e])):[]}sync(t){const e=this.syncResource.bind(this);return"included"in t&&t.included.map(e),Array.isArray(t.data)?t.data.map(e):e(t.data)}syncResource(t){const{type:e,id:s}=t;return this.graph[e]=this.graph[e]||{},this.graph[e][s]?this.graph[e][s].merge(t):this.graph[e][s]=this.createModel(t),this.graph[e][s]}createModel(e){return new(this.models[e.type]||this.models["*"]||t)(e,this)}forget(t){delete this.graph[t.type][t.id]}reset(){this.graph={}}}; |
@@ -1,1 +0,1 @@ | ||
var t=function(){function t(t,e){this.store=e,this.attributes={},this.relationships={},this.meta={},this.links={},this.merge(t)}return t.prototype.identifier=function(){return{id:this.id,type:this.type}},t.prototype.merge=function(t){var e=this;if("type"in t&&(this.type=t.type),"id"in t&&(this.id=t.id),"attributes"in t&&(Object.assign(this.attributes,t.attributes),Object.keys(t.attributes).forEach((function(t){Object.getOwnPropertyDescriptor(Object.getPrototypeOf(e),t)||Object.defineProperty(e,t,{configurable:!0,get:function(){return e.attributes[t]}})}))),"relationships"in t)for(var i=function(t,i){r.relationships[t]=r.relationships[t]||{},Object.assign(r.relationships[t],i),Object.getOwnPropertyDescriptor(Object.getPrototypeOf(r),t)||Object.defineProperty(r,t,{configurable:!0,get:function(){return e.store.find(e.relationships[t].data)}})},r=this,n=0,o=Object.entries(t.relationships);n<o.length;n++){var s=o[n];i(s[0],s[1])}"links"in t&&(this.links=t.links),"meta"in t&&(this.meta=t.meta)},t}(),e=function(){function e(t){void 0===t&&(t={}),this.models=t,this.graph={}}return e.prototype.model=function(t,e){this.models[t]=e},e.prototype.find=function(t,e){var i=this;return null===t?null:Array.isArray(t)?t.map((function(t){return i.find(t)})):"object"==typeof t?this.find(t.type,t.id):this.graph[t]&&this.graph[t][e]||null},e.prototype.findAll=function(t){var e=this;return this.graph[t]?Object.keys(this.graph[t]).map((function(i){return e.graph[t][i]})):[]},e.prototype.sync=function(t){var e=this.syncResource.bind(this);return"included"in t&&t.included.map(e),Array.isArray(t.data)?t.data.map(e):e(t.data)},e.prototype.syncResource=function(t){var e=t.type,i=t.id;return this.graph[e]=this.graph[e]||{},this.graph[e][i]?this.graph[e][i].merge(t):this.graph[e][i]=this.createModel(t),this.graph[e][i]},e.prototype.createModel=function(e){return new(this.models[e.type]||this.models["*"]||t)(e,this)},e.prototype.forget=function(t){delete this.graph[t.type][t.id]},e.prototype.reset=function(){this.graph={}},e}();function i(t){return encodeURIComponent(t).replace(/[!'()*]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()}))}var r=function(){function t(t){void 0===t&&(t={}),this.query=Object.assign({},t)}return t.prototype.push=function(t,e){var i=this;return"object"==typeof t?Object.entries(t).map((function(t){return i.push.apply(i,t)})):!function(t){return"include"===t||/fields\[[^\]]+]/i.test(t)}(t)?this.query[t]=e:this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this},t.prototype.toString=function(){return Object.entries(this.query).sort((function(t,e){return t[0].localeCompare(e[0])})).map((function(t){var e=t[0],r=t[1];return i(e)+"="+i(r)})).join("&")},t}();export{t as Model,r as Query,e as Store}; | ||
class t{constructor(t,e){this.store=e,this.attributes={},this.relationships={},this.meta={},this.links={},this.casts={},this.merge(t)}getAttribute(t){const e=this.attributes[t];return this.casts[t]&&null!=e?this.casts[t](e):e}getRelationship(t){return this.store.find(this.relationships[t].data)}identifier(){return{id:this.id,type:this.type}}merge(t){if("type"in t&&(this.type=t.type),"id"in t&&(this.id=t.id),"attributes"in t&&(Object.assign(this.attributes,t.attributes),Object.keys(t.attributes).forEach((t=>{Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this),t)||Object.defineProperty(this,t,{configurable:!0,get:()=>this.getAttribute(t)})}))),"relationships"in t)for(const[e,i]of Object.entries(t.relationships))this.relationships[e]=this.relationships[e]||{},Object.assign(this.relationships[e],i),Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this),e)||Object.defineProperty(this,e,{configurable:!0,get:()=>this.getRelationship(e)});"links"in t&&(this.links=t.links),"meta"in t&&(this.meta=t.meta)}}class e{constructor(t={}){this.models=t,this.graph={}}model(t,e){this.models[t]=e}find(t,e){return null===t?null:Array.isArray(t)?t.map((t=>this.find(t))):"object"==typeof t?this.find(t.type,t.id):this.graph[t]&&this.graph[t][e]||null}findAll(t){return this.graph[t]?Object.keys(this.graph[t]).map((e=>this.graph[t][e])):[]}sync(t){const e=this.syncResource.bind(this);return"included"in t&&t.included.map(e),Array.isArray(t.data)?t.data.map(e):e(t.data)}syncResource(t){const{type:e,id:i}=t;return this.graph[e]=this.graph[e]||{},this.graph[e][i]?this.graph[e][i].merge(t):this.graph[e][i]=this.createModel(t),this.graph[e][i]}createModel(e){return new(this.models[e.type]||this.models["*"]||t)(e,this)}forget(t){delete this.graph[t.type][t.id]}reset(){this.graph={}}}function i(t){return encodeURIComponent(t).replace(/[!'()*]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()}))}class s{constructor(t={}){this.query=Object.assign({},t)}push(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.push.apply(this,t))):!function(t){return"include"===t||/fields\[[^\]]+]/i.test(t)}(t)?this.query[t]=e:this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this}toString(){return Object.entries(this.query).sort(((t,e)=>t[0].localeCompare(e[0]))).map((([t,e])=>i(t)+"="+i(e))).join("&")}}export{t as Model,s as Query,e as Store}; |
@@ -1,1 +0,1 @@ | ||
var JsonApiModels=function(t){"use strict";var e=function(){function t(t,e){this.store=e,this.attributes={},this.relationships={},this.meta={},this.links={},this.merge(t)}return t.prototype.identifier=function(){return{id:this.id,type:this.type}},t.prototype.merge=function(t){var e=this;if("type"in t&&(this.type=t.type),"id"in t&&(this.id=t.id),"attributes"in t&&(Object.assign(this.attributes,t.attributes),Object.keys(t.attributes).forEach((function(t){Object.getOwnPropertyDescriptor(Object.getPrototypeOf(e),t)||Object.defineProperty(e,t,{configurable:!0,get:function(){return e.attributes[t]}})}))),"relationships"in t)for(var i=function(t,i){r.relationships[t]=r.relationships[t]||{},Object.assign(r.relationships[t],i),Object.getOwnPropertyDescriptor(Object.getPrototypeOf(r),t)||Object.defineProperty(r,t,{configurable:!0,get:function(){return e.store.find(e.relationships[t].data)}})},r=this,n=0,o=Object.entries(t.relationships);n<o.length;n++){var s=o[n];i(s[0],s[1])}"links"in t&&(this.links=t.links),"meta"in t&&(this.meta=t.meta)},t}(),i=function(){function t(t){void 0===t&&(t={}),this.models=t,this.graph={}}return t.prototype.model=function(t,e){this.models[t]=e},t.prototype.find=function(t,e){var i=this;return null===t?null:Array.isArray(t)?t.map((function(t){return i.find(t)})):"object"==typeof t?this.find(t.type,t.id):this.graph[t]&&this.graph[t][e]||null},t.prototype.findAll=function(t){var e=this;return this.graph[t]?Object.keys(this.graph[t]).map((function(i){return e.graph[t][i]})):[]},t.prototype.sync=function(t){var e=this.syncResource.bind(this);return"included"in t&&t.included.map(e),Array.isArray(t.data)?t.data.map(e):e(t.data)},t.prototype.syncResource=function(t){var e=t.type,i=t.id;return this.graph[e]=this.graph[e]||{},this.graph[e][i]?this.graph[e][i].merge(t):this.graph[e][i]=this.createModel(t),this.graph[e][i]},t.prototype.createModel=function(t){return new(this.models[t.type]||this.models["*"]||e)(t,this)},t.prototype.forget=function(t){delete this.graph[t.type][t.id]},t.prototype.reset=function(){this.graph={}},t}();function r(t){return encodeURIComponent(t).replace(/[!'()*]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()}))}var n=function(){function t(t){void 0===t&&(t={}),this.query=Object.assign({},t)}return t.prototype.push=function(t,e){var i=this;return"object"==typeof t?Object.entries(t).map((function(t){return i.push.apply(i,t)})):!function(t){return"include"===t||/fields\[[^\]]+]/i.test(t)}(t)?this.query[t]=e:this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this},t.prototype.toString=function(){return Object.entries(this.query).sort((function(t,e){return t[0].localeCompare(e[0])})).map((function(t){var e=t[0],i=t[1];return r(e)+"="+r(i)})).join("&")},t}();return t.Model=e,t.Query=n,t.Store=i,Object.defineProperty(t,"__esModule",{value:!0}),t}({}); | ||
var JsonApiModels=function(t){"use strict";class e{constructor(t,e){this.store=e,this.attributes={},this.relationships={},this.meta={},this.links={},this.casts={},this.merge(t)}getAttribute(t){const e=this.attributes[t];return this.casts[t]&&null!=e?this.casts[t](e):e}getRelationship(t){return this.store.find(this.relationships[t].data)}identifier(){return{id:this.id,type:this.type}}merge(t){if("type"in t&&(this.type=t.type),"id"in t&&(this.id=t.id),"attributes"in t&&(Object.assign(this.attributes,t.attributes),Object.keys(t.attributes).forEach((t=>{Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this),t)||Object.defineProperty(this,t,{configurable:!0,get:()=>this.getAttribute(t)})}))),"relationships"in t)for(const[e,i]of Object.entries(t.relationships))this.relationships[e]=this.relationships[e]||{},Object.assign(this.relationships[e],i),Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this),e)||Object.defineProperty(this,e,{configurable:!0,get:()=>this.getRelationship(e)});"links"in t&&(this.links=t.links),"meta"in t&&(this.meta=t.meta)}}function i(t){return encodeURIComponent(t).replace(/[!'()*]/g,(function(t){return"%"+t.charCodeAt(0).toString(16).toUpperCase()}))}return t.Model=e,t.Query=class{constructor(t={}){this.query=Object.assign({},t)}push(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.push.apply(this,t))):!function(t){return"include"===t||/fields\[[^\]]+]/i.test(t)}(t)?this.query[t]=e:this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this}toString(){return Object.entries(this.query).sort(((t,e)=>t[0].localeCompare(e[0]))).map((([t,e])=>i(t)+"="+i(e))).join("&")}},t.Store=class{constructor(t={}){this.models=t,this.graph={}}model(t,e){this.models[t]=e}find(t,e){return null===t?null:Array.isArray(t)?t.map((t=>this.find(t))):"object"==typeof t?this.find(t.type,t.id):this.graph[t]&&this.graph[t][e]||null}findAll(t){return this.graph[t]?Object.keys(this.graph[t]).map((e=>this.graph[t][e])):[]}sync(t){const e=this.syncResource.bind(this);return"included"in t&&t.included.map(e),Array.isArray(t.data)?t.data.map(e):e(t.data)}syncResource(t){const{type:e,id:i}=t;return this.graph[e]=this.graph[e]||{},this.graph[e][i]?this.graph[e][i].merge(t):this.graph[e][i]=this.createModel(t),this.graph[e][i]}createModel(t){return new(this.models[t.type]||this.models["*"]||e)(t,this)}forget(t){delete this.graph[t.type][t.id]}reset(){this.graph={}}},Object.defineProperty(t,"__esModule",{value:!0}),t}({}); |
{ | ||
"name": "json-api-models", | ||
"description": "A lightweight layer for working with JSON:API data.", | ||
"version": "0.1.0-beta.2", | ||
"version": "0.1.0-beta.3", | ||
"author": "Toby Zerner", | ||
@@ -20,14 +20,32 @@ "license": "MIT", | ||
"build": "rollup -c", | ||
"build:watch": "rollup -cw" | ||
"build:watch": "rollup -cw", | ||
"release": "release-it" | ||
}, | ||
"devDependencies": { | ||
"@release-it/keep-a-changelog": "^2.2.2", | ||
"@rollup/plugin-typescript": "^8.1.0", | ||
"@types/jest": "^26.0.20", | ||
"jest": "^26.6.3", | ||
"release-it": "^14.6.1", | ||
"rollup": "^2.36.2", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"@rollup/plugin-typescript": "^8.1.0", | ||
"ts-jest": "^26.4.4", | ||
"tslib": "^2.1.0", | ||
"typescript": "^4.1.3", | ||
"ts-jest": "^26.4.4" | ||
"typescript": "^4.1.3" | ||
}, | ||
"release-it": { | ||
"github": { | ||
"release": true | ||
}, | ||
"plugins": { | ||
"@release-it/keep-a-changelog": { | ||
"filename": "CHANGELOG.md", | ||
"addUnreleased": true, | ||
"addVersionUrl": true | ||
} | ||
}, | ||
"hooks": { | ||
"after:bump": "npm run build" | ||
} | ||
} | ||
} |
@@ -133,2 +133,17 @@ # json-api-models | ||
#### Attribute Casts | ||
You can define typecasts for attributes on your custom models: | ||
```ts | ||
class User extends Model { | ||
public name: string; | ||
public createdAt: Date; | ||
protected casts = { | ||
createdAt: Date | ||
}; | ||
} | ||
``` | ||
### API Consumption Tips | ||
@@ -135,0 +150,0 @@ |
@@ -8,2 +8,6 @@ import { Store } from './store'; | ||
export type TCastAttributes = { | ||
[key: string]: Function; | ||
} | ||
export class Model implements IJsonApiResource { | ||
@@ -17,2 +21,4 @@ public type: string; | ||
protected casts: TCastAttributes = {}; | ||
[field: string]: any; | ||
@@ -24,2 +30,16 @@ | ||
public getAttribute(name: string): any { | ||
const value = this.attributes[name]; | ||
if (this.casts[name] && value !== null && value !== undefined) { | ||
return this.casts[name](value); | ||
} | ||
return value; | ||
} | ||
public getRelationship(name: string): any { | ||
return this.store.find(this.relationships[name].data as IJsonApiIdentifier); | ||
} | ||
/** | ||
@@ -54,3 +74,3 @@ * Make a resource identifier object for this model. | ||
configurable: true, | ||
get: () => this.attributes[name], | ||
get: () => this.getAttribute(name), | ||
}); | ||
@@ -70,3 +90,3 @@ } | ||
configurable: true, | ||
get: () => this.store.find(this.relationships[name].data as IJsonApiIdentifier), | ||
get: () => this.getRelationship(name), | ||
}); | ||
@@ -73,0 +93,0 @@ } |
328
3.47%222
7.25%24510
-0.29%10
25%