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