@nwire/openapi 0.14.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 +39 -0
- package/dist/openapi.d.ts +28 -0
- package/dist/openapi.js +96 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Gefter / 200apps Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @nwire/openapi
|
|
2
|
+
|
|
3
|
+
> OpenAPI 3.1 generation from a Nwire wire collection — transport-agnostic.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add @nwire/openapi
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
`generateOpenApi(wires)` reads `app.interface.wires`, picks the HTTP bindings,
|
|
10
|
+
and emits an OpenAPI 3.1 document from each binding's `openapi` metadata and its
|
|
11
|
+
zod schemas. No HTTP server and no framework lock-in: an HTTP adopter (koa,
|
|
12
|
+
express) serves the result, and a build step can write it to a file without
|
|
13
|
+
booting a transport.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { generateOpenApi } from "@nwire/openapi";
|
|
17
|
+
|
|
18
|
+
const spec = await generateOpenApi(app.interface.wires, {
|
|
19
|
+
title: "Orders API",
|
|
20
|
+
version: "1.0.0",
|
|
21
|
+
prefix: "/api",
|
|
22
|
+
});
|
|
23
|
+
// serve `JSON.stringify(spec)` at /openapi.json, or write it to disk
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This package owns `@asteasolutions/zod-to-openapi` as a direct dependency, so the
|
|
27
|
+
generator is always present where generation lives and a consumer produces a spec
|
|
28
|
+
with nothing extra to install.
|
|
29
|
+
|
|
30
|
+
## Options
|
|
31
|
+
|
|
32
|
+
| Option | Meaning |
|
|
33
|
+
| ----------------------------------- | ----------------------------------------------------------------------- |
|
|
34
|
+
| `title` / `version` / `description` | `info` block of the document |
|
|
35
|
+
| `serverUrl` | a single entry for the `servers` array |
|
|
36
|
+
| `prefix` | path prefix prepended to every route (match your HTTP adopter's prefix) |
|
|
37
|
+
|
|
38
|
+
Bindings carry the rest: `operation`, `summary`, `tags`, request schemas
|
|
39
|
+
(`params` / `query` / `body`), and `returns` / `errors` for responses.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/openapi` — turn a Nwire wire collection into an OpenAPI 3.1 document.
|
|
3
|
+
*
|
|
4
|
+
* Transport-agnostic by design: it reads `app.interface.wires`, picks the HTTP
|
|
5
|
+
* bindings, and emits one path entry per `(verb, path)` from the binding's
|
|
6
|
+
* `openapi` metadata + zod schemas. No HTTP server, no koa — any HTTP adopter
|
|
7
|
+
* (koa, express) serves the result, and a CLI can write it to a file
|
|
8
|
+
* (`nwire openapi > spec.json`) without booting a transport.
|
|
9
|
+
*
|
|
10
|
+
* `@asteasolutions/zod-to-openapi` is a direct dependency, so the generator is
|
|
11
|
+
* always present wherever generation lives; a consumer serves `/openapi.json`
|
|
12
|
+
* without installing anything extra.
|
|
13
|
+
*/
|
|
14
|
+
import type { Wire } from "@nwire/wires";
|
|
15
|
+
export interface GenerateOpenApiOptions {
|
|
16
|
+
readonly title?: string;
|
|
17
|
+
readonly version?: string;
|
|
18
|
+
readonly description?: string;
|
|
19
|
+
readonly serverUrl?: string;
|
|
20
|
+
/** Prefix the spec paths with this. Match the HTTP adopter prefix. */
|
|
21
|
+
readonly prefix?: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Build an OpenAPI 3.1 document from a wire collection.
|
|
25
|
+
*
|
|
26
|
+
* Returns a promise — HTTP adapters `await` it at mount time.
|
|
27
|
+
*/
|
|
28
|
+
export declare function generateOpenApi(wires: ReadonlyArray<Wire>, options?: GenerateOpenApiOptions): Promise<object>;
|
package/dist/openapi.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/openapi` — turn a Nwire wire collection into an OpenAPI 3.1 document.
|
|
3
|
+
*
|
|
4
|
+
* Transport-agnostic by design: it reads `app.interface.wires`, picks the HTTP
|
|
5
|
+
* bindings, and emits one path entry per `(verb, path)` from the binding's
|
|
6
|
+
* `openapi` metadata + zod schemas. No HTTP server, no koa — any HTTP adopter
|
|
7
|
+
* (koa, express) serves the result, and a CLI can write it to a file
|
|
8
|
+
* (`nwire openapi > spec.json`) without booting a transport.
|
|
9
|
+
*
|
|
10
|
+
* `@asteasolutions/zod-to-openapi` is a direct dependency, so the generator is
|
|
11
|
+
* always present wherever generation lives; a consumer serves `/openapi.json`
|
|
12
|
+
* without installing anything extra.
|
|
13
|
+
*/
|
|
14
|
+
import { OpenAPIRegistry, OpenApiGeneratorV31, extendZodWithOpenApi, } from "@asteasolutions/zod-to-openapi";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
// Patch zod's prototype with `.openapi()` once, at module load. It applies
|
|
17
|
+
// retroactively to every schema instance the bindings already hold, so the
|
|
18
|
+
// order of "build schema" vs "load this module" doesn't matter.
|
|
19
|
+
extendZodWithOpenApi(z);
|
|
20
|
+
function isOpenApiBinding(b) {
|
|
21
|
+
return (typeof b === "object" &&
|
|
22
|
+
b !== null &&
|
|
23
|
+
b.$adapter === "http" &&
|
|
24
|
+
typeof b.verb === "string" &&
|
|
25
|
+
typeof b.path === "string");
|
|
26
|
+
}
|
|
27
|
+
// Convert wire-style `/users/:id` to OpenAPI `{id}` notation.
|
|
28
|
+
function toOpenApiPath(p) {
|
|
29
|
+
return p.replace(/:(\w+)/g, "{$1}");
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Build an OpenAPI 3.1 document from a wire collection.
|
|
33
|
+
*
|
|
34
|
+
* Returns a promise — HTTP adapters `await` it at mount time.
|
|
35
|
+
*/
|
|
36
|
+
export async function generateOpenApi(wires, options = {}) {
|
|
37
|
+
const registry = new OpenAPIRegistry();
|
|
38
|
+
const prefix = options.prefix ?? "";
|
|
39
|
+
for (const wire of wires) {
|
|
40
|
+
if (!isOpenApiBinding(wire.binding))
|
|
41
|
+
continue;
|
|
42
|
+
const b = wire.binding;
|
|
43
|
+
if (!b.openapi)
|
|
44
|
+
continue;
|
|
45
|
+
const path = prefix + toOpenApiPath(b.path);
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
+
const responses = {
|
|
48
|
+
"200": { description: "Success" },
|
|
49
|
+
};
|
|
50
|
+
// Returns from the binding override the default 200 envelope.
|
|
51
|
+
if (b.openapi.returns?.length) {
|
|
52
|
+
const first = b.openapi.returns[0];
|
|
53
|
+
const status = String(first?.status ?? 200);
|
|
54
|
+
const schema = first?.resource?.schema;
|
|
55
|
+
responses[status] = schema
|
|
56
|
+
? {
|
|
57
|
+
description: "Success",
|
|
58
|
+
content: { "application/json": { schema } },
|
|
59
|
+
}
|
|
60
|
+
: { description: "Success" };
|
|
61
|
+
}
|
|
62
|
+
if (b.openapi.errors?.length) {
|
|
63
|
+
for (const e of b.openapi.errors) {
|
|
64
|
+
if (typeof e.status === "number") {
|
|
65
|
+
responses[String(e.status)] = {
|
|
66
|
+
description: e.code ?? "Error",
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
registry.registerPath({
|
|
72
|
+
method: b.verb,
|
|
73
|
+
path,
|
|
74
|
+
operationId: b.openapi.operation,
|
|
75
|
+
tags: b.openapi.tags ? [...b.openapi.tags] : undefined,
|
|
76
|
+
summary: b.openapi.summary,
|
|
77
|
+
description: b.openapi.description,
|
|
78
|
+
request: {
|
|
79
|
+
...(b.params ? { params: b.params } : {}),
|
|
80
|
+
...(b.query ? { query: b.query } : {}),
|
|
81
|
+
...(b.body ? { body: { content: { "application/json": { schema: b.body } } } } : {}),
|
|
82
|
+
},
|
|
83
|
+
responses,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const generator = new OpenApiGeneratorV31(registry.definitions);
|
|
87
|
+
return generator.generateDocument({
|
|
88
|
+
openapi: "3.1.0",
|
|
89
|
+
info: {
|
|
90
|
+
title: options.title ?? "API",
|
|
91
|
+
version: options.version ?? "1.0.0",
|
|
92
|
+
description: options.description,
|
|
93
|
+
},
|
|
94
|
+
servers: options.serverUrl ? [{ url: options.serverUrl }] : undefined,
|
|
95
|
+
});
|
|
96
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nwire/openapi",
|
|
3
|
+
"version": "0.14.0",
|
|
4
|
+
"description": "Nwire — OpenAPI 3.1 generation from a Nwire wire collection. Transport-agnostic: reads app.interface.wires, picks the HTTP bindings, and emits a spec from binding metadata + zod schemas. Owns @asteasolutions/zod-to-openapi as a direct dependency, so any HTTP adopter (koa, express) or a CLI produces a spec with nothing extra to install.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nwire",
|
|
7
|
+
"openapi",
|
|
8
|
+
"spec",
|
|
9
|
+
"swagger"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"main": "./dist/openapi.js",
|
|
19
|
+
"types": "./dist/openapi.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": "./dist/openapi.js",
|
|
23
|
+
"types": "./dist/openapi.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@asteasolutions/zod-to-openapi": "^8.0.0",
|
|
31
|
+
"zod": "^4.0.0",
|
|
32
|
+
"@nwire/wires": "0.14.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^22.19.9",
|
|
36
|
+
"typescript": "^5.9.3",
|
|
37
|
+
"vitest": "^4.0.18"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc && node ../../scripts/fix-dist-extensions.mjs dist",
|
|
41
|
+
"dev": "tsc --watch",
|
|
42
|
+
"typecheck": "tsc --noEmit"
|
|
43
|
+
}
|
|
44
|
+
}
|