Skip to content

Commit 236cb11

Browse files
authored
Link to docs site, minor docs site revisions (#1241)
1 parent 4c93067 commit 236cb11

File tree

8 files changed

+360
-728
lines changed

8 files changed

+360
-728
lines changed

docs/package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
"update-contributors": "node scripts/update-contributors.js"
1010
},
1111
"dependencies": {
12-
"@algolia/client-search": "^4.18.0",
12+
"@algolia/client-search": "^4.19.1",
1313
"@astrojs/preact": "^2.2.1",
1414
"@astrojs/react": "^2.2.1",
1515
"@docsearch/css": "^3.5.1",
1616
"@docsearch/react": "^3.5.1",
17-
"@types/react": "^18.2.14",
18-
"@types/react-dom": "^18.2.6",
19-
"astro": "^2.8.0",
20-
"preact": "^10.15.1",
17+
"@types/react": "^18.2.16",
18+
"@types/react-dom": "^18.2.7",
19+
"astro": "^2.9.2",
20+
"preact": "^10.16.0",
2121
"react": "^18.2.0",
2222
"react-dom": "^18.2.0",
23-
"sass": "^1.63.6"
23+
"sass": "^1.64.1"
2424
},
2525
"devDependencies": {
26-
"@astrojs/sitemap": "^1.3.3",
27-
"@types/node": "^20.4.0",
26+
"@astrojs/sitemap": "^2.0.1",
27+
"@types/node": "^20.4.4",
2828
"html-escaper": "^3.0.3",
2929
"typescript": "^5.1.6"
3030
}

docs/src/content/docs/node.md

+12-69
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ description: Node.js API
55

66
The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. Pass in either a JSON-friendly object to load a schema from memory, or a string to load a schema from a local file or remote URL (it will load the file quickly using built-in Node methods).
77

8+
## Setup
9+
810
```bash
911
npm i --save-dev openapi-typescript
1012
```
1113

14+
Note that the Node.js API requires [ESM support](https://nodejs.org/api/esm.html) in Node. This can be enabled by adding
15+
16+
```json
17+
"type": "module"
18+
```
19+
20+
To your `package.json`. Or it can be consumed in a `.mjs` file extension (rather than `.js` or `.cjs`)
21+
22+
## Usage
23+
1224
```js
1325
import fs from "node:fs";
1426
import openapiTS from "openapi-typescript";
@@ -113,72 +125,3 @@ Resultant diff with correctly-typed `file` property:
113125
Any [Schema Object](https://spec.openapis.org/oas/latest.html#schema-object) present in your schema will be run through this formatter (even remote ones!). Also be sure to check the `metadata` parameter for additional context that may be helpful.
114126
115127
There are many other uses for this besides checking `format`. Because this must return a **string** you can produce any arbitrary TypeScript code you’d like (even your own custom types).
116-
117-
## Node.js API with browser build target
118-
119-
Projects with a browser build target (e.g. a React app) may be unable to run a Node.js script, due to differences between Node.js and and browser module systems. Some minor configuration is needed.
120-
121-
Here's an example script implementing the above transforms:
122-
123-
```ts
124-
// scripts/generateTypes.ts
125-
import fs from "node:fs";
126-
import openapiTS from "openapi-typescript";
127-
128-
const OPENAPI_URL = "https://petstore3.swagger.io/api/v3/openapi.json";
129-
const OUTPUT_FILE = "src/services/api/schema.d.ts";
130-
131-
async function main() {
132-
process.stdout.write(`Generating types "${OPENAPI_URL}" --> "${OUTPUT_FILE}"...`);
133-
const types = await openapiTS(OPENAPI_URL, {
134-
transform: (schemaObject, metadata): string | undefined => {
135-
if ("format" in schemaObject && schemaObject.format === "binary") {
136-
return schemaObject.nullable ? "Blob | null" : "Blob";
137-
}
138-
if ("format" in schemaObject && schemaObject.format === "date-time") {
139-
return schemaObject.nullable ? "Date | null" : "Date";
140-
}
141-
},
142-
});
143-
fs.writeFileSync(OUTPUT_FILE, types);
144-
process.stdout.write(` OK!\r\n`);
145-
}
146-
147-
main();
148-
```
149-
150-
Add a `package.json` to the scripts folder declaring the module type:
151-
152-
```json
153-
// scripts/package.json
154-
{
155-
"type": "module"
156-
}
157-
```
158-
159-
Now you can run your script using `ts-node`. The `--esm` flag tells `ts-node` how to handle the imports:
160-
161-
```bash
162-
npx ts-node --esm scripts/generateTypes.ts
163-
```
164-
165-
If you the project uses JSX (e.g. React), you may need to pass in `compilerOptions` with correct `"jsx"`property:
166-
167-
```bash
168-
npx ts-node --esm -compilerOptions {\"jsx\":\"preserve\"} scripts/generateTypes.ts
169-
```
170-
171-
A alternative to the CLI flags is to add a `ts-node` section to your `tsconfig.json`:
172-
173-
```json
174-
// tsconfig.json
175-
{
176-
... // your existing config
177-
"ts-node": {
178-
"compilerOptions": { // `ts-node`-specific compiler options
179-
"jsx": "preserve" // this may not be the correct value for your project
180-
},
181-
"esm": true // support ECMAScript modules
182-
}
183-
}
184-
```

docs/src/content/docs/openapi-fetch/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ description: Get Started with openapi-fetch
55

66
<img src="/assets/openapi-fetch.svg" alt="openapi-fetch" width="216" height="40" />
77

8-
openapi-fetch applies your OpenAPI types to the native fetch API via TypeScript. Weighs in at **1 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
8+
openapi-fetch applies your OpenAPI types to the native fetch API via TypeScript. Weighs in at **2 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
99

1010
| Library | Size (min) |
1111
| :----------------------------- | ---------: |
12-
| **openapi-fetch** | `1 kB` |
12+
| **openapi-fetch** | `2 kB` |
1313
| **openapi-typescript-fetch** | `4 kB` |
1414
| **openapi-typescript-codegen** | `345 kB` |
1515

docs/src/styles/_base.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ thead th {
246246
}
247247

248248
tbody tr:not(:first-of-type) td {
249-
border-top: 1px solid hsla(var(--color-gray-90), 100%);
249+
border-top: 1px solid hsla(var(--color-gray-90), 50%);
250250
}
251251

252252
td,

docs/src/styles/_theme.scss

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
:root {
2-
--font-fallback: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji,
3-
Segoe UI Emoji;
2+
--font-fallback: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji;
43
--font-body: "Neue Montreal", system-ui, var(--font-fallback);
5-
--font-mono: "IBM Plex Mono", Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter",
6-
"DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier,
4+
--font-mono: "IBM Plex Mono", Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier,
75
monospace;
86

97
/*
@@ -97,15 +95,15 @@ body {
9795

9896
/* @@@: not used anywhere */
9997
--theme-text-lighter: hsla(var(--color-gray-40), 1);
100-
--theme-bg: hsla(215, 28%, 17%, 1);
101-
--theme-bg-hover: hsla(var(--color-gray-40), 1);
98+
--theme-bg: hsla(215, 10%, 10%, 1);
99+
--theme-bg-hover: hsla(var(--color-gray-20), 1);
102100
--theme-bg-offset: hsla(var(--color-gray-5), 1);
103101
--theme-code-inline-bg: hsla(var(--color-gray-10), 1);
104102
--theme-code-inline-bg-darker: hsla(var(--color-gray-10), 1);
105103
--theme-code-inline-text: hsla(var(--color-base-white), 100%, 1);
106104
--theme-code-bg: hsla(var(--color-gray-5), 1);
107105
--theme-code-text: hsla(var(--color-base-white), 100%, 1);
108-
--theme-navbar-bg: hsla(215, 28%, 17%, 1);
106+
--theme-navbar-bg: hsla(215, 10%, 10%, 1);
109107
--theme-selection-color: hsla(var(--color-base-white), 100%, 1);
110108
--theme-selection-bg: hsla(var(--color-purple), var(--theme-accent-opacity));
111109

packages/openapi-fetch/README.md

+4-131
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<img src="../../docs/public/assets/openapi-fetch.svg" alt="openapi-fetch" width="216" height="40" />
22

3-
openapi-fetch applies your OpenAPI types to the native fetch API via TypeScript. Weighs in at **1 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
3+
openapi-fetch applies your OpenAPI types to the native fetch API via TypeScript. Weighs in at **2 kb** and has virtually zero runtime. Works with React, Vue, Svelte, or vanilla JS.
44

55
| Library | Size (min) |
66
| :----------------------------- | ---------: |
7-
| **openapi-fetch** | `1 kB` |
7+
| **openapi-fetch** | `2 kB` |
88
| **openapi-typescript-fetch** | `4 kB` |
99
| **openapi-typescript-codegen** | `345 kB` |
1010

@@ -104,133 +104,6 @@ const { data, error } = await put("/blogposts", {
104104
});
105105
```
106106

107-
### Pathname
107+
## 📓 Docs
108108

109-
The pathname of `get()`, `put()`, `post()`, etc. **must match your schema literally.** Note in the example, the URL is `/blogposts/{post_id}`. This library will replace all `path` params for you (so they can be typechecked)
110-
111-
> **Tip**
112-
>
113-
> openapi-fetch infers types from the URL. Prefer static string values over dynamic runtime values, e.g.:
114-
>
115-
> - ✅ `"/blogposts/{post_id}"`
116-
> - ❌ `[...pathParts].join("/") + "{post_id}"`
117-
118-
### Request
119-
120-
The `get()` request shown needed the `params` object that groups <a href="https://spec.openapis.org/oas/latest.html#parameter-object" target="_blank" rel="noopener noreferrer">parameters by type</a> (`path` or `query`). If a required param is missing, or the wrong type, a type error will be thrown.
121-
122-
The `post()` request required a `body` object that provided all necessary <a href="https://spec.openapis.org/oas/latest.html#request-body-object" target="_blank" rel="noopener noreferrer">requestBody</a> data.
123-
124-
### Response
125-
126-
All methods return an object with **data**, **error**, and **response**.
127-
128-
- **data** will contain that endpoint’s `2xx` response if the server returned `2xx`; otherwise it will be `undefined`
129-
- **error** likewise contains that endpoint’s `4xx`/`5xx` response if the server returned either; otherwise it will be `undefined`
130-
- _Note: `default` will also be interpreted as `error`, since its intent is handling unexpected HTTP codes_
131-
- **response** has response info like `status`, `headers`, etc. It is not typechecked.
132-
133-
## Version Support
134-
135-
openapi-fetch implements the [native fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) which is available in all major browsers.
136-
137-
If using in a Node.js environment, version 18 or greater is recommended (newer is better).
138-
139-
TypeScript support is pretty far-reaching as this library doesn’t use any cutting-edge features, but using the latest version of TypeScript is always recommended for accuracy.
140-
141-
## API
142-
143-
### Create Client
144-
145-
**createClient** accepts the following options, which set the default settings for all subsequent fetch calls.
146-
147-
```ts
148-
createClient<paths>(options);
149-
```
150-
151-
| Name | Type | Description |
152-
| :---------------- | :-------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
153-
| `baseUrl` | `string` | Prefix all fetch URLs with this option (e.g. `"https://myapi.dev/v1/"`). |
154-
| `fetch` | `fetch` | Fetch function used for requests (defaults to `globalThis.fetch`) |
155-
| `querySerializer` | QuerySerializer | (optional) Serialize query params for all requests (default: `new URLSearchParams()`) |
156-
| `bodySerializer` | BodySerializer | (optional) Serialize request body object for all requests (default: `JSON.stringify()`) |
157-
| (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal` …) (<a href="https://developer.mozilla.org/en-US/docs/Web/API/fetch#options" target="_blank" rel="noopener noreferrer">docs</a>) |
158-
159-
### Fetch options
160-
161-
The following options apply to all request methods (`.get()`, `.post()`, etc.)
162-
163-
```ts
164-
client.get("/my-url", options);
165-
```
166-
167-
| Name | Type | Description |
168-
| :---------------- | :---------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
169-
| `params` | ParamsObject | Provide `path` and `query` params from the OpenAPI schema |
170-
| `params.path` | `{ [name]: value }` | Provide all `path` params (params that are part of the URL) |
171-
| `params.query` | `{ [name]: value }` | Provide all `query params (params that are part of the <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams" target="_blank" rel="noopener noreferrer">searchParams</a> |
172-
| `body` | `{ [name]:value }` | The <a href="https://spec.openapis.org/oas/latest.html#request-body-object" target="_blank" rel="noopener noreferrer">requestBody</a> data, if needed (PUT/POST/PATCH/DEL only) |
173-
| `querySerializer` | QuerySerializer | (optional) Serialize query params for this request only (default: `new URLSearchParams()`) |
174-
| `bodySerializer` | BodySerializer | (optional) Serialize request body for this request only (default: `JSON.stringify()`) |
175-
| `parseAs` | `"json"` \| `"text"` \| `"arrayBuffer"` \| `"blob"` \| `"stream"` | Parse the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Response/body" target="_blank" rel="noopener noreferrer">response body</a>, with `"stream"` skipping processing altogether (default: `"json"`) |
176-
| (Fetch options) | | Any valid fetch option (`headers`, `mode`, `cache`, `signal` …) (<a href="https://developer.mozilla.org/en-US/docs/Web/API/fetch#options" target="_blank" rel="noopener noreferrer">docs</a>) |
177-
178-
### querySerializer
179-
180-
This library uses <a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams" target="_blank" rel="noopener noreferrer">URLSearchParams</a> to <a href="https://swagger.io/docs/specification/serialization/" target="_blank" rel="noopener noreferrer">serialize query parameters</a>. For complex query param types (e.g. arrays) you’ll need to provide your own `querySerializer()` method that transforms query params into a URL-safe string:
181-
182-
```ts
183-
const { data, error } = await get("/search", {
184-
params: {
185-
query: { tags: ["food", "california", "healthy"] },
186-
},
187-
querySerializer(q) {
188-
let s = "";
189-
for (const [k, v] of Object.entries(q)) {
190-
if (Array.isArray(v)) {
191-
s += `${k}[]=${v.join(",")}`;
192-
} else {
193-
s += `${k}=${v}`;
194-
}
195-
}
196-
return s; // ?tags[]=food&tags[]=california&tags[]=healthy
197-
},
198-
});
199-
```
200-
201-
### bodySerializer
202-
203-
Similar to [querySerializer](#querySerializer), bodySerializer works for requestBody. You probably only need this when using `multipart/form-data`:
204-
205-
```ts
206-
const { data, error } = await put("/submit", {
207-
body: {
208-
name: "",
209-
query: { version: 2 },
210-
},
211-
bodySerializer(body) {
212-
const fd = new FormData();
213-
for (const [k, v] of Object.entries(body)) {
214-
fd.append(k, v);
215-
}
216-
return fd;
217-
},
218-
});
219-
```
220-
221-
If your `body` is already a `FormData`, provide an identity function:
222-
223-
```ts
224-
const { data, error } = await put("/submit", {
225-
body: myFormData,
226-
bodySerializer: (body) => body,
227-
});
228-
```
229-
230-
For `multipart/form-data`, [do not set the `Content-Type` header](https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects#sending_files_using_a_formdata_object). The browser will set that for you, along with the boundary expression, which serves as a delimiter for the form fields.
231-
232-
## 🎯 Project Goals
233-
234-
1. Infer types automatically from OpenAPI schemas **without generics** (or, only the absolute minimum needed)
235-
2. Respect the native `fetch()` API while reducing boilerplate (such as `await res.json()`)
236-
3. Be as small and light as possible
109+
[View Docs](https://openapi-ts.pages.dev/openapi-fetch/)

0 commit comments

Comments
 (0)