enlace-openapi 0.0.1-beta.1 → 0.0.1-beta.3

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Enlace
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,225 @@
1
+ # enlace-openapi
2
+
3
+ Generate OpenAPI 3.0 specifications from TypeScript API schema types.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add enlace-openapi
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### CLI
14
+
15
+ ```bash
16
+ enlace-openapi --schema ./types/APISchema.ts --output ./openapi.json
17
+ ```
18
+
19
+ ### Options
20
+
21
+ | Option | Description | Default |
22
+ |--------|-------------|---------|
23
+ | `-s, --schema <path>` | Path to TypeScript schema file | (required) |
24
+ | `-t, --type <name>` | Schema type name to export | `ApiSchema` |
25
+ | `-o, --output <path>` | Output file path | stdout |
26
+ | `--title <title>` | API title | `API` |
27
+ | `--version <version>` | API version | `1.0.0` |
28
+ | `--base-url <url>` | Base URL for servers array | - |
29
+
30
+ ### Example
31
+
32
+ ```bash
33
+ enlace-openapi \
34
+ --schema ./types/APISchema.ts \
35
+ --type ApiSchema \
36
+ --title "My API" \
37
+ --version "2.0.0" \
38
+ --base-url "https://api.example.com" \
39
+ --output ./openapi.json
40
+ ```
41
+
42
+ ## Schema Format
43
+
44
+ Define your API schema using the `Endpoint` type from `enlace-core`:
45
+
46
+ ```typescript
47
+ import { Endpoint } from "enlace-core";
48
+
49
+ type User = {
50
+ id: string;
51
+ name: string;
52
+ email: string;
53
+ };
54
+
55
+ type CreateUserBody = {
56
+ name: string;
57
+ email: string;
58
+ };
59
+
60
+ type ValidationError = {
61
+ field: string;
62
+ message: string;
63
+ };
64
+
65
+ export type ApiSchema = {
66
+ users: {
67
+ $get: Endpoint<User[]>;
68
+ $post: Endpoint<User, CreateUserBody, ValidationError>;
69
+ _: {
70
+ $get: Endpoint<User>;
71
+ $put: Endpoint<User, Partial<CreateUserBody>>;
72
+ $delete: Endpoint<{ success: boolean }>;
73
+ };
74
+ };
75
+ };
76
+ ```
77
+
78
+ This generates:
79
+
80
+ ```json
81
+ {
82
+ "openapi": "3.0.0",
83
+ "info": { "title": "My API", "version": "2.0.0" },
84
+ "servers": [{ "url": "https://api.example.com" }],
85
+ "paths": {
86
+ "/users": {
87
+ "get": {
88
+ "responses": {
89
+ "200": {
90
+ "description": "Successful response",
91
+ "content": {
92
+ "application/json": {
93
+ "schema": { "type": "array", "items": { "$ref": "#/components/schemas/User" } }
94
+ }
95
+ }
96
+ }
97
+ }
98
+ },
99
+ "post": {
100
+ "requestBody": {
101
+ "required": true,
102
+ "content": {
103
+ "application/json": {
104
+ "schema": { "$ref": "#/components/schemas/CreateUserBody" }
105
+ }
106
+ }
107
+ },
108
+ "responses": {
109
+ "200": { "..." },
110
+ "400": {
111
+ "description": "Error response",
112
+ "content": {
113
+ "application/json": {
114
+ "schema": { "$ref": "#/components/schemas/ValidationError" }
115
+ }
116
+ }
117
+ }
118
+ }
119
+ }
120
+ },
121
+ "/users/{userId}": {
122
+ "parameters": [{ "name": "userId", "in": "path", "required": true, "schema": { "type": "string" } }],
123
+ "get": { "..." },
124
+ "put": { "..." },
125
+ "delete": { "..." }
126
+ }
127
+ },
128
+ "components": {
129
+ "schemas": {
130
+ "User": { "..." },
131
+ "CreateUserBody": { "..." },
132
+ "ValidationError": { "..." }
133
+ }
134
+ }
135
+ }
136
+ ```
137
+
138
+ ## Endpoint Type
139
+
140
+ The `Endpoint` type accepts three generic parameters:
141
+
142
+ ```typescript
143
+ Endpoint<TData, TBody?, TError?>
144
+ ```
145
+
146
+ | Parameter | Description |
147
+ |-----------|-------------|
148
+ | `TData` | Response data type (required) |
149
+ | `TBody` | Request body type (optional) |
150
+ | `TError` | Error response type (optional) |
151
+
152
+ ## Path Parameters
153
+
154
+ Use `_` to define dynamic path segments:
155
+
156
+ ```typescript
157
+ type ApiSchema = {
158
+ users: {
159
+ _: {
160
+ // /users/{userId}
161
+ posts: {
162
+ _: {
163
+ // /users/{userId}/posts/{postId}
164
+ $get: Endpoint<Post>;
165
+ };
166
+ };
167
+ };
168
+ };
169
+ };
170
+ ```
171
+
172
+ Parameter names are auto-generated from the parent segment (e.g., `users` → `userId`, `posts` → `postId`).
173
+
174
+ ## Supported Types
175
+
176
+ - Primitives: `string`, `number`, `boolean`, `null`
177
+ - Literals: `"active"`, `42`, `true`
178
+ - Arrays: `User[]`, `Array<User>`
179
+ - Objects: `{ name: string; age: number }`
180
+ - Optional properties: `{ name?: string }`
181
+ - Nullable: `string | null`
182
+ - Unions: `"pending" | "active" | "inactive"`
183
+ - Intersections: `BaseUser & { role: string }`
184
+ - Date: converted to `{ type: "string", format: "date-time" }`
185
+ - Named types: extracted to `#/components/schemas`
186
+
187
+ ## Programmatic API
188
+
189
+ ### Next.js + Swagger UI Example
190
+
191
+ ```typescript
192
+ import SwaggerUI from "swagger-ui-react";
193
+ import "swagger-ui-react/swagger-ui.css";
194
+ import { parseSchema, generateOpenAPISpec } from "enlace-openapi";
195
+
196
+ const spec = (() => {
197
+ const { endpoints, schemas } = parseSchema(
198
+ "./APISchema.ts",
199
+ "ApiSchema"
200
+ );
201
+ return generateOpenAPISpec(endpoints, schemas, {
202
+ title: "My API",
203
+ version: "1.0.0",
204
+ baseUrl: "https://api.example.com",
205
+ });
206
+ })();
207
+
208
+ const DocsPage = () => {
209
+ return <SwaggerUI spec={spec} />;
210
+ };
211
+
212
+ export default DocsPage;
213
+ ```
214
+
215
+ ## Viewing the OpenAPI Spec
216
+
217
+ Use [Swagger UI](https://swagger.io/tools/swagger-ui/) or [Swagger Editor](https://editor.swagger.io/) to visualize the generated spec.
218
+
219
+ Quick local preview with Docker:
220
+
221
+ ```bash
222
+ docker run -p 8080:8080 -e SWAGGER_JSON=/openapi.json -v $(pwd)/openapi.json:/openapi.json swaggerapi/swagger-ui
223
+ ```
224
+
225
+ Then open http://localhost:8080
package/dist/cli.d.mts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node