From 708a94f5b39a6dbf34b716b7a3f7f6d896e8384d Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 31 Aug 2021 15:31:33 +1000 Subject: [PATCH] Prepare for release --- CHANGELOG.md | 7 +++++- dist/index.cjs.js | 2 +- dist/index.d.ts | 7 +++--- dist/index.es.js | 2 +- dist/index.js | 2 +- dist/jsonApi.d.ts | 27 ---------------------- dist/model.d.ts | 48 ++++++++++++++++++++++----------------- dist/query.d.ts | 17 +++++++++----- dist/store.d.ts | 58 +++++++++++++++++++++++------------------------ dist/types.d.ts | 27 ++++++++++++++++++++++ tsconfig.json | 1 + 11 files changed, 108 insertions(+), 90 deletions(-) delete mode 100644 dist/jsonApi.d.ts create mode 100644 dist/types.d.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d9dff6..474c8eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Query: add `append`, `set`, and `delete` methods. +- Export useful JSON:API types: `JsonApiDocument`, `JsonApiIdentifier`, `JsonApiResource`, `JsonApiRelationships`, and `JsonApiRelationship`. + +### Removed +- Query: remove `push` method. ## [0.1.0-beta.4] - 2021-05-26 ### Fixed @@ -22,6 +28,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Initial release [Unreleased]: https://github.com/tobyzerner/json-api-models/compare/v0.1.0-beta.4...HEAD - [0.1.0-beta.4]: https://github.com/tobyzerner/json-api-models/compare/v0.1.0-beta.3...v0.1.0-beta.4 [0.1.0-beta.3]: https://github.com/tobyzerner/json-api-models/compare/v0.1.0-beta.1...v0.1.0-beta.3 diff --git a/dist/index.cjs.js b/dist/index.cjs.js index c0ffba4..deee581 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -1 +1 @@ -"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],s=this.casts[t];return s&&null!=e?new s(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={}}}; +"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],s=this.casts[t];return s&&null!=e?new s(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)}append(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.append.apply(this,t))):this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this}set(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.set.apply(this,t))):this.query[t]=e,this}delete(t){return Array.isArray(t)?t.forEach((t=>this.delete(t))):delete this.query[t],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={}}}; diff --git a/dist/index.d.ts b/dist/index.d.ts index ca8b2b4..13d69fd 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,3 +1,4 @@ -export { Store } from './store'; -export { Model } from './model'; -export { Query } from './query'; +export * from './types'; +export { Store } from './store'; +export { Model } from './model'; +export { Query } from './query'; diff --git a/dist/index.es.js b/dist/index.es.js index 26a3dc6..0a6b00d 100644 --- a/dist/index.es.js +++ b/dist/index.es.js @@ -1 +1 @@ -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],i=this.casts[t];return i&&null!=e?new i(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}; +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],i=this.casts[t];return i&&null!=e?new i(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)}append(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.append.apply(this,t))):this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this}set(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.set.apply(this,t))):this.query[t]=e,this}delete(t){return Array.isArray(t)?t.forEach((t=>this.delete(t))):delete this.query[t],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}; diff --git a/dist/index.js b/dist/index.js index 270f772..318807a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -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],i=this.casts[t];return i&&null!=e?new i(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}({}); +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],i=this.casts[t];return i&&null!=e?new i(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)}append(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.append.apply(this,t))):this.query[t]=(this.query[t]?this.query[t]+",":"")+e,this}set(t,e){return"object"==typeof t?Object.entries(t).map((t=>this.set.apply(this,t))):this.query[t]=e,this}delete(t){return Array.isArray(t)?t.forEach((t=>this.delete(t))):delete this.query[t],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}({}); diff --git a/dist/jsonApi.d.ts b/dist/jsonApi.d.ts deleted file mode 100644 index 0e65511..0000000 --- a/dist/jsonApi.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -export declare type TKeyValueObject = { - [key: string]: any; -}; -export interface IJsonApiDocument { - data: IJsonApiResource | IJsonApiResource[]; - included?: IJsonApiResource[]; - meta?: TKeyValueObject; - links?: TKeyValueObject; -} -export interface IJsonApiIdentifier { - type: string; - id: string; -} -export interface IJsonApiResource extends IJsonApiIdentifier { - attributes?: TKeyValueObject; - relationships?: IJsonApiRelationships; - meta?: TKeyValueObject; - links?: TKeyValueObject; -} -export interface IJsonApiRelationships { - [relationName: string]: IJsonApiRelationship; -} -export interface IJsonApiRelationship { - data?: IJsonApiIdentifier | IJsonApiIdentifier[]; - meta?: TKeyValueObject; - links?: TKeyValueObject; -} diff --git a/dist/model.d.ts b/dist/model.d.ts index d6e96a3..109a651 100644 --- a/dist/model.d.ts +++ b/dist/model.d.ts @@ -1,21 +1,27 @@ -import { Store } from './store'; -import { IJsonApiIdentifier, IJsonApiRelationships, IJsonApiResource, TKeyValueObject } from './jsonApi'; -export declare class Model implements IJsonApiResource { - protected store: Store; - type: string; - id: string; - attributes: TKeyValueObject; - relationships: IJsonApiRelationships; - meta: TKeyValueObject; - links: TKeyValueObject; - [field: string]: any; - constructor(data: IJsonApiResource, store: Store); - /** - * Make a resource identifier object for this model. - */ - identifier(): IJsonApiIdentifier; - /** - * Merge new JSON:API resource data into the model. - */ - merge(data: IJsonApiResource): void; -} +import { Store } from './store'; +import { JsonApiIdentifier, JsonApiRelationships, JsonApiResource, KeyValueObject } from './types'; +export declare type TCastAttributes = { + [key: string]: FunctionConstructor; +}; +export declare class Model implements JsonApiResource { + protected store: Store; + type: string; + id: string; + attributes: KeyValueObject; + relationships: JsonApiRelationships; + meta: KeyValueObject; + links: KeyValueObject; + protected casts: TCastAttributes; + [field: string]: any; + constructor(data: JsonApiResource, store: Store); + getAttribute(name: string): any; + getRelationship(name: string): any; + /** + * Make a resource identifier object for this model. + */ + identifier(): JsonApiIdentifier; + /** + * Merge new JSON:API resource data into the model. + */ + merge(data: JsonApiResource): void; +} diff --git a/dist/query.d.ts b/dist/query.d.ts index c59a662..3145dab 100644 --- a/dist/query.d.ts +++ b/dist/query.d.ts @@ -1,6 +1,11 @@ -export declare class Query { - readonly query: any; - constructor(query?: any); - push(key: object | string, value?: any): this; - toString(): string; -} +export declare class Query { + readonly query: any; + constructor(query?: any); + append(key: string, value: any): this; + append(values: object): this; + set(key: string, value: any): this; + set(values: object): this; + delete(key: string): this; + delete(keys: string[]): this; + toString(): string; +} diff --git a/dist/store.d.ts b/dist/store.d.ts index 3efeab6..5a39f90 100644 --- a/dist/store.d.ts +++ b/dist/store.d.ts @@ -1,29 +1,29 @@ -import { Model } from './model'; -import { IJsonApiDocument, IJsonApiIdentifier, IJsonApiResource } from './jsonApi'; -interface Graph { - [type: string]: { - [id: string]: Model; - }; -} -declare type ModelConstructor = { - new (data: IJsonApiResource, store?: Store): Model; -}; -interface ModelCollection { - [type: string]: ModelConstructor; -} -export declare class Store { - models: ModelCollection; - protected graph: Graph; - constructor(models?: ModelCollection); - model(type: string, model: ModelConstructor): void; - find(identifier: IJsonApiIdentifier): Model; - find(identifiers: IJsonApiIdentifier[]): Model[]; - find(type: string, id: string): Model; - findAll(type: string): Model[]; - sync(document: IJsonApiDocument): Model | Model[]; - syncResource(data: IJsonApiIdentifier): Model; - private createModel; - forget(data: IJsonApiIdentifier): void; - reset(): void; -} -export {}; +import { Model } from './model'; +import { JsonApiDocument, JsonApiIdentifier, JsonApiResource } from './types'; +interface Graph { + [type: string]: { + [id: string]: Model; + }; +} +declare type ModelConstructor = { + new (data: JsonApiResource, store?: Store): Model; +}; +interface ModelCollection { + [type: string]: ModelConstructor; +} +export declare class Store { + models: ModelCollection; + protected graph: Graph; + constructor(models?: ModelCollection); + model(type: string, model: ModelConstructor): void; + find(identifier: JsonApiIdentifier): Model; + find(identifiers: JsonApiIdentifier[]): Model[]; + find(type: string, id: string): Model; + findAll(type: string): Model[]; + sync(document: JsonApiDocument): Model | Model[]; + syncResource(data: JsonApiIdentifier): Model; + private createModel; + forget(data: JsonApiIdentifier): void; + reset(): void; +} +export {}; diff --git a/dist/types.d.ts b/dist/types.d.ts new file mode 100644 index 0000000..a535fbf --- /dev/null +++ b/dist/types.d.ts @@ -0,0 +1,27 @@ +export declare type KeyValueObject = { + [key: string]: any; +}; +export interface JsonApiDocument { + data: JsonApiResource | JsonApiResource[]; + included?: JsonApiResource[]; + meta?: KeyValueObject; + links?: KeyValueObject; +} +export interface JsonApiIdentifier { + type: string; + id: string; +} +export interface JsonApiResource extends JsonApiIdentifier { + attributes?: KeyValueObject; + relationships?: JsonApiRelationships; + meta?: KeyValueObject; + links?: KeyValueObject; +} +export interface JsonApiRelationships { + [relationName: string]: JsonApiRelationship; +} +export interface JsonApiRelationship { + data?: JsonApiIdentifier | JsonApiIdentifier[]; + meta?: KeyValueObject; + links?: KeyValueObject; +} diff --git a/tsconfig.json b/tsconfig.json index db0dff3..cfe7faf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "declaration": true, "declarationDir": "./dist", "module": "es6", + "moduleResolution": "node", "noImplicitAny": true, "outDir": "./dist", "target": "es2017",