Skip to content

Commit 786210b

Browse files
committed
feat: add global transformer option
1 parent 15dd511 commit 786210b

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

lib/builders/response-body.builder.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export class ResponseBodyBuilder<T> {
1010
readonly options?: ClassTransformOptions,
1111
) {}
1212

13-
build(plain: Record<string, unknown>): T {
14-
return plainToInstance(this.cls, plain, this.options);
13+
build(plain: Record<string, unknown>, options?: ClassTransformOptions): T {
14+
return plainToInstance(this.cls, plain, {
15+
...this.options,
16+
...options,
17+
});
1518
}
1619
}

lib/http-interface.module.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import {
66
} from '@nestjs/core';
77
import { FetchHttpClient } from './supports/fetch-http-client';
88
import { NodeFetchInjector } from './supports/node-fetch.injector';
9-
import { type HttpClient } from './types';
10-
11-
export interface HttpInterfaceConfig {
12-
timeout?: number;
13-
httpClient?: HttpClient;
14-
}
9+
import { type HttpInterfaceConfig } from './types/http-interface-config';
1510

1611
export class HttpInterfaceModule {
1712
static forRoot(config: HttpInterfaceConfig = {}): DynamicModule {
@@ -33,6 +28,7 @@ export class HttpInterfaceModule {
3328
metadataScanner,
3429
discoveryService,
3530
config.httpClient ?? new FetchHttpClient(timeout),
31+
config,
3632
);
3733
},
3834
},

lib/supports/node-fetch.injector.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import {
99
RESPONSE_BODY_METADATA,
1010
} from '../decorators';
1111
import { HttpClient } from '../types';
12+
import { HttpInterfaceConfig } from '../types/http-interface-config';
1213

1314
@Injectable()
1415
export class NodeFetchInjector implements OnModuleInit {
1516
constructor(
1617
private readonly metadataScanner: MetadataScanner,
1718
private readonly discoveryService: DiscoveryService,
1819
private readonly httpClient: HttpClient,
20+
private readonly httpInterfaceConfig?: HttpInterfaceConfig,
1921
) {}
2022

2123
onModuleInit(): void {
@@ -54,7 +56,10 @@ export class NodeFetchInjector implements OnModuleInit {
5456
if (responseBodyBuilder != null) {
5557
const res = await response.json();
5658

57-
return responseBodyBuilder.build(res);
59+
return responseBodyBuilder.build(
60+
res,
61+
this.httpInterfaceConfig?.transformOption,
62+
);
5863
}
5964

6065
return await response.text();

lib/types/http-client.interface.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { type HttpInterfaceConfig } from '../http-interface.module';
1+
import { type HttpInterfaceConfig } from './http-interface-config';
22

3-
export type HttpClientOptions = Omit<HttpInterfaceConfig, 'httpClient'>;
3+
export type HttpClientOptions = Omit<
4+
HttpInterfaceConfig,
5+
'httpClient' | 'transformOption'
6+
>;
47

58
export interface HttpClient {
69
request: (request: Request, options?: HttpClientOptions) => Promise<Response>;

lib/types/http-interface-config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { type ClassTransformOptions } from 'class-transformer';
2+
import { type HttpClient } from './http-client.interface';
3+
4+
export interface HttpInterfaceConfig {
5+
timeout?: number;
6+
httpClient?: HttpClient;
7+
transformOption?: ClassTransformOptions;
8+
}

lib/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './http-client.interface';
22
export * from './http-method';
33
export * from './param-decorator-option.interface';
44
export * from './async-function';
5+
export * from './http-interface-config';

0 commit comments

Comments
 (0)