Skip to content

Commit 3d776aa

Browse files
jbl428imdudu1
andcommitted
refactor: apply url builder to injector
Co-authored-by: imdudu1 <[email protected]>
1 parent 0655c0f commit 3d776aa

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

lib/node-fetch.injector.ts

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type RequestParamMetadata,
1212
} from "./decorators";
1313
import { HttpClient } from "./types/http-client.interface";
14+
import { URLBuilder } from "./utils/url-builder";
1415

1516
@Injectable()
1617
export class NodeFetchInjector implements OnModuleInit {
@@ -44,53 +45,29 @@ export class NodeFetchInjector implements OnModuleInit {
4445
const pathMetadata = getMetadata<PathVariableMetadata>(
4546
PATH_VARIABLE_METADATA
4647
);
47-
4848
const requestParamMetadata = getMetadata<RequestParamMetadata>(
4949
REQUEST_PARAM_METADATA
5050
);
5151

5252
wrapper.instance[methodName] = async (...args: any[]) => {
53-
const url = [...(pathMetadata?.entries() ?? [])].reduce(
54-
(url, [index, value]) =>
55-
url.replace(new RegExp(`{${value}}`, "g"), args[index]),
56-
`${baseUrl}${httpExchangeMetadata.url}`
57-
);
58-
const searchParams = new URLSearchParams();
59-
requestParamMetadata?.forEach((queryParamKey, paramIndex) => {
60-
if (typeof queryParamKey === "undefined") {
61-
this.toArray(args[paramIndex]).forEach(([key, value]) => {
62-
searchParams.set(key, `${value?.toString() ?? ""}`);
63-
});
64-
65-
return;
53+
const urlBuilder = new URLBuilder(
54+
baseUrl,
55+
httpExchangeMetadata.url,
56+
args,
57+
{
58+
pathParam: pathMetadata,
59+
queryParam: requestParamMetadata,
6660
}
67-
searchParams.set(queryParamKey, args[paramIndex]);
68-
});
69-
70-
const request = new Request(
71-
[url, searchParams.toString()].filter(Boolean).join("?")
7261
);
7362

7463
return await this.httpClient
75-
.request(request)
64+
.request(new Request(urlBuilder.build()))
7665
.then(async (response) => await response.json());
7766
};
7867
});
7968
});
8069
}
8170

82-
private toArray<T>(value: T): Array<[string, unknown]> {
83-
if (value instanceof Map) {
84-
return [...value.values()];
85-
}
86-
87-
if (typeof value === "object" && value !== null) {
88-
return Object.entries(value);
89-
}
90-
91-
return [];
92-
}
93-
9471
private getHttpProviders(): InstanceWrapper[] {
9572
return this.discoveryService.getProviders().filter((wrapper) => {
9673
const metadata = Reflect.getMetadata(

lib/utils/url-builder.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class URLBuilder {
1616
queryParam?: RequestParamMetadata;
1717
} = {}
1818
) {
19+
if (this.host.length === 0) {
20+
this.host = this.path;
21+
this.path = "";
22+
}
1923
this.#pathParams = this.toArray(metadata.pathParam);
2024
this.#queryParams = this.toArray(metadata.queryParam);
2125
}
@@ -59,7 +63,7 @@ export class URLBuilder {
5963
return this.host;
6064
}
6165

62-
if (this.isStartProtocol()) {
66+
if (this.isStartWithProtocol()) {
6367
const [protocol, host] = this.host.split("://");
6468

6569
return protocol + "://" + this.replaceSlash(`${host}/${this.path}`);
@@ -68,7 +72,7 @@ export class URLBuilder {
6872
return this.replaceSlash(`${this.host}/${this.path}`);
6973
}
7074

71-
private isStartProtocol(): boolean {
75+
private isStartWithProtocol(): boolean {
7276
return this.host.match(/^https?:\/\//) != null;
7377
}
7478

0 commit comments

Comments
 (0)