-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
full.example.ts
188 lines (182 loc) · 5.34 KB
/
full.example.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
import { Linker, Serializer, Relator, Metaizer, Paginator } from '../src';
import { User, Article } from '../test/models';
import { getJSON } from '../test/utils/get-json';
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* SETUP */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
for (let i = 0; i < 5; i++) {
User.save(new User(String(i)));
}
for (let i = 0; i < 5; i++) {
Article.save(new Article(String(i), User.storage[0]));
}
const domain = 'https://www.example.com';
const pathTo = (path: string) => domain + path;
const ArticleSerializer = new Serializer('articles');
const UserArticleRelationshipLinker = new Linker((user, articles) =>
Array.isArray(articles)
? pathTo(`/users/${user.id}/relationships/articles/`)
: pathTo(`/users/${user.id}/relationships/articles/${articles.id}`)
);
const UserArticleLinker = new Linker((user, articles) =>
Array.isArray(articles)
? pathTo(`/users/${user.id}/articles/`)
: pathTo(`/users/${user.id}/articles/${articles.id}`)
);
const UserArticleMetaizer = new Metaizer((user, articles) =>
Array.isArray(articles)
? { userCreatedAt: user.createdAt }
: { userCreatedAt: user.createdAt, articleCreatedAt: articles.createdAt }
);
const UserArticlesRelator = new Relator(
async (user: User) => user.getArticles(),
ArticleSerializer,
{
linkers: {
relationship: UserArticleRelationshipLinker,
related: UserArticleLinker,
},
metaizer: UserArticleMetaizer,
}
);
const UserLinker = new Linker<[User]>((users) =>
Array.isArray(users) ? pathTo(`/users/`) : pathTo(`/users/${users.id}`)
);
const UserPaginator = new Paginator<User>((users) => {
if (Array.isArray(users)) {
const nextPage = Number(users[0].id) + 1;
const prevPage = Number(users[users.length - 1].id) - 1;
return {
first: pathTo('/users/0'),
last: pathTo(`/users/${User.storage.length - 1}`),
next: nextPage <= User.storage.length - 1 ? pathTo(`/users/${nextPage}`) : null,
prev: prevPage >= 0 ? pathTo(`/users/${prevPage}`) : null,
};
}
return;
});
const JSONAPIMetaizer = new Metaizer(() => ({
somefiller: 'nothing really fascinating',
}));
const UserDocumentMetaizer = new Metaizer(() => ({
requestedAt: new Date(),
}));
const UserMetaizer = new Metaizer((user) => ({
createdAt: user.createdAt,
}));
/* -------------------------------------------------------------------------- */
/**
* Serializes Users
*/
const UserSerializer = new Serializer('users', {
depth: 1,
projection: {},
relators: UserArticlesRelator,
linkers: {
resource: UserLinker,
paginator: UserPaginator,
},
metaizers: {
jsonapi: JSONAPIMetaizer,
document: UserDocumentMetaizer,
resource: UserMetaizer,
},
});
(async () => {
const user = User.storage[0];
const document = await UserSerializer.serialize(user);
console.dir(getJSON(document), { depth: null });
// Output: {
// jsonapi: {
// version: "1.0",
// meta: { somefiller: "nothing really fascinating" },
// },
// meta: { requestedAt: "2020-05-21T01:33:09.537Z" },
// links: {
// first: "https://www.example.com/users/0",
// last: "https://www.example.com/users/5",
// prev: null,
// next: null,
// },
// data: {
// type: "users",
// id: "sample_user_id",
// meta: { createdAt: "2020-05-21T01:33:09.505Z" },
// attributes: {},
// relationships: {
// articles: {
// links: {
// self: "https://www.example.com/users/sample_user_id/relationships/articles/",
// related: "https://www.example.com/users/sample_user_id/articles/",
// },
// data: [
// { type: "articles", id: "same_article_id" },
// { type: "articles", id: "0" },
// { type: "articles", id: "1" },
// { type: "articles", id: "2" },
// { type: "articles", id: "3" },
// { type: "articles", id: "4" },
// ],
// meta: { userCreatedAt: "2020-05-21T01:33:09.505Z" },
// },
// },
// links: { self: "https://www.example.com/users/sample_user_id" },
// },
// included: [
// {
// type: "articles",
// id: "same_article_id",
// attributes: {
// createdAt: "2020-05-21T01:33:09.505Z",
// comments: [],
// author: "sample_user_id",
// },
// },
// {
// type: "articles",
// id: "0",
// attributes: {
// createdAt: "2020-05-21T01:33:09.537Z",
// comments: [],
// author: "sample_user_id",
// },
// },
// {
// type: "articles",
// id: "1",
// attributes: {
// createdAt: "2020-05-21T01:33:09.537Z",
// comments: [],
// author: "sample_user_id",
// },
// },
// {
// type: "articles",
// id: "2",
// attributes: {
// createdAt: "2020-05-21T01:33:09.537Z",
// comments: [],
// author: "sample_user_id",
// },
// },
// {
// type: "articles",
// id: "3",
// attributes: {
// createdAt: "2020-05-21T01:33:09.537Z",
// comments: [],
// author: "sample_user_id",
// },
// },
// {
// type: "articles",
// id: "4",
// attributes: {
// createdAt: "2020-05-21T01:33:09.537Z",
// comments: [],
// author: "sample_user_id",
// },
// },
// ],
// };
})();