Skip to content

Commit 271fcfb

Browse files
jbl428imdudu1
andcommitted
feat: add cookie value decorator
Co-authored-by: imdudu1 <[email protected]>
1 parent 177afec commit 271fcfb

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ export class AppModule {
9595
- `@RequestHeader(key?: string, defaultValue?: string)`: Specifies the request header, requiring the key of the header
9696
optionally.
9797

98+
- `@Bearer()`: Specifies the bearer token using the `Authorization` header.
99+
100+
- `@Cookie(key: string)`: Specifies the cookie using the `Cookie` header, requiring the key of the cookie.
101+
98102
- `@RequestBody(key?: string, defalutValue?: unkown)`: Specifies the request body using `application/json` as the
99103
content type, requiring the key of the body optionally.
100104

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, test } from 'vitest';
2+
import { CookieValue } from './cookie-value.decorator';
3+
import { type RequestHeaderBuilder } from '../../builders/request-header.builder';
4+
import { REQUEST_HEADER_METADATA } from '../constants';
5+
6+
describe('CookieValue', () => {
7+
test('should set request header metadata with key', () => {
8+
// given
9+
class TestService {
10+
request(@CookieValue('foo') bar: string): string {
11+
return bar;
12+
}
13+
}
14+
15+
// when
16+
const result: RequestHeaderBuilder = Reflect.getMetadata(
17+
REQUEST_HEADER_METADATA,
18+
TestService.prototype,
19+
'request',
20+
);
21+
22+
// then
23+
expect(result.metadata).toHaveLength(1);
24+
expect(result.metadata[0].key).toBe('Cookie');
25+
expect(result.metadata[0].transform?.('bar')).toBe('foo=bar');
26+
});
27+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { RequestHeader } from '../request-header.decorator';
2+
3+
export function CookieValue(key: string): ParameterDecorator {
4+
return RequestHeader('Cookie', {
5+
transform: (value) => `${key}=${value}`,
6+
});
7+
}

lib/decorators/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './bearer.decorator';
2+
export * from './cookie-value.decorator';

0 commit comments

Comments
 (0)