@rest-rpc/next 0.1.0-beta.7 → 0.1.0-beta.9
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/README.md +63 -27
- package/dist/server.d.ts +5 -4
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,44 +2,80 @@
|
|
|
2
2
|
|
|
3
3
|
REST-shaped APIs with function-shaped TypeScript.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
options, and OpenAPI documents.
|
|
5
|
+
Define your HTTP API once as a shared TypeScript contract, then derive typed
|
|
6
|
+
server handlers, fetch clients, openAPI documents, and more from it.
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- Shared TypeScript contracts for HTTP APIs.
|
|
11
|
+
- Typed server handlers for Express, Hono, Fastify, Next.js, and Fetch runtimes.
|
|
12
|
+
- Typed fetch client.
|
|
13
|
+
- Typed TanStack Query helpers.
|
|
14
|
+
- Typed WebSockets, streaming, non-JSON requests/responses.
|
|
15
|
+
- OpenAPI documents generated from the TypeScript contract.
|
|
16
|
+
- Standard Schema support.
|
|
17
|
+
|
|
18
|
+
## Minimal Example
|
|
19
|
+
|
|
20
|
+
Define a contract
|
|
11
21
|
|
|
12
22
|
```ts
|
|
13
|
-
|
|
23
|
+
import { router } from "@rest-rpc/core";
|
|
24
|
+
import { z } from "zod";
|
|
25
|
+
|
|
26
|
+
export const api = router({
|
|
27
|
+
todos: {
|
|
28
|
+
get: {
|
|
29
|
+
method: "GET",
|
|
30
|
+
path: "/todos/:id",
|
|
31
|
+
response: z.object({
|
|
32
|
+
id: z.string(),
|
|
33
|
+
title: z.string(),
|
|
34
|
+
}),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
14
38
|
```
|
|
15
39
|
|
|
16
|
-
|
|
40
|
+
Implement the contract on the server
|
|
17
41
|
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
|
|
42
|
+
```ts
|
|
43
|
+
const routes = router(api, {
|
|
44
|
+
todos: {
|
|
45
|
+
get({ id }) {
|
|
46
|
+
return getTodo(id);
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
});
|
|
21
50
|
|
|
22
|
-
|
|
51
|
+
registerRoutes(app, routes);
|
|
52
|
+
```
|
|
23
53
|
|
|
24
|
-
|
|
25
|
-
- `@rest-rpc/express`: Express server adapter.
|
|
26
|
-
- `@rest-rpc/hono`: Hono server adapter.
|
|
27
|
-
- `@rest-rpc/fastify`: Fastify server adapter.
|
|
28
|
-
- `@rest-rpc/web`: Web `Request`/`Response` HTTP handler adapter.
|
|
29
|
-
- `@rest-rpc/next`: Next.js server adapter.
|
|
30
|
-
- `@rest-rpc/tanstack-query`: TanStack Query options and key helpers.
|
|
54
|
+
use the contract on the client with RPC-style function calls
|
|
31
55
|
|
|
32
|
-
|
|
33
|
-
|
|
56
|
+
```ts
|
|
57
|
+
import { initClient } from "@rest-rpc/core";
|
|
58
|
+
import { api } from "./contract";
|
|
34
59
|
|
|
35
|
-
|
|
60
|
+
const client = initClient(api, {
|
|
61
|
+
baseUrl: "https://api.example.com",
|
|
62
|
+
});
|
|
36
63
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
- Schema libraries, OpenAPI UI, middleware, auth, and app structure stay your choice.
|
|
64
|
+
const todo = await client.todos.get.fetch({
|
|
65
|
+
id: "todo_1",
|
|
66
|
+
});
|
|
67
|
+
```
|
|
42
68
|
|
|
43
69
|
## Documentation
|
|
44
70
|
|
|
45
|
-
[rest-rpc.dev](https://rest-rpc.dev)
|
|
71
|
+
Full documentation is available at [rest-rpc.dev](https://rest-rpc.dev)
|
|
72
|
+
|
|
73
|
+
## Packages
|
|
74
|
+
|
|
75
|
+
- [`@rest-rpc/core`](https://npmx.dev/package/@rest-rpc/core): API contract, client and openAPI generation.
|
|
76
|
+
- [`@rest-rpc/express`](https://npmx.dev/package/@rest-rpc/express): Express server adapter.
|
|
77
|
+
- [`@rest-rpc/fastify`](https://npmx.dev/package/@rest-rpc/fastify): Fastify server adapter.
|
|
78
|
+
- [`@rest-rpc/hono`](https://npmx.dev/package/@rest-rpc/hono): Hono server adapter.
|
|
79
|
+
- [`@rest-rpc/next`](https://npmx.dev/package/@rest-rpc/next): Next.js adapter.
|
|
80
|
+
- [`@rest-rpc/web`](https://npmx.dev/package/@rest-rpc/web): Fetch runtime adapter
|
|
81
|
+
- [`@rest-rpc/tanstack-query`](https://npmx.dev/package/@rest-rpc/tanstack-query): TanStack Query adapter.
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { HttpMethod, HttpRouteDeclaration, RequestBodySchema } from "@rest-rpc/core/contract";
|
|
2
|
-
import { type CreateWebHandlerOptions, type RouteHandler as WebRouteHandler, type WebRouteParseBodyInput, type RouteRequest as WebRouteRequest, type RouteResponse as WebRouteResponse } from "@rest-rpc/web";
|
|
2
|
+
import { type CreateWebHandlerOptions, type RouteHandler as WebRouteHandler, type WebRouteParseBodyInput, type RouteRequest as WebRouteRequest, type RouteResponse as WebRouteResponse, type WebRouteRuntimeContext } from "@rest-rpc/web";
|
|
3
3
|
type WebContract = HttpRouteDeclaration | {
|
|
4
4
|
[key: string]: WebContract;
|
|
5
5
|
};
|
|
@@ -13,16 +13,17 @@ type RouteHandlerMap<E extends HttpRouteDeclaration> = {
|
|
|
13
13
|
type RouterHandlerMap = {
|
|
14
14
|
[K in HttpMethod]: WebRequestHandler;
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
type NextRouteAdapterContext = {
|
|
17
17
|
request: Request;
|
|
18
18
|
};
|
|
19
|
-
export type
|
|
19
|
+
export type NextRouteHandlerContext = WebRouteRuntimeContext<NextRouteAdapterContext>;
|
|
20
|
+
export type RouteRequest<E extends HttpRouteDeclaration> = WebRouteRequest<E, NextRouteAdapterContext>;
|
|
20
21
|
export type RouteResponse<E extends HttpRouteDeclaration> = WebRouteResponse<E>;
|
|
21
22
|
export type RouteHandler<E extends HttpRouteDeclaration> = WebRouteHandler<E, NextRouteHandlerContext>;
|
|
22
23
|
export type NextRouteParseBodyInput = WebRouteParseBodyInput;
|
|
23
24
|
export type NextRouteParseBody = (input: NextRouteParseBodyInput) => unknown | Promise<unknown>;
|
|
24
25
|
export type CreateRouteHandlerOptions = {
|
|
25
|
-
errorHandlers?: CreateWebHandlerOptions<
|
|
26
|
+
errorHandlers?: CreateWebHandlerOptions<NextRouteAdapterContext>["errorHandlers"];
|
|
26
27
|
parseBody?: NextRouteParseBody;
|
|
27
28
|
};
|
|
28
29
|
export declare function createRouteHandler<E extends HttpRouteDeclaration>(route: E, handler: RouteHandler<E>, options?: CreateRouteHandlerOptions): RouteHandlerMap<E>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rest-rpc/next",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@rest-rpc/core": "^0.1.0-beta.
|
|
23
|
-
"@rest-rpc/web": "^0.1.0-beta.
|
|
22
|
+
"@rest-rpc/core": "^0.1.0-beta.9",
|
|
23
|
+
"@rest-rpc/web": "^0.1.0-beta.9"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "tsc -p tsconfig.build.json",
|