cloesce 0.0.5-unstable.2 → 0.0.5-unstable.3
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/ast.d.ts +100 -80
- package/dist/ast.js +12 -12
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +368 -331
- package/dist/extractor/err.d.ts +26 -26
- package/dist/extractor/err.js +129 -100
- package/dist/extractor/extract.d.ts +60 -17
- package/dist/extractor/extract.js +826 -764
- package/dist/router/crud.d.ts +1 -1
- package/dist/router/crud.js +42 -43
- package/dist/router/router.d.ts +135 -98
- package/dist/router/router.js +424 -381
- package/dist/router/validator.d.ts +11 -6
- package/dist/router/validator.js +158 -144
- package/dist/router/wasm.d.ts +56 -22
- package/dist/router/wasm.js +91 -79
- package/dist/ui/backend.d.ts +214 -181
- package/dist/ui/backend.js +258 -245
- package/dist/ui/client.d.ts +1 -1
- package/dist/ui/common.d.ts +54 -31
- package/dist/ui/common.js +171 -159
- package/package.json +1 -1
package/dist/router/crud.d.ts
CHANGED
package/dist/router/crud.js
CHANGED
|
@@ -6,55 +6,54 @@ import { NO_DATA_SOURCE } from "../ast.js";
|
|
|
6
6
|
* calling a default implementation.
|
|
7
7
|
*/
|
|
8
8
|
export function proxyCrud(obj, ctor, d1) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
9
|
+
return new Proxy(obj, {
|
|
10
|
+
get(target, method) {
|
|
11
|
+
// If the instance defines the method, always use it (override allowed)
|
|
12
|
+
const value = Reflect.get(target, method);
|
|
13
|
+
if (typeof value === "function") {
|
|
14
|
+
return value.bind(target);
|
|
15
|
+
}
|
|
16
|
+
// Fallback to CRUD methods
|
|
17
|
+
if (method === "save") {
|
|
18
|
+
return (body, ds) => upsert(ctor, body, ds, d1);
|
|
19
|
+
}
|
|
20
|
+
if (method === "list") {
|
|
21
|
+
return (ds) => list(ctor, ds, d1);
|
|
22
|
+
}
|
|
23
|
+
if (method === "get") {
|
|
24
|
+
return (id, ds) => _get(ctor, id, ds, d1);
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
29
|
}
|
|
30
30
|
async function upsert(ctor, body, dataSource, d1) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
: HttpResult.fail(500, getRes.value);
|
|
31
|
+
const includeTree = findIncludeTree(dataSource, ctor);
|
|
32
|
+
const orm = Orm.fromD1(d1);
|
|
33
|
+
const result = await orm.upsert(ctor, body, includeTree);
|
|
34
|
+
if (result.isLeft()) return HttpResult.fail(500, result.value);
|
|
35
|
+
const getRes = await orm.get(ctor, result.value, includeTree);
|
|
36
|
+
return getRes.isRight()
|
|
37
|
+
? HttpResult.ok(200, getRes.value)
|
|
38
|
+
: HttpResult.fail(500, getRes.value);
|
|
40
39
|
}
|
|
41
40
|
async function _get(ctor, id, dataSource, d1) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
const includeTree = findIncludeTree(dataSource, ctor);
|
|
42
|
+
const orm = Orm.fromD1(d1);
|
|
43
|
+
const res = await orm.get(ctor, id, includeTree);
|
|
44
|
+
return res.isRight()
|
|
45
|
+
? HttpResult.ok(200, res.value)
|
|
46
|
+
: HttpResult.fail(500, res.value);
|
|
48
47
|
}
|
|
49
48
|
async function list(ctor, dataSource, d1) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
const includeTree = findIncludeTree(dataSource, ctor);
|
|
50
|
+
const orm = Orm.fromD1(d1);
|
|
51
|
+
const res = await orm.list(ctor, { includeTree });
|
|
52
|
+
return res.isRight()
|
|
53
|
+
? HttpResult.ok(200, res.value)
|
|
54
|
+
: HttpResult.fail(500, res.value);
|
|
56
55
|
}
|
|
57
56
|
function findIncludeTree(dataSource, ctor) {
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
const normalizedDs = dataSource === NO_DATA_SOURCE ? null : dataSource;
|
|
58
|
+
return normalizedDs ? ctor[normalizedDs] : null;
|
|
60
59
|
}
|
package/dist/router/router.d.ts
CHANGED
|
@@ -19,136 +19,173 @@ export type ConstructorRegistry = Record<string, new () => any>;
|
|
|
19
19
|
* These values are guaranteed to never change throughout a workers lifetime.
|
|
20
20
|
*/
|
|
21
21
|
export declare class RuntimeContainer {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
readonly ast: CloesceAst;
|
|
23
|
+
readonly constructorRegistry: ConstructorRegistry;
|
|
24
|
+
readonly wasm: OrmWasmExports;
|
|
25
|
+
private static instance;
|
|
26
|
+
private constructor();
|
|
27
|
+
static init(
|
|
28
|
+
ast: CloesceAst,
|
|
29
|
+
constructorRegistry: ConstructorRegistry,
|
|
30
|
+
wasm?: WebAssembly.Instance,
|
|
31
|
+
): Promise<void>;
|
|
32
|
+
static get(): RuntimeContainer;
|
|
33
|
+
/**
|
|
34
|
+
* Disposes the singleton instance. For testing purposes only.
|
|
35
|
+
*/
|
|
36
|
+
static dispose(): void;
|
|
33
37
|
}
|
|
34
38
|
/**
|
|
35
39
|
* Given a request, this represents a map of each body / url param name to
|
|
36
40
|
* its actual value. Unknown, as the a request can be anything.
|
|
37
41
|
*/
|
|
38
42
|
export type RequestParamMap = Record<string, unknown>;
|
|
39
|
-
export type MiddlewareFn = (
|
|
40
|
-
|
|
43
|
+
export type MiddlewareFn = (
|
|
44
|
+
di: DependencyContainer,
|
|
45
|
+
) => Promise<HttpResult | void>;
|
|
46
|
+
export type ResultMiddlewareFn = (
|
|
47
|
+
di: DependencyContainer,
|
|
48
|
+
result: HttpResult,
|
|
49
|
+
) => Promise<HttpResult | void>;
|
|
41
50
|
/**
|
|
42
51
|
* Expected states in which the router may exit.
|
|
43
52
|
*/
|
|
44
53
|
export declare enum RouterError {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
UnknownPrefix = 0,
|
|
55
|
+
UnknownRoute = 1,
|
|
56
|
+
UnmatchedHttpVerb = 2,
|
|
57
|
+
InstantiatedMethodMissingId = 3,
|
|
58
|
+
RequestMissingBody = 4,
|
|
59
|
+
RequestBodyMissingParameters = 5,
|
|
60
|
+
RequestBodyInvalidParameter = 6,
|
|
61
|
+
InstantiatedMethodMissingDataSource = 7,
|
|
62
|
+
MissingDependency = 8,
|
|
63
|
+
InvalidDatabaseQuery = 9,
|
|
64
|
+
ModelNotFound = 10,
|
|
65
|
+
UncaughtException = 11,
|
|
57
66
|
}
|
|
58
67
|
export declare class CloesceApp {
|
|
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
|
-
|
|
68
|
+
routePrefix: string;
|
|
69
|
+
private globalMiddleware;
|
|
70
|
+
/**
|
|
71
|
+
* Registers global middleware which runs before any route matching.
|
|
72
|
+
*
|
|
73
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
74
|
+
*
|
|
75
|
+
* @param m - The middleware function to register.
|
|
76
|
+
*/
|
|
77
|
+
onRequest(m: MiddlewareFn): void;
|
|
78
|
+
private resultMiddleware;
|
|
79
|
+
/**
|
|
80
|
+
* Registers middleware which runs after the response is generated, but before
|
|
81
|
+
* it is returned to the client.
|
|
82
|
+
*
|
|
83
|
+
* Optionally, return a new HttpResult to short-circuit the response.
|
|
84
|
+
*
|
|
85
|
+
* Errors thrown in response middleware are caught and returned as a 500 response.
|
|
86
|
+
*
|
|
87
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
88
|
+
*
|
|
89
|
+
* @param m - The middleware function to register.
|
|
90
|
+
*/
|
|
91
|
+
onResult(m: ResultMiddlewareFn): void;
|
|
92
|
+
private namespaceMiddleware;
|
|
93
|
+
/**
|
|
94
|
+
* Registers middleware for a specific namespace (being, a model or service)
|
|
95
|
+
*
|
|
96
|
+
* Runs before request validation and method middleware.
|
|
97
|
+
*
|
|
98
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
99
|
+
*
|
|
100
|
+
* @typeParam T - The namespace type
|
|
101
|
+
* @param ctor - The namespace's constructor (used to derive its name).
|
|
102
|
+
* @param m - The middleware function to register.
|
|
103
|
+
*/
|
|
104
|
+
onNamespace<T>(ctor: new () => T, m: MiddlewareFn): void;
|
|
105
|
+
private methodMiddleware;
|
|
106
|
+
/**
|
|
107
|
+
* Registers middleware for a specific method on a namespace
|
|
108
|
+
*
|
|
109
|
+
* Runs after namespace middleware and request validation.
|
|
110
|
+
*
|
|
111
|
+
* TODO: Middleware may violate the API contract and return unexpected types
|
|
112
|
+
*
|
|
113
|
+
* @typeParam T - The namespace type
|
|
114
|
+
* @param ctor - The namespace constructor
|
|
115
|
+
* @param method - The method name on the namespace.
|
|
116
|
+
* @param m - The middleware function to register.
|
|
117
|
+
*/
|
|
118
|
+
onMethod<T>(
|
|
119
|
+
ctor: new () => T,
|
|
120
|
+
method: KeysOfType<T, (...args: any) => any>,
|
|
121
|
+
m: MiddlewareFn,
|
|
122
|
+
): void;
|
|
123
|
+
private router;
|
|
124
|
+
/**
|
|
125
|
+
* Runs the Cloesce app. Intended to be called from the generated workers code.
|
|
126
|
+
*/
|
|
127
|
+
run(
|
|
128
|
+
request: Request,
|
|
129
|
+
env: any,
|
|
130
|
+
ast: CloesceAst,
|
|
131
|
+
ctorReg: ConstructorRegistry,
|
|
132
|
+
): Promise<Response>;
|
|
115
133
|
}
|
|
116
134
|
export type MatchedRoute = {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
135
|
+
kind: "model" | "service";
|
|
136
|
+
namespace: string;
|
|
137
|
+
method: ApiMethod;
|
|
138
|
+
id: string | null;
|
|
139
|
+
model?: Model;
|
|
140
|
+
service?: Service;
|
|
123
141
|
};
|
|
124
142
|
/**
|
|
125
143
|
* Matches a request to an ApiInvocation
|
|
126
144
|
* @param apiRoute The route from the domain to the actual API, ie https://foo.com/route/to/api => route/to/api/
|
|
127
145
|
* @returns 404 or a matched route.
|
|
128
146
|
*/
|
|
129
|
-
declare function matchRoute(
|
|
147
|
+
declare function matchRoute(
|
|
148
|
+
request: Request,
|
|
149
|
+
ast: CloesceAst,
|
|
150
|
+
routePrefix: string,
|
|
151
|
+
): Either<HttpResult, MatchedRoute>;
|
|
130
152
|
/**
|
|
131
153
|
* Validates the request's body/search params against a ModelMethod
|
|
132
154
|
* @returns 400 or a `RequestParamMap` consisting of each parameters name mapped to its value, and
|
|
133
155
|
* a data source
|
|
134
156
|
*/
|
|
135
|
-
declare function validateRequest(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
157
|
+
declare function validateRequest(
|
|
158
|
+
request: Request,
|
|
159
|
+
ast: CloesceAst,
|
|
160
|
+
ctorReg: ConstructorRegistry,
|
|
161
|
+
route: MatchedRoute,
|
|
162
|
+
): Promise<
|
|
163
|
+
Either<
|
|
164
|
+
HttpResult,
|
|
165
|
+
{
|
|
166
|
+
params: RequestParamMap;
|
|
167
|
+
dataSource: string | null;
|
|
168
|
+
}
|
|
169
|
+
>
|
|
170
|
+
>;
|
|
139
171
|
/**
|
|
140
172
|
* Calls a method on a model given a list of parameters.
|
|
141
173
|
* @returns 500 on an uncaught client error, 200 with a result body on success
|
|
142
174
|
*/
|
|
143
|
-
declare function methodDispatch(
|
|
175
|
+
declare function methodDispatch(
|
|
176
|
+
obj: any,
|
|
177
|
+
di: DependencyContainer,
|
|
178
|
+
route: MatchedRoute,
|
|
179
|
+
params: Record<string, unknown>,
|
|
180
|
+
): Promise<HttpResult<unknown>>;
|
|
144
181
|
/**
|
|
145
182
|
* For testing purposes
|
|
146
183
|
*/
|
|
147
184
|
export declare const _cloesceInternal: {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
185
|
+
matchRoute: typeof matchRoute;
|
|
186
|
+
validateRequest: typeof validateRequest;
|
|
187
|
+
methodDispatch: typeof methodDispatch;
|
|
188
|
+
RuntimeContainer: typeof RuntimeContainer;
|
|
152
189
|
};
|
|
153
190
|
export {};
|
|
154
|
-
//# sourceMappingURL=router.d.ts.map
|
|
191
|
+
//# sourceMappingURL=router.d.ts.map
|