Skip to content

Commit 204d01e

Browse files
jbl428imdudu1
andcommitted
refactor: change primitive map to metadata map
Co-authored-by: imdudu1 <[email protected]>
1 parent 4886849 commit 204d01e

9 files changed

+46
-31
lines changed

lib/decorators/path-variable.decorator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PATH_VARIABLE_METADATA } from "./constants";
2+
import { MetadataMap } from "../types/metadata-map";
23

3-
export type PathVariableMetadata = Map<number, string>;
4+
export type PathVariableMetadata = MetadataMap<number, string>;
45

56
export function PathVariable(name: string): ParameterDecorator {
67
return (target, propertyKey, parameterIndex) => {
@@ -10,7 +11,7 @@ export function PathVariable(name: string): ParameterDecorator {
1011

1112
const metadata: PathVariableMetadata =
1213
Reflect.getMetadata(PATH_VARIABLE_METADATA, target, propertyKey) ??
13-
new Map();
14+
new MetadataMap();
1415

1516
metadata.set(parameterIndex, name);
1617

lib/decorators/request-body.decorator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { REQUEST_BODY_METADATA } from "./constants";
2+
import { MetadataMap } from "../types/metadata-map";
23

34
export type RequestBodyMetadata = Map<number, string | undefined>;
45

@@ -10,7 +11,7 @@ export function RequestBody(key?: string): ParameterDecorator {
1011

1112
const metadata =
1213
Reflect.getMetadata(REQUEST_BODY_METADATA, target, propertyKey) ??
13-
new Map();
14+
new MetadataMap();
1415

1516
metadata.set(parameterIndex, key);
1617

lib/decorators/request-form.decorator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { REQUEST_FORM_METADATA } from "./constants";
2+
import { MetadataMap } from "../types/metadata-map";
23

3-
export type RequestFromMetadata = Map<number, string | undefined>;
4+
export type RequestFromMetadata = MetadataMap<number, string | undefined>;
45

56
export function RequestForm(key?: string): ParameterDecorator {
67
return (target, propertyKey, parameterIndex) => {
@@ -10,7 +11,7 @@ export function RequestForm(key?: string): ParameterDecorator {
1011

1112
const metadata =
1213
Reflect.getMetadata(REQUEST_FORM_METADATA, target, propertyKey) ??
13-
new Map();
14+
new MetadataMap();
1415

1516
metadata.set(parameterIndex, key);
1617

lib/decorators/request-header.decorator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { REQUEST_HEADER_METADATA } from "./constants";
2+
import { MetadataMap } from "../types/metadata-map";
23

3-
export type RequestHeaderMetadata = Map<number, string>;
4+
export type RequestHeaderMetadata = MetadataMap<number, string>;
45

56
export function RequestHeader(key?: string): ParameterDecorator {
67
return (target, propertyKey, parameterIndex) => {
@@ -10,7 +11,7 @@ export function RequestHeader(key?: string): ParameterDecorator {
1011

1112
const metadata =
1213
Reflect.getMetadata(REQUEST_HEADER_METADATA, target, propertyKey) ??
13-
new Map();
14+
new MetadataMap();
1415

1516
metadata.set(parameterIndex, key);
1617

lib/decorators/request-param.decorator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { REQUEST_PARAM_METADATA } from "./constants";
2+
import { MetadataMap } from "../types/metadata-map";
23

3-
export type RequestParamMetadata = Map<number, string | undefined>;
4+
export type RequestParamMetadata = MetadataMap<number, string | undefined>;
45

56
export function RequestParam(key?: string): ParameterDecorator {
67
return (target, propertyKey, parameterIndex) => {
@@ -10,7 +11,7 @@ export function RequestParam(key?: string): ParameterDecorator {
1011

1112
const metadata =
1213
Reflect.getMetadata(REQUEST_PARAM_METADATA, target, propertyKey) ??
13-
new Map();
14+
new MetadataMap();
1415

1516
metadata.set(parameterIndex, key);
1617

lib/types/metadata-map.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { TupleArrayBuilder } from "../utils/tuple-array-builder";
2+
3+
export class MetadataMap<K, V> extends Map<K, V> {
4+
toArray(): Array<[K, V]> {
5+
return TupleArrayBuilder.of(this);
6+
}
7+
}

lib/utils/tuple-array-builder.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export class TupleArrayBuilder {
2+
static of<A, B>(value: unknown): Array<[A, B]> {
3+
if (typeof value === "undefined") {
4+
return [];
5+
}
6+
7+
if (value instanceof Map) {
8+
return [...value.entries()];
9+
}
10+
11+
if (typeof value === "object" && value !== null) {
12+
return Object.entries(value) as Array<[A, B]>;
13+
}
14+
15+
return [];
16+
}
17+
}

lib/utils/url-builder.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type PathVariableMetadata,
55
type RequestParamMetadata,
66
} from "../decorators";
7+
import { MetadataMap } from "../types/metadata-map";
78

89
describe("URLBuilder", () => {
910
test("should build with base url", () => {
@@ -25,7 +26,7 @@ describe("URLBuilder", () => {
2526
const host = "https://example.com";
2627
const path = "api/users/{id}";
2728
const args = [1, 2];
28-
const pathParam: PathVariableMetadata = new Map([[1, "id"]]);
29+
const pathParam: PathVariableMetadata = new MetadataMap([[1, "id"]]);
2930
const urlBuilder = new URLBuilder(host, path, args, { pathParam });
3031

3132
// when
@@ -40,7 +41,7 @@ describe("URLBuilder", () => {
4041
const host = "https://example.com";
4142
const path = "";
4243
const args = ["search"];
43-
const queryParam: RequestParamMetadata = new Map([[0, "keyword"]]);
44+
const queryParam: RequestParamMetadata = new MetadataMap([[0, "keyword"]]);
4445
const urlBuilder = new URLBuilder(host, path, args, { queryParam });
4546

4647
// when
@@ -55,7 +56,7 @@ describe("URLBuilder", () => {
5556
const host = "https://example.com";
5657
const path = "api/user";
5758
const args = [{ keyword: "search" }];
58-
const queryParam: RequestParamMetadata = new Map([[0, undefined]]);
59+
const queryParam: RequestParamMetadata = new MetadataMap([[0, undefined]]);
5960
const urlBuilder = new URLBuilder(host, path, args, { queryParam });
6061

6162
// when

lib/utils/url-builder.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TupleArrayBuilder } from "./tuple-array-builder";
12
import {
23
type PathVariableMetadata,
34
type RequestParamMetadata,
@@ -20,8 +21,8 @@ export class URLBuilder {
2021
this.host = this.path;
2122
this.path = "";
2223
}
23-
this.#pathParams = this.toArray(metadata.pathParam);
24-
this.#queryParams = this.toArray(metadata.queryParam);
24+
this.#pathParams = metadata.pathParam?.toArray() ?? [];
25+
this.#queryParams = metadata.queryParam?.toArray() ?? [];
2526
}
2627

2728
build(): string {
@@ -48,7 +49,7 @@ export class URLBuilder {
4849
return;
4950
}
5051

51-
this.toArray<string, unknown>(this.args[paramIndex]).forEach(
52+
TupleArrayBuilder.of<string, unknown>(this.args[paramIndex]).forEach(
5253
([key, value]) => {
5354
searchParams.set(key, `${value?.toString() ?? ""}`);
5455
}
@@ -79,20 +80,4 @@ export class URLBuilder {
7980
private replaceSlash(url: string): string {
8081
return url.replace(/\/{2,}/g, "/");
8182
}
82-
83-
private toArray<A, B>(value: unknown): Array<[A, B]> {
84-
if (typeof value === "undefined") {
85-
return [];
86-
}
87-
88-
if (value instanceof Map) {
89-
return [...value.entries()];
90-
}
91-
92-
if (typeof value === "object" && value !== null) {
93-
return Object.entries(value) as Array<[A, B]>;
94-
}
95-
96-
return [];
97-
}
9883
}

0 commit comments

Comments
 (0)