-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
142 lines (119 loc) · 3.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import * as JSON from 'json-typescript';
/**
* A JSON object MUST be at the root of every JSON API request and responsecontaining data.
* This object defines a document’s “top level”.
* A document MUST contain at least one of the following top-level members:
*/
export type MetaObject = JSON.Object;
/**
* this type is no longer required, as the meta has been moved to the DocBase
* this type can be safely removed in future versions
*/
export interface DocWithMeta extends DocBase {
meta: MetaObject; // a meta object that contains non-standard meta-information.
}
export interface DocWithData<T extends PrimaryData = PrimaryData>
extends DocBase {
data: T; // the document’s “primary data”
included?: Included;
}
export interface DocWithErrors extends DocBase {
errors: Errors; // an array of error objects
}
// The members data and errors MUST NOT coexist in the same document.
// ⛔️ NOT EXPRESSIBLE IN TYPESCRIPT
// If a document does not contain a top-level data key,
// the included member MUST NOT be present either.
// ⛔️ NOT EXPRESSIBLE IN TYPESCRIPT
/* A document MAY contain any of these top-level members: */
export interface DocBase {
jsonapi?: ImplementationInfo;
links?: Links | PaginationLinks;
meta?: MetaObject; // a meta object that contains non-standard meta-information.
}
export type Document = DocWithErrors | DocWithMeta | DocWithData;
export type SingleResourceDoc<
T extends string = string,
A extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
> = DocWithData<ResourceObject<T, A>>;
export type CollectionResourceDoc<
T extends string = string,
A extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
> = DocWithData<Array<ResourceObject<T, A>>>;
// an object describing the server’s implementation
export interface ImplementationInfo {
version?: string;
meta?: MetaObject;
}
export type Link = string | { href: string; meta?: MetaObject };
// The top-level links object MAY contain the following members:
export interface Links {
self?: Link; // the link that generated the current response document.
related?: Link; // a related resource link when the primary data represents a resource relationship.
// TODO pagination links for the primary data.
}
export interface PaginationLinks {
first?: Link | null; // the first page of data
last?: Link | null; // the last page of data
prev?: Link | null; // the previous page of data
next?: Link | null; // the next page of data
}
export type Included = ResourceObject[];
export interface ErrorObject {
id?: number | string;
links?: Links;
status?: string;
code?: string;
title?: string;
detail?: string;
source?: {
pointer?: any;
parameter?: string;
};
meta?: MetaObject;
}
export type PrimaryData<
T extends string = string,
A extends AttributesObject = AttributesObject
> = ResourceObject<T, A> | Array<ResourceObject<T, A>>;
export interface ResourceObject<
T extends string = string,
A extends AttributesObject = AttributesObject
> {
id?: string;
type: T;
attributes?: AttributesObject<A>;
relationships?: RelationshipsObject;
links?: Links;
meta?: MetaObject;
}
export interface ResourceIdentifierObject {
id: string;
type: string;
meta?: MetaObject;
}
export type ResourceLinkage =
| null
| never[]
| ResourceIdentifierObject
| ResourceIdentifierObject[];
export interface RelationshipsWithLinks {
links: Links;
}
export interface RelationshipsWithData {
data: ResourceLinkage;
}
export interface RelationshipsWithMeta {
meta: MetaObject;
}
export type RelationshipObject =
| RelationshipsWithData
| RelationshipsWithLinks
| RelationshipsWithMeta;
export interface RelationshipsObject {
[k: string]: RelationshipObject;
}
export type AttributesObject<
ATTRS extends { [k: string]: JSON.Value } = { [k: string]: JSON.Value }
> = { [K in keyof ATTRS]: ATTRS[K] };
export type Errors = ErrorObject[];