Repository navigation
Expand file tree
/
Copy pathresources.ts
More file actions
119 lines (113 loc) · 4.83 KB
/
Copy pathresources.ts
File metadata and controls
119 lines (113 loc) · 4.83 KB
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
/**
* A GitHub-like API's resources: users, repositories, issues, issue comments,
* pull requests and labels. Each one is defined once and everything else
* (identifiers, payloads, documents, query parameters, endpoints) derives
* from these definitions.
*
* The relationship graph exercises all four relationship kinds:
*
* Repository ──owner──────────────▶ User `one`
* Issue ──repository──▶ Repository ──owner──▶ User (a 2-hop include path)
* Issue ──author──────────────────▶ User `one`
* Issue ──assignee────────────────▶ User `optional` (may be null)
* Issue ──labels──────────────────▶ Label `many` (bounded, inlined)
* Issue ──comments────────────────▶ IssueComment `paginated` (unbounded, linked)
* IssueComment ──author───────────▶ User `one`
* PullRequest ──repository────────▶ Repository `one`
* PullRequest ──author────────────▶ User `one`
* PullRequest ──reviewers─────────▶ User `many`
*/
import * as Schema from "effect/Schema";
import { Filter, Relationship, Resource } from "@thomasfosterau/effect-jsonapi";
export const User = Resource.make("users", {
attributes: {
login: Schema.NonEmptyString,
name: Schema.optionalKey(Schema.String),
bio: Schema.optionalKey(Schema.String),
// Wire form is an ISO-8601 string; decoded form is a `Date`.
createdAt: Schema.DateFromString,
},
});
export const Label = Resource.make("labels", {
attributes: {
name: Schema.NonEmptyString,
/** Six-character hex color code, without the leading `#`. */
color: Schema.NonEmptyString,
description: Schema.optionalKey(Schema.String),
},
});
export const Repository = Resource.make("repositories", {
attributes: {
name: Schema.NonEmptyString,
description: Schema.optionalKey(Schema.String),
private: Schema.Boolean,
language: Schema.optionalKey(Schema.String),
stargazerCount: Schema.Int,
createdAt: Schema.DateFromString,
},
relationships: {
// Every repository has an owner: required to-one.
owner: Relationship.one(() => User),
},
});
export const IssueComment = Resource.make("issueComments", {
attributes: {
body: Schema.NonEmptyString,
createdAt: Schema.DateFromString,
},
relationships: {
author: Relationship.one(() => User),
},
});
// Issues are the one collection that speaks the filter grammar (`filter: true`
// on `GET /issues`): every attribute but `body`, and
// the three to-one relationships, valued by the related id, are filter fields
// over the whole operator core (`Filter.able()`; a relationship's `filter:
// true` is `eq ne in nin isnull`).
export const Issue = Resource.make("issues", {
attributes: {
number: Schema.Int.pipe(Filter.able()),
title: Schema.NonEmptyString.pipe(Filter.able()),
body: Schema.String,
// Closed attribute set — `state` only ever decodes to one of these.
state: Schema.Literals(["open", "closed"]).pipe(Filter.able()),
locked: Schema.Boolean.pipe(Filter.able()),
createdAt: Schema.DateFromString.pipe(Filter.able()),
},
relationships: {
// An issue is always opened against a repository, by someone.
repository: Relationship.one(() => Repository, { filter: true }),
author: Relationship.one(() => User, { filter: true }),
// ... but may be unassigned: `filter[assignee][isnull]=true` names those.
assignee: Relationship.optional(() => User, { filter: true }),
// Labels are few and bounded: inlined identifiers, manageable through
// the /issues/:id/relationships/labels endpoints.
labels: Relationship.many(() => Label),
// Comments are unbounded: reachable only through the related link
// (GET /issues/:id/comments), never inlined.
comments: Relationship.paginated(() => IssueComment),
},
});
export const PullRequest = Resource.make("pulls", {
attributes: {
number: Schema.Int,
title: Schema.NonEmptyString,
body: Schema.String,
state: Schema.Literals(["open", "closed", "merged"]),
draft: Schema.Boolean,
headRef: Schema.NonEmptyString,
baseRef: Schema.NonEmptyString,
createdAt: Schema.DateFromString,
},
relationships: {
repository: Relationship.one(() => Repository),
author: Relationship.one(() => User),
reviewers: Relationship.many(() => User),
},
});
export type User = typeof User.Type;
export type Label = typeof Label.Type;
export type Repository = typeof Repository.Type;
export type IssueComment = typeof IssueComment.Type;
export type Issue = typeof Issue.Type;
export type PullRequest = typeof PullRequest.Type;