-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement RPC API handlers and client
This introduces a new way of writing RPC styled API handlers and abstract away the use of status codes, HTTP responses etc. The input validation, type-cheking and OpenAPI spec generation are handled in the same way as with the REST endpoint handlers. Additionally, a new RPC client function is introduced that allows calling the procedures both on the server and in the client.
- Loading branch information
Showing
42 changed files
with
1,949 additions
and
694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { rpcOperation, rpcRouteHandler } from 'next-rest-framework'; | ||
import { z } from 'zod'; | ||
|
||
const TODOS = [ | ||
{ | ||
id: 1, | ||
name: 'TODO 1', | ||
completed: false | ||
} | ||
]; | ||
|
||
// Example App Router RPC handler. | ||
export const POST = rpcRouteHandler({ | ||
getTodos: rpcOperation({ | ||
// Optional OpenAPI operation documentation. | ||
operationId: 'getTodos', | ||
tags: ['example-api', 'todos', 'app-router', 'rpc'] | ||
}) | ||
// Output schema for strictly-typed responses and OpenAPI documentation. | ||
.output([ | ||
z.array( | ||
z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
completed: z.boolean() | ||
}) | ||
) | ||
]) | ||
.handler(() => { | ||
// Type-checked response. | ||
return TODOS; | ||
}), | ||
|
||
getTodoById: rpcOperation({ | ||
operationId: 'getTodoById', | ||
tags: ['example-api', 'todos', 'app-router', 'rpc'] | ||
}) | ||
.input(z.string()) | ||
.output([ | ||
z.object({ | ||
error: z.string() | ||
}), | ||
z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
completed: z.boolean() | ||
}) | ||
]) | ||
.handler((id) => { | ||
const todo = TODOS.find((t) => t.id === Number(id)); | ||
|
||
if (!todo) { | ||
// Type-checked response. | ||
return { error: 'TODO not found.' }; | ||
} | ||
|
||
// Type-checked response. | ||
return todo; | ||
}), | ||
|
||
createTodo: rpcOperation({ | ||
// Optional OpenAPI operation documentation. | ||
operationId: 'createTodo', | ||
tags: ['example-api', 'todos', 'app-router', 'rpc'] | ||
}) | ||
// Input schema for strictly-typed request, request validation and OpenAPI documentation. | ||
.input( | ||
z.object({ | ||
name: z.string() | ||
}) | ||
) | ||
// Output schema for strictly-typed responses and OpenAPI documentation. | ||
.output([z.object({ message: z.string() })]) | ||
.handler(async ({ name }) => { | ||
// Type-checked response. | ||
return { message: `New TODO created: ${name}` }; | ||
}), | ||
|
||
deleteTodo: rpcOperation({ | ||
operationId: 'deleteTodo', | ||
tags: ['example-api', 'todos', 'app-router', 'rpc'] | ||
}) | ||
.input(z.string()) | ||
.output([ | ||
z.object({ error: z.string() }), | ||
z.object({ message: z.string() }) | ||
]) | ||
.handler((id) => { | ||
// Delete todo. | ||
const todo = TODOS.find((t) => t.id === Number(id)); | ||
|
||
if (!todo) { | ||
// Type-checked response. | ||
return { | ||
error: 'TODO not found.' | ||
}; | ||
} | ||
|
||
// Type-checked response. | ||
return { message: 'TODO deleted.' }; | ||
}) | ||
}); | ||
|
||
export type AppRouterRpcClient = typeof POST.client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { rpcClient } from 'next-rest-framework/dist/client'; | ||
import { type AppRouterRpcClient } from '../api/routes/rpc/route'; | ||
|
||
// Works both on server and client. | ||
const client = rpcClient<AppRouterRpcClient>({ | ||
url: 'http://localhost:3000/api/routes/rpc' | ||
}); | ||
|
||
// Simple example - the client can be easily integrated with any data fetching framework, like React Query or RTKQ. | ||
export default async function Page() { | ||
const data = await client.getTodos(); | ||
return <>{JSON.stringify(data)}</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { rpcApiRouteHandler } from 'next-rest-framework'; | ||
|
||
// Example Pages Router RPC handler. | ||
const handler = rpcApiRouteHandler({ | ||
// ... | ||
// Exactly the same as the App Router example. | ||
}); | ||
|
||
export default handler; | ||
|
||
export type RpcApiRouteClient = typeof handler.client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './docs-route-handler'; | ||
export * from './route-handler'; | ||
export * from './route-operation'; | ||
export * from './rpc-route-handler'; | ||
export { TypedNextResponse } from './typed-next-response'; |
Oops, something went wrong.