@softistx/alxia-openapi 0.0.0-stage → 0.7.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/LICENSE +21 -0
- package/README.md +268 -2
- package/dist/api-docs-spec.d.ts +20 -0
- package/dist/api-docs-spec.d.ts.map +1 -0
- package/dist/api-docs-ui.d.ts +22 -0
- package/dist/api-docs-ui.d.ts.map +1 -0
- package/dist/api-docs.d.ts +34 -0
- package/dist/api-docs.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +255 -0
- package/dist/index.js.map +13 -0
- package/dist/routes.d.ts +61 -0
- package/dist/routes.d.ts.map +1 -0
- package/docs/README.md +24 -0
- package/docs/guide/api-docs.md +200 -0
- package/docs/guide/checks.md +238 -0
- package/docs/guide/matching.md +59 -0
- package/docs/guide/spec-first.md +335 -0
- package/docs/guide/testing.md +90 -0
- package/docs/roadmap.md +101 -0
- package/docs/troubleshooting.md +494 -0
- package/package.json +52 -5
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
# Spec first
|
|
2
|
+
|
|
3
|
+
This page takes an API from its OpenAPI document to routes that serve it,
|
|
4
|
+
a test that holds them to it, and a client generated from the same file.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { alxia } from '@softistx/alxia';
|
|
8
|
+
import { operations } from './generated/alxia'; // generated from openapi.yaml
|
|
9
|
+
|
|
10
|
+
export const app = alxia().route(operations.getTodo, ({ params, reply }) =>
|
|
11
|
+
params.id === 1 // a number: the spec types the path parameter
|
|
12
|
+
? reply.ok({ id: 1, title: 'Write the spec', done: true })
|
|
13
|
+
: reply.notFound({ error: 'not_found' as const }),
|
|
14
|
+
);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
| Step | Who | What it gives |
|
|
18
|
+
| --- | --- | --- |
|
|
19
|
+
| [1. Write the document](#1-write-the-document) | you | the contract: operations, bodies, replies, alxia's 400 |
|
|
20
|
+
| [2. Generate](#2-generate-the-operations) | `@nxgt/openapi-codegen` 0.11.0, `alxia: true` | `src/generated/alxia.ts`: each operation as `{ method, path, schema }` |
|
|
21
|
+
| [3. Bind the routes](#3-bind-the-routes) | `@softistx/alxia`'s `app.route()` | the handlers, typed by the spec, with their middlewares |
|
|
22
|
+
| [4. Check](#4-check-the-app-against-the-spec) | `@softistx/alxia-openapi`'s `matchesSpec` | a failing test while an operation has no route |
|
|
23
|
+
| [5. A client](#5-a-client-from-the-same-document) | `@nxgt/openapi-httpyz`, or any OpenAPI client | the other side of the contract |
|
|
24
|
+
|
|
25
|
+
`bunx @softistx/alxia-create my-api --template api` does the first four, and its
|
|
26
|
+
generated `operations.ts` is ready for step 5; this page is what that project
|
|
27
|
+
holds, explained.
|
|
28
|
+
|
|
29
|
+
## 1. Write the document
|
|
30
|
+
|
|
31
|
+
OpenAPI 3.1 or 3.2, YAML or JSON. Give each operation an `operationId`: it
|
|
32
|
+
names the generated constant.
|
|
33
|
+
|
|
34
|
+
```yaml
|
|
35
|
+
# openapi.yaml (excerpt)
|
|
36
|
+
paths:
|
|
37
|
+
/todos/{id}:
|
|
38
|
+
get:
|
|
39
|
+
operationId: getTodo
|
|
40
|
+
parameters:
|
|
41
|
+
- { name: id, in: path, required: true, schema: { type: integer, minimum: 1 } }
|
|
42
|
+
responses:
|
|
43
|
+
"200":
|
|
44
|
+
description: The todo
|
|
45
|
+
content: { application/json: { schema: { $ref: "#/components/schemas/Todo" } } }
|
|
46
|
+
"400": { $ref: "#/components/responses/ValidationError" }
|
|
47
|
+
"404":
|
|
48
|
+
description: No todo has this id
|
|
49
|
+
content: { application/json: { schema: { $ref: "#/components/schemas/NotFound" } } }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Declare alxia's own 400
|
|
53
|
+
|
|
54
|
+
A request the schemas refuse is answered by alxia, not by your handler, with
|
|
55
|
+
`400 { error: 'validation', issues }`. Declare that body once and refer to
|
|
56
|
+
it from each operation that takes a parameter or a body, so a client
|
|
57
|
+
generated from the document knows it:
|
|
58
|
+
|
|
59
|
+
```yaml
|
|
60
|
+
components:
|
|
61
|
+
responses:
|
|
62
|
+
ValidationError:
|
|
63
|
+
description: alxia's own 400, for a request the schemas refuse
|
|
64
|
+
content: { application/json: { schema: { $ref: "#/components/schemas/ValidationError" } } }
|
|
65
|
+
schemas:
|
|
66
|
+
ValidationError:
|
|
67
|
+
type: object
|
|
68
|
+
required: [error, issues]
|
|
69
|
+
properties:
|
|
70
|
+
error: { type: string, enum: [validation] }
|
|
71
|
+
issues: { type: array, items: { $ref: "#/components/schemas/ValidationIssue" } }
|
|
72
|
+
ValidationIssue:
|
|
73
|
+
type: object
|
|
74
|
+
required: [target, path, code, message]
|
|
75
|
+
properties:
|
|
76
|
+
target: { type: string, enum: [params, query, headers, cookies, body] }
|
|
77
|
+
path: { type: array, items: { anyOf: [{ type: string }, { type: integer }] } }
|
|
78
|
+
code: { type: string }
|
|
79
|
+
message: { type: string }
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
An app that answers refusals in its own format, with a middleware that
|
|
83
|
+
catches them around `await next()`, declares that format instead:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { alxia, problem, refusalOf } from '@softistx/alxia';
|
|
87
|
+
|
|
88
|
+
const app = alxia().use(async (_ctx, next) => {
|
|
89
|
+
try {
|
|
90
|
+
return await next();
|
|
91
|
+
} catch (error) {
|
|
92
|
+
const refusal = refusalOf(error);
|
|
93
|
+
if (refusal === undefined) throw error;
|
|
94
|
+
return problem({ status: 400, detail: `the ${refusal.part} is invalid` });
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 2. Generate the operations
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
// openapi-codegen.config.ts
|
|
103
|
+
import { defineConfig } from '@nxgt/openapi-codegen';
|
|
104
|
+
|
|
105
|
+
export default defineConfig({
|
|
106
|
+
input: 'openapi.yaml',
|
|
107
|
+
output: 'src/generated',
|
|
108
|
+
alxia: true,
|
|
109
|
+
validationErrors: false,
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
| Option | Type | Default | Effect |
|
|
114
|
+
| --- | --- | --- | --- |
|
|
115
|
+
| `input` | `string` | — | the document, relative to the config file |
|
|
116
|
+
| `output` | `string` | `'generated/openapi'` | the folder the files are written to |
|
|
117
|
+
| `alxia` | `boolean` | `false` | also writes `alxia.ts`, the operations `app.route()` takes |
|
|
118
|
+
| `validationErrors` | `boolean` | `true` | `true` declares `@nxgt/openapi-hono`'s 400, `{ status, message, timestamp, issues }`, in `types.ts`, `zod.ts`, `operations.ts` and `paths.ts`. alxia never sends that body, so a client generated from them expects the wrong 400. `false` keeps the spec's own 400, the one declared above |
|
|
119
|
+
|
|
120
|
+
`alxia.ts` never carries the `validationErrors` 400, whichever way it is
|
|
121
|
+
set; the client-facing files do. Every other option is in the generator's
|
|
122
|
+
[options guide](https://github.com/softistx/nxgt-http/blob/develop/packages/openapi-codegen/docs/guide/options.md).
|
|
123
|
+
|
|
124
|
+
`bunx nxgt-openapi generate` writes `types.ts`, `zod.ts`, `operations.ts`, `paths.ts`
|
|
125
|
+
and `alxia.ts`. `alxia.ts` holds one `as const` constant per operation,
|
|
126
|
+
named by its `operationId`, then `operations`, all of them:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
// src/generated/alxia.ts (excerpt). Generated: do not edit.
|
|
130
|
+
export const getTodo = {
|
|
131
|
+
method: 'GET',
|
|
132
|
+
path: '/todos/:id', // '/todos/{id}' in the spec
|
|
133
|
+
schema: {
|
|
134
|
+
params: z.object({ id: numeric.pipe(z.int().min(1)) }), // read from the path's text
|
|
135
|
+
response: { 200: zTodo, 400: zValidationError, 404: zNotFound },
|
|
136
|
+
detail: { operationId: 'getTodo', summary: 'One todo' },
|
|
137
|
+
},
|
|
138
|
+
} as const;
|
|
139
|
+
|
|
140
|
+
export const operations = { listTodos, createTodo, getTodo } as const;
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`params`, `query`, `headers` and `body` are the request's schemas,
|
|
144
|
+
`response` a schema per status, `detail` the operation's `operationId`,
|
|
145
|
+
`summary`, `tags` and `deprecated`. The generator's
|
|
146
|
+
[`alxia.ts` page](https://github.com/softistx/nxgt-http/blob/develop/packages/openapi-codegen/docs/guide/generated-code.md#alxiats)
|
|
147
|
+
details each part.
|
|
148
|
+
|
|
149
|
+
### Commit `src/generated/`, and check it in CI
|
|
150
|
+
|
|
151
|
+
Commit the generated files: the project then builds with no generation
|
|
152
|
+
step, offline and in Docker. `--check` writes nothing and exits 1 when the
|
|
153
|
+
files are not what the document gives, so drift fails CI:
|
|
154
|
+
|
|
155
|
+
```json
|
|
156
|
+
{
|
|
157
|
+
"scripts": {
|
|
158
|
+
"generate": "nxgt-openapi generate",
|
|
159
|
+
"verify": "bun run generate --check && bun run typecheck && bun test"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Pin `@nxgt/openapi-codegen` exactly (`bun add -d --exact @nxgt/openapi-codegen`):
|
|
165
|
+
another release may write the files differently, and `--check` would then
|
|
166
|
+
fail until you generate again. Keep your linter and formatter off
|
|
167
|
+
`src/generated/`.
|
|
168
|
+
|
|
169
|
+
## 3. Bind the routes
|
|
170
|
+
|
|
171
|
+
`app.route(operation, ...middlewares, handler)` declares the route the
|
|
172
|
+
operation describes. The handler's `params`, `query`, `headers`, `body` and
|
|
173
|
+
`reply` are typed by the spec; a status the spec does not declare does not
|
|
174
|
+
compile.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
// src/app.ts
|
|
178
|
+
import { alxia, defineMiddleware } from '@softistx/alxia';
|
|
179
|
+
import { operations } from './generated/alxia';
|
|
180
|
+
import type { Todo } from './generated/types';
|
|
181
|
+
|
|
182
|
+
const apiKey = Bun.env['API_KEY'] ?? 'dev-key';
|
|
183
|
+
|
|
184
|
+
// The spec's `security` is not generated: authentication is a middleware.
|
|
185
|
+
const requireKey = defineMiddleware(({ request, reply }, next) =>
|
|
186
|
+
request.headers.get('x-api-key') === apiKey
|
|
187
|
+
? next()
|
|
188
|
+
: reply(401, { error: 'unauthorized' as const }),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const todos: Todo[] = [];
|
|
192
|
+
|
|
193
|
+
export const app = alxia()
|
|
194
|
+
.route(operations.listTodos, ({ reply }) => reply.ok(todos))
|
|
195
|
+
.route(operations.createTodo, requireKey, ({ body, reply }) => {
|
|
196
|
+
const todo = { id: todos.length + 1, title: body.title, done: false };
|
|
197
|
+
todos.push(todo);
|
|
198
|
+
return reply.created(todo);
|
|
199
|
+
})
|
|
200
|
+
.route(operations.getTodo, ({ params, reply }) => {
|
|
201
|
+
const todo = todos.find(({ id }) => id === params.id);
|
|
202
|
+
return todo ? reply.ok(todo) : reply.notFound({ error: 'not_found' as const });
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
The operation's schema runs as two middlewares the route adds itself, just
|
|
207
|
+
before the handler:
|
|
208
|
+
|
|
209
|
+
- **a `validate` of its request.** So `requireKey` answers a stranger 401
|
|
210
|
+
before his body is read. Place `validate(operation)` among the
|
|
211
|
+
middlewares to validate earlier; the route then runs no other.
|
|
212
|
+
- **a `responds` of its responses, after it.** The handler's reply is
|
|
213
|
+
checked against the schema of its status, which must be one the
|
|
214
|
+
operation declares; a body the schema refuses is a 500. A middleware's
|
|
215
|
+
own reply, `requireKey`'s 401 above, is sent as it is. Place
|
|
216
|
+
`responds(operation)` among the middlewares to check the replies of
|
|
217
|
+
those after it too, `requireKey`'s against the spec's `Unauthorized`;
|
|
218
|
+
the route then runs no other.
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
import { alxia, responds, validate } from '@softistx/alxia';
|
|
222
|
+
|
|
223
|
+
// requireKey's 401 is checked against the spec's Unauthorized
|
|
224
|
+
const checked = alxia().route(
|
|
225
|
+
operations.createTodo,
|
|
226
|
+
responds(operations.createTodo),
|
|
227
|
+
requireKey,
|
|
228
|
+
({ body, reply }) => reply.created({ id: todos.length + 1, title: body.title, done: false }),
|
|
229
|
+
);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
// a bad body gets its 400 before anyone is asked for a key
|
|
234
|
+
const strict = alxia().route(
|
|
235
|
+
operations.createTodo,
|
|
236
|
+
validate(operations.createTodo),
|
|
237
|
+
requireKey,
|
|
238
|
+
({ body, reply }) => reply.created({ id: todos.length + 1, title: body.title, done: false }),
|
|
239
|
+
);
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Middlewares before the `validate` read `params` as strings and `body` as
|
|
243
|
+
`undefined`. The core's
|
|
244
|
+
[routes as data](https://github.com/softistx/alxia/blob/develop/packages/core/docs/guide/routes.md#routes-as-data-route)
|
|
245
|
+
and [middleware](https://github.com/softistx/alxia/blob/develop/packages/core/docs/guide/middleware.md#where-validate-stands)
|
|
246
|
+
guides have the detail.
|
|
247
|
+
|
|
248
|
+
## 4. Check the app against the spec
|
|
249
|
+
|
|
250
|
+
Nothing above notices an operation with no route: the app compiles, and the
|
|
251
|
+
request gets a 404. `matchesSpec` does, from the same `operations`. It does not mind a route the
|
|
252
|
+
document lacks (a health check, a proxied route); `strict: true` does:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
// src/app.spec.ts
|
|
256
|
+
import { expect, test } from 'bun:test';
|
|
257
|
+
import { matchesSpec } from '@softistx/alxia-openapi';
|
|
258
|
+
import { app } from './app';
|
|
259
|
+
import { operations } from './generated/alxia';
|
|
260
|
+
|
|
261
|
+
test('routes every operation of openapi.yaml', () => {
|
|
262
|
+
matchesSpec(app, operations);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test("answers alxia's 400, as the spec declares it", async () => {
|
|
266
|
+
const response = await app.request('/todos/first');
|
|
267
|
+
expect(response.status).toBe(400);
|
|
268
|
+
expect((await response.json()).error).toBe('validation');
|
|
269
|
+
});
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
```text
|
|
273
|
+
TypeError: matchesSpec(): 1 operation has no route: GET /todos/:id (getTodo)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
[The checks](checks.md) covers `implemented`, `exclude`, and
|
|
277
|
+
calling the check at startup. Prefixes are in [matching.md](matching.md#under-a-prefix).
|
|
278
|
+
|
|
279
|
+
## 5. A client from the same document
|
|
280
|
+
|
|
281
|
+
alxia generates no client: the document is the contract, and any OpenAPI
|
|
282
|
+
client works against it. The same run of `@nxgt/openapi-codegen` already
|
|
283
|
+
wrote `operations.ts`, the table [`@nxgt/openapi-httpyz`](https://www.npmjs.com/package/@nxgt/openapi-httpyz)
|
|
284
|
+
binds onto an [`@nxgt/httpyz`](https://www.npmjs.com/package/@nxgt/httpyz)
|
|
285
|
+
client, and `paths.ts`, in openapi-typescript's shape, for a client that
|
|
286
|
+
reads it:
|
|
287
|
+
|
|
288
|
+
```sh
|
|
289
|
+
bun add @nxgt/httpyz @nxgt/openapi-httpyz
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
import { createHttpClient } from '@nxgt/httpyz';
|
|
294
|
+
import { createOpenApiClient } from '@nxgt/openapi-httpyz';
|
|
295
|
+
import { operations } from './generated/operations';
|
|
296
|
+
|
|
297
|
+
const api = createOpenApiClient(
|
|
298
|
+
createHttpClient({ baseUrl: 'http://localhost:3000' }),
|
|
299
|
+
operations,
|
|
300
|
+
);
|
|
301
|
+
const reply = await api.get('/todos/{id}', { param: { id: 1 } });
|
|
302
|
+
// reply.status: 200 | 400 | 404, and reply.data narrowed by it
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
With `validationErrors: false`, the 400 a client is typed by is the one the
|
|
306
|
+
spec declares, the one alxia sends. In another project, generate from the
|
|
307
|
+
same `openapi.yaml`. A test calls the app through the same client, in
|
|
308
|
+
process: [Testing with the generated client](testing.md).
|
|
309
|
+
|
|
310
|
+
## What the generator leaves out (0.11.0)
|
|
311
|
+
|
|
312
|
+
| In the document | What happens | Do instead |
|
|
313
|
+
| --- | --- | --- |
|
|
314
|
+
| `security` | not generated | a middleware, as `requireKey` above |
|
|
315
|
+
| a `cookie` parameter in the client files | left out of `types.ts`, `zod.ts`, `operations.ts` and `paths.ts`, with an `ignored` warning: only `alxia.ts` validates it, as `cookies` | nothing: read `cookies` in the handler or a middleware |
|
|
316
|
+
| server-sent events whose data is text, or with no `itemSchema` naming them | the operation is left out of `alxia.ts`, with an `ignored` warning | give each event's data a JSON `contentSchema`, or declare the route by hand, with `eventStream({ ... })` from `@softistx/alxia` |
|
|
317
|
+
| a `readOnly` property in a request body, a `writeOnly` one in a reply (`readOnly` and `writeOnly` default to `'omit'` since 0.11) | not part of what the operation validates: the body is generated as `<Name>Write`, the reply as `<Name>Read`, so a client need not send `id` nor a server a password | nothing; pass `readOnly: 'keep'` or `writeOnly: 'keep'` to validate every property as written |
|
|
318
|
+
| a `TRACE`, a binary body, a binary, JSON Lines or form reply, a path alxia cannot route | left out, with an `ignored` warning | declare the route by hand, if you serve it |
|
|
319
|
+
|
|
320
|
+
An operation left out is not in `operations`, so the checks do not know it:
|
|
321
|
+
read the generator's warnings. Each message is in
|
|
322
|
+
[Troubleshooting](../troubleshooting.md#generator).
|
|
323
|
+
|
|
324
|
+
## The types
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
// @softistx/alxia: what alxia.ts holds, and app.route() takes, with at most 8 middlewares
|
|
328
|
+
interface RouteOperation {
|
|
329
|
+
readonly method: Method; // 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'QUERY'
|
|
330
|
+
readonly path: RoutePath; // `/${string}`, with :name parameters
|
|
331
|
+
readonly schema?: RouteSchema; // params, query, headers, cookies, body, response, detail
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
The signatures of the checks are in [The checks](checks.md#the-signatures).
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Testing with the generated client
|
|
2
|
+
|
|
3
|
+
The same run of `@nxgt/openapi-codegen` that writes `alxia.ts` writes
|
|
4
|
+
`operations.ts`, the table [`@nxgt/openapi-httpyz`](https://www.npmjs.com/package/@nxgt/openapi-httpyz)
|
|
5
|
+
binds onto an [`@nxgt/httpyz`](https://www.npmjs.com/package/@nxgt/httpyz)
|
|
6
|
+
client. The client takes a `fetch`: give it `app.fetch`, and a test calls the
|
|
7
|
+
app in process, with no server and no port, and every path, parameter, body
|
|
8
|
+
and reply typed by `openapi.yaml`. Any OpenAPI client works the same way,
|
|
9
|
+
since the document is the contract.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bun add -d @nxgt/httpyz @nxgt/openapi-httpyz
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { expect, test } from 'bun:test';
|
|
17
|
+
import { createHttpClient } from '@nxgt/httpyz';
|
|
18
|
+
import { createOpenApiClient } from '@nxgt/openapi-httpyz';
|
|
19
|
+
import { app } from './app';
|
|
20
|
+
import { operations } from './generated/operations';
|
|
21
|
+
|
|
22
|
+
const http = createHttpClient({
|
|
23
|
+
baseUrl: 'http://alxia.test',
|
|
24
|
+
fetch: (request) => app.fetch(request),
|
|
25
|
+
});
|
|
26
|
+
const api = createOpenApiClient(http, operations);
|
|
27
|
+
|
|
28
|
+
test('creates a todo', async () => {
|
|
29
|
+
const reply = await api.op('createTodo', { json: { title: 'Write a route' } });
|
|
30
|
+
expect(reply.status).toBe(201);
|
|
31
|
+
if (reply.status === 201) expect(reply.data.title).toBe('Write a route');
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`baseUrl` only has to be a valid URL: nothing is sent to it. The client calls
|
|
36
|
+
`fetch` with a `Request`, which `app.fetch` takes as is.
|
|
37
|
+
|
|
38
|
+
## What the types catch
|
|
39
|
+
|
|
40
|
+
A test that no longer matches the document stops compiling, before it runs:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
await api.get('/todos/{id}', { param: { id: 'first' } }); // id is a number
|
|
44
|
+
await api.op('createTodo', { json: { name: 'x' } }); // no such field
|
|
45
|
+
await api.get('/todo'); // no such path
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
A reply is a union narrowed on its status: `reply.data` is the success body
|
|
49
|
+
when `reply.status` is 200 or 201, and `NotFound`, `Unauthorized` or alxia's
|
|
50
|
+
400 for those statuses. `reply.response` is the `Response`, for its headers.
|
|
51
|
+
A status the spec does not declare for the operation throws
|
|
52
|
+
`UndeclaredStatusError`.
|
|
53
|
+
|
|
54
|
+
## Headers the spec does not list
|
|
55
|
+
|
|
56
|
+
An API key declared under `security` is not a parameter, so pass it as a
|
|
57
|
+
header of the client, or of one call:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const http = createHttpClient({
|
|
61
|
+
baseUrl: 'http://alxia.test',
|
|
62
|
+
fetch: (request) => app.fetch(request),
|
|
63
|
+
headers: { 'x-api-key': 'test-key' },
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
await api.op('createTodo', { json: { title: 'x' } }, { headers: { 'x-api-key': 'wrong' } });
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Keep one `app.request` test
|
|
70
|
+
|
|
71
|
+
The client checks a request against the spec before it sends it, and throws
|
|
72
|
+
a `ValidationError` for one the server would refuse. To see the server
|
|
73
|
+
refuse it, bind a client with `createOpenApiClient(http, operations, {
|
|
74
|
+
validate: { request: false } })`. A request the types forbid, an id that is
|
|
75
|
+
not a number or a missing header, needs `app.request`, which sends what it
|
|
76
|
+
is told:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
test('refuses an id that is not a number', async () => {
|
|
80
|
+
expect((await app.request('/todos/first')).status).toBe(400);
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Use the client for the contract, and `app.request` for what breaks it.
|
|
85
|
+
|
|
86
|
+
## Beside `matchesSpec`
|
|
87
|
+
|
|
88
|
+
`matchesSpec(app, operations)` ([the checks](checks.md)) proves every
|
|
89
|
+
operation has a route (and, under `strict`, every route an operation), and the client proves a call to it answers as the
|
|
90
|
+
spec says. Run both.
|
package/docs/roadmap.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# Roadmap
|
|
2
|
+
|
|
3
|
+
What `@softistx/alxia-openapi` gives an app, and what is coming. This page is a
|
|
4
|
+
direction, not a commitment: the version something shipped in is the only
|
|
5
|
+
number on it. Every release, with each change it made, is in
|
|
6
|
+
[`CHANGELOG.md`](https://github.com/softistx/alxia/blob/develop/packages/openapi/CHANGELOG.md).
|
|
7
|
+
|
|
8
|
+
## Now
|
|
9
|
+
|
|
10
|
+
- **Moving to the `@softistx` scope.** The package is now `@softistx/alxia-openapi` (it was `@alxia/openapi`). Same code and API; replace the dependency and the imports ([migration guide](https://github.com/softistx/alxia/blob/develop/packages/core/docs/guide/migrating-from-alxia-scope.md)). The old name is deprecated.
|
|
11
|
+
|
|
12
|
+
## Next
|
|
13
|
+
|
|
14
|
+
- **A client from the document, in this package.** Today you bring your own
|
|
15
|
+
client: `@nxgt/openapi-httpyz` over the generated `operations.ts`, or any
|
|
16
|
+
OpenAPI client ([Spec first](guide/spec-first.md#5-a-client-from-the-same-document)).
|
|
17
|
+
A client generated here, from the same document, is the direction; it is
|
|
18
|
+
not designed yet.
|
|
19
|
+
|
|
20
|
+
## Later
|
|
21
|
+
|
|
22
|
+
Nothing scheduled yet.
|
|
23
|
+
|
|
24
|
+
## Not planned
|
|
25
|
+
|
|
26
|
+
- **Generating the document from the app.** That was `@alxia/openapi` 0.1
|
|
27
|
+
to 0.3, now retired: with the document written first, a second document
|
|
28
|
+
derived from the routes would be a second source of truth.
|
|
29
|
+
- **Checking the schemas.** The checks read each route's method and path.
|
|
30
|
+
A route declared with `app.route(operation, ...middlewares, handler)`
|
|
31
|
+
takes its schemas from the operation itself, so they cannot differ; a
|
|
32
|
+
route written by hand is the app's to keep in step.
|
|
33
|
+
- **Registering the routes.** `app.route(operation, ...middlewares,
|
|
34
|
+
handler)` is in `@softistx/alxia`, where it keeps the chain that types the
|
|
35
|
+
app.
|
|
36
|
+
- **A runtime dependency.** `@softistx/alxia-openapi` declares no dependency, only
|
|
37
|
+
`@softistx/alxia`, whose types it reads, and `typescript` as peers.
|
|
38
|
+
|
|
39
|
+
## Shipped
|
|
40
|
+
|
|
41
|
+
Before 0.4.0, the name `@alxia/openapi` (0.1.0 to 0.3.0) belonged to a
|
|
42
|
+
different package, which wrote a document from the app's routes; it is
|
|
43
|
+
retired. The releases below are this package's, under its former name,
|
|
44
|
+
`@alxia/openapi-routes`.
|
|
45
|
+
|
|
46
|
+
### `@alxia/openapi` 0.6.0
|
|
47
|
+
|
|
48
|
+
- **A lenient `matchesSpec`.** An app may serve routes its document does not
|
|
49
|
+
describe (proxied, health, docs, hand-written): they no longer fail the
|
|
50
|
+
check, and come back as `extra`. `strict: true` restores the exhaustive
|
|
51
|
+
check ([the checks](guide/checks.md#strict-true)).
|
|
52
|
+
|
|
53
|
+
### `@alxia/openapi` 0.5.0
|
|
54
|
+
|
|
55
|
+
- **API docs, with no configuration.** `import spec from
|
|
56
|
+
'../openapi.yaml'`, then `app.plugin(apiDocs({ spec }))`, serves an
|
|
57
|
+
interactive page (Scalar, or Swagger UI) at `/docs` and the document at
|
|
58
|
+
`/docs/openapi.yaml` and `.json`. The page loads from a pinned CDN
|
|
59
|
+
version with an integrity hash, sets its own `Content-Security-Policy` —
|
|
60
|
+
that version's folder alone, requests to the app and the document's
|
|
61
|
+
servers alone — with Scalar's AI agent, MCP, developer tools and
|
|
62
|
+
telemetry off, and `matchesSpec` leaves its routes out.
|
|
63
|
+
- **The spec-first `@alxia/openapi`.** The OpenAPI document is the source:
|
|
64
|
+
`@nxgt/openapi-codegen`'s `alxia` option generates the operations,
|
|
65
|
+
`@alxia/core`'s `app.route(operation, ...middlewares, handler)` binds
|
|
66
|
+
them, and `implemented` and `matchesSpec` check the app against them.
|
|
67
|
+
The package that was `@alxia/openapi-routes` now carries this name.
|
|
68
|
+
The docs cover the whole workflow, from the document to the generated
|
|
69
|
+
operations, routes with middlewares, the check and a client.
|
|
70
|
+
- **The deprecated names removed.** `@alxia/openapi-routes`, the
|
|
71
|
+
re-export package, is gone, with `exactly` and `ExactlyOptions`, the
|
|
72
|
+
aliases of `matchesSpec` and `MatchesSpecOptions`: import
|
|
73
|
+
`implemented` and `matchesSpec` from `@alxia/openapi`, same options and
|
|
74
|
+
messages. Ships as 0.5.0.
|
|
75
|
+
- **The probes left out of `matchesSpec`.** The routes of `@alxia/core`'s
|
|
76
|
+
`health()` are no operation of a document, and `matchesSpec` skips them
|
|
77
|
+
as it skips `apiDocs()`'s, with no `exclude`
|
|
78
|
+
([the checks](guide/checks.md#matchesspec)).
|
|
79
|
+
|
|
80
|
+
### `@alxia/openapi-routes` 0.2.0
|
|
81
|
+
|
|
82
|
+
Removed in 0.5: `matchesSpec` from `@alxia/openapi` is its name.
|
|
83
|
+
|
|
84
|
+
- **One name for the check both ways.** `matchesSpec(app, operations)`,
|
|
85
|
+
the new name of `exactly`, which stayed as a deprecated alias until 0.5.
|
|
86
|
+
|
|
87
|
+
### `@alxia/openapi-routes` 0.1.0
|
|
88
|
+
|
|
89
|
+
Removed in 0.5: `implemented` and `matchesSpec` from `@alxia/openapi`
|
|
90
|
+
are these checks.
|
|
91
|
+
|
|
92
|
+
- **Every operation has a route.** `implemented(app, operations)` throws,
|
|
93
|
+
listing each operation of the document that the app does not serve, by
|
|
94
|
+
method, path and operation id.
|
|
95
|
+
- **Only the operations.** `exactly(app, operations)` also lists each route
|
|
96
|
+
the document does not declare; `exclude` leaves out the ones it should
|
|
97
|
+
not, such as a health check.
|
|
98
|
+
- **The generated shape.** Both take the `operations` object
|
|
99
|
+
`@nxgt/openapi-codegen`'s `alxia` option writes, or a list of operations,
|
|
100
|
+
typed as `@alxia/core`'s `RouteOperation`; `prefix` looks them up under
|
|
101
|
+
the app's prefix.
|