react-router-define-api 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +11 -66
  2. package/package.json +10 -2
package/README.md CHANGED
@@ -16,103 +16,48 @@ npm install react-router-define-api
16
16
  // app/routes/api.users.ts
17
17
  import { defineApi } from 'react-router-define-api';
18
18
 
19
- const api = defineApi({
19
+ export const { loader, action } = defineApi({
20
20
  GET: async ({ params }) => {
21
21
  const users = await db.users.findMany();
22
22
  return { users };
23
23
  },
24
24
  POST: async ({ request }) => {
25
25
  const body = await request.formData();
26
- const user = await db.users.create({ name: body.get('name') });
27
- return { user };
26
+ return { user: await db.users.create({ name: body.get('name') }) };
28
27
  },
29
28
  DELETE: async ({ params }) => {
30
29
  await db.users.delete(params.id);
31
30
  return { deleted: true };
32
31
  },
33
32
  });
34
-
35
- export const loader = api.loader;
36
- export const action = api.action;
37
33
  ```
38
34
 
39
35
  ### How it works
40
36
 
41
- - `GET` handler exported as `loader`
42
- - `POST`, `PUT`, `PATCH`, `DELETE` handlers → dispatched inside `action` by `request.method`
43
- - Undefined methods return a `405 Method Not Allowed` response
44
-
45
- ### All methods
46
-
47
- ```ts
48
- const api = defineApi({
49
- GET: async (args) => {
50
- /* ... */
51
- },
52
- POST: async (args) => {
53
- /* ... */
54
- },
55
- PUT: async (args) => {
56
- /* ... */
57
- },
58
- PATCH: async (args) => {
59
- /* ... */
60
- },
61
- DELETE: async (args) => {
62
- /* ... */
63
- },
64
- });
65
- ```
66
-
67
- ## API
68
-
69
- ### `defineApi(handlers)`
37
+ - `GET` → `loader`
38
+ - `POST`, `PUT`, `PATCH`, `DELETE` → dispatched inside `action` by `request.method`
39
+ - Undefined methods `405 Method Not Allowed`
70
40
 
71
- | Parameter | Type | Description |
72
- | ---------- | ------------- | ---------------------------------------- |
73
- | `handlers` | `ApiHandlers` | Map of HTTP methods to handler functions |
74
-
75
- Returns `{ loader, action }` — each is `undefined` if no relevant methods are defined.
76
-
77
- Handler args are the same as React Router's `LoaderFunctionArgs` / `ActionFunctionArgs`:
78
-
79
- - `request` — the incoming `Request` object
80
- - `params` — route parameters
81
- - `context` — app context
82
-
83
- ## TypeScript
41
+ ### Response type helpers
84
42
 
85
- Full type inference — return types are inferred from your handler functions.
43
+ Access inferred response types for client-side fetchers or shared contracts:
86
44
 
87
45
  ```ts
88
46
  const api = defineApi({
89
47
  GET: async ({ params }) => ({ id: params.id, name: 'John' }),
90
- POST: async ({ request }) => {
91
- const body = await request.formData();
92
- return { created: true, name: body.get('name') };
93
- },
48
+ POST: async () => ({ created: true }),
94
49
  });
95
50
 
96
- // api.loader return type is inferred as { id: string | undefined, name: string }
97
- // api.action is undefined when no action methods defined
98
- ```
99
-
100
- ### Response type helpers
51
+ export const { loader, action } = api;
101
52
 
102
- Access inferred response types via `typeof api.*Response` — useful for typing client-side fetchers or shared contracts:
103
-
104
- ```ts
105
53
  type GetRes = typeof api.GetResponse;
106
54
  // → { id: string | undefined; name: string }
107
55
 
108
56
  type PostRes = typeof api.PostResponse;
109
- // → { created: boolean; name: FormDataEntryValue | null }
110
-
111
- // Undefined methods → never
112
- type PutRes = typeof api.PutResponse; // → never
57
+ // → { created: boolean }
113
58
  ```
114
59
 
115
- These are **type-only** — zero runtime cost.
60
+ These are **type-only** — zero runtime cost. Undefined methods resolve to `never`.
116
61
 
117
62
  ## License
118
63
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-router-define-api",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Define HTTP method handlers for React Router v7 routes",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -49,5 +49,13 @@
49
49
  "typescript": "^6.0.2",
50
50
  "ultracite": "^7.4.0",
51
51
  "vitest": "^4.1.2"
52
- }
52
+ },
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "https://github.com/sonicname/react-router-define-api.git"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/sonicname/react-router-define-api/issues"
59
+ },
60
+ "homepage": "https://github.com/sonicname/react-router-define-api#readme"
53
61
  }