-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(json-api-nestjs): new-version 3.x.x
support TypeOrm 3.x.x and NestJS 9 BREAKING CHANGE: change init custom controller change init config
- Loading branch information
Alexander Kharkovey
committed
Oct 7, 2022
1 parent
b8f032c
commit f45d811
Showing
2 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,11 +1,96 @@ | ||
|
||
# json-api-nestjs | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
## Installation | ||
|
||
```bash | ||
$ npm install json-api-nestjs | ||
``` | ||
|
||
## Example | ||
|
||
Once the installation process is complete, we can import the **JsonApiModule** into the root **AppModule**. | ||
|
||
```typescript | ||
import { Module } from '@nestjs/common'; | ||
import { JsonApiModule } from 'json-api-nestjs'; | ||
import { Users } from 'database'; | ||
|
||
@Module({ | ||
imports: [ | ||
JsonApiModule.forRoot({ | ||
entities: [Users] | ||
}), | ||
], | ||
}) | ||
export class AppModule {} | ||
``` | ||
After this, you have prepare crude with ready to use end point: | ||
|
||
|
||
- GET /users | ||
- POST /users | ||
- GET /users/:id | ||
- PATCH /users/:id | ||
- DELETE /users/:id | ||
- GET /users/{id}/relationships/{relName} | ||
- POST /users/{id}/relationships/{relName} | ||
- PATCH /users/{id}/relationships/{relName} | ||
- DELETE /users/{id}/relationships/{relName} | ||
|
||
## Configuration params | ||
|
||
## Building | ||
The following interface is using for the configuration: | ||
```typescript | ||
export interface ModuleOptions { | ||
entities: Entity[]; // List of typeOrm Entity | ||
controllers?: NestController[]; // List of controller, if you need extend default present | ||
connectionName?: string; // Type orm connection name: "default" is default name | ||
providers?: NestProvider[]; // List of addition provider for useing in custom controller | ||
options?: { | ||
requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default | ||
debug?: boolean; // Debug info in result object | ||
}; | ||
} | ||
``` | ||
You can extend default controller: | ||
```typescript | ||
import { Get, Param, Inject, BadRequestException } from '@nestjs/common'; | ||
|
||
import { Users } from 'database'; | ||
import { | ||
JsonApi, | ||
excludeMethod, | ||
JsonBaseController, | ||
InjectService, | ||
JsonApiService, | ||
QueryParams, | ||
} from 'json-api-nestjs'; | ||
import { ExampleService } from '../../service/example/example.service'; | ||
|
||
@JsonApi(Users, { | ||
allowMethod: excludeMethod(['deleteRelationship']), | ||
requiredSelectField: true, | ||
}) | ||
export class ExtendUserController extends JsonBaseController<Users> { | ||
@InjectService() public service: JsonApiService<Users>; | ||
@Inject(ExampleService) protected exampleService: ExampleService; | ||
|
||
public override getAll(query: QueryParams<Users>) { | ||
if (!this.exampleService.someCheck(query)) { | ||
throw new BadRequestException({}); | ||
} | ||
return this.service.getAll({ query }); | ||
} | ||
|
||
@Get(':id/example') | ||
testOne(@Param('id') id: string): string { | ||
return this.exampleService.testMethode(id); | ||
} | ||
} | ||
``` | ||
|
||
Run `nx build json-api-nestjs` to build the library. | ||
## Swagger UI | ||
|
||
## Running unit tests | ||
For using swagger, you should only add [@nestjs/swagger](https://docs.nestjs.com/openapi/introduction) | ||
|
||
Run `nx test json-api-nestjs` to execute the unit tests via [Jest](https://jestjs.io). |