@afora/express 0.0.12 → 27.0.0-alpha.17
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/README.md +9 -5
- package/dist/index.d.ts +8 -1
- package/dist/index.js +38 -10
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -68,7 +68,7 @@ const app = Api.build(); // an Express app
|
|
|
68
68
|
app.listen(3000);
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
```
|
|
71
|
+
```text
|
|
72
72
|
GET /api/posts/hello -> 200 { "post": { "slug": "hello", ... } }
|
|
73
73
|
GET /api/posts/Not%20A%20Slug -> 400 application/problem+json, with "issues"
|
|
74
74
|
GET /openapi.json -> the OpenAPI 3.1 document of the same routes
|
|
@@ -104,16 +104,20 @@ Invalid options throw `InvalidOutputStage` when the stage is made, and unknown o
|
|
|
104
104
|
|
|
105
105
|
The requests are handled by the route table of the core (`compileRoutes` of `@afora/core/adapters`), shared by every afora sink that serves HTTP, so they all answer the same way. This package only routes and sends. The behavior is described once, in [Outputs](../../docs/outputs.md); in short:
|
|
106
106
|
|
|
107
|
-
- **Input.** `post`, `put` and `patch` read a JSON body, `get` and `delete` the query string. Over it come the keys the [route of the use case](../../docs/use-cases.md#routes) places: `{name}` segments of the path, and the query keys, headers and cookies it declares. Text is converted to the numbers and booleans the input takes, and the blueprint of the use case validates it: a 400 problem with `issues`.
|
|
107
|
+
- **Input.** `post`, `put` and `patch` read a JSON body, `get` and `delete` the query string (a `delete` with `body: true` in its route reads a JSON body). Over it come the keys the [route of the use case](../../docs/use-cases.md#routes) places: `{name}` segments of the path, and the query keys, headers and cookies it declares. Text is converted to the numbers and booleans the input takes, and the blueprint of the use case validates it: a 400 problem with `issues`. An input that holds a file (`ctx.fileOf`) is read from a `multipart/form-data` body when one is sent: see [Uploads](../../docs/outputs.md). A `multipart/form-data` body is read from the stream with the platform `Request.formData()` (or from the bytes an `express.raw()` parser kept); a parser that already took it (multer, `express.text()`) leaves nothing to read: the route answers 500 `MultipartBodyAlreadyRead` (exported), a misconfiguration to fix by mounting the afora routes before that parser or keeping the bytes with `express.raw()`.
|
|
108
108
|
- **Access.** An operation whose use case declares an [`access`](../../docs/auth.md) asks the `authenticator` hook (or `auth`) who calls, and answers a 401 or a 403 problem before the input is validated. With neither, such an operation always answers 401.
|
|
109
109
|
- **Success.** The status of the route (`200` by default) with the response as JSON; records go through `toJSON`, so sensitive values are masked. A `204` has no body. The keys a route sends with `setCookie` leave the body and become `set-cookie` headers.
|
|
110
|
-
- **Errors.** `application/problem+json`, with the status of `statusOf`: the `statuses` of the output first, then 400, 401, 403, 404, 409, 422 for a declared error, else 500
|
|
110
|
+
- **Errors.** `application/problem+json`, with the status of `statusOf`: the `statuses` of the output first, then 400, 401, 403, 404, 409, 422 for a declared error, else 500. The detail of a 5xx is `"Internal error."` unless `debug` is on or the output declares the exception (its name in `statuses`, or one of the use case `errors`).
|
|
111
111
|
- **Unknown routes** are a 404 problem and anything thrown outside an operation is a problem too, unless you gave the `app`.
|
|
112
112
|
- **The body.** The body is read from the request as text and parsed by the route table, so the sink needs no `express.json()`. Behind one (on an `app` of yours), the body it parsed is used.
|
|
113
113
|
|
|
114
114
|
## Middlewares and your own app
|
|
115
115
|
|
|
116
|
-
<!-- docs:code
|
|
116
|
+
<!-- docs:code context
|
|
117
|
+
import { express } from "@afora/express";
|
|
118
|
+
|
|
119
|
+
declare function tenantOf(host: string | undefined): string; // your code
|
|
120
|
+
-->
|
|
117
121
|
```ts
|
|
118
122
|
import cors from "cors";
|
|
119
123
|
|
|
@@ -145,7 +149,7 @@ export { stages } from "@afora/express";
|
|
|
145
149
|
```
|
|
146
150
|
|
|
147
151
|
```bash
|
|
148
|
-
bunx afora-codegen generate --stages ./stages.ts
|
|
152
|
+
bunx afora-codegen generate --stages ./stages.ts # npx on Node
|
|
149
153
|
```
|
|
150
154
|
|
|
151
155
|
## Testing a sink
|
package/dist/index.d.ts
CHANGED
|
@@ -29,6 +29,13 @@ export interface ExpressHooks {
|
|
|
29
29
|
*/
|
|
30
30
|
readonly app?: Express;
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* A `multipart/form-data` body that a parser of the app (multer,
|
|
34
|
+
* `express.text()`...) read before the route: there is nothing left to read,
|
|
35
|
+
* so the server is misconfigured (a 500). Mount the afora routes before
|
|
36
|
+
* that parser, or keep the bytes with `express.raw()`.
|
|
37
|
+
*/
|
|
38
|
+
export declare const MultipartBodyAlreadyRead: import("@afora/core").DeclaredExceptionClass<undefined>;
|
|
32
39
|
/**
|
|
33
40
|
* An Express app that serves the operations of an output: the route each use
|
|
34
41
|
* case declares (`route` or `@Route`), else
|
|
@@ -46,7 +53,7 @@ export interface ExpressHooks {
|
|
|
46
53
|
*/
|
|
47
54
|
export declare const express: import("@afora/core/adapters").SinkFactory<Express, HttpOptions, ExpressHooks>;
|
|
48
55
|
/** The runtime catalog of this package: the stage name `express` resolves to the sink. */
|
|
49
|
-
export declare const ExpressStages: import("
|
|
56
|
+
export declare const ExpressStages: import("@afora/core").IStageCatalog<Readonly<Record<string, import("@afora/core").StageFactory>>>;
|
|
50
57
|
/** The catalog entries codegen reads (`--stages`): `{ name, module, export, kind }`. */
|
|
51
58
|
export declare const stages: readonly [{
|
|
52
59
|
readonly name: "express";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { ExceptionLayers, exceptionOf } from "@afora/core";
|
|
2
3
|
import {
|
|
3
4
|
compileRoutes,
|
|
4
5
|
HTTP_OPTION_KEYS,
|
|
@@ -20,15 +21,7 @@ function first(value) {
|
|
|
20
21
|
return first(value[0]);
|
|
21
22
|
return typeof value === "string" ? value : undefined;
|
|
22
23
|
}
|
|
23
|
-
function
|
|
24
|
-
const parsed = request.body;
|
|
25
|
-
if (typeof parsed === "string")
|
|
26
|
-
return parsed;
|
|
27
|
-
if (parsed instanceof Uint8Array)
|
|
28
|
-
return new TextDecoder().decode(parsed);
|
|
29
|
-
if (parsed !== undefined && parsed !== null && request.readableEnded) {
|
|
30
|
-
return JSON.stringify(parsed);
|
|
31
|
-
}
|
|
24
|
+
function bytesOf(request) {
|
|
32
25
|
return new Promise((resolve, reject) => {
|
|
33
26
|
const chunks = [];
|
|
34
27
|
request.on("data", (chunk) => chunks.push(chunk));
|
|
@@ -42,11 +35,42 @@ function textOf(request) {
|
|
|
42
35
|
bytes.set(chunk, at);
|
|
43
36
|
at += chunk.byteLength;
|
|
44
37
|
}
|
|
45
|
-
resolve(
|
|
38
|
+
resolve(bytes);
|
|
46
39
|
});
|
|
47
40
|
request.on("error", reject);
|
|
48
41
|
});
|
|
49
42
|
}
|
|
43
|
+
function textOf(request) {
|
|
44
|
+
const parsed = request.body;
|
|
45
|
+
if (typeof parsed === "string")
|
|
46
|
+
return parsed;
|
|
47
|
+
if (parsed instanceof Uint8Array)
|
|
48
|
+
return new TextDecoder().decode(parsed);
|
|
49
|
+
if (parsed !== undefined && parsed !== null && request.readableEnded) {
|
|
50
|
+
return JSON.stringify(parsed);
|
|
51
|
+
}
|
|
52
|
+
return bytesOf(request).then((bytes) => new TextDecoder().decode(bytes));
|
|
53
|
+
}
|
|
54
|
+
var MultipartBodyAlreadyRead = exceptionOf({
|
|
55
|
+
layer: ExceptionLayers.Output,
|
|
56
|
+
name: "MultipartBodyAlreadyRead",
|
|
57
|
+
message: "A parser of the app read the multipart/form-data body before the afora route: mount the afora routes before it (multer, express.text()...), or keep the bytes with express.raw()."
|
|
58
|
+
});
|
|
59
|
+
async function formOf(request) {
|
|
60
|
+
const parsed = request.body;
|
|
61
|
+
let bytes;
|
|
62
|
+
if (parsed instanceof Uint8Array)
|
|
63
|
+
bytes = parsed;
|
|
64
|
+
else if (request.readableEnded || parsed !== undefined && parsed !== null) {
|
|
65
|
+
throw new MultipartBodyAlreadyRead({ source: "express" });
|
|
66
|
+
} else
|
|
67
|
+
bytes = await bytesOf(request);
|
|
68
|
+
return new globalThis.Request("http://express.local/", {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: { "content-type": first(request.headers["content-type"]) ?? "" },
|
|
71
|
+
body: new Uint8Array(bytes)
|
|
72
|
+
}).formData();
|
|
73
|
+
}
|
|
50
74
|
|
|
51
75
|
class ExpressHttpRequest {
|
|
52
76
|
native;
|
|
@@ -71,6 +95,9 @@ class ExpressHttpRequest {
|
|
|
71
95
|
text() {
|
|
72
96
|
return textOf(this.native);
|
|
73
97
|
}
|
|
98
|
+
form() {
|
|
99
|
+
return formOf(this.native);
|
|
100
|
+
}
|
|
74
101
|
variable(name) {
|
|
75
102
|
return Object.hasOwn(this.response.locals, name) ? this.response.locals[name] : this.native[name];
|
|
76
103
|
}
|
|
@@ -138,6 +165,7 @@ var stages = [
|
|
|
138
165
|
];
|
|
139
166
|
export {
|
|
140
167
|
ExpressStages,
|
|
168
|
+
MultipartBodyAlreadyRead,
|
|
141
169
|
express,
|
|
142
170
|
stages
|
|
143
171
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@afora/express",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "27.0.0-alpha.17",
|
|
4
4
|
"description": "An Express sink for afora outputs: one route per operation, problem+json errors, sensitive data masked, declared access enforced and an optional OpenAPI document.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"afora",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@types/express": "^5.0.6"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
|
-
"@afora/core": ">=0.0.
|
|
56
|
+
"@afora/core": ">=27.0.0-alpha.17",
|
|
57
57
|
"express": "^5.3.0",
|
|
58
58
|
"typebox": "^1"
|
|
59
59
|
}
|