json-api-models
Advanced tools
Comparing version
@@ -1,1 +0,1 @@ | ||
const e=class{constructor(e){Object.defineProperty(this,"type",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"id",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"attributes",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"relationships",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"meta",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"links",{enumerable:!0,configurable:!0,writable:!0,value:{}}),this.type=e.type,this.id=e.id,this.merge(e)}identifier(){return{id:this.id,type:this.type}}merge(e){this.links=e.links??this.links,this.meta=e.meta??this.meta,e.attributes&&Object.assign(this.attributes,e.attributes),e.relationships&&Object.entries(e.relationships).forEach((([e,t])=>{this.relationships[e]=this.relationships[e]||{},Object.assign(this.relationships[e],t)}))}};class t{constructor(e={}){Object.defineProperty(this,"models",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"graph",{enumerable:!0,configurable:!0,writable:!0,value:{}})}find(e,t){return null===e?null:Array.isArray(e)?e.map((e=>this.find(e))):"object"==typeof e?this.find(e.type,e.id):t&&this.graph[e]?.[t]||null}findAll(e){return this.graph[e]?Object.keys(this.graph[e]).map((t=>this.graph[e][t])):[]}sync(e){return e.included?.map((e=>this.syncResource(e))),Array.isArray(e.data)?e.data.map((e=>this.syncResource(e))):this.syncResource(e.data)}syncResource(e){const{type:t,id:i}=e;return this.graph[t]=this.graph[t]||{},this.graph[t][i]?this.graph[t][i].merge(e):this.graph[t][i]=this.createModel(e),this.graph[t][i]}createModel(t){const i=this.models[t.type]||e;return new Proxy(new i(t),{get:(e,t,i)=>{if("string"==typeof t){if(e.attributes?.[t])return e.attributes[t];const i=e.relationships?.[t]?.data;if(i)return Array.isArray(i),this.find(i)}return Reflect.get(e,t,i)}})}forget(e){delete this.graph[e.type][e.id]}reset(){this.graph={}}}export{e as Model,t as Store}; | ||
const e=class{constructor(e){Object.defineProperty(this,"type",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"id",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"attributes",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"relationships",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"meta",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"links",{enumerable:!0,configurable:!0,writable:!0,value:{}}),this.type=e.type,this.id=e.id,this.merge(e)}identifier(){return{id:this.id,type:this.type}}merge(e){this.links=e.links??this.links,this.meta=e.meta??this.meta,e.attributes&&Object.assign(this.attributes,e.attributes),e.relationships&&Object.entries(e.relationships).forEach((([e,t])=>{this.relationships[e]=this.relationships[e]||{},Object.assign(this.relationships[e],t)}))}};class t{constructor(e={}){Object.defineProperty(this,"models",{enumerable:!0,configurable:!0,writable:!0,value:e}),Object.defineProperty(this,"graph",{enumerable:!0,configurable:!0,writable:!0,value:{}})}find(e,t){return null===e?null:Array.isArray(e)?e.map((e=>this.find(e))):"object"==typeof e?this.find(e.type,e.id):t&&this.graph[e]?.[t]||null}findAll(e){return this.graph[e]?Object.keys(this.graph[e]).map((t=>this.graph[e][t])):[]}sync(e){return e.included?.map((e=>this.syncResource(e))),Array.isArray(e.data)?e.data.map((e=>this.syncResource(e))):e.data?this.syncResource(e.data):null}syncResource(e){const{type:t,id:i}=e;return this.graph[t]=this.graph[t]||{},this.graph[t][i]?this.graph[t][i].merge(e):this.graph[t][i]=this.createModel(e),this.graph[t][i]}createModel(t){const i=this.models[t.type]||e;return new Proxy(new i(t),{get:(e,t,i)=>{if("string"==typeof t){if(e.attributes?.[t])return e.attributes[t];const i=e.relationships?.[t]?.data;if(i)return Array.isArray(i),this.find(i)}return Reflect.get(e,t,i)}})}forget(e){delete this.graph[e.type][e.id]}reset(){this.graph={}}}export{e as Model,t as Store}; |
import { Model } from './model.ts'; | ||
export interface JsonApiDocument<Type extends string = string> { | ||
data: JsonApiResource<Type> | JsonApiResource<Type>[]; | ||
data: JsonApiResource<Type> | JsonApiResource<Type>[] | null; | ||
included?: JsonApiResource[]; | ||
@@ -5,0 +5,0 @@ meta?: Record<string, any>; |
{ | ||
"name": "json-api-models", | ||
"description": "A lightweight layer for working with JSON:API data.", | ||
"version": "0.2.0-beta.1", | ||
"version": "0.2.0-beta.2", | ||
"author": "Toby Zerner", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -64,5 +64,11 @@ import { Model } from './model'; | ||
return Array.isArray(document.data) | ||
? document.data.map((resource) => this.syncResource(resource)) | ||
: this.syncResource(document.data); | ||
if (Array.isArray(document.data)) { | ||
return document.data.map((resource) => this.syncResource(resource)); | ||
} | ||
if (document.data) { | ||
return this.syncResource(document.data); | ||
} | ||
return null; | ||
} | ||
@@ -69,0 +75,0 @@ |
import { Model } from './model.ts'; | ||
export interface JsonApiDocument<Type extends string = string> { | ||
data: JsonApiResource<Type> | JsonApiResource<Type>[]; | ||
data: JsonApiResource<Type> | JsonApiResource<Type>[] | null; | ||
included?: JsonApiResource[]; | ||
@@ -6,0 +6,0 @@ meta?: Record<string, any>; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23409
0.47%321
1.58%0
-100%