Skip to content

Commit

Permalink
feat: update documentation in API Action section (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
fruneen committed Jan 22, 2024
1 parent bdad65f commit f075d70
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions docs/api-reference/api-action.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@ title: "API action"

## Overview

**API action** — is HTTP handler that perform database updates and other logic required by the business logic. Actions should reside in the /actions folder within resource. Usually action is a single file that has meaningful name, e.x. list, getById, updateEmail. If action has a lot of logic and require multiple files it needs to be placed into the folder with name of the action and action need to exposed using module pattern (index.ts file). Direct database updates of the current resource entity are allowed within action.
**API action** — is HTTP handler that perform database updates and other logic required by the business logic.
Actions should reside in the `/actions` folder within resource.
Usually action is a single file that has meaningful name, e.x. `list`, `get-by-id`, `update-email`.

If action has a lot of logic and require multiple files it needs to be placed into the folder with name of the action and action need to exposed using module pattern (index.ts file).

Direct database updates of the current resource entity are allowed within action.

## Examples

```typescript
import Router from '@koa/router';
import { KoaContext, ValidateResult } from 'types';
import { validationUtil } from '@paralect/utils';

import { AppKoaContext } from 'types';

import { validateMiddleware } from 'middlewares';

type GetCompanies = {
userId: string;
};

async function handler(ctx: KoaContext<GetCompanies>) {
async function handler(ctx: AppKoaContext<GetCompanies>) {
const { userId } = ctx.validatedData; // validatedData is returned by API validator

ctx.body = {}; // action result sent to the client
}

export default (router: Router) => {
// see Rest API validator
router.get('/companies', validationUtil.validateMiddleware(validator), handler);
router.get('/companies', validateMiddleware(schema), handler);
};
```

0 comments on commit f075d70

Please sign in to comment.