Skip to content

Commit 177afec

Browse files
jbl428imdudu1
andcommitted
feat: add default option
Co-authored-by: imdudu1 <cd80@kakao.com>
1 parent 82d2ef3 commit 177afec

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

lib/builders/request-param.builder.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ describe('RequestParamBuilder', () => {
1414
expect(actual).toBe('?keyword=search');
1515
});
1616

17+
test('should build query string with explicit key and default', () => {
18+
// given
19+
const builder = new RequestParamBuilder(0, 'keyword', 'default');
20+
const args = [null];
21+
22+
// when
23+
const actual = builder.build(args);
24+
25+
// then
26+
expect(actual).toBe('?keyword=default');
27+
});
28+
1729
test('should build query string without key', () => {
1830
// given
1931
const builder = new RequestParamBuilder(1);

lib/builders/request-param.builder.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@ import querystring from 'node:querystring';
22
import { TupleArrayBuilder } from './tuple-array.builder';
33

44
export class RequestParamBuilder {
5-
metadata: Array<[index: number, key: string | undefined]> = [];
5+
metadata: Array<
6+
[
7+
index: number,
8+
value: [key: string | undefined, defaultValue: string | undefined],
9+
]
10+
> = [];
611

7-
constructor(index: number, key?: string) {
8-
this.add(index, key);
12+
constructor(index: number, key?: string, defaultValue?: string) {
13+
this.add(index, key, defaultValue);
914
}
1015

11-
add(index: number, key?: string): void {
12-
this.metadata.push([index, key]);
16+
add(index: number, key?: string, defaultValue?: string): void {
17+
this.metadata.push([index, [key, defaultValue]]);
1318
}
1419

1520
build(args: any[]): string {
1621
const result = this.metadata.reduce<Record<string, any>>(
17-
(acc, [index, key]) => {
22+
(acc, [index, [key, defaultValue]]) => {
1823
if (key != null) {
19-
acc[key] = String(args[index]);
24+
acc[key] = String(args[index] ?? defaultValue ?? '');
2025
return acc;
2126
}
2227

lib/decorators/request-param.decorator.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('RequestParam', () => {
4040

4141
// then
4242
expect(result.metadata).toHaveLength(1);
43-
expect(result.metadata[0]).toEqual([0, undefined]);
43+
expect(result.metadata[0]).toEqual([0, [undefined, undefined]]);
4444
});
4545

4646
test('should set request param metadata with key', () => {
@@ -60,14 +60,14 @@ describe('RequestParam', () => {
6060

6161
// then
6262
expect(result.metadata).toHaveLength(1);
63-
expect(result.metadata[0]).toEqual([1, 'bar']);
63+
expect(result.metadata[0]).toEqual([1, ['bar', undefined]]);
6464
});
6565

6666
test('should set request param metadata with multiple decorator', () => {
6767
// given
6868
class TestService {
6969
request(
70-
@RequestParam('foo') foo: string,
70+
@RequestParam('foo', 'default') foo: string,
7171
@RequestParam() bar: { bar: string },
7272
): string {
7373
return foo;
@@ -83,8 +83,8 @@ describe('RequestParam', () => {
8383

8484
// then
8585
expect(result.metadata).toEqual([
86-
[1, undefined],
87-
[0, 'foo'],
86+
[1, [undefined, undefined]],
87+
[0, ['foo', 'default']],
8888
]);
8989
});
9090
});

lib/decorators/request-param.decorator.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { REQUEST_PARAM_METADATA } from './constants';
22
import { RequestParamBuilder } from '../builders/request-param.builder';
33

4-
export function RequestParam(key?: string): ParameterDecorator {
4+
export function RequestParam(key?: string): ParameterDecorator;
5+
export function RequestParam(
6+
key: string,
7+
defaultValue?: string,
8+
): ParameterDecorator;
9+
export function RequestParam(
10+
key?: string,
11+
defaultValue?: string,
12+
): ParameterDecorator {
513
return (target, propertyKey, parameterIndex) => {
614
if (propertyKey == null) {
715
return;
@@ -14,13 +22,13 @@ export function RequestParam(key?: string): ParameterDecorator {
1422
);
1523

1624
if (builder != null) {
17-
builder.add(parameterIndex, key);
25+
builder.add(parameterIndex, key, defaultValue);
1826
return;
1927
}
2028

2129
Reflect.defineMetadata(
2230
REQUEST_PARAM_METADATA,
23-
new RequestParamBuilder(parameterIndex, key),
31+
new RequestParamBuilder(parameterIndex, key, defaultValue),
2432
target,
2533
propertyKey,
2634
);

0 commit comments

Comments
 (0)