Skip to content

Commit ded9e22

Browse files
committed
Misc. bootstrapping
- Add License - Fixup README - Create typed fetch implementation - Finish metaprogramming implementation
1 parent eaf2aec commit ded9e22

File tree

14 files changed

+998
-464
lines changed

14 files changed

+998
-464
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2024-present, Bryan Gillespie
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
13+
all 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
21+
THE SOFTWARE.

README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# typed-fetch
22

3-
typed-fetch is intended to be a drop-in replacement for [openapi-typescript/openapi-fetch](https://github.com/openapi-ts/openapi-typescript) but with a much simpler TypeScript implementation that I believe results in fewer issues and enhanced readability.
3+
typed-fetch is intended to be a drop-in replacement for [openapi-fetch](https://github.com/openapi-ts/openapi-typescript) but using a simplified TypeScript implementation that I believe results in fewer issues and superior usability/readability/debuggability.
44

55
## Quickstart
66

77
```bash
88
# Generate TypeScript types from OpenAPI document
9-
typed-fetch --openapi examples/petstore-openapi.yaml --output petstore-openapi.ts
9+
typed-fetch --openapi examples/petstore-openapi.yaml --output petstore-openapi.d.ts
1010
```
1111

1212
```ts
1313
// Use the generated library in your .ts files
14-
import { createClient } from "./petstore-openapi";
14+
import type { Client as PetstoreClient } from "./petstore-openapi"; // petstore-openapi.d.ts
15+
import { createClient } from "./typed-fetch"; // typed-fetch.ts file from root of this repo
1516

16-
const client = createClient({ baseUrl: "https://petstore.swagger.io/v2" });
17+
const client = createClient<PetstoreClient>({ baseUrl: "https://petstore.swagger.io/v2" });
1718

1819
const { data, error } = await client.POST("/store/order", {
1920
body: {
@@ -43,26 +44,30 @@ Currently this utility is not being published to npm, but it's a future possibil
4344

4445
Type-checked [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) calls using OpenAPI + TypeScript.
4546

46-
Mostly API-compatible with [openapi-fetch](https://github.com/openapi-ts/openapi-typescript).
47+
Mostly API-compatible with [openapi-fetch](https://github.com/openapi-ts/openapi-typescript). Doesn't include extra bells and whistles like middleware support since native fetch doesn't support middlewares either.
4748

48-
Why create this library, if it's nearly identical to [openapi-fetch](https://github.com/openapi-ts/openapi-typescript)? It's because openapi-fetch uses a complex generics/constraints-based TypeScript implementation for type checking, which in my opinion makes it *very* difficult to understand, test, and maintain. The goal of typed-fetch, on the other hand, is to generate simple, straightforward types given an OpenAPI document so that any generalist programmer could inspect the generated TypeScript and easiy understand it -- [see for yourself, no PhD in TypeScript required](examples/petstore-openapi.ts).
49+
Why create this library, if it's nearly identical to [openapi-fetch](https://github.com/openapi-ts/openapi-typescript)? It's because openapi-fetch uses a complex generics/constraints-based TypeScript implementation for type checking, which in my opinion makes it *very* difficult to understand, test, and debug because it [requires knowledge of esoteric TypeScript behavior](https://github.com/openapi-ts/openapi-typescript/issues/1778#issuecomment-2276217668).
4950

50-
TypeScript generated with this utility is composed almost entirely of type definitions which are stripped out at compile time, resulting in an extremely lightweight fetch wrapper. As a result, typed-fetch should have the same size and performance characteristics as openapi-fetch.
51+
The goal of typed-fetch, on the other hand, is to generate simple, straightforward "dumb" types given an OpenAPI document so that any generalist programmer could inspect the generated TypeScript and easily understand it -- [see for yourself, no TypeScript black belt required](examples/petstore-openapi.d.ts). Note there are 5 lines of "evil" TypeScript required to make this library play more nicely with VSCode intellisense, but they could be removed if VSCode had better intellisense for overloaded functions.
52+
53+
Like [openapi-fetch](https://github.com/openapi-ts/openapi-typescript), TypeScript generated with this utility is composed entirely of type definitions which are stripped out at compile time, resulting in an extremely lightweight fetch wrapper. Currently weighs in at **1.4 KiB** minified (**700 bytes** if minified and compressed) which means typed-fetch should meet or exceed the size and performance characteristics of openapi-fetch.
5154

5255
Features:
53-
- Generated TypeScript definitions are at least an order of magnitude simpler and more straightforward than openapi-fetch, which means you can easily jump to and inspect type definitions in your favorite IDE without fear
54-
- Arbitrary combinations of required and optional parameters in request bodies are correctly type-checked (broken in openapi-fetch as of July 2024)
55-
- View your OpenAPI documentation in VSCode when you hover on functions and property names
56+
- Generated TypeScript definitions are *at least* an order of magnitude simpler and more straightforward than openapi-fetch, which means you don't have to be a TypeScript ninja to contribute to or debug issues with the type checking.
57+
- Arbitrary combinations of required and optional parameters in request bodies are correctly type-checked (broken in openapi-fetch as of August 2024 - check if [this issue](https://github.com/openapi-ts/openapi-typescript/issues/1769) is still open)
5658
- Like esbuild, typed-fetch is written in golang, so it's lightning fast
5759

5860
Limitations:
59-
- Only OpenAPI 3.1+ specifications officially supported (see [migration guide](https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0)), though most OpenAPI 3.0 documents will also work.
60-
- Some OpenAPI 3 features not currently implemented, especially if it's unclear how it would map to a TypeScript API.
61+
- Only OpenAPI 3.1+ specifications "officially" supported (see [migration guide](https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0)), though all OpenAPI 3.0 documents I've tested so far also work.
62+
- Some of the more obscure OpenAPI 3 features are not currently implemented (polymorphism, links, callbacks, etc), and I don't plan to implement them unless there's both a strong use case and a clean way to map them to *both* fetch *and* TypeScript.
6163

62-
Missing functionality?
64+
# Missing functionality?
6365

64-
Please open an issue with the following 2 things:
66+
Please open an issue with the following 3 things:
6567
- Snippet of valid OpenAPI 3 yaml
66-
- Expected TypeScript to be generated
68+
- Expected behavior
69+
- Actual behavior
70+
71+
# Note to openapi-fetch maintainers
6772

68-
Also, I'm open to the idea of migrating this utility/approach *into* openapi-fetch if the maintainer(s) there are on-board; I'd rather there be one great tool everyone uses than further fragment the space.
73+
Feel free to copy any approach and/or techniques you see in this repo. I personally think the approach I implemented here in go could be fairly easily replicated in openapi-typescript, and would result in a more-maintainable and more-contributor-friendly openapi-fetch implementation. I'd be happy to help if you're interested which would obsolete this repo.

0 commit comments

Comments
 (0)