-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJsonApi.ts
217 lines (175 loc) · 5.21 KB
/
JsonApi.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
export class JsonApiRequest {
data: JsonApiData[] | JsonApiData;
errors: JsonApiError[];
included: JsonApiData[];
links: { [key: string]: MetaLink };
//model: any;
meta: Meta;
jsonapi: JsonApiVersion;
constructor() {
// Do this later
//this.jsonapi = new JsonApiVersion();
//this.jsonapi.version = '1.0';
}
//GetErrorsAsString(): string {
// var err: string = '';
// if (this.errors && this.errors.length) {
// for (var i = 0; i < this.errors.length; i++) {
// err += this.errors[i].detail + ' ';
// }
// err = err.trim();
// } else {
// //Should not ever happen
// err = 'No response from server';
// }
// return err;
//}
WithError(error: JsonApiError): JsonApiRequest {
this.errors = this.errors || new Array<JsonApiError>();
this.errors.push(error);
return this;
}
WithData(data: JsonApiData): JsonApiRequest {
var d : JsonApiData[] = <JsonApiData[]>this.data || new Array<JsonApiData>();
d.push(data);
this.data = d;
return this;
}
WithSingleData(data: JsonApiData): JsonApiRequest {
this.data = data;
return this;
}
WithIncluded(data: JsonApiData): JsonApiRequest {
this.included = this.included || new Array<JsonApiData>();
this.included.push(data);
return this;
}
WithLink(linkType: string, uri: string): JsonApiRequest {
this.links = this.links || {};
this.links[linkType] = new MetaLink(uri);
return this;
}
AddLink(linkType: string, link: MetaLink): JsonApiRequest {
this.links = this.links || {};
this.links[linkType] = link;
return this;
}
WithMeta(key: string, value: string): JsonApiRequest {
this.meta = this.meta || new Meta();
this.meta[key] = value;
return this;
}
}
export class JsonApiData {
id: string;
type: string;
attributes: { [key: string]: any; };
links: { [key: string]: MetaLink };
relationships: { [key: string]: JsonApiRelationship };
constructor(type: string) {
this.id = undefined;
this.type = type;
this.attributes = undefined;
this.links = undefined;
this.relationships = undefined;
}
WithAttribute(key: string, value: string): JsonApiData {
this.attributes = this.attributes || {};
this.attributes[key] = value;
return this;
}
AddLink(linkType: string, link: MetaLink): JsonApiData {
this.links = this.links || {};
this.links[linkType] = link;
return this;
}
WithLink(linkType: string, uri: string): JsonApiData {
this.links = this.links || {};
this.links[linkType] = new MetaLink(uri);
return this;
}
WithId(id: string): JsonApiData {
this.id = id;
return this;
}
WithRelationship(relationName: string, relation: JsonApiRelationship) {
this.relationships = this.relationships || {};
this.relationships[relationName] = relation;
return this;
}
GetSelfLink(): string {
if (this.links && this.links['self']) {
return this.links['self'].href;
} else {
return null;
}
}
}
export class JsonApiRelationship {
links: { [key: string]: MetaLink };
data: JsonApiData[] | JsonApiData;
meta: Meta;
constructor(isArray: boolean) {
this.links = {};
if (isArray === true) {
this.data = new Array<JsonApiData>();
} else {
this.data = null;
}
}
WithData(type: string, id: string): JsonApiRelationship {
if (type && id) {
if (this.data && Array.isArray(this.data)) {
(<JsonApiData[]>this.data).push(new JsonApiData(type).WithId(id));
} else {
this.data = new JsonApiData(type).WithId(id);
}
return this;
} else {
throw new Error('Invalid call to "JsonApiRelationship.WithData" type and id required');
}
}
AddLink(linkType: string, link: MetaLink): JsonApiRelationship {
this.links[linkType] = link;
return this;
}
WithLink(linkType: string, url: string): JsonApiRelationship {
this.links[linkType] = new MetaLink(url);
return this;
}
FindLinkType(linkType: string): string {
return (this.links && this.links[linkType]) ? this.links[linkType].href : null;
}
}
export class MetaLink {
href: string;
meta: Meta;
constructor(href: string, meta?: Meta) {
this.href = href;
this.meta = meta || new Meta();
}
}
export class Meta {
fields: { [key: string]: string; };
constructor() {
this.fields = undefined;
}
}
export class JsonApiError {
id: string;
links: { [key: string]: MetaLink };
status: number;
code: number;
title: string;
detail: string;
//source: Source;
AddLink(linkType: string, link: MetaLink): JsonApiError {
this.links[linkType] = link;
return this;
}
}
export class JsonApiVersion {
version: string;
//meta: Meta;
}