-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec.ts
204 lines (182 loc) · 4.29 KB
/
spec.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
import type { Payload } from "payload";
import type { CollectionConfig, Field, Where } from "payload/types";
import { APIError } from "payload/errors";
import type { Concrete } from "../kit/typing";
import collections, { type Spec } from "../spec/definition/collections";
import feat, { type Feature, type FeatureValue } from "../spec/definition/feat";
import type {
Label as SpecLabel,
Labels as SpecLabels,
} from "../spec/definition/label";
import lookup from "../spec/definition/lookup";
import type { RuleLabel } from "../spec/definition/rule";
import verb, { type VerbLabel } from "../spec/definition/verb";
import type { Warden } from "../spec/warden";
/**
* The {@link Warding} options.
*/
export type Options = {
slug: string;
fields?: Field[];
tag?: SpecLabels;
};
/**
* The {@link Concrete} variant of {@link Options}.
*/
export type ConcreteOptions = Concrete<Options, "fields">;
/**
* The {@link Warding} label map.
*/
export type Label = {
[k in
| "active"
| "roleName"
| "features"
| "feature"
| "verbs"
| "traits"
| keyof RuleLabel
| keyof VerbLabel]: SpecLabel;
};
/**
* The result type of {@link Warding.build}.
*/
export type Built = {
collections: { user: CollectionConfig; role: CollectionConfig };
warden: Warden;
populate: Populate;
initialize: Initialize;
initializationExtra?: { user: any; role: any };
};
export type Initialize = (
payload: Payload,
root?: { email: string; password: string },
extra?: { user: any; role: any },
) => Promise<void>;
export type Populate = {
role: RolePopulate;
user: UserPopulate;
};
export type RolePopulate = (
payload: Payload,
role: { name: string; features: FeatureValue[] },
extra?: any,
) => Promise<string | number>;
export type UserPopulate = (
payload: Payload,
user: {
email: string;
password: string;
roles: (string | number)[];
},
extra?: any,
) => Promise<string | number>;
/**
* The {@link APIError} for a conflict condition caused by something that is
* still in use.
*/
export class StillInUse extends APIError {
constructor(slug: string) {
super(`This ${slug} is still in use!`, 404, {}, true);
}
}
export function theInitialize(
populate: Populate,
features: Feature[],
): Initialize {
return async (payload, root, extra) => {
const role = await populate.role(
payload,
{
name: collections.root,
features: features
.map(x => {
const t = feat.traits(x)?.map(([value]) => value);
if (!t) return undefined;
return {
feature: x.slug,
traits: t,
verbs: verb.verbs as string[],
};
})
.filter((x): x is FeatureValue => !!x),
},
extra?.role,
);
if (!root) return;
await populate.user(
payload,
{
email: root?.email,
password: root?.password,
roles: [role],
},
extra?.user,
);
};
}
export function theRolePopulate(slug: string): RolePopulate {
return async (payload, { name, features }, extra) => {
const id = await has(payload, {
slug,
where: { name: { equals: name } },
});
if (id) {
return id;
}
return (
await payload.create({
collection: slug,
data: {
...extra,
name,
[collections.name("features")]: features,
[collections.name("lookup")]: lookup.toLookup(features),
},
})
).id;
};
}
export function theUserPopulate(slug: Spec): UserPopulate {
return async (payload, { email, password, roles }, extra) => {
const id = await has(payload, {
slug: slug.user,
where: { email: { equals: email } },
});
if (id) {
return id;
}
return (
await payload.create({
collection: slug.user,
data: {
...extra,
email,
password,
[slug.role]: roles,
},
})
).id;
};
}
export async function has(
payload: Payload,
{
slug,
where,
}: {
slug: string;
where: Where;
},
): Promise<string | number> {
const v = await payload.find({
collection: slug,
where,
limit: 1,
});
if (v.totalDocs > 0) {
return v.docs[0].id;
}
return 0;
}
export default { theRolePopulate, theUserPopulate, has };