Skip to content

Commit 566e95f

Browse files
committed
docs: add migration guide from v1 to v2 with code examples
1 parent e13fc6c commit 566e95f

File tree

3 files changed

+149
-6
lines changed

3 files changed

+149
-6
lines changed

docs/migrations/v1-to-v2.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Migration from v1 to v2
2+
3+
- Removed package `@yme/react-api`
4+
- Initial client from `createApi` to `new ApiClient()`
5+
6+
```ts
7+
// Before
8+
import { createApi } from "@yme/api";
9+
const api = createApi({
10+
http,
11+
});
12+
13+
// After
14+
import { ApiClient } from "@yme/api/client";
15+
const api = new ApiClient({
16+
action,
17+
});
18+
```
19+
20+
- Change path params define from `:param` to `[param]`
21+
22+
```ts
23+
// Before
24+
const api = createApi({
25+
routes: {
26+
users: {
27+
one: r.get("/users/:id"),
28+
},
29+
},
30+
});
31+
32+
// After
33+
const api = new ApiClient({});
34+
const users = {
35+
one: api.get("/users/[id]"),
36+
};
37+
```
38+
39+
- Returns a pure function instead of a route configuration
40+
41+
```ts
42+
// Before
43+
const api = new ApiClient({
44+
routes: {
45+
users: {
46+
one: r.get("/users/[id]"),
47+
},
48+
},
49+
});
50+
api.users.one({ id: 1 });
51+
52+
// After
53+
const api = new ApiClient({});
54+
const users = {
55+
one: api.get("/users/[id]"),
56+
};
57+
users.one({ id: 1 });
58+
```
59+
60+
- Move path params replacement to the middleware `replacePathParams`
61+
62+
```ts
63+
// Before
64+
const api = createApi({ ... });
65+
api.users.one({ id: 1 }); // /users/1
66+
67+
// After
68+
const api = new ApiClient({
69+
// ...
70+
middlewares: [
71+
replacePathParams(),
72+
],
73+
});
74+
const users = {
75+
one: api.get("/users/[id]"),
76+
};
77+
users.one({ id: 1 }); // /users/1
78+
```
79+
80+
- Add middleware support
81+
82+
```ts
83+
import { logger } from "@yem/api/middleware";
84+
import { replacePathParams } from "@yem/api/client";
85+
86+
const api = new ApiClient({
87+
middlewares: [
88+
logger(),
89+
replacePathParams(),
90+
// ...
91+
],
92+
});
93+
```
94+
95+
- Add server action for next.js.
96+
97+
```ts
98+
import { NextAction } from "@yme/api/next";
99+
const action = new NextAction({
100+
middlewares: [
101+
// ...
102+
],
103+
});
104+
105+
const createUser = action
106+
.post({
107+
// initial
108+
})
109+
.bindArgs([z.string()])
110+
.validator(
111+
z.object({
112+
name: z.string().min(1).max(255),
113+
})
114+
)
115+
.action(async (req) => {
116+
// ^type: { input, parsedInput: { name: string }, parsedBindArgs: [string] }
117+
// do some logic and return response
118+
return { id: "1" };
119+
});
120+
121+
const createUserWithOrg = createUser.bind(null, "Organization");
122+
createUserWithOrg({ name: "John" }); // { id: '1' }
123+
```

packages/api/readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
> This is v2 of `@yme/api`. If you are looking for v1, please visit [v1 branch](https://github.com/minosss/api/tree/v1).
2-
31
Hey 👋, `@yme/api` is a package that defines the type-safe API requests. No server required and zero dependencies.
42

53
> If you are developing a full-stack web application, you should take a look [tRPC](https://trpc.io/).
@@ -18,8 +16,20 @@ Hey 👋, `@yme/api` is a package that defines the type-safe API requests. No se
1816
[input] -> [middlewares, action] -> [output]
1917
```
2018

19+
## Migration from v1 to v2
20+
21+
Check out the [migration guide](./docs/migrations/v1-to-v2.md).
22+
23+
## Installation
24+
25+
```bash
26+
npm install @yme/api
27+
```
28+
2129
## Quick Start
2230

31+
Use `ApiClient` with `fetch` (Action first)
32+
2333
```ts
2434
import { ApiClient, replacePathParams } from "@yme/api/client";
2535
import { logger } from "@yme/api/middleware";
@@ -68,7 +78,7 @@ const newUserId = await createUser(
6878
);
6979
```
7080

71-
Use Next.js (Server Action)
81+
Use `NextAction` for Next.js server action (Action last)
7282

7383
```ts
7484
import { NextAction } from "@yme/api/next";

readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
> This is v2 of `@yme/api`. If you are looking for v1, please visit [v1 branch](https://github.com/minosss/api/tree/v1).
2-
31
Hey 👋, `@yme/api` is a package that defines the type-safe API requests. No server required and zero dependencies.
42

53
> If you are developing a full-stack web application, you should take a look [tRPC](https://trpc.io/).
@@ -18,8 +16,20 @@ Hey 👋, `@yme/api` is a package that defines the type-safe API requests. No se
1816
[input] -> [middlewares, action] -> [output]
1917
```
2018

19+
## Migration from v1 to v2
20+
21+
Check out the [migration guide](./docs/migrations/v1-to-v2.md).
22+
23+
## Installation
24+
25+
```bash
26+
npm install @yme/api
27+
```
28+
2129
## Quick Start
2230

31+
Use `ApiClient` with `fetch` (Action first)
32+
2333
```ts
2434
import { ApiClient, replacePathParams } from "@yme/api/client";
2535
import { logger } from "@yme/api/middleware";
@@ -68,7 +78,7 @@ const newUserId = await createUser(
6878
);
6979
```
7080

71-
Use Next.js (Server Action)
81+
Use `NextAction` for Next.js server action (Action last)
7282

7383
```ts
7484
import { NextAction } from "@yme/api/next";

0 commit comments

Comments
 (0)