react-router-define-api 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +119 -0
- package/dist/index.cjs +50 -0
- package/dist/index.d.cts +69 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.js +23 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# react-router-define-api
|
|
2
|
+
|
|
3
|
+
Define HTTP method handlers for React Router v7 routes — no more manual `request.method` checks.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install react-router-define-api
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Peer dependency:** `react-router@^7.0.0`
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// app/routes/api.users.ts
|
|
17
|
+
import { defineApi } from 'react-router-define-api';
|
|
18
|
+
|
|
19
|
+
const api = defineApi({
|
|
20
|
+
GET: async ({ params }) => {
|
|
21
|
+
const users = await db.users.findMany();
|
|
22
|
+
return { users };
|
|
23
|
+
},
|
|
24
|
+
POST: async ({ request }) => {
|
|
25
|
+
const body = await request.formData();
|
|
26
|
+
const user = await db.users.create({ name: body.get('name') });
|
|
27
|
+
return { user };
|
|
28
|
+
},
|
|
29
|
+
DELETE: async ({ params }) => {
|
|
30
|
+
await db.users.delete(params.id);
|
|
31
|
+
return { deleted: true };
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const loader = api.loader;
|
|
36
|
+
export const action = api.action;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### How it works
|
|
40
|
+
|
|
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)`
|
|
70
|
+
|
|
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
|
|
84
|
+
|
|
85
|
+
Full type inference — return types are inferred from your handler functions.
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const api = defineApi({
|
|
89
|
+
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
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
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
|
|
101
|
+
|
|
102
|
+
Access inferred response types via `typeof api.*Response` — useful for typing client-side fetchers or shared contracts:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
type GetRes = typeof api.GetResponse;
|
|
106
|
+
// → { id: string | undefined; name: string }
|
|
107
|
+
|
|
108
|
+
type PostRes = typeof api.PostResponse;
|
|
109
|
+
// → { created: boolean; name: FormDataEntryValue | null }
|
|
110
|
+
|
|
111
|
+
// Undefined methods → never
|
|
112
|
+
type PutRes = typeof api.PutResponse; // → never
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
These are **type-only** — zero runtime cost.
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
defineApi: () => defineApi
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/define-api.ts
|
|
28
|
+
var ACTION_METHODS = [
|
|
29
|
+
"POST",
|
|
30
|
+
"PUT",
|
|
31
|
+
"PATCH",
|
|
32
|
+
"DELETE"
|
|
33
|
+
];
|
|
34
|
+
function defineApi(handlers) {
|
|
35
|
+
const loader = handlers.GET ?? void 0;
|
|
36
|
+
const hasActionHandlers = ACTION_METHODS.some((m) => handlers[m] != null);
|
|
37
|
+
const action = hasActionHandlers ? async (args) => {
|
|
38
|
+
const method = args.request.method.toUpperCase();
|
|
39
|
+
const handler = handlers[method];
|
|
40
|
+
if (typeof handler === "function") {
|
|
41
|
+
return handler(args);
|
|
42
|
+
}
|
|
43
|
+
throw new Response("Method Not Allowed", { status: 405 });
|
|
44
|
+
} : void 0;
|
|
45
|
+
return { loader, action };
|
|
46
|
+
}
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
defineApi
|
|
50
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { LoaderFunctionArgs, ActionFunctionArgs } from 'react-router';
|
|
2
|
+
|
|
3
|
+
/** Supported HTTP methods */
|
|
4
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
5
|
+
/** Handler function for a given HTTP method */
|
|
6
|
+
type MethodHandler<Args, R = unknown> = (args: Args) => R | Promise<R>;
|
|
7
|
+
/** Map of HTTP method handlers passed to defineApi */
|
|
8
|
+
type ApiHandlers = {
|
|
9
|
+
GET?: (args: LoaderFunctionArgs) => unknown;
|
|
10
|
+
POST?: (args: ActionFunctionArgs) => unknown;
|
|
11
|
+
PUT?: (args: ActionFunctionArgs) => unknown;
|
|
12
|
+
PATCH?: (args: ActionFunctionArgs) => unknown;
|
|
13
|
+
DELETE?: (args: ActionFunctionArgs) => unknown;
|
|
14
|
+
};
|
|
15
|
+
/** Extract the awaited return type from a handler */
|
|
16
|
+
type HandlerReturn<T> = T extends (...args: never[]) => infer R ? Awaited<R> : never;
|
|
17
|
+
/** Checks if handler map includes any action methods */
|
|
18
|
+
type HasActionMethods<H> = 'POST' extends keyof H ? true : 'PUT' extends keyof H ? true : 'PATCH' extends keyof H ? true : 'DELETE' extends keyof H ? true : false;
|
|
19
|
+
/** Build union return type from all defined action handlers */
|
|
20
|
+
type ActionReturn<H> = (H extends {
|
|
21
|
+
POST: infer F;
|
|
22
|
+
} ? HandlerReturn<F> : never) | (H extends {
|
|
23
|
+
PUT: infer F;
|
|
24
|
+
} ? HandlerReturn<F> : never) | (H extends {
|
|
25
|
+
PATCH: infer F;
|
|
26
|
+
} ? HandlerReturn<F> : never) | (H extends {
|
|
27
|
+
DELETE: infer F;
|
|
28
|
+
} ? HandlerReturn<F> : never);
|
|
29
|
+
/** Extract response type for a specific method, or never if not defined */
|
|
30
|
+
type ResponseOf<H, M extends HttpMethod> = H extends Record<M, infer F> ? HandlerReturn<F> : never;
|
|
31
|
+
/** Return type of defineApi — loader/action presence and return types inferred from handlers */
|
|
32
|
+
type ApiExports<H extends ApiHandlers> = {
|
|
33
|
+
loader: H extends {
|
|
34
|
+
GET: infer F;
|
|
35
|
+
} ? (args: LoaderFunctionArgs) => Promise<HandlerReturn<F>> : undefined;
|
|
36
|
+
action: HasActionMethods<H> extends true ? (args: ActionFunctionArgs) => Promise<ActionReturn<H>> : undefined;
|
|
37
|
+
/** Inferred return type of the GET handler */
|
|
38
|
+
GetResponse: ResponseOf<H, 'GET'>;
|
|
39
|
+
/** Inferred return type of the POST handler */
|
|
40
|
+
PostResponse: ResponseOf<H, 'POST'>;
|
|
41
|
+
/** Inferred return type of the PUT handler */
|
|
42
|
+
PutResponse: ResponseOf<H, 'PUT'>;
|
|
43
|
+
/** Inferred return type of the PATCH handler */
|
|
44
|
+
PatchResponse: ResponseOf<H, 'PATCH'>;
|
|
45
|
+
/** Inferred return type of the DELETE handler */
|
|
46
|
+
DeleteResponse: ResponseOf<H, 'DELETE'>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Define HTTP method handlers for a React Router v7 route.
|
|
51
|
+
* Returns `{ loader, action }` ready to export from a route module.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const api = defineApi({
|
|
56
|
+
* GET: async ({ params }) => ({ user: params.id }),
|
|
57
|
+
* POST: async ({ request }) => {
|
|
58
|
+
* const body = await request.formData();
|
|
59
|
+
* return { created: true };
|
|
60
|
+
* },
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* export const loader = api.loader;
|
|
64
|
+
* export const action = api.action;
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function defineApi<const H extends ApiHandlers>(handlers: H): ApiExports<H>;
|
|
68
|
+
|
|
69
|
+
export { type ApiHandlers, type HttpMethod, type MethodHandler, defineApi };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { LoaderFunctionArgs, ActionFunctionArgs } from 'react-router';
|
|
2
|
+
|
|
3
|
+
/** Supported HTTP methods */
|
|
4
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
5
|
+
/** Handler function for a given HTTP method */
|
|
6
|
+
type MethodHandler<Args, R = unknown> = (args: Args) => R | Promise<R>;
|
|
7
|
+
/** Map of HTTP method handlers passed to defineApi */
|
|
8
|
+
type ApiHandlers = {
|
|
9
|
+
GET?: (args: LoaderFunctionArgs) => unknown;
|
|
10
|
+
POST?: (args: ActionFunctionArgs) => unknown;
|
|
11
|
+
PUT?: (args: ActionFunctionArgs) => unknown;
|
|
12
|
+
PATCH?: (args: ActionFunctionArgs) => unknown;
|
|
13
|
+
DELETE?: (args: ActionFunctionArgs) => unknown;
|
|
14
|
+
};
|
|
15
|
+
/** Extract the awaited return type from a handler */
|
|
16
|
+
type HandlerReturn<T> = T extends (...args: never[]) => infer R ? Awaited<R> : never;
|
|
17
|
+
/** Checks if handler map includes any action methods */
|
|
18
|
+
type HasActionMethods<H> = 'POST' extends keyof H ? true : 'PUT' extends keyof H ? true : 'PATCH' extends keyof H ? true : 'DELETE' extends keyof H ? true : false;
|
|
19
|
+
/** Build union return type from all defined action handlers */
|
|
20
|
+
type ActionReturn<H> = (H extends {
|
|
21
|
+
POST: infer F;
|
|
22
|
+
} ? HandlerReturn<F> : never) | (H extends {
|
|
23
|
+
PUT: infer F;
|
|
24
|
+
} ? HandlerReturn<F> : never) | (H extends {
|
|
25
|
+
PATCH: infer F;
|
|
26
|
+
} ? HandlerReturn<F> : never) | (H extends {
|
|
27
|
+
DELETE: infer F;
|
|
28
|
+
} ? HandlerReturn<F> : never);
|
|
29
|
+
/** Extract response type for a specific method, or never if not defined */
|
|
30
|
+
type ResponseOf<H, M extends HttpMethod> = H extends Record<M, infer F> ? HandlerReturn<F> : never;
|
|
31
|
+
/** Return type of defineApi — loader/action presence and return types inferred from handlers */
|
|
32
|
+
type ApiExports<H extends ApiHandlers> = {
|
|
33
|
+
loader: H extends {
|
|
34
|
+
GET: infer F;
|
|
35
|
+
} ? (args: LoaderFunctionArgs) => Promise<HandlerReturn<F>> : undefined;
|
|
36
|
+
action: HasActionMethods<H> extends true ? (args: ActionFunctionArgs) => Promise<ActionReturn<H>> : undefined;
|
|
37
|
+
/** Inferred return type of the GET handler */
|
|
38
|
+
GetResponse: ResponseOf<H, 'GET'>;
|
|
39
|
+
/** Inferred return type of the POST handler */
|
|
40
|
+
PostResponse: ResponseOf<H, 'POST'>;
|
|
41
|
+
/** Inferred return type of the PUT handler */
|
|
42
|
+
PutResponse: ResponseOf<H, 'PUT'>;
|
|
43
|
+
/** Inferred return type of the PATCH handler */
|
|
44
|
+
PatchResponse: ResponseOf<H, 'PATCH'>;
|
|
45
|
+
/** Inferred return type of the DELETE handler */
|
|
46
|
+
DeleteResponse: ResponseOf<H, 'DELETE'>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Define HTTP method handlers for a React Router v7 route.
|
|
51
|
+
* Returns `{ loader, action }` ready to export from a route module.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const api = defineApi({
|
|
56
|
+
* GET: async ({ params }) => ({ user: params.id }),
|
|
57
|
+
* POST: async ({ request }) => {
|
|
58
|
+
* const body = await request.formData();
|
|
59
|
+
* return { created: true };
|
|
60
|
+
* },
|
|
61
|
+
* });
|
|
62
|
+
*
|
|
63
|
+
* export const loader = api.loader;
|
|
64
|
+
* export const action = api.action;
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function defineApi<const H extends ApiHandlers>(handlers: H): ApiExports<H>;
|
|
68
|
+
|
|
69
|
+
export { type ApiHandlers, type HttpMethod, type MethodHandler, defineApi };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/define-api.ts
|
|
2
|
+
var ACTION_METHODS = [
|
|
3
|
+
"POST",
|
|
4
|
+
"PUT",
|
|
5
|
+
"PATCH",
|
|
6
|
+
"DELETE"
|
|
7
|
+
];
|
|
8
|
+
function defineApi(handlers) {
|
|
9
|
+
const loader = handlers.GET ?? void 0;
|
|
10
|
+
const hasActionHandlers = ACTION_METHODS.some((m) => handlers[m] != null);
|
|
11
|
+
const action = hasActionHandlers ? async (args) => {
|
|
12
|
+
const method = args.request.method.toUpperCase();
|
|
13
|
+
const handler = handlers[method];
|
|
14
|
+
if (typeof handler === "function") {
|
|
15
|
+
return handler(args);
|
|
16
|
+
}
|
|
17
|
+
throw new Response("Method Not Allowed", { status: 405 });
|
|
18
|
+
} : void 0;
|
|
19
|
+
return { loader, action };
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
defineApi
|
|
23
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-router-define-api",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Define HTTP method handlers for React Router v7 routes",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"keywords": [
|
|
27
|
+
"react-router",
|
|
28
|
+
"react-router-v7",
|
|
29
|
+
"loader",
|
|
30
|
+
"action",
|
|
31
|
+
"api",
|
|
32
|
+
"http-methods"
|
|
33
|
+
],
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react-router": "^7.0.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:watch": "vitest",
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"lint": "npm exec -- ultracite check",
|
|
43
|
+
"lint:fix": "npm exec -- ultracite fix",
|
|
44
|
+
"prepublishOnly": "npm run build"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"react-router": "^7.13.2",
|
|
48
|
+
"tsup": "^8.5.1",
|
|
49
|
+
"typescript": "^6.0.2",
|
|
50
|
+
"ultracite": "^7.4.0",
|
|
51
|
+
"vitest": "^4.1.2"
|
|
52
|
+
}
|
|
53
|
+
}
|