monorise 1.14.0 → 1.15.0-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base/index.d.ts +219 -1
- package/dist/base/index.js +381 -1
- package/dist/base/index.js.map +1 -1
- package/dist/cli/cli.js +291 -122
- package/dist/cli/cli.js.map +1 -1
- package/dist/cli/templates/files/proxy-route.ts +12 -22
- package/dist/cli/templates/files/sst.config.ts +4 -1
- package/dist/cli/templates/files/ws-ticket-route.ts +21 -0
- package/dist/core/index.d.ts +55 -20
- package/dist/core/index.js +743 -128
- package/dist/core/index.js.map +1 -1
- package/dist/index.d.ts +23 -0
- package/dist/proxy/index.d.ts +270 -2
- package/dist/proxy/index.js +645 -37
- package/dist/proxy/index.js.map +1 -1
- package/dist/react/actions/core.action.d.ts.map +1 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +33 -60
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/cli/templates/files/proxy-request.ts +0 -50
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// Re-export all packages from their respective modules
|
|
2
2
|
export {
|
|
3
|
+
ApiKeyPrincipal,
|
|
3
4
|
DependencyContainer,
|
|
5
|
+
EntityAction,
|
|
4
6
|
EntityRepository,
|
|
5
7
|
EntityService,
|
|
6
8
|
Mutual,
|
|
@@ -23,6 +25,7 @@ export {
|
|
|
23
25
|
mutualProcessor,
|
|
24
26
|
prejoinProcessor,
|
|
25
27
|
replicationProcessor,
|
|
28
|
+
requires,
|
|
26
29
|
setupCommonRoutes,
|
|
27
30
|
startAnalyticsBackfill,
|
|
28
31
|
tagProcessor,
|
|
@@ -34,9 +37,26 @@ export {
|
|
|
34
37
|
export {
|
|
35
38
|
AdjustmentCondition,
|
|
36
39
|
AdjustmentConditionFn,
|
|
40
|
+
ApiKeyAccessLevel,
|
|
41
|
+
ApiKeyEntityAccess,
|
|
42
|
+
ApiKeyEntityGrant,
|
|
43
|
+
ApiKeyEntityTags,
|
|
44
|
+
ApiKeyFieldAccess,
|
|
45
|
+
ApiKeyFieldList,
|
|
46
|
+
ApiKeyMutualAccess,
|
|
47
|
+
ApiKeyMutualDataFields,
|
|
48
|
+
ApiKeyMutualGrant,
|
|
49
|
+
ApiKeyPolicies,
|
|
50
|
+
ApiKeyPolicy,
|
|
51
|
+
ApiKeyPolicyEntityConfig,
|
|
52
|
+
ApiKeyRoutePermission,
|
|
53
|
+
ApiKeyRoutePermissions,
|
|
54
|
+
ApiKeySystemField,
|
|
55
|
+
CompiledApiKeyPolicy,
|
|
37
56
|
CreatedEntity,
|
|
38
57
|
DraftEntity,
|
|
39
58
|
EntitySchemaMap,
|
|
59
|
+
FULL_API_KEY_POLICY,
|
|
40
60
|
MonoriseEntityConfig,
|
|
41
61
|
MutualConfig,
|
|
42
62
|
MutualConfigInput,
|
|
@@ -46,9 +66,12 @@ export {
|
|
|
46
66
|
WhereClause,
|
|
47
67
|
WhereConditions,
|
|
48
68
|
WhereOperator,
|
|
69
|
+
compileApiKeyPolicy,
|
|
49
70
|
createEntityConfig,
|
|
50
71
|
createMutualConfig,
|
|
72
|
+
defineApiKeyPolicies,
|
|
51
73
|
resolveEffectiveMutualSchema,
|
|
74
|
+
validateApiKeyPolicies,
|
|
52
75
|
} from './base/index';
|
|
53
76
|
export {
|
|
54
77
|
store,
|
package/dist/proxy/index.d.ts
CHANGED
|
@@ -1,4 +1,272 @@
|
|
|
1
|
-
import { Entity } from '../base/index';
|
|
1
|
+
import { EntitySchemaMap, ApiKeyMutualDataFields, Entity } from '../base/index';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A request to Monorise Core, described as the operations rules are written
|
|
5
|
+
* against: one per thing it reads or writes. Rules on operations rather than
|
|
6
|
+
* URLs can't be bypassed by a different request shape: a transaction's
|
|
7
|
+
* `deleteEntity` and a `DELETE /entity/...` are the same operation.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* `adjust` adds an amount to numeric fields (`POST /entity/:type/:id/adjust`).
|
|
12
|
+
* API key policies count it as an update; rules keep it apart, so a rule
|
|
13
|
+
* written for the values an update sets never approves an adjust.
|
|
14
|
+
*/
|
|
15
|
+
type EntityAction = 'get' | 'list' | 'create' | 'update' | 'adjust' | 'delete';
|
|
16
|
+
/** Relationships have a single `read`: their listings are bounded by `byEntityId`. */
|
|
17
|
+
type RelationshipAction = 'read' | 'create' | 'update' | 'delete';
|
|
18
|
+
/**
|
|
19
|
+
* An entity's fields, from the generated `EntitySchemaMap`. Rules see the
|
|
20
|
+
* payload before Core validates it, so every field may be missing.
|
|
21
|
+
*/
|
|
22
|
+
type EntityPayload<T extends string = string> = string extends T ? Record<string, unknown> : T extends keyof EntitySchemaMap ? Partial<EntitySchemaMap[T]> : Record<string, unknown>;
|
|
23
|
+
/** An entity's numeric fields: what an `adjust` adds amounts to. */
|
|
24
|
+
type EntityAdjustments<T extends string = string> = string extends T ? Record<string, number> : T extends keyof EntitySchemaMap ? {
|
|
25
|
+
[K in keyof EntitySchemaMap[T] as NonNullable<EntitySchemaMap[T][K]> extends number ? K : never]?: number;
|
|
26
|
+
} : Record<string, number>;
|
|
27
|
+
type MutualDataField<By extends string, To extends string> = By extends keyof ApiKeyMutualDataFields ? To extends keyof ApiKeyMutualDataFields[By] ? Extract<ApiKeyMutualDataFields[By][To], string> : string : string;
|
|
28
|
+
/** A relationship's `mutualData`, with the field names the generated config declares. */
|
|
29
|
+
type MutualDataPayload<By extends string = string, To extends string = string> = {
|
|
30
|
+
[K in MutualDataField<By, To>]?: unknown;
|
|
31
|
+
};
|
|
32
|
+
type EntityOperation<T extends string = string, A extends EntityAction = EntityAction> = {
|
|
33
|
+
kind: 'entity';
|
|
34
|
+
action: A;
|
|
35
|
+
entityType: T;
|
|
36
|
+
entityId?: string;
|
|
37
|
+
/** The fields a `create` or `update` sets. */
|
|
38
|
+
payload?: A extends 'adjust' ? never : EntityPayload<T>;
|
|
39
|
+
/** The amount an `adjust` adds to each numeric field. */
|
|
40
|
+
adjustments?: 'adjust' extends A ? EntityAdjustments<T> : never;
|
|
41
|
+
/**
|
|
42
|
+
* The relationship this entity materializes, when creating the relationship
|
|
43
|
+
* also creates it (`asEntity`).
|
|
44
|
+
*/
|
|
45
|
+
asEntityOf?: Pick<RelationshipOperation, 'byEntityType' | 'byEntityId' | 'entityType' | 'entityId'>;
|
|
46
|
+
/** The search term of a `list`, as Core receives it. */
|
|
47
|
+
query?: string;
|
|
48
|
+
/** The field and value of a get by unique field. */
|
|
49
|
+
unique?: {
|
|
50
|
+
field: string;
|
|
51
|
+
value: string;
|
|
52
|
+
};
|
|
53
|
+
/** The tag a `list` queries. */
|
|
54
|
+
tag?: string;
|
|
55
|
+
/** The tag group a tag listing reads, often what a rule scopes by. */
|
|
56
|
+
group?: string;
|
|
57
|
+
/** The range a `list` reads: Core's `start` and `end` query parameters. */
|
|
58
|
+
start?: string;
|
|
59
|
+
end?: string;
|
|
60
|
+
/** Position in a transaction's `operations`. */
|
|
61
|
+
operationIndex?: number;
|
|
62
|
+
};
|
|
63
|
+
type RelationshipOperation<By extends string = string, To extends string = string, A extends RelationshipAction = RelationshipAction> = {
|
|
64
|
+
kind: 'relationship';
|
|
65
|
+
action: A;
|
|
66
|
+
byEntityType: By;
|
|
67
|
+
/**
|
|
68
|
+
* Absent for a relationship field on an entity created without an ID, and
|
|
69
|
+
* for every hop of a chain query after the first.
|
|
70
|
+
*/
|
|
71
|
+
byEntityId?: string;
|
|
72
|
+
entityType: To;
|
|
73
|
+
/** Absent when listing, and for the relationships an update's new list removes. */
|
|
74
|
+
entityId?: string;
|
|
75
|
+
payload?: MutualDataPayload<By, To>;
|
|
76
|
+
operationIndex?: number;
|
|
77
|
+
};
|
|
78
|
+
type RouteOperation = {
|
|
79
|
+
kind: 'route';
|
|
80
|
+
method: string;
|
|
81
|
+
/** The path under `/core`, e.g. `/app/invite-casting`. */
|
|
82
|
+
path: string;
|
|
83
|
+
body?: unknown;
|
|
84
|
+
/** Segments the matching route rule's `:param`s captured. */
|
|
85
|
+
params?: Record<string, string>;
|
|
86
|
+
};
|
|
87
|
+
type ProxyOperation = EntityOperation | RelationshipOperation | RouteOperation;
|
|
88
|
+
/**
|
|
89
|
+
* The parts of the generated config the parser reads: which payload fields of
|
|
90
|
+
* each entity are relationships, to which entity type, and which
|
|
91
|
+
* relationships materialize an entity (`asEntity`).
|
|
92
|
+
*/
|
|
93
|
+
type ProxyEntityConfig = Record<string, {
|
|
94
|
+
mutual?: {
|
|
95
|
+
mutualFields?: Record<string, {
|
|
96
|
+
entityType: string;
|
|
97
|
+
mutual?: {
|
|
98
|
+
asEntity?: {
|
|
99
|
+
name: string;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
}>;
|
|
103
|
+
};
|
|
104
|
+
}>;
|
|
105
|
+
|
|
106
|
+
/** Calls Monorise Core with the request's key, for checks such as "is this record in my organization?". */
|
|
107
|
+
type CoreClient = {
|
|
108
|
+
/** A path under `/core`, e.g. `/mutual/hirer-organization/h1/project/p1`. */
|
|
109
|
+
get(path: string): Promise<Response>;
|
|
110
|
+
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
111
|
+
};
|
|
112
|
+
type RuleContext<Session = unknown> = {
|
|
113
|
+
/** What `authenticate` returned; `undefined` for an anonymous visitor. */
|
|
114
|
+
session: Session | undefined;
|
|
115
|
+
request: Request;
|
|
116
|
+
core: CoreClient;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* `true` forwards the operation; `false` refuses it with 403; a `Response`
|
|
120
|
+
* is sent back as is. Only `true` forwards, so a rule that forgets to return
|
|
121
|
+
* fails closed.
|
|
122
|
+
*/
|
|
123
|
+
type RuleResult = boolean | Response;
|
|
124
|
+
/**
|
|
125
|
+
* Decides whether this user may perform one operation. Write shared rules
|
|
126
|
+
* as `ProxyRule<Session>`; under an entity or action, the operation is
|
|
127
|
+
* narrowed to that entity type and action.
|
|
128
|
+
*/
|
|
129
|
+
type ProxyRule<Session = unknown, Operation = ProxyOperation> = (operation: Operation, context: RuleContext<Session>) => RuleResult | Promise<RuleResult>;
|
|
130
|
+
/** A rule for operations that need no ownership check: the key alone decides. */
|
|
131
|
+
declare const allow: () => RuleResult;
|
|
132
|
+
/** A rule that always refuses. */
|
|
133
|
+
declare const deny: () => RuleResult;
|
|
134
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
135
|
+
type EntityName = keyof typeof Entity extends never ? string : Entity;
|
|
136
|
+
type ActionOf<Key, All> = Key extends All ? Key : All;
|
|
137
|
+
/** Rules for one direction of a relationship, e.g. learner→course. */
|
|
138
|
+
type RelationshipRules<Session = unknown, By extends string = string, To extends string = string> = ProxyRule<Session, RelationshipOperation<By, To>> | {
|
|
139
|
+
[A in RelationshipAction | '*']?: ProxyRule<Session, RelationshipOperation<By, To, ActionOf<A, RelationshipAction>>>;
|
|
140
|
+
};
|
|
141
|
+
type MutualsRules<Session, By extends string> = {
|
|
142
|
+
/** Every relationship from this entity that has no entry. */
|
|
143
|
+
'*'?: ProxyRule<Session, RelationshipOperation<By, EntityName>> | {
|
|
144
|
+
[A in RelationshipAction]?: ProxyRule<Session, RelationshipOperation<By, EntityName, A>>;
|
|
145
|
+
};
|
|
146
|
+
} & {
|
|
147
|
+
[To in EntityName]?: RelationshipRules<Session, By, To>;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Rules for one entity type. A single rule covers every action on the entity
|
|
151
|
+
* and its relationships. An object names actions, with `'*'` for the rest of
|
|
152
|
+
* the entity's actions, and `mutuals` for relationships from it.
|
|
153
|
+
*/
|
|
154
|
+
type EntityRules<Session = unknown, T extends string = string> = ProxyRule<Session, EntityOperation<T> | RelationshipOperation<T, EntityName>> | ({
|
|
155
|
+
[A in EntityAction | '*']?: ProxyRule<Session, EntityOperation<T, ActionOf<A, EntityAction>>>;
|
|
156
|
+
} & {
|
|
157
|
+
mutuals?: MutualsRules<Session, T>;
|
|
158
|
+
});
|
|
159
|
+
type TableRules<Session> = ProxyRule<Session> | {
|
|
160
|
+
[A in EntityAction | RelationshipAction]?: ProxyRule<Session, (A extends EntityAction ? EntityOperation<string, A> : never) | (A extends RelationshipAction ? RelationshipOperation<string, string, A> : never)>;
|
|
161
|
+
};
|
|
162
|
+
type UntypedRules<Session> = ProxyRule<Session> | ({
|
|
163
|
+
[A in EntityAction | RelationshipAction | '*']?: ProxyRule<Session>;
|
|
164
|
+
} & {
|
|
165
|
+
mutuals?: Record<string, ProxyRule<Session> | {
|
|
166
|
+
[A in RelationshipAction | '*']?: ProxyRule<Session>;
|
|
167
|
+
}>;
|
|
168
|
+
});
|
|
169
|
+
/**
|
|
170
|
+
* Rules keyed by entity type. `'*'` applies to anything the table doesn't
|
|
171
|
+
* name: a single rule for every operation, or rules per action.
|
|
172
|
+
*/
|
|
173
|
+
type ProxyRules<Session = unknown> = string extends EntityName ? Record<string, UntypedRules<Session>> : {
|
|
174
|
+
'*'?: TableRules<Session>;
|
|
175
|
+
} & {
|
|
176
|
+
[T in EntityName]?: EntityRules<Session, T>;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Rules for custom routes, keyed by `'METHOD /app/path'`. A `:param` segment
|
|
180
|
+
* matches any one segment and a trailing `*` matches the rest; `'*'` applies
|
|
181
|
+
* to every route the table doesn't name.
|
|
182
|
+
*/
|
|
183
|
+
type RouteRules<Session = unknown> = {
|
|
184
|
+
[K in `${HttpMethod} /app/${string}` | '*']?: ProxyRule<Session, RouteOperation>;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/** One kind of caller: the key it calls Core with, and the rules for its users. */
|
|
188
|
+
type ProxyCaller<Session = unknown> = {
|
|
189
|
+
/** A key in `API_KEYS`, usually mapped to a policy for this kind of caller. */
|
|
190
|
+
apiKey: string | undefined;
|
|
191
|
+
rules?: ProxyRules<Session>;
|
|
192
|
+
routes?: RouteRules<Session>;
|
|
193
|
+
};
|
|
194
|
+
type AuthenticateResult<Session> = Session | null | undefined | Response;
|
|
195
|
+
type SharedOptions<Session> = {
|
|
196
|
+
/** The generated config, which says which payload fields are relationships. */
|
|
197
|
+
config: {
|
|
198
|
+
EntityConfig: ProxyEntityConfig;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Reads the session. `undefined` or `null` is an anonymous visitor; a
|
|
202
|
+
* `Response`, such as a 401 that clears an expired cookie, is sent back as is.
|
|
203
|
+
*/
|
|
204
|
+
authenticate?: (request: Request) => AuthenticateResult<Session> | Promise<AuthenticateResult<Session>>;
|
|
205
|
+
/** The prefix stripped before forwarding. Defaults to `/api/core`. */
|
|
206
|
+
basePath?: string;
|
|
207
|
+
/** Core's URL. Defaults to `process.env.API_BASE_URL`. */
|
|
208
|
+
apiBaseUrl?: string;
|
|
209
|
+
/** Logs the rule to add when none matches. Defaults to on outside production. */
|
|
210
|
+
logMissingRules?: boolean;
|
|
211
|
+
};
|
|
212
|
+
type ProxyHandlerOptions<Session = unknown> = SharedOptions<Session> & ({
|
|
213
|
+
/** Names the kind of caller for this request; `undefined` answers 401. */
|
|
214
|
+
caller: (args: {
|
|
215
|
+
session: Session | undefined;
|
|
216
|
+
request: Request;
|
|
217
|
+
}) => string | undefined | Promise<string | undefined>;
|
|
218
|
+
callers: Record<string, ProxyCaller<Session>>;
|
|
219
|
+
apiKey?: never;
|
|
220
|
+
rules?: never;
|
|
221
|
+
routes?: never;
|
|
222
|
+
} | ({
|
|
223
|
+
caller?: never;
|
|
224
|
+
callers?: never;
|
|
225
|
+
} & ProxyCaller<Session>));
|
|
226
|
+
type CheckResult<Session = unknown> = {
|
|
227
|
+
allowed: true;
|
|
228
|
+
/** The kind of caller, or `undefined` for an app with one kind. */
|
|
229
|
+
caller: string | undefined;
|
|
230
|
+
session: Session | undefined;
|
|
231
|
+
operations: ProxyOperation[];
|
|
232
|
+
} | {
|
|
233
|
+
allowed: false;
|
|
234
|
+
response: Response;
|
|
235
|
+
};
|
|
236
|
+
type RequestHandler = (request: Request) => Promise<Response>;
|
|
237
|
+
type ProxyHandler<Session = unknown> = {
|
|
238
|
+
/** Checks a request to Core and forwards it, or answers with the refusal. */
|
|
239
|
+
handle: RequestHandler;
|
|
240
|
+
/** Runs every step but forwarding, so rules can be unit-tested. */
|
|
241
|
+
checkRequest: (request: Request) => Promise<CheckResult<Session>>;
|
|
242
|
+
GET: RequestHandler;
|
|
243
|
+
POST: RequestHandler;
|
|
244
|
+
PUT: RequestHandler;
|
|
245
|
+
PATCH: RequestHandler;
|
|
246
|
+
DELETE: RequestHandler;
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Puts Monorise Core behind your app's own route. Each request is checked
|
|
250
|
+
* against the rules for its kind of caller, then forwarded with that
|
|
251
|
+
* caller's key: the key decides what the caller can reach, and the rules
|
|
252
|
+
* decide whose records.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* const proxy = createProxyHandler({
|
|
257
|
+
* config,
|
|
258
|
+
* authenticate: (request) => readSession(request),
|
|
259
|
+
* caller: ({ session }) => session?.type ?? 'public',
|
|
260
|
+
* callers: {
|
|
261
|
+
* hirer: { apiKey: process.env.HIRER_API_KEY, rules: hirerRules },
|
|
262
|
+
* public: { apiKey: process.env.PUBLIC_API_KEY, rules: publicRules },
|
|
263
|
+
* },
|
|
264
|
+
* });
|
|
265
|
+
*
|
|
266
|
+
* export const { GET, POST, PUT, PATCH, DELETE } = proxy;
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
declare const createProxyHandler: <Session = unknown>(options: ProxyHandlerOptions<Session>) => ProxyHandler<Session>;
|
|
2
270
|
|
|
3
271
|
interface GenerateTicketOptions {
|
|
4
272
|
entityType: Entity;
|
|
@@ -32,4 +300,4 @@ interface TicketResponse {
|
|
|
32
300
|
*/
|
|
33
301
|
declare const generateWebSocketTicket: (options: GenerateTicketOptions) => Promise<TicketResponse>;
|
|
34
302
|
|
|
35
|
-
export { generateWebSocketTicket };
|
|
303
|
+
export { type CheckResult, type CoreClient, type EntityAction, type EntityOperation, type EntityRules, type ProxyCaller, type ProxyEntityConfig, type ProxyHandler, type ProxyHandlerOptions, type ProxyOperation, type ProxyRule, type ProxyRules, type RelationshipAction, type RelationshipOperation, type RelationshipRules, type RouteOperation, type RouteRules, type RuleContext, type RuleResult, allow, createProxyHandler, deny, generateWebSocketTicket };
|