Skip to content

Commit

Permalink
docs: readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ssukienn committed Oct 10, 2023
1 parent 0bd4fbd commit 5a7b9f0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
### TBD
Motivation
===

Tidying up `@Controller` classes and `@Get` (etc.) handlers from excessive number of `@nestjs/swagger`
api decorators. All api decorators exposed from `@nestjs.swagger` can be applied from singel decorator.


## Requirements

* `@nestjs/common@6.9` or newer - needs to have `applyDecorators` utility method
* `@nestjs/swagger` any version

## Usage

### 1. Install dependency

```
$ npm install ssukienn/nestjs-swagger-api-spec
```

### 2. Use `@ApiSpecification` decorator

Decorate controller or handler wih `@ApiSpecification`. It can apply all `@Api` decorators passed in.
The order of appliance depends on the JS iteration order of keys and the order in which Nest applies decorators.


```typescript
import { ApiSpecification, ApiOptions } from 'nestjs-swagger-api-spec';

const exampleSpec: ApiOptions = {
apiResponseOptions: apiDecorator => [apiDecorator({status: 201, type: Number}), apiDecorator(...), ...],
//..
}

@ApiSpecification(exampleSpec)
@Controller()
class Example {

@Get()
@ApiSpecification({
apiResponseOptions: apiDecorator => [apiDecorator({status: 200, type: Number}), apiDecorator(...), ...]
//...
})
getExample() {
//...
}
}
````

## Caveats

The implementation depends on:
- the decorator factories being named with `Api` prefix. So any decorator diverging from this convention will not be applied.
- breaking the contract of `applyDecorators` in future Nest versions.

There were no changes that would break the contract in `@nestjs/common` since `6.9.0` and each decorator in `@nestjs/swagger` has `Api` prefix
so it should be safe to use.

## Getting Support & Contributing

- Open issue or pull request.

## License

- [MIT licensed](LICENSE).
10 changes: 5 additions & 5 deletions lib/api-specification.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { applyDecorators } from '@nestjs/common';
import * as nestjsSwaggerModule from '@nestjs/swagger';

const isTruthy = <T>(argument: T | undefined | null): argument is T => {
return !!argument;
};

export const ApiSpecification = (spec: ApiOptions) => {
let decorators: Array<MethodDecorator | ClassDecorator | PropertyDecorator>;

Expand All @@ -23,11 +19,15 @@ export const ApiSpecification = (spec: ApiOptions) => {
return descriptors;
}
})
.filter(isTruthy);
.filter(isDefined);

return applyDecorators(...decorators);
};

const isDefined = <T>(argument: T | undefined | null): argument is T => {
return !!argument;
};

type ExtractMethods<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => any ? T[K] : never;
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"test:e2e": "cd example/typescript-starter && pnpm i && pnpm run test:e2e"
},
"peerDependencies": {
"@nestjs/common": ">= 6.9.0",
"@nestjs/swagger": ">= 1.x.x"
"@nestjs/common": ">= 6.9",
"@nestjs/swagger": "*"
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5a7b9f0

Please sign in to comment.