-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
47 lines (41 loc) · 1.11 KB
/
api.js
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
import {StrictType as T} from 'typed-props'
let userType
let postType
let commentType
userType = T.exact({
$type: T.is('http://api.rumk.in/v1#user'),
id: T.number,
username: T.string,
email: T.string,
balance: T.number,
// Posts is an array. Each post has an author which type is User
posts: () => T.arrayOf(postType),
})
postType = T.exact({
$type: T.is('http://api.rumk.in/v1#post'),
id: T.number,
title: T.string,
text: T.string,
stars: T.number,
// Author has type User which has posts
author: () => userType,
// Comments property is an array It has reference to the post type
comments: () => T.arrayOf(commentType),
publishedAt: T.instanceOf(Date),
})
commentType = T.exact({
$type: T.is('http://api/rumk.in/v1#comment'),
id: T.number,
text: T.string,
stars: T.number,
author: () => userType,
publishedAt: T.instanceOf(Date),
})
function typeIs(type) {
return ({$type}) => $type === type
}
export default T.select(
[typeIs('http://api.rumk.in/v1#user'), userType],
[typeIs('http://api.rumk.in/v1#post'), postType],
[typeIs('http://api.rumk.in/v1#comment'), commentType],
)