Skip to content

Commit 0e7414e

Browse files
committed
chore: update documentation
1 parent ec56a24 commit 0e7414e

8 files changed

+155
-143
lines changed

README.md

+74-67
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@
3333
- [Service](#service)
3434
- [Controller](#controller)
3535
- [API Endpoints](#api-endpoints)
36+
- [Swagger](#swagger)
3637
- [Query Parameters](#query-parameters)
3738
- [Repository Service](#repository-service)
3839
- [Restful Options](#restful-options)
3940
- [Crud Controller](#crud-controller)
41+
- [Restful Options merge](#restful-options-merge)
42+
- [Path Filter](#path-filter)
4043
- [Validation](#validation)
4144
- [IntelliSense](#intellisense)
4245
- [Method Override](#method-override)
43-
- [Restful Options merge](#restful-options-merge)
44-
- [Path Filter](#path-filter)
4546
- [Additional Decorators](#additional-decorators)
4647
- [Example Project](#example-project)
4748
- [Contribution](#contribution)
@@ -184,6 +185,10 @@ _Request Params:_ `:id` - entity id
184185
_Result:_: empty | error object
185186
_Status codes:_ 200 | 404
186187

188+
## Swagger
189+
190+
[Swagger](https://docs.nestjs.com/recipes/swagger) support is present out of the box, including [Query Parameters](#query-parameters) and [Path Filter](#path-filter).
191+
187192
## Query Parameters
188193

189194
`GET` endpoints that are generated by CRUD controller support some useful query parameters (all of them are _optional_):
@@ -533,7 +538,70 @@ Cache can be [reseted](#cache) by using the query parameter in your `GET` reques
533538

534539
## Crud Controller
535540

536-
Our newly generated working horse. Let's dive in some details.
541+
Our newly generated working horse. `@Crud()` decorator accepts two arguments - Entity class and `CrudOptions` object. Let's dive in some details.
542+
543+
### Restful Options merge
544+
545+
```typescript
546+
...
547+
import { Crud } from '@nestjsx/crud';
548+
549+
@Crud(Hero, {
550+
options: {
551+
// RestfulOptions goes here
552+
}
553+
})
554+
@Controller('heroes')
555+
export class HeroesCrudController {
556+
constructor(public service: HeroesService) {}
557+
}
558+
```
559+
560+
`CrudOptions` object may have `options` parameter which is the same object as [Restful Options](#restful-options).
561+
562+
**_Notice:_** If you have this options set up in your `RepositoryService`, in that case they will be **merged**.
563+
564+
### Path Filter
565+
566+
`CrudOptions` object may have `params` parameter that will be used for auto filtering by URL path parameters.
567+
568+
Assume, you have an entity `User` that belongs to some `Company` and has a field `companyId`. And you whant to create `UsersController` so that an admin could access users from his own Company only. Let's do this:
569+
570+
```typescript
571+
...
572+
import { Crud } from '@nestjsx/crud';
573+
574+
@Crud(Hero, {
575+
params: ['companyId']
576+
})
577+
@Controller('/company/:companyId/users')
578+
export class UsersCrud {
579+
constructor(public service: UsersService) {}
580+
}
581+
```
582+
583+
In this example you're URL param name `companyId` should match the name of `User.companyId` field. If not, you can do mapping, like this:
584+
585+
```typescript
586+
...
587+
import { Crud } from '@nestjsx/crud';
588+
589+
@Crud(Hero, {
590+
params: {
591+
company: 'companyId'
592+
}
593+
})
594+
@Controller('/company/:company/users')
595+
export class UsersCrud {
596+
constructor(public service: UsersService) {}
597+
}
598+
```
599+
600+
Where `company` is the name of the URL param, and `companyId` is the name of the entity field.
601+
602+
As you might guess, all request will add `companyId` to the DB queries alongside with the `:id` of `GET`, `PATCH`, `DELETE` requests. On `POST` (both: one and bulk) requests, `companyId` will be added to the `dto` automatically.
603+
604+
When you done with the controller, you'll need to add some logic to your `AuthGuard` or any other interface, where you do the authorization of a requester. You will need to match `companyId` URL param with the `user.companyId` entity that has been validated from the DB.
537605

538606
### Validation
539607

@@ -723,67 +791,6 @@ export class HeroesCrud implements CrudController<HeroesService, Hero> {
723791
}
724792
```
725793

726-
### Restful Options merge
727-
728-
```typescript
729-
...
730-
import { Crud, CrudController, RestfulOptions } from '@nestjsx/crud';
731-
732-
@Crud(Hero)
733-
@Controller('heroes')
734-
export class HeroesCrud implements CrudController<HeroesService, Hero> {
735-
options: RestfulOptions = {};
736-
737-
constructor(public service: HeroesService) {}
738-
}
739-
```
740-
741-
Controller can accept optioanl `options` parameter and it's the same object as [Restful Options](#restful-options). It's very useful when you have one `RepositoryService` and several controllers.
742-
743-
**_Notice:_** If you have this options set up in your `RepositoryService`, in that case they will be **merged**.
744-
745-
### Path Filter
746-
747-
Assume, you have an entity `User` that belongs to some `Company` and has a field `companyId`. And you whant to create `UsersController` so that an admin could access users from his own Company only. Let's do this:
748-
749-
```typescript
750-
...
751-
import { Crud, CrudController } from '@nestjsx/crud';
752-
753-
@Crud(Hero)
754-
@Controller('/company/:companyId/users')
755-
export class UsersCrud implements CrudController<UsersService, User> {
756-
paramsFilter = ['companyId'];
757-
758-
constructor(public service: UsersService) {}
759-
}
760-
```
761-
762-
A property `paramsFilter` is designed fo that purpose.
763-
764-
In this example you're URL param name `companyId` should match the name of `User.companyId` field. If not, you can do mapping, like this:
765-
766-
```typescript
767-
...
768-
import { Crud, CrudController } from '@nestjsx/crud';
769-
770-
@Crud(Hero)
771-
@Controller('/company/:company/users')
772-
export class UsersCrud implements CrudController<UsersService, User> {
773-
paramsFilter = {
774-
company: 'companyId'
775-
};
776-
777-
constructor(public service: UsersService) {}
778-
}
779-
```
780-
781-
Where `company` - name of the URL param, and `companyId` - name of the entity field.
782-
783-
As you might guess, all request will add `companyId` to the DB queries alongside with the `:id` of `GET`, `PATCH`, `DELETE` requests. On `POST` (both: one and bulk) requests, `companyId` will be added to the `dto` automatically.
784-
785-
When you done with the controller, you'll need to add some logic to your `AuthGuard` or any other interface, where you do the authorization of a requester. You will need to match `companyId` URL param with the `user.companyId` entity that has been validated from the DB.
786-
787794
### Additional Decorators
788795

789796
There are two additional decorators that come out of the box: `@Feature()` and `@Action()`:
@@ -818,7 +825,7 @@ enum CrudActions {
818825
```typescript
819826
import { Reflector } from '@nestjs/core';
820827
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
821-
import { FEAUTURE_NAME_METADATA, ACTION_NAME_METADATA } from '@nestjsx/crud';
828+
import { getFeature, getAction } from '@nestjsx/crud';
822829

823830
@Injectable()
824831
export class ACLGuard implements CanActivate {
@@ -828,8 +835,8 @@ export class ACLGuard implements CanActivate {
828835
const handler = ctx.getHandler();
829836
const controller = ctx.getClass();
830837

831-
const feature = this.reflector.get(FEAUTURE_NAME_METADATA, controller);
832-
const action = this.reflector.get(ACTION_NAME_METADATA, handler);
838+
const feature = getFeature(controller);
839+
const action = getAction(handler);
833840

834841
console.log(`${feature}-${action}`); // e.g. 'Heroes-Read-All'
835842

dist/README.md

+74-67
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@
3333
- [Service](#service)
3434
- [Controller](#controller)
3535
- [API Endpoints](#api-endpoints)
36+
- [Swagger](#swagger)
3637
- [Query Parameters](#query-parameters)
3738
- [Repository Service](#repository-service)
3839
- [Restful Options](#restful-options)
3940
- [Crud Controller](#crud-controller)
41+
- [Restful Options merge](#restful-options-merge)
42+
- [Path Filter](#path-filter)
4043
- [Validation](#validation)
4144
- [IntelliSense](#intellisense)
4245
- [Method Override](#method-override)
43-
- [Restful Options merge](#restful-options-merge)
44-
- [Path Filter](#path-filter)
4546
- [Additional Decorators](#additional-decorators)
4647
- [Example Project](#example-project)
4748
- [Contribution](#contribution)
@@ -184,6 +185,10 @@ _Request Params:_ `:id` - entity id
184185
_Result:_: empty | error object
185186
_Status codes:_ 200 | 404
186187

188+
## Swagger
189+
190+
[Swagger](https://docs.nestjs.com/recipes/swagger) support is present out of the box, including [Query Parameters](#query-parameters) and [Path Filter](#path-filter).
191+
187192
## Query Parameters
188193

189194
`GET` endpoints that are generated by CRUD controller support some useful query parameters (all of them are _optional_):
@@ -533,7 +538,70 @@ Cache can be [reseted](#cache) by using the query parameter in your `GET` reques
533538

534539
## Crud Controller
535540

536-
Our newly generated working horse. Let's dive in some details.
541+
Our newly generated working horse. `@Crud()` decorator accepts two arguments - Entity class and `CrudOptions` object. Let's dive in some details.
542+
543+
### Restful Options merge
544+
545+
```typescript
546+
...
547+
import { Crud } from '@nestjsx/crud';
548+
549+
@Crud(Hero, {
550+
options: {
551+
// RestfulOptions goes here
552+
}
553+
})
554+
@Controller('heroes')
555+
export class HeroesCrudController {
556+
constructor(public service: HeroesService) {}
557+
}
558+
```
559+
560+
`CrudOptions` object may have `options` parameter which is the same object as [Restful Options](#restful-options).
561+
562+
**_Notice:_** If you have this options set up in your `RepositoryService`, in that case they will be **merged**.
563+
564+
### Path Filter
565+
566+
`CrudOptions` object may have `params` parameter that will be used for auto filtering by URL path parameters.
567+
568+
Assume, you have an entity `User` that belongs to some `Company` and has a field `companyId`. And you whant to create `UsersController` so that an admin could access users from his own Company only. Let's do this:
569+
570+
```typescript
571+
...
572+
import { Crud } from '@nestjsx/crud';
573+
574+
@Crud(Hero, {
575+
params: ['companyId']
576+
})
577+
@Controller('/company/:companyId/users')
578+
export class UsersCrud {
579+
constructor(public service: UsersService) {}
580+
}
581+
```
582+
583+
In this example you're URL param name `companyId` should match the name of `User.companyId` field. If not, you can do mapping, like this:
584+
585+
```typescript
586+
...
587+
import { Crud } from '@nestjsx/crud';
588+
589+
@Crud(Hero, {
590+
params: {
591+
company: 'companyId'
592+
}
593+
})
594+
@Controller('/company/:company/users')
595+
export class UsersCrud {
596+
constructor(public service: UsersService) {}
597+
}
598+
```
599+
600+
Where `company` is the name of the URL param, and `companyId` is the name of the entity field.
601+
602+
As you might guess, all request will add `companyId` to the DB queries alongside with the `:id` of `GET`, `PATCH`, `DELETE` requests. On `POST` (both: one and bulk) requests, `companyId` will be added to the `dto` automatically.
603+
604+
When you done with the controller, you'll need to add some logic to your `AuthGuard` or any other interface, where you do the authorization of a requester. You will need to match `companyId` URL param with the `user.companyId` entity that has been validated from the DB.
537605

538606
### Validation
539607

@@ -723,67 +791,6 @@ export class HeroesCrud implements CrudController<HeroesService, Hero> {
723791
}
724792
```
725793

726-
### Restful Options merge
727-
728-
```typescript
729-
...
730-
import { Crud, CrudController, RestfulOptions } from '@nestjsx/crud';
731-
732-
@Crud(Hero)
733-
@Controller('heroes')
734-
export class HeroesCrud implements CrudController<HeroesService, Hero> {
735-
options: RestfulOptions = {};
736-
737-
constructor(public service: HeroesService) {}
738-
}
739-
```
740-
741-
Controller can accept optioanl `options` parameter and it's the same object as [Restful Options](#restful-options). It's very useful when you have one `RepositoryService` and several controllers.
742-
743-
**_Notice:_** If you have this options set up in your `RepositoryService`, in that case they will be **merged**.
744-
745-
### Path Filter
746-
747-
Assume, you have an entity `User` that belongs to some `Company` and has a field `companyId`. And you whant to create `UsersController` so that an admin could access users from his own Company only. Let's do this:
748-
749-
```typescript
750-
...
751-
import { Crud, CrudController } from '@nestjsx/crud';
752-
753-
@Crud(Hero)
754-
@Controller('/company/:companyId/users')
755-
export class UsersCrud implements CrudController<UsersService, User> {
756-
paramsFilter = ['companyId'];
757-
758-
constructor(public service: UsersService) {}
759-
}
760-
```
761-
762-
A property `paramsFilter` is designed fo that purpose.
763-
764-
In this example you're URL param name `companyId` should match the name of `User.companyId` field. If not, you can do mapping, like this:
765-
766-
```typescript
767-
...
768-
import { Crud, CrudController } from '@nestjsx/crud';
769-
770-
@Crud(Hero)
771-
@Controller('/company/:company/users')
772-
export class UsersCrud implements CrudController<UsersService, User> {
773-
paramsFilter = {
774-
company: 'companyId'
775-
};
776-
777-
constructor(public service: UsersService) {}
778-
}
779-
```
780-
781-
Where `company` - name of the URL param, and `companyId` - name of the entity field.
782-
783-
As you might guess, all request will add `companyId` to the DB queries alongside with the `:id` of `GET`, `PATCH`, `DELETE` requests. On `POST` (both: one and bulk) requests, `companyId` will be added to the `dto` automatically.
784-
785-
When you done with the controller, you'll need to add some logic to your `AuthGuard` or any other interface, where you do the authorization of a requester. You will need to match `companyId` URL param with the `user.companyId` entity that has been validated from the DB.
786-
787794
### Additional Decorators
788795

789796
There are two additional decorators that come out of the box: `@Feature()` and `@Action()`:
@@ -818,7 +825,7 @@ enum CrudActions {
818825
```typescript
819826
import { Reflector } from '@nestjs/core';
820827
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
821-
import { FEAUTURE_NAME_METADATA, ACTION_NAME_METADATA } from '@nestjsx/crud';
828+
import { getFeature, getAction } from '@nestjsx/crud';
822829

823830
@Injectable()
824831
export class ACLGuard implements CanActivate {
@@ -828,8 +835,8 @@ export class ACLGuard implements CanActivate {
828835
const handler = ctx.getHandler();
829836
const controller = ctx.getClass();
830837

831-
const feature = this.reflector.get(FEAUTURE_NAME_METADATA, controller);
832-
const action = this.reflector.get(ACTION_NAME_METADATA, handler);
838+
const feature = getFeature(controller);
839+
const action = getAction(handler);
833840

834841
console.log(`${feature}-${action}`); // e.g. 'Heroes-Read-All'
835842

dist/classes/restful-service.class.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export declare abstract class RestfulService<T> {
1010
abstract updateOne(...args: any[]): Promise<T>;
1111
abstract deleteOne(...args: any[]): Promise<void>;
1212
throwBadRequestException(msg?: any): BadRequestException;
13-
throwNotFoundException(name?: string): NotFoundException;
13+
throwNotFoundException(name: string): NotFoundException;
1414
}

dist/classes/restful-service.class.js

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)