You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/openapi-typescript-server/README.md
+32-23Lines changed: 32 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,24 @@
2
2
3
3
`openapi-typescript-server` is a CLI and minimal runtime library that helps you implement type-safe APIs documented by OpenAPI.
4
4
5
-
It works by generating a TypeScript server interface based on your OpenAPI spec using types from [openapi-typescript](https://github.com/openapi-ts/openapi-typescript).
5
+
## Key Features
6
+
7
+
- ✅ Code gen server implementations of OpenAPI 3.0 and 3.1
8
+
- ✅ Framework-agnostic core with Express adapter available
9
+
- ✅ Minimal runtime footprint
10
+
- ✅ Built on top of [openapi-typescript](https://github.com/openapi-ts/openapi-typescript)
11
+
12
+
## Overview
13
+
14
+
This library works by generating a TypeScript server interface based on your OpenAPI spec using types from [openapi-typescript](https://github.com/openapi-ts/openapi-typescript).
6
15
7
16
You provide a concrete implementation that satisfies the interface for each path operation.
8
17
9
18
At runtime, your implementation is converted into HTTP handlers for various frameworks like [Express](https://github.com/jasonblanchard/openapi-typescript-server/tree/main/packages/openapi-typescript-server).
10
19
11
20
## Stability
12
21
13
-
⚠️ This package is in very early development. Breaking changes may be introduced as the design and implementation takes shape towards a stable release. ⚠️
14
-
15
-
For now, proceed with caution!
22
+
⚠️ This package is in very early development. Breaking changes may be introduced as the design and implementation takes shape towards a stable release. For now, proceed with caution! ⚠️
You'll see that TypeScript will tell you exactly what needs to change in your code to conform to the new OpenAPI spec:
@@ -258,7 +267,7 @@ You'll see that TypeScript will tell you exactly what needs to change in your co
258
267
Property 'vibe' is missing in type '{ greeting: string; }' but required in type '{ greeting: string; vibe: "friendly" | "fierce" | "playful" | "sleepy"; }'.ts(2322)
259
268
```
260
269
261
-
> **Note**: You may need to restart the TS Server in VS Code to see the TypeScript error.
270
+
> **Note**: You may need to restart the TS Server in VS Code to see the TypeScript error after re-generating the types and server interface.
262
271
263
272
Satisfy the compiler to get it working again:
264
273
@@ -288,7 +297,7 @@ This pattern also guides you when adding or removing routes.
288
297
289
298
### Design goals
290
299
291
-
The main goal of this package is to ensure that the server implementation stays in sync with the API documentation by generating a typed interface _from_ the OpenAPI spec.
300
+
The main goal of this package is to ensure that your server implementation stays in sync with the API documentation by generating a typed interface _from_ the OpenAPI spec.
292
301
293
302
This schema-first approach documents your system for humans or LLM coding agents, and helps you achieve type safety across the system.
294
303
@@ -329,17 +338,17 @@ The route handler name uses the `operationId` if present. Otherwise it's generat
329
338
330
339
The return value is enveloped in `content` so that you can provide `headers` and `status` at the same level. `status` is required when the response variant is "default".
331
340
332
-
> **Note**: These return values are a bit verbose, but, since the HTTP handling is delegated to the adapters, they have the added benefit of being easily testable functions with data in and out.
341
+
> **Note**: These return values are a bit verbose, but, since the HTTP handling side effects are delegated to the adapters, they have the added benefit of being easily testable functions with data in and out.
333
342
334
-
Your route handlers are packaged up into the generated `registerRouteHandlers` function which returns a list of objects containing the route method, path, and handler function.
343
+
Your route handlers are packaged up in the generated `registerRouteHandlers` function which returns a list of objects containing route method, path, and handler function.
335
344
336
345
Adapters supply a `registerRoutes` function. It iterates through this data structure and uses the underlying framework (i.e. Express) to add each route, set headers, serialize response bodies, and terminate the response.
337
346
338
347
#### Content type serialization
339
348
340
-
Content type serialization and deserialization happens in the adapter layer.
349
+
Content type serialization and deserialization happens in or near the adapter layer.
341
350
342
-
For Express, request bodies can be deserialized to JavaScript objects by middleware. The Express adapter assumes this has already happened and passes the deserialized (and presumably validated and coerced) JavaScript objects to your route handlers. The content type is passed as an argument to your handler in case you need to return a different response shape per.
351
+
For Express, request bodies are deserialized into JavaScript objects prior to the Express adapter by Express middleware. The Express adapter assumes this has already happened, and passes the deserialized (and presumably validated and coerced) JavaScript objects to your route handlers. The content type is passed as an argument to your handler in case you need to return a different response shape per.
343
352
344
353
For response body serialization, `application/json` is built in and happens automatically. Other content types can be handled by supplying a `serializers` option to the `registerRoutes` function with custom serialization functions keyed by content type.
345
354
@@ -433,21 +442,17 @@ components:
433
442
import type * as ServerTypes from "./gen/server.ts";
message: "This is what it looks like when something goes wrong",
447
453
},
448
454
},
449
455
},
450
-
status: 418,
451
456
};
452
457
},
453
458
};
@@ -456,3 +461,7 @@ export default API;
456
461
```
457
462
458
463
Otherwise, thrown errors will propagate up the call stack to your global error handler. You can check for `err instanceof NotImplementedError` if you want to handle those with a `501 Not Implemented` status code.
464
+
465
+
## Contributing
466
+
467
+
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to this project.
0 commit comments