json-api-models
Advanced tools
Comparing version
@@ -1,5 +0,5 @@ | ||
import { JsonApiResource, ModelForType, SchemaCollection } from './types'; | ||
import { JsonApiResource, SchemaCollection } from './types'; | ||
import { Store } from './store.ts'; | ||
declare class ModelBase<Schema extends JsonApiResource = JsonApiResource> { | ||
protected store: Store; | ||
declare class ModelBase<Schema extends JsonApiResource = JsonApiResource, Schemas extends SchemaCollection = SchemaCollection> { | ||
protected store: Store<Schemas>; | ||
type: Schema['type']; | ||
@@ -11,3 +11,3 @@ id: Schema['id']; | ||
links: Schema['links']; | ||
constructor(data: Schema, store: Store); | ||
constructor(data: Schema, store: Store<Schemas>); | ||
/** | ||
@@ -26,3 +26,3 @@ * Make a resource identifier object for this model. | ||
} | ||
type ProxiedModel<Schema extends JsonApiResource, Schemas extends Record<string, JsonApiResource>> = Schema & Schema['attributes'] & { | ||
type ProxiedModel<Schema extends JsonApiResource, Schemas extends SchemaCollection> = Schema & Schema['attributes'] & { | ||
[Property in keyof NonNullable<Schema['relationships']>]?: NonNullable<NonNullable<Schema['relationships']>[Property]> extends { | ||
@@ -38,2 +38,6 @@ data?: infer Data; | ||
export declare const Model: new <Schema extends JsonApiResource = JsonApiResource, Schemas extends SchemaCollection = SchemaCollection>(data: JsonApiResource<Schema['type']>, store: Store<Schemas>) => Model<Schema, Schemas>; | ||
export type ModelMap<Schemas extends SchemaCollection = SchemaCollection> = { | ||
[Type in keyof Schemas & string]?: new (data: JsonApiResource<Type>, store: Store<Schemas>) => Schemas[Type]; | ||
}; | ||
export type ModelForType<Type extends string, Schemas extends SchemaCollection> = Type extends keyof Schemas ? Schemas[Type] extends ModelBase<JsonApiResource<Type>, Schemas> ? Schemas[Type] : Model<JsonApiResource<Type>, Schemas> : Model<JsonApiResource<Type>, Schemas>; | ||
export {}; |
@@ -1,2 +0,3 @@ | ||
import { JsonApiDocument, JsonApiIdentifier, JsonApiResource, ModelForType, ModelMap, SchemaCollection } from './types'; | ||
import { ModelForType, ModelMap } from './model'; | ||
import { JsonApiDocument, JsonApiIdentifier, JsonApiResource, SchemaCollection } from './types'; | ||
export declare class Store<Schemas extends SchemaCollection = {}> { | ||
@@ -3,0 +4,0 @@ models: ModelMap<Schemas>; |
@@ -1,3 +0,1 @@ | ||
import { Model } from './model.ts'; | ||
import { Store } from './store.ts'; | ||
export interface JsonApiDocument<Type extends string = string> { | ||
@@ -34,5 +32,1 @@ data: JsonApiResource<Type> | JsonApiResource<Type>[] | null; | ||
}; | ||
export type ModelMap<Schemas extends SchemaCollection = SchemaCollection> = { | ||
[Type in keyof Schemas & string]?: new (data: JsonApiResource<Type>, store: Store<Schemas>) => Schemas[Type]; | ||
}; | ||
export type ModelForType<Type extends string, Schemas extends SchemaCollection> = Type extends keyof Schemas ? Model<Schemas[Type], Schemas> : Model<JsonApiResource<Type>, Schemas>; |
{ | ||
"name": "json-api-models", | ||
"description": "A lightweight layer for working with JSON:API data.", | ||
"version": "0.2.0-beta.6", | ||
"version": "0.2.0-beta.7", | ||
"author": "Toby Zerner", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -1,5 +0,8 @@ | ||
import { JsonApiResource, ModelForType, SchemaCollection } from './types'; | ||
import { JsonApiResource, SchemaCollection } from './types'; | ||
import { Store } from './store.ts'; | ||
class ModelBase<Schema extends JsonApiResource = JsonApiResource> { | ||
class ModelBase< | ||
Schema extends JsonApiResource = JsonApiResource, | ||
Schemas extends SchemaCollection = SchemaCollection, | ||
> { | ||
public type: Schema['type']; | ||
@@ -14,3 +17,3 @@ public id: Schema['id']; | ||
data: Schema, | ||
protected store: Store, | ||
protected store: Store<Schemas>, | ||
) { | ||
@@ -107,3 +110,3 @@ this.type = data.type; | ||
Schema extends JsonApiResource, | ||
Schemas extends Record<string, JsonApiResource>, | ||
Schemas extends SchemaCollection, | ||
> = Schema & | ||
@@ -136,1 +139,17 @@ Schema['attributes'] & { | ||
) => Model<Schema, Schemas> = ModelBase as any; | ||
export type ModelMap<Schemas extends SchemaCollection = SchemaCollection> = { | ||
[Type in keyof Schemas & string]?: new ( | ||
data: JsonApiResource<Type>, | ||
store: Store<Schemas>, | ||
) => Schemas[Type]; | ||
}; | ||
export type ModelForType< | ||
Type extends string, | ||
Schemas extends SchemaCollection, | ||
> = Type extends keyof Schemas | ||
? Schemas[Type] extends ModelBase<JsonApiResource<Type>, Schemas> | ||
? Schemas[Type] | ||
: Model<JsonApiResource<Type>, Schemas> | ||
: Model<JsonApiResource<Type>, Schemas>; |
@@ -1,2 +0,2 @@ | ||
import { Model } from './model'; | ||
import { Model, ModelForType, ModelMap } from './model'; | ||
import { | ||
@@ -6,4 +6,2 @@ JsonApiDocument, | ||
JsonApiResource, | ||
ModelForType, | ||
ModelMap, | ||
SchemaCollection, | ||
@@ -10,0 +8,0 @@ } from './types'; |
@@ -1,4 +0,1 @@ | ||
import { Model } from './model.ts'; | ||
import { Store } from './store.ts'; | ||
export interface JsonApiDocument<Type extends string = string> { | ||
@@ -43,15 +40,1 @@ data: JsonApiResource<Type> | JsonApiResource<Type>[] | null; | ||
export type SchemaCollection = { [Type in string]: JsonApiResource<Type> }; | ||
export type ModelMap<Schemas extends SchemaCollection = SchemaCollection> = { | ||
[Type in keyof Schemas & string]?: new ( | ||
data: JsonApiResource<Type>, | ||
store: Store<Schemas>, | ||
) => Schemas[Type]; | ||
}; | ||
export type ModelForType< | ||
Type extends string, | ||
Schemas extends SchemaCollection, | ||
> = Type extends keyof Schemas | ||
? Model<Schemas[Type], Schemas> | ||
: Model<JsonApiResource<Type>, Schemas>; |
25281
0.62%