Skip to content

Commit 1f20805

Browse files
jbl428imdudu1
andcommitted
refactor: add factory method for http request builder
Co-authored-by: imdudu1 <cd80@kakao.com>
1 parent d155606 commit 1f20805

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

lib/builders/http-request.builder.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class HttpRequestBuilder {
1919
private readonly requestHeaderBuilder?: RequestHeaderBuilder;
2020
private readonly payloadBuilder: PayloadBuilder;
2121

22-
constructor(
22+
private constructor(
2323
readonly target: object,
2424
readonly propertyKey: string,
2525
readonly method: HttpMethod,
@@ -37,6 +37,40 @@ export class HttpRequestBuilder {
3737
);
3838
}
3939

40+
static forRest(
41+
target: object,
42+
propertyKey: string,
43+
method: HttpMethod,
44+
url: string,
45+
options?: HttpClientOptions,
46+
): HttpRequestBuilder {
47+
return new HttpRequestBuilder(
48+
target,
49+
propertyKey,
50+
method,
51+
url,
52+
undefined,
53+
options,
54+
);
55+
}
56+
57+
static forGraphQL(
58+
target: object,
59+
propertyKey: string,
60+
query: string,
61+
options?: HttpClientOptions,
62+
url = '/graphql',
63+
): HttpRequestBuilder {
64+
return new HttpRequestBuilder(
65+
target,
66+
propertyKey,
67+
'POST',
68+
url,
69+
query,
70+
options,
71+
);
72+
}
73+
4074
setBaseUrl(baseUrl: string): void {
4175
this.baseUrl = baseUrl;
4276
}

lib/decorators/graphql-exchange.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function GraphQLExchange(
1313
) => {
1414
Reflect.defineMetadata(
1515
HTTP_EXCHANGE_METADATA,
16-
new HttpRequestBuilder(target, propertyKey, 'POST', url, query, options),
16+
HttpRequestBuilder.forGraphQL(target, propertyKey, query, options, url),
1717
target,
1818
propertyKey,
1919
);

lib/decorators/http-exchange.decorator.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ export const HttpExchange =
1111
<P extends string>(target: Record<P, AsyncFunction>, propertyKey: P) => {
1212
Reflect.defineMetadata(
1313
HTTP_EXCHANGE_METADATA,
14-
new HttpRequestBuilder(
15-
target,
16-
propertyKey,
17-
method,
18-
url,
19-
undefined,
20-
options,
21-
),
14+
HttpRequestBuilder.forRest(target, propertyKey, method, url, options),
2215
target,
2316
propertyKey,
2417
);

lib/supports/fetch-http-client.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('FetchHttpClient', () => {
1212
let fastify: FastifyInstance;
1313

1414
beforeAll(async () => {
15-
fastify = Fastify({ logger: false });
15+
fastify = Fastify({ logger: false, forceCloseConnections: true });
1616

1717
fastify.get('/', async (request, reply) => {
1818
await reply.send({ hello: 'world' });
@@ -115,13 +115,13 @@ describe('FetchHttpClient', () => {
115115
const address = fastify.server.address() as AddressInfo;
116116
const httpClient = new FetchHttpClient(60000);
117117
const request = new Request(`http://localhost:${address.port}/timeout`);
118-
const httpClientOptions = { timeout: 300 };
118+
const httpClientOptions = { timeout: 1000 };
119119

120120
// when
121121
const doRequest = async (): Promise<Response> =>
122122
await httpClient.request(request, httpClientOptions);
123123

124124
// then
125-
await expect(doRequest).rejects.toThrowError('Request Timeout: 300ms');
125+
await expect(doRequest).rejects.toThrowError('Request Timeout: 1000ms');
126126
});
127127
});

0 commit comments

Comments
 (0)