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: README.md
+79-17Lines changed: 79 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,26 +2,27 @@
2
2
3
3
Contract-first, type-safe API definitions for itty-router.
4
4
5
-
`itty-spec` is a small layer on top of itty-router that turns an API contract into:
5
+
`itty-spec` is a super lightweight dependency that builds on [itty-router](https://itty.dev/itty-router), [Standard Schema V1](https://github.com/standard-schema/spec), and [Standard Community](https://github.com/standard-community) packages. Designed for small workers, lambdas, and backend servers, it turns an API contract into:
6
6
7
7
- A ready-to-export `fetch` handler
8
8
- Automatic request parsing and validation (params, query, headers, body)
9
-
- Fully inferred TypeScript types in your route handlers
9
+
- Fully inferred TypeScript types in your route handlers with end-to-end type safety
- Optional OpenAPI 3.1 spec generation from the same contract (Zod v4 supported)
11
+
- Optional OpenAPI 3.1 spec generation and serving from the same contract
12
12
13
-
If you like itty-router’s “tiny router for Fetch” mental model, `itty-spec` keeps that model and adds a single source of truth: the contract.
13
+
If you like itty-router's "tiny router for Fetch" mental model, `itty-spec` keeps that model and adds a single source of truth: the contract. Perfect for Cloudflare Workers, AWS Lambda, Node.js servers, Bun, and Deno.
14
14
15
15
---
16
16
17
17
## What this project provides
18
18
19
19
-**Contract-first API design**: define routes, inputs, and outputs once.
20
-
-**Runtime validation**: invalid requests are rejected before your handler runs.
-**Typed response builder**: responses must match the contract (status/content-type/body) - TypeScript errors catch mismatches at compile time.
23
24
-**Fetch-first compatibility**: works in any environment that supports the Fetch API.
24
-
-**OpenAPI generation (optional)**: generate an OpenAPI 3.1 document from the contract (currently Zod v4).
25
+
-**OpenAPI generation and serving**: generate and serve OpenAPI 3.1 specifications from the same contract using `@standard-community/standard-openapi`.
25
26
26
27
## What this project is not
27
28
@@ -31,6 +32,18 @@ If you like itty-router’s “tiny router for Fetch” mental model, `itty-spec
31
32
32
33
---
33
34
35
+
## Foundation
36
+
37
+
`itty-spec` is built on a lightweight foundation of battle-tested libraries:
38
+
39
+
-**[itty-router](https://itty.dev/itty-router)** (v5): The tiny router for Fetch that powers routing and request handling.
40
+
-**[Standard Schema V1](https://github.com/standard-schema/spec)** (`@standard-schema/spec`): Provides a common interface for schema validation, enabling compatibility with multiple schema libraries.
41
+
-**[Standard Community OpenAPI](https://github.com/standard-community/standard-openapi)** (`@standard-community/standard-openapi`): Converts Standard Schema V1 schemas to OpenAPI 3.1 format for documentation and tooling.
42
+
43
+
This architecture ensures minimal bundle size while providing maximum type safety and developer experience. The library is designed to work seamlessly in edge/serverless environments where every byte counts.
44
+
45
+
---
46
+
34
47
## Installation
35
48
36
49
```bash
@@ -146,6 +159,21 @@ export default {
146
159
147
160
---
148
161
162
+
## Target environments
163
+
164
+
`itty-spec` is designed to be lightweight and efficient, making it ideal for:
165
+
166
+
- **Cloudflare Workers**: Edge computing with minimal cold start times
167
+
- **AWS Lambda**: Serverless functions with size constraints
168
+
- **Node.js servers**: Traditional backend servers
169
+
- **Bun**: Fast JavaScript runtime
170
+
- **Deno**: Secure runtime for JavaScript and TypeScript
171
+
- **Any Fetch-compatible environment**: Works wherever the Fetch API is available
172
+
173
+
The library's minimal dependencies and small bundle size ensure fast startup times and low memory footprint, critical for edge and serverless deployments.
174
+
175
+
---
176
+
149
177
## Core concepts
150
178
151
179
### Contract
@@ -174,32 +202,64 @@ The shape of that response is type-checked against the contract for the current
174
202
175
203
## Schema support
176
204
177
-
`itty-spec` is designed to work with schema libraries that implement the Standard Schema V1 interface. In practice:
205
+
`itty-spec` uses the [Standard Schema V1](https://github.com/standard-schema/spec) interface, which provides a common abstraction layer for schema validation. This means you can use any Standard Schema V1 compatible library:
206
+
207
+
* **Zod (v4)**: Fully supported with excellent TypeScript inference and OpenAPI generation. Recommended for the best developer experience.
208
+
* **Valibot**: Fully supported with OpenAPI generation via `@standard-community/standard-openapi`.
209
+
* **Other Standard Schema compatible libraries**: Can be used for validation; OpenAPI support depends on the library's Standard Schema V1 implementation.
178
210
179
-
* Zod (v4) is supported and is the best experience today (including OpenAPI generation).
180
-
* Other Standard Schema compatible libraries can be used for validation; OpenAPI support may vary.
211
+
The Standard Schema V1 interface ensures that your contracts remain portable across different schema libraries while maintaining type safety and runtime validation.
181
212
182
213
---
183
214
184
-
## OpenAPI 3.1 generation (optional)
215
+
## OpenAPI 3.1 generation and serving (optional)
185
216
186
-
If you want a formal API spec (documentation, SDK generation, Postman/Insomnia import), generate an OpenAPI document directly from your contract:
217
+
Generate an OpenAPI 3.1 specification directly from your contract and serve it as a documentation endpoint:
187
218
188
219
```ts
189
220
import { createOpenApiSpecification } from "itty-spec/openapi";
OpenAPI generation currently supports Zod v4 schemas via `toJSONSchema()`.
260
+
OpenAPI generation uses `@standard-community/standard-openapi` to convert Standard Schema V1 schemas to OpenAPI 3.1 format. This supports Zod v4 and Valibot schemas out of the box. You can then use tools like [Swagger UI](https://swagger.io/tools/swagger-ui/), [Redoc](https://github.com/Redocly/redoc), or [Elements](https://github.com/stoplightio/elements) to render interactive documentation from the served specification.
261
+
262
+
See the `examples/simple` and `examples/complex` directories forcomplete examples of serving OpenAPI documentation.
203
263
204
264
205
265
## Repository layout
@@ -210,7 +270,9 @@ OpenAPI generation currently supports Zod v4 schemas via `toJSONSchema()`.
210
270
211
271
## References
212
272
213
-
- [itty-router](https://itty.dev/itty-router)
273
+
- [itty-router](https://itty.dev/itty-router) - The tiny router for Fetch
274
+
- [Standard Schema V1](https://github.com/standard-schema/spec) - Common schema interface
275
+
- [Standard Community OpenAPI](https://github.com/standard-community/standard-openapi) - OpenAPI generation from Standard Schema
0 commit comments