-
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.
Fix misc typings, RPC logic, OpenAPI generation etc. (#108)
This bumps the Next.js version to 14 and adds various typing improvements, OpenAPI generation, RPC and docs improvements for client implementation.
- Loading branch information
Showing
54 changed files
with
2,166 additions
and
1,738 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,83 @@ | ||
'use server'; | ||
|
||
import { rpcOperation } from 'next-rest-framework'; | ||
import { MOCK_TODOS, todoSchema } from 'utils'; | ||
import { z } from 'zod'; | ||
|
||
// The RPC operations can be used as server-actions and imported in the RPC route handlers. | ||
|
||
export const getTodos = rpcOperation({ | ||
tags: ['RPC'] | ||
}) | ||
.outputs([ | ||
{ | ||
schema: z.array(todoSchema) | ||
} | ||
]) | ||
.handler(() => { | ||
return MOCK_TODOS; // Type-checked output. | ||
}); | ||
|
||
export const getTodoById = rpcOperation({ | ||
tags: ['RPC'] | ||
}) | ||
.input(z.string()) | ||
.outputs([ | ||
{ | ||
schema: z.object({ | ||
error: z.string() | ||
}) | ||
}, | ||
{ | ||
schema: todoSchema | ||
} | ||
]) | ||
.handler((id) => { | ||
const todo = MOCK_TODOS.find((t) => t.id === Number(id)); | ||
|
||
if (!todo) { | ||
return { error: 'TODO not found.' }; // Type-checked output. | ||
} | ||
|
||
return todo; // Type-checked output. | ||
}); | ||
|
||
export const createTodo = rpcOperation({ | ||
tags: ['RPC'] | ||
}) | ||
.input( | ||
z.object({ | ||
name: z.string() | ||
}) | ||
) | ||
.outputs([{ schema: todoSchema }]) | ||
.handler( | ||
async ({ | ||
name // Strictly-typed input. | ||
}) => { | ||
// Create todo. | ||
const todo = { id: 2, name, completed: false }; | ||
return todo; // Type-checked output. | ||
} | ||
); | ||
|
||
export const deleteTodo = rpcOperation({ | ||
tags: ['RPC'] | ||
}) | ||
.input(z.string()) | ||
.outputs([ | ||
{ schema: z.object({ error: z.string() }) }, | ||
{ schema: z.object({ message: z.string() }) } | ||
]) | ||
.handler((id) => { | ||
// Delete todo. | ||
const todo = MOCK_TODOS.find((t) => t.id === Number(id)); | ||
|
||
if (!todo) { | ||
return { | ||
error: 'TODO not found.' // Type-checked output. | ||
}; | ||
} | ||
|
||
return { message: 'TODO deleted.' }; // Type-checked output. | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
import { docsRoute } from 'next-rest-framework'; | ||
|
||
export const { GET } = docsRoute({ | ||
deniedPaths: ['/api/routes/third-party-endpoint'], | ||
openApiJsonPath: '/openapi.json' | ||
}); |
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 { createTodo, deleteTodo, getTodoById, getTodos } from 'actions'; | ||
import { rpcRoute } from 'next-rest-framework'; | ||
|
||
const { POST, client } = rpcRoute({ | ||
getTodos, | ||
getTodoById, | ||
createTodo, | ||
deleteTodo | ||
}); | ||
|
||
export type RpcClient = typeof client; | ||
|
||
export { POST }; |
File renamed without changes.
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
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,38 @@ | ||
'use client'; | ||
|
||
import { type RpcClient } from 'app/api/v2/rpc/[operationId]/route'; | ||
import { rpcClient } from 'next-rest-framework/dist/client/rpc-client'; | ||
import { useEffect, useState } from 'react'; | ||
import { type Todo } from 'utils'; | ||
|
||
const client = rpcClient<RpcClient>({ | ||
url: 'http://localhost:3000/api/v2/rpc' | ||
}); | ||
|
||
export const ClientExample: React.FC = () => { | ||
const [loading, setLoading] = useState(true); | ||
const [todos, setTodos] = useState<Todo[]>([]); | ||
|
||
useEffect(() => { | ||
client | ||
.getTodos() | ||
.then(setTodos) | ||
.catch(console.error) | ||
.finally(() => { | ||
setLoading(false); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div className="flex flex-col gap-4"> | ||
<h1 className="text-2xl font-bold">RPC client example</h1> | ||
{loading ? ( | ||
<p>Loading...</p> | ||
) : ( | ||
<> | ||
<p>Data:</p> <p>{JSON.stringify(todos)}</p> | ||
</> | ||
)} | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.
6f16b75
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-rest-framework – ./docs
next-rest-framework-git-main-blomqma.vercel.app
next-rest-framework.vercel.app
next-rest-framework-blomqma.vercel.app
6f16b75
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-rest-framework-demo – ./apps/example
next-rest-framework-demo.vercel.app
next-rest-framework-demo-blomqma.vercel.app
next-rest-framework-demo-git-main-blomqma.vercel.app