1 | "use strict";
|
2 | var HttpModule_1;
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.HttpModule = void 0;
|
5 | const tslib_1 = require("tslib");
|
6 | const axios_1 = require("axios");
|
7 | const module_decorator_1 = require("../decorators/modules/module.decorator");
|
8 | const random_string_generator_util_1 = require("../utils/random-string-generator.util");
|
9 | const http_constants_1 = require("./http.constants");
|
10 | const http_service_1 = require("./http.service");
|
11 |
|
12 |
|
13 |
|
14 | let HttpModule = HttpModule_1 = class HttpModule {
|
15 | static register(config) {
|
16 | return {
|
17 | module: HttpModule_1,
|
18 | providers: [
|
19 | {
|
20 | provide: http_constants_1.AXIOS_INSTANCE_TOKEN,
|
21 | useValue: axios_1.default.create(config),
|
22 | },
|
23 | {
|
24 | provide: http_constants_1.HTTP_MODULE_ID,
|
25 | useValue: random_string_generator_util_1.randomStringGenerator(),
|
26 | },
|
27 | ],
|
28 | };
|
29 | }
|
30 | static registerAsync(options) {
|
31 | return {
|
32 | module: HttpModule_1,
|
33 | imports: options.imports,
|
34 | providers: [
|
35 | ...this.createAsyncProviders(options),
|
36 | {
|
37 | provide: http_constants_1.AXIOS_INSTANCE_TOKEN,
|
38 | useFactory: (config) => axios_1.default.create(config),
|
39 | inject: [http_constants_1.HTTP_MODULE_OPTIONS],
|
40 | },
|
41 | {
|
42 | provide: http_constants_1.HTTP_MODULE_ID,
|
43 | useValue: random_string_generator_util_1.randomStringGenerator(),
|
44 | },
|
45 | ...(options.extraProviders || []),
|
46 | ],
|
47 | };
|
48 | }
|
49 | static createAsyncProviders(options) {
|
50 | if (options.useExisting || options.useFactory) {
|
51 | return [this.createAsyncOptionsProvider(options)];
|
52 | }
|
53 | return [
|
54 | this.createAsyncOptionsProvider(options),
|
55 | {
|
56 | provide: options.useClass,
|
57 | useClass: options.useClass,
|
58 | },
|
59 | ];
|
60 | }
|
61 | static createAsyncOptionsProvider(options) {
|
62 | if (options.useFactory) {
|
63 | return {
|
64 | provide: http_constants_1.HTTP_MODULE_OPTIONS,
|
65 | useFactory: options.useFactory,
|
66 | inject: options.inject || [],
|
67 | };
|
68 | }
|
69 | return {
|
70 | provide: http_constants_1.HTTP_MODULE_OPTIONS,
|
71 | useFactory: async (optionsFactory) => optionsFactory.createHttpOptions(),
|
72 | inject: [options.useExisting || options.useClass],
|
73 | };
|
74 | }
|
75 | };
|
76 | HttpModule = HttpModule_1 = tslib_1.__decorate([
|
77 | module_decorator_1.Module({
|
78 | providers: [
|
79 | http_service_1.HttpService,
|
80 | {
|
81 | provide: http_constants_1.AXIOS_INSTANCE_TOKEN,
|
82 | useValue: axios_1.default,
|
83 | },
|
84 | ],
|
85 | exports: [http_service_1.HttpService],
|
86 | })
|
87 | ], HttpModule);
|
88 | exports.HttpModule = HttpModule;
|