Skip to content

Commit

Permalink
feat(express-middlewares): add UseHeader.Append decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyben committed Oct 4, 2021
1 parent 429b0fd commit dd208d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions express-middlewares/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Use **[HTTP status enums](https://github.com/jeremyben/tshttp/tree/master/status
## Response headers
### Set headers
> 🔦 `@UseHeader(header, value)|(headers)`<br>
> 💫 Related Express method: [`res.set`](https://expressjs.com/en/4x/api.html#res.set)
Expand All @@ -205,6 +207,17 @@ class Controller {
Use **[HTTP response headers enum](https://github.com/jeremyben/tshttp/tree/master/header)** from the same maintainer for an even better developer experience.
### Append header value
> 🔦 `@UseHeader.Append(header, value)`<br>
> 💫 Related Express method: [`res.set`](https://expressjs.com/en/4x/api.html#res.append)
Appends the specified `value` to the HTTP response header `field`. If the header is not already set, it creates the header with the specified `value`.
```ts
@UseHeader.Append('link', ['<http://localhost/>', '<http://localhost:3000/>'])
```
## Response type
> 🔦 `@UseType(contentType)`<br>
Expand Down
22 changes: 22 additions & 0 deletions express-middlewares/src/response-headers-decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ export function UseHeader(field: string | ResponseHeaders, value?: string) {
})
}

export namespace UseHeader {
/**
* Appends the specified `value` to the HTTP response header `field`.
* @see https://expressjs.com/fr/api.html#res.append
* @example
* ```ts
* @UseHeader.Append('link', ['<http://localhost/>', '<http://localhost:3000/>'])
* ```
* ------
* @public
*/
export function Append<H extends string = ResponseHeaderName>(
field: H extends ResponseHeaderName ? ResponseHeaderName : string,
value: string | string[]
) {
return Use((req, res, next) => {
res.append(field, value)
next()
})
}
}

/**
* Sets the `Content-Type` HTTP header to the specified MIME type.
* @see https://expressjs.com/en/api.html#res.type
Expand Down

0 comments on commit dd208d7

Please sign in to comment.