-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from storyofams/docs
Docs
- Loading branch information
Showing
12 changed files
with
358 additions
and
412 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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
title: Custom parameter decorators | ||
slug: /api/create-param-decorator | ||
--- | ||
|
||
Parameter decorators are there to simplify the task of geting a specific data you need by using the request (`req`) object or by generating it. For example the `@Body` decorator we provide simply returns the `req.body` object. | ||
|
||
By using the `createParamDecorator` function, you can create your own decorators that fulfill the needs of your application. | ||
|
||
As a basic example, let's get the browser information of the client via a decorator. | ||
|
||
First we create our decorator: | ||
```ts | ||
import { createParamDecorator } from '@storyofams/next-api-decorators'; | ||
|
||
export const UserAgent = createParamDecorator<string | undefined>( | ||
req => req.headers['user-agent'] | ||
); | ||
``` | ||
|
||
Later we can use the decorator in our handler: | ||
```ts | ||
... | ||
class CommentHandler { | ||
@Get() | ||
public comments(@UserAgent() userAgent?: string) { | ||
return `Someone requested the comments via "${userAgent ?? 'Unknown browser'}"`; | ||
} | ||
} | ||
... | ||
``` |
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
Oops, something went wrong.