Skip to content

Commit f9db88d

Browse files
authored
docs: add api handler service usage (#448)
1 parent 08ee2be commit f9db88d

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

docs/reference/server-adapters/nestjs.mdx

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,97 @@ export class MyController {
6666

6767
You can still use the regular Prisma service by injecting as usual.
6868

69+
### Using the API handler service to automate request handling
70+
71+
This module also provides an `ApiHandlerService` that can be used to automate the request handling using API handlers. This is useful if you want to handle requests in a more structured way, such as using a controller or middleware. Before using it, you should ensure `ENHANCED_PRISMA` is registered in the module. For example:
72+
73+
```ts
74+
import { ZenStackModule, ApiHandlerService } from '@zenstackhq/server/nestjs';
75+
import { enhance } from '@zenstackhq/runtime';
76+
import { PrismaService } from './prisma.service';
77+
78+
@Module({
79+
imports: [
80+
// Register the ZenStack module
81+
// The module exports the ENHANCED_PRISMA token and could be used in the ApiHandlerService
82+
ZenStackModule.registerAsync({
83+
useFactory: (prisma: PrismaService) => {
84+
return {
85+
getEnhancedPrisma: () => enhance(prisma, { user: ... }),
86+
};
87+
},
88+
inject: [PrismaService],
89+
extraProviders: [PrismaService],
90+
}),
91+
],
92+
providers: [ApiHandlerService]
93+
})
94+
export class AppModule {}
95+
```
96+
97+
Then, you can inject the `ApiHandlerService` into your code and use it to handle requests. The service provides a method `handleRequest` to handle request using API handler automatically.
98+
99+
RPC API Handler:
100+
101+
```ts
102+
import { Controller, Get, Post } from '@nestjs/common';
103+
import { ApiHandlerService } from '@zenstackhq/server/nestjs';
104+
105+
@Controller('post')
106+
export class PostController {
107+
constructor(private readonly apiHandlerService: ApiHandlerService) {}
108+
109+
// should align with the route generated by API handler
110+
@Get('findMany')
111+
async findMany() {
112+
return this.apiHandlerService.handleRequest()
113+
}
114+
115+
@Post('create')
116+
async create() {
117+
return this.apiHandlerService.handleRequest()
118+
}
119+
}
120+
```
121+
122+
RESTful API Handler:
123+
124+
```ts
125+
import { Controller, Get, Post } from '@nestjs/common';
126+
import { ApiHandlerService } from '@zenstackhq/server/nestjs';
127+
import RESTApiHandler from '@zenstackhq/server/api/rest';
128+
129+
const ENDPOINT = 'http://localhost';
130+
131+
@Controller('post')
132+
export class PostController {
133+
constructor(private readonly apiHandlerService: ApiHandlerService) {}
134+
135+
// should align with the route generated by API handler
136+
@Get()
137+
async list() {
138+
return this.apiHandlerService.handleRequest(
139+
{
140+
handler: RESTApiHandler({
141+
endpoint: ENDPOINT,
142+
}),
143+
}
144+
)
145+
}
146+
147+
@Post()
148+
async create() {
149+
return this.apiHandlerService.handleRequest(
150+
{
151+
handler: RESTApiHandler({
152+
endpoint: ENDPOINT,
153+
}),
154+
}
155+
)
156+
}
157+
}
158+
```
159+
69160
### API reference
70161

71162
#### `ZenStackModule.registerAsync`
@@ -121,3 +212,57 @@ interface ZenStackModuleOptions {
121212
getEnhancedPrisma: (model?: string | symbol) => unknown;
122213
}
123214
```
215+
216+
#### `ApiHandlerService.handleRequest`
217+
218+
##### Signature
219+
220+
```ts
221+
handleRequest(options?: ApiHandlerOptions): Promise<unknown>;
222+
```
223+
224+
##### Parameter `options`
225+
226+
```ts
227+
interface ApiHandlerOptions {
228+
/**
229+
* Logger settings
230+
*/
231+
logger?: LoggerConfig;
232+
233+
/**
234+
* Model metadata. By default loaded from the `node_module/.zenstack/model-meta`
235+
* module. You can pass it in explicitly if you configured ZenStack to output to
236+
* a different location.
237+
*/
238+
modelMeta?: ModelMeta;
239+
240+
/**
241+
* Zod schemas for validating request input. Pass `true` to load from standard location
242+
* (need to enable `@core/zod` plugin in schema.zmodel) or omit to disable input validation.
243+
*/
244+
zodSchemas?: ZodSchemas | boolean;
245+
246+
/**
247+
* Api request handler function. Can be created using `@zenstackhq/server/api/rest` or `@zenstackhq/server/api/rpc` factory functions.
248+
* Defaults to RPC-style API handler.
249+
*/
250+
handler?: HandleRequestFn;
251+
252+
/**
253+
* The base URL for the API handler. This is used to determine the base path for the API requests.
254+
* If you are using the ApiHandlerService in a route with a prefix, you should set this to the prefix.
255+
*
256+
* e.g.
257+
* without baseUrl(API handler default route):
258+
* - RPC API handler: [model]/findMany
259+
* - RESTful API handler: /:type
260+
*
261+
* with baseUrl(/api/crud):
262+
* - RPC API handler: /api/crud/[model]/findMany
263+
* - RESTful API handler: /api/crud/:type
264+
*/
265+
baseUrl?: string;
266+
}
267+
```
268+

0 commit comments

Comments
 (0)