Skip to content

Commit 09eeb23

Browse files
committed
docs: update custom API documentation with improved schema examples and data access notes
1 parent 99d93c4 commit 09eeb23

1 file changed

Lines changed: 35 additions & 35 deletions

File tree

adminforth/documentation/docs/tutorial/03-Customization/06-customPages.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -303,38 +303,6 @@ Now we have to define this endpoint in the backend to make our page work:
303303
304304
## Defining custom API for own page and components
305305
306-
> ☝️ Using `admin.express.withSchema(...)` is the recommended approach because it adds your route to `/api/v1/openapi.json` and `/api-docs` (Solar), performs early runtime validation for API calls, and gives agent plugins a machine-readable API contract they can use in skills. It is still optional though, and you can register plain Express routes without `withSchema(...)` if you prefer.
307-
308-
> ☝️ If you do not want to use Zod, you can pass a plain JSON Schema object instead of a Zod schema. For example, this Zod response schema:
309-
>
310-
> ```ts
311-
> response: z.object({
312-
> apartsByDays: z.array(z.record(z.string(), z.unknown())),
313-
> totalAparts: z.number(),
314-
> }).catchall(z.unknown()),
315-
> ```
316-
>
317-
> can be written as pure JSON Schema:
318-
>
319-
> ```ts
320-
> response: {
321-
> type: 'object',
322-
> properties: {
323-
> apartsByDays: {
324-
> type: 'array',
325-
> items: {
326-
> type: 'object',
327-
> additionalProperties: true,
328-
> },
329-
> },
330-
> totalAparts: {
331-
> type: 'number',
332-
> },
333-
> },
334-
> required: ['apartsByDays', 'totalAparts'],
335-
> additionalProperties: true,
336-
> },
337-
> ```
338306
339307
340308
Open `index.ts` file and add the following code *BEFORE* `admin.express.serve(` !
@@ -458,15 +426,47 @@ admin.discoverDatabases();
458426
459427
Install and import Zod before using this pattern: `pnpm add zod` or `npm install zod`, then `import * as z from 'zod';`. `admin.express.withSchema(...)` will convert the Zod schema to OpenAPI for you.
460428
429+
If you created the app with the CLI defaults, start it and open `http://localhost:3500/api-docs` in your browser to see this custom method in the generated API docs.
430+
461431
462432
> ☝️ Please note that we are using `admin.express.authorize` middleware to check if the user is logged in. If you want to make this endpoint public, you can remove this middleware. If user is not logged in, the request will return 401 Unauthorized status code, and protect our statistics from leak.
463433
464434
> ☝️ Moreover if you wrap your endpoint with `admin.express.authorize` middleware, you can access `req.adminUser` object in your endpoint to get the current user information.
465435
466-
> ☝️ Wrapping the route with `admin.express.withSchema(...)` registers it in `/api/v1/openapi.json` and `/api-docs`. Define custom routes before `admin.express.serve(app)` so AdminForth can pick them up.
436+
> ☝️ Using `admin.express.withSchema(...)` is the recommended approach because it adds your route to `/api/v1/openapi.json` and `/api-docs` (Solar), performs early runtime validation for API calls, and gives agent plugins a machine-readable API contract they can use in skills. It is still optional though, and you can register plain Express routes without `withSchema(...)` if you prefer.
437+
438+
> ☝️ If you do not want to use Zod, you can pass a plain JSON Schema (or convert it from e.g. typebox) object instead of a Zod schema. For example, this Zod response schema:
439+
>
440+
> ```ts
441+
> response: z.object({
442+
> apartsByDays: z.array(z.record(z.string(), z.unknown())),
443+
> totalAparts: z.number(),
444+
> }).catchall(z.unknown()),
445+
> ```
446+
>
447+
> can be written as pure JSON Schema:
448+
>
449+
> ```ts
450+
> response: {
451+
> type: 'object',
452+
> properties: {
453+
> apartsByDays: {
454+
> type: 'array',
455+
> items: {
456+
> type: 'object',
457+
> additionalProperties: true,
458+
> },
459+
> },
460+
> totalAparts: {
461+
> type: 'number',
462+
> },
463+
> },
464+
> required: ['apartsByDays', 'totalAparts'],
465+
> additionalProperties: true,
466+
> },
467+
> ```
467468
468-
> ☝️ AdminForth does not provide any facility to access data in database. You are free to use any ORM like Prisma, TypeORM, Sequelize,
469-
mongoose, or just use raw SQL queries against your tables.
469+
> ☝️ AdminForth does provide own data access facility called [DATA API](./11-dataApi.md) to access data in database. But it is very basic and mostly covers only simple CRUD operations. For complex queries like in this example, it is better to use your own data access code with any ORM. You are free to use any ORM like Prisma, TypeORM, Sequelize, mongoose, or just use raw SQL queries against your tables.
470470
471471
472472
Demo:

0 commit comments

Comments
 (0)