Skip to content

Commit 45a3760

Browse files
committed
update doc
1 parent fbcf58f commit 45a3760

16 files changed

+158
-158
lines changed

pkgs/docs/docs/02_getting-started.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ sidebar_position: 2
44

55
# Getting Started
66

7-
Let's assume you have a npm project with code like this that __fetch__ from GitHub:
7+
Let's assume you have a npm project with code like this that **fetch** from GitHub:
88

99
```typescript
1010
const GITHUB_API_ORIGIN = "https://api.github.com";
1111
const url = `/repos/mpppk/typed-api-spec/topics?page=1`;
1212
const response = await fetch(`${GITHUB_API_ORIGIN}${url}`);
1313
if (!response.ok) {
1414
// response.json() returns any
15-
const { message } = await response.json()
15+
const { message } = await response.json();
1616
return console.error(message);
1717
}
1818
// response.json() returns any
19-
const { names } = await response.json()
19+
const { names } = await response.json();
2020
console.log(names); // => ["api-spec", "fetch", "typescript"]
2121
```
2222

2323
## Installation
2424

2525
```bash
26-
npm install @mpppk/typed-api-spec
26+
npm install @notainc/typed-api-spec
2727
```
2828

2929
## Define API Spec
@@ -34,8 +34,8 @@ type Spec = DefineApiEndpoints<{
3434
get: {
3535
query: { page?: string };
3636
responses: {
37-
200: { body: { names: string[] }; };
38-
400: { body: { message: string; }; };
37+
200: { body: { names: string[] } };
38+
400: { body: { message: string } };
3939
};
4040
};
4141
};
@@ -45,7 +45,7 @@ type Spec = DefineApiEndpoints<{
4545
## Add types to fetch function
4646

4747
```typescript
48-
import { fetchT } from "@mpppk/typed-api-spec";
48+
import { fetchT } from "@notainc/typed-api-spec";
4949
const fetch = fetch as FetchT<typeof GITHUB_API_ORIGIN, Spec>;
5050
```
5151

@@ -58,15 +58,15 @@ const fetchT = fetch as FetchT<typeof GITHUB_API_ORIGIN, Spec>;
5858
const response = await fetchT(`${GITHUB_API_ORIGIN}${url}`);
5959
if (!response.ok) {
6060
// response.json() is typed as { message: string } because response is not ok
61-
const { message } = await response.json()
61+
const { message } = await response.json();
6262
return console.error(message);
6363
}
6464
// response.json() is typed as { names: string[] } because response is ok
65-
const { names } = await response.json()
65+
const { names } = await response.json();
6666
console.log(names); // => ["api-spec", "fetch", "typescript"]
6767
```
6868

69-
Notice that only few (highlighted) lines have been changed from original, but now the __fetch__ is type-safe.
69+
Notice that only few (highlighted) lines have been changed from original, but now the **fetch** is type-safe.
7070

7171
## Playground
7272

pkgs/docs/docs/03_api-specification.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ type Spec = DefineApiEndpoints<{
2424
// Method
2525
get: {
2626
// Spec
27-
responses: { 200: { body: { userNames: string[] }; }; };
27+
responses: { 200: { body: { userNames: string[] } } };
2828
};
2929
};
3030
}>;
3131
```
3232

3333
the above code defines:
34+
3435
- Path: `/users`
3536
- Method: `get`
3637
- Spec: `{ responses: { 200: { body: { userNames: string[] }; }; }; }`
@@ -43,13 +44,12 @@ Path is the path of the API endpoint.
4344
It can contain path parameters like `:id`.
4445

4546
```typescript
46-
4747
type Spec = DefineApiEndpoints<{
4848
// Path has path parameter `:id`
4949
"/users/:id": {
5050
get: {
5151
params: { id: string };
52-
responses: { 200: { body: { user: { id: string; name: string }; }; }; };
52+
responses: { 200: { body: { user: { id: string; name: string } } } };
5353
};
5454
};
5555
}>;
@@ -59,6 +59,7 @@ type Spec = DefineApiEndpoints<{
5959

6060
Method is the HTTP method of the API endpoint.
6161
It can be one of the following:
62+
6263
- `get`
6364
- `post`
6465
- `put`
@@ -70,33 +71,36 @@ It can be one of the following:
7071
```typescript
7172
type Spec = DefineApiEndpoints<{
7273
"/users": {
73-
get: { responses: { 200: { body: { userNames: string[] }; }; }; };
74-
post: { responses: { 200: { body: { userName: string }; }; }; };
74+
get: { responses: { 200: { body: { userNames: string[] } } } };
75+
post: { responses: { 200: { body: { userName: string } } } };
7576
};
7677
}>;
7778
```
7879

7980
### Spec
8081

8182
Spec has the following properties:
83+
8284
- `params`: The path parameters of the request.
8385
- `query`: The query parameters of the request.
8486
- `headers`: The headers of the request.
8587
- `body`: The body of the request.
8688
- `responses`: The response schema of the request.
8789
- `body`: The body of the response.
8890
- `headers`: The headers of the response.
89-
91+
9092
```typescript
9193
type Spec = DefineApiEndpoints<{
9294
"/users/:id": {
9395
get: {
9496
params: { id: string };
9597
query: { page?: string };
9698
headers: { "x-api-key": string };
97-
responses: { 200: {
98-
headers: {"content-type": "application/json"};
99-
body: { userNames: string[] }; };
99+
responses: {
100+
200: {
101+
headers: { "content-type": "application/json" };
102+
body: { userNames: string[] };
103+
};
100104
};
101105
};
102106
};
@@ -110,7 +114,7 @@ For example, you can use zod to define the schema of the request and response.
110114

111115
```typescript
112116
import { z } from "zod";
113-
import { ZodApiEndpoints } from "./index";
117+
import { ApiEndpointsSchema } from "@notainc/typed-api-spec/core";
114118

115119
const Spec = {
116120
"/users/:id": {
@@ -122,11 +126,11 @@ const Spec = {
122126
200: {
123127
headers: { "content-type": z.literal("application/json") },
124128
body: { userNames: z.array(z.string()) },
125-
}
129+
},
126130
},
127131
},
128132
},
129-
} satisfies ZodApiEndpoints
133+
} satisfies ApiEndpointsSchema;
130134
```
131135

132136
For more information, see the [Validation](/docs/category/validation) page.
@@ -141,9 +145,9 @@ If you write wrong API specification, DefineApiEndpoints will throw a type error
141145
```typescript
142146
type Spec = DefineApiEndpoints<{
143147
"/users": {
144-
get: { responses: { 200: { body: { userNames: string[] }; }; }; };
148+
get: { responses: { 200: { body: { userNames: string[] } } } };
145149
};
146150
}>;
147151
```
148152

149-
TODO: Add StackBlitz demo
153+
TODO: Add StackBlitz demo

pkgs/docs/docs/04_client/overview.md

+29-28
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ zero-fetch just add type information to native fetch, and does not add any runti
1212
Type information is erased during compilation, so it does not affect the runtime behavior.
1313
As a result, it does not increase bundle size and does not have any runtime dependencies.
1414

15-
````typescript
15+
```typescript
1616
// fetchT is just native fetch, so it does not have any additional runtime dependencies
1717
const fetchT = fetch as FetchT<"", Spec>;
18-
````
18+
```
1919

2020
:::
2121

@@ -29,7 +29,7 @@ zero-fetch provides type information for the response data based on the API spec
2929
type Spec = DefineApiEndpoints<{
3030
"/users": {
3131
get: {
32-
responses: { 200: { body: { names: string[] }; }; };
32+
responses: { 200: { body: { names: string[] } } };
3333
};
3434
};
3535
}>;
@@ -42,18 +42,18 @@ const data = await res.json(); // data is { userNames: string[] }
4242
If the response have multiple status codes, response type is union of each status code type.
4343

4444
```typescript
45-
type Headers = { headers: { 'Content-Type': 'application/json' } };
45+
type Headers = { headers: { "Content-Type": "application/json" } };
4646
type Spec = DefineApiEndpoints<{
4747
"/users": {
4848
get: {
4949
responses: {
50-
200: { body: { names: string[] }; } & Headers;
51-
201: { body: { ok: boolean }; } & Headers;
52-
400: { body: { message: string; }; } & Headers;
53-
500: { body: { internalError: string; }; } & Headers;
50+
200: { body: { names: string[] } } & Headers;
51+
201: { body: { ok: boolean } } & Headers;
52+
400: { body: { message: string } } & Headers;
53+
500: { body: { internalError: string } } & Headers;
5454
};
5555
};
56-
}
56+
};
5757
}>;
5858

5959
const fetchT = fetch as FetchT<"", Spec>;
@@ -62,12 +62,12 @@ if (!res.ok) {
6262
// If res.ok is false, status code is 400 or 500
6363
// So res.json() returns { message: string } | { internalError: string }
6464
const data = await res.json();
65-
65+
6666
// Response headers are also type-checked. Content-Type is always 'application/json'
67-
const contentType: 'application/json' = res.headers.get('Content-Type');
67+
const contentType: "application/json" = res.headers.get("Content-Type");
6868
// and, hasContentType is inferred as true, not boolean
69-
const hasContentType: true = res.headers.has('Content-Type');
70-
69+
const hasContentType: true = res.headers.has("Content-Type");
70+
7171
return console.error(data);
7272
}
7373
// If res.ok is true, status code is 200 or 201
@@ -83,26 +83,24 @@ It means that you can not `append`, `set` or `delete` operation after the respon
8383
This is a limitation of the type system, not a runtime change. If you need mutable operations, you can cast types.
8484

8585
```typescript
86-
const immutableHeaders = res.headers
86+
const immutableHeaders = res.headers;
8787
const mutableHeaders = res.headers as Headers;
8888
```
8989

9090
:::
9191

92-
9392
### Path & Path parameters
9493

9594
zero-fetch accepts only the path that is defined in the API specification.
9695
Path parameters are also supported as `:paramName` in the path.
9796

9897
```typescript
99-
10098
type Spec = DefineApiEndpoints<{
10199
"/users": {
102-
get: { responses: { 200: { body: { names: string[] }; }; }; };
100+
get: { responses: { 200: { body: { names: string[] } } } };
103101
};
104102
"/users/:id": {
105-
get: { responses: { 200: { body: { name: string }; }; }; };
103+
get: { responses: { 200: { body: { name: string } } } };
106104
};
107105
}>;
108106
const fetchT = fetch as FetchT<"", Spec>;
@@ -122,7 +120,7 @@ type Spec = DefineApiEndpoints<{
122120
"/users": {
123121
get: {
124122
query: { page: string };
125-
responses: { 200: { body: { names: string[] }; }; };
123+
responses: { 200: { body: { names: string[] } } };
126124
};
127125
};
128126
}>;
@@ -142,7 +140,7 @@ type Spec = DefineApiEndpoints<{
142140
"/users": {
143141
get: {
144142
headers: { "x-api-key": string };
145-
responses: { 200: { body: { names: string[] }; }; };
143+
responses: { 200: { body: { names: string[] } } };
146144
};
147145
};
148146
}>;
@@ -158,19 +156,22 @@ zero-fetch accepts only the body that is defined in the API specification.
158156
Please note that when converting an object to a string, you must use the `JSONT` type provided by typed-api-spec.
159157

160158
```typescript
161-
import { JSONT } from "@mpppk/typed-api-spec/json";
159+
import { JSONT } from "@notainc/typed-api-spec/json";
162160
type Spec = DefineApiEndpoints<{
163161
"/users": {
164162
post: {
165163
body: { name: string };
166-
responses: { 200: { body: { id: string }; }; };
164+
responses: { 200: { body: { id: string } } };
167165
};
168166
};
169167
}>;
170168
const fetchT = fetch as FetchT<"", Spec>;
171169
const JSONT = JSON as JSONT;
172170

173-
await fetchT("/users", { method: "POST", body: JSONT.stringify({ name: "name" }) }); // OK
171+
await fetchT("/users", {
172+
method: "POST",
173+
body: JSONT.stringify({ name: "name" }),
174+
}); // OK
174175
await fetchT("/users", { method: "POST", body: JSONT.stringify({ name: 1 }) }); // Error: Type TypedString<{ userName: number; }> is not assignable to type TypedString<{ userName: string; }>
175176
```
176177

@@ -181,7 +182,6 @@ We hope to address this issue in the future, but for now, you must provide an em
181182

182183
:::
183184

184-
185185
## API
186186

187187
### FetchT
@@ -199,10 +199,11 @@ JSONT is a type that adds type information to native JSON.
199199
If you want to check body parameter type, you need to use JSONT.stringify to convert object to string.
200200

201201
```typescript
202-
import { JSONT } from "@mpppk/typed-api-spec/json";
202+
import { JSONT } from "@notainc/typed-api-spec/json";
203203
const JSONT = JSON as JSONT;
204204
// body parameter type will be checked by using JSONT.stringify
205-
await fetchT("/users", { method: "POST", body: JSONT.stringify({ name: "name" }) });
205+
await fetchT("/users", {
206+
method: "POST",
207+
body: JSONT.stringify({ name: "name" }),
208+
});
206209
```
207-
208-

0 commit comments

Comments
 (0)