@afora/fastify 0.0.11 → 0.0.13
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 +16 -6
- package/dist/index.js +16 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -68,7 +68,7 @@ const app = Api.build(); // a FastifyInstance
|
|
|
68
68
|
await app.listen({ port: 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,17 +104,27 @@ 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).
|
|
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
|
-
- **The body.** The routes are registered in an encapsulated plugin whose content type parser keeps the body as text, so an invalid JSON body is a problem like the others, not Fastify's own error.
|
|
112
|
+
- **The body.** The routes are registered in an encapsulated plugin whose content type parser keeps the body as text, so an invalid JSON body is a problem like the others, not Fastify's own error. A `multipart/form-data` body is kept as its bytes and read with the platform `Request.formData()` (no multipart dependency), only by routes whose input holds a file (the others read it as text: the 400 of a body that is not JSON); Fastify's `bodyLimit` (1 MiB by default) bounds it.
|
|
113
113
|
|
|
114
114
|
## Middlewares and your own app
|
|
115
115
|
|
|
116
|
-
<!-- docs:code
|
|
116
|
+
<!-- docs:code context
|
|
117
|
+
import { fastify } from "@afora/fastify";
|
|
118
|
+
|
|
119
|
+
declare function tenantOf(host: string | undefined): string; // your code
|
|
120
|
+
-->
|
|
117
121
|
```ts
|
|
122
|
+
declare module "fastify" {
|
|
123
|
+
interface FastifyRequest {
|
|
124
|
+
tenant?: string; // set by the middleware, read by context.variables
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
fastify(
|
|
119
129
|
{ context: { variables: { tenant: "tenant" } } },
|
|
120
130
|
{
|
|
@@ -141,7 +151,7 @@ export { stages } from "@afora/fastify";
|
|
|
141
151
|
```
|
|
142
152
|
|
|
143
153
|
```bash
|
|
144
|
-
bunx afora-codegen generate --stages ./stages.ts
|
|
154
|
+
bunx afora-codegen generate --stages ./stages.ts # npx on Node
|
|
145
155
|
```
|
|
146
156
|
|
|
147
157
|
## Testing a sink
|
package/dist/index.js
CHANGED
|
@@ -40,7 +40,21 @@ class FastifyHttpRequest {
|
|
|
40
40
|
return first(this.native.headers[name]);
|
|
41
41
|
}
|
|
42
42
|
text() {
|
|
43
|
-
|
|
43
|
+
const { body } = this.native;
|
|
44
|
+
if (body instanceof Uint8Array)
|
|
45
|
+
return new TextDecoder().decode(body);
|
|
46
|
+
return typeof body === "string" ? body : "";
|
|
47
|
+
}
|
|
48
|
+
form() {
|
|
49
|
+
const { body, headers } = this.native;
|
|
50
|
+
if (!(body instanceof Uint8Array)) {
|
|
51
|
+
return Promise.reject(new TypeError("The body is not multipart/form-data."));
|
|
52
|
+
}
|
|
53
|
+
return new Request("http://fastify.local/", {
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: { "content-type": first(headers["content-type"]) ?? "" },
|
|
56
|
+
body: new Uint8Array(body)
|
|
57
|
+
}).formData();
|
|
44
58
|
}
|
|
45
59
|
variable(name) {
|
|
46
60
|
return this.native[name];
|
|
@@ -72,6 +86,7 @@ var fastify = sinkOf({
|
|
|
72
86
|
app.register(async (scope) => {
|
|
73
87
|
scope.removeAllContentTypeParsers();
|
|
74
88
|
scope.addContentTypeParser("*", { parseAs: "string" }, (_request, body, done) => done(null, body));
|
|
89
|
+
scope.addContentTypeParser("multipart/form-data", { parseAs: "buffer" }, (_request, body, done) => done(null, body));
|
|
75
90
|
for (const middleware of hooks?.middlewares ?? []) {
|
|
76
91
|
scope.addHook("onRequest", middleware);
|
|
77
92
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@afora/fastify",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "A Fastify 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",
|