|
| 1 | + |
1 | 2 | # json-api-nestjs |
2 | 3 |
|
3 | | -This library was generated with [Nx](https://nx.dev). |
| 4 | +## Installation |
| 5 | + |
| 6 | +```bash |
| 7 | +$ npm install json-api-nestjs |
| 8 | +``` |
| 9 | + |
| 10 | +## Example |
| 11 | + |
| 12 | +Once the installation process is complete, we can import the **JsonApiModule** into the root **AppModule**. |
| 13 | + |
| 14 | +```typescript |
| 15 | +import { Module } from '@nestjs/common'; |
| 16 | +import { JsonApiModule } from 'json-api-nestjs'; |
| 17 | +import { Users } from 'database'; |
| 18 | + |
| 19 | +@Module({ |
| 20 | + imports: [ |
| 21 | + JsonApiModule.forRoot({ |
| 22 | + entities: [Users] |
| 23 | + }), |
| 24 | + ], |
| 25 | +}) |
| 26 | +export class AppModule {} |
| 27 | +``` |
| 28 | +After this, you have prepare crude with ready to use end point: |
| 29 | + |
| 30 | + |
| 31 | +- GET /users |
| 32 | +- POST /users |
| 33 | +- GET /users/:id |
| 34 | +- PATCH /users/:id |
| 35 | +- DELETE /users/:id |
| 36 | +- GET /users/{id}/relationships/{relName} |
| 37 | +- POST /users/{id}/relationships/{relName} |
| 38 | +- PATCH /users/{id}/relationships/{relName} |
| 39 | +- DELETE /users/{id}/relationships/{relName} |
| 40 | + |
| 41 | +## Configuration params |
4 | 42 |
|
5 | | -## Building |
| 43 | +The following interface is using for the configuration: |
| 44 | +```typescript |
| 45 | +export interface ModuleOptions { |
| 46 | + entities: Entity[]; // List of typeOrm Entity |
| 47 | + controllers?: NestController[]; // List of controller, if you need extend default present |
| 48 | + connectionName?: string; // Type orm connection name: "default" is default name |
| 49 | + providers?: NestProvider[]; // List of addition provider for useing in custom controller |
| 50 | + options?: { |
| 51 | + requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default |
| 52 | + debug?: boolean; // Debug info in result object |
| 53 | + }; |
| 54 | +} |
| 55 | +``` |
| 56 | +You can extend default controller: |
| 57 | +```typescript |
| 58 | +import { Get, Param, Inject, BadRequestException } from '@nestjs/common'; |
| 59 | + |
| 60 | +import { Users } from 'database'; |
| 61 | +import { |
| 62 | + JsonApi, |
| 63 | + excludeMethod, |
| 64 | + JsonBaseController, |
| 65 | + InjectService, |
| 66 | + JsonApiService, |
| 67 | + QueryParams, |
| 68 | +} from 'json-api-nestjs'; |
| 69 | +import { ExampleService } from '../../service/example/example.service'; |
| 70 | + |
| 71 | +@JsonApi(Users, { |
| 72 | + allowMethod: excludeMethod(['deleteRelationship']), |
| 73 | + requiredSelectField: true, |
| 74 | +}) |
| 75 | +export class ExtendUserController extends JsonBaseController<Users> { |
| 76 | + @InjectService() public service: JsonApiService<Users>; |
| 77 | + @Inject(ExampleService) protected exampleService: ExampleService; |
| 78 | + |
| 79 | + public override getAll(query: QueryParams<Users>) { |
| 80 | + if (!this.exampleService.someCheck(query)) { |
| 81 | + throw new BadRequestException({}); |
| 82 | + } |
| 83 | + return this.service.getAll({ query }); |
| 84 | + } |
| 85 | + |
| 86 | + @Get(':id/example') |
| 87 | + testOne(@Param('id') id: string): string { |
| 88 | + return this.exampleService.testMethode(id); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
6 | 92 |
|
7 | | -Run `nx build json-api-nestjs` to build the library. |
| 93 | +## Swagger UI |
8 | 94 |
|
9 | | -## Running unit tests |
| 95 | +For using swagger, you should only add [@nestjs/swagger](https://docs.nestjs.com/openapi/introduction) |
10 | 96 |
|
11 | | -Run `nx test json-api-nestjs` to execute the unit tests via [Jest](https://jestjs.io). |
|
0 commit comments