@adatechnology/http-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +596 -0
  3. package/dist/http.interface.js +27 -0
  4. package/dist/http.module.js +37 -0
  5. package/dist/http.provider.js +137 -0
  6. package/dist/http.token.js +6 -0
  7. package/dist/implementations/axios/axios.http.module.js +41 -0
  8. package/dist/implementations/axios/axios.http.provider.js +424 -0
  9. package/dist/implementations/axios/axios.http.token.js +4 -0
  10. package/dist/implementations/http.implementation.module.js +20 -0
  11. package/dist/index.js +11 -0
  12. package/dist/types/http.interface.d.ts +175 -0
  13. package/dist/types/http.module.d.ts +9 -0
  14. package/dist/types/http.provider.d.ts +43 -0
  15. package/dist/types/http.token.d.ts +3 -0
  16. package/dist/types/implementations/axios/axios.http.module.d.ts +5 -0
  17. package/dist/types/implementations/axios/axios.http.provider.d.ts +226 -0
  18. package/dist/types/implementations/axios/axios.http.token.d.ts +1 -0
  19. package/dist/types/implementations/http.implementation.module.d.ts +2 -0
  20. package/dist/types/index.d.ts +4 -0
  21. package/package.json +19 -0
  22. package/src/http.interface.ts +259 -0
  23. package/src/http.module.ts +27 -0
  24. package/src/http.provider.ts +219 -0
  25. package/src/http.token.ts +3 -0
  26. package/src/implementations/axios/axios.http.module.ts +33 -0
  27. package/src/implementations/axios/axios.http.provider.ts +603 -0
  28. package/src/implementations/axios/axios.http.token.ts +1 -0
  29. package/src/implementations/http.implementation.module.ts +9 -0
  30. package/src/index.ts +8 -0
  31. package/tsconfig.json +16 -0
  32. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,259 @@
1
+ import { Observable } from "rxjs";
2
+
3
+ /**
4
+ * HTTP methods supported
5
+ */
6
+ export enum HttpMethod {
7
+ GET = "GET",
8
+ POST = "POST",
9
+ PUT = "PUT",
10
+ PATCH = "PATCH",
11
+ DELETE = "DELETE",
12
+ HEAD = "HEAD",
13
+ OPTIONS = "OPTIONS",
14
+ }
15
+
16
+ /**
17
+ * Content types for HTTP requests
18
+ */
19
+ export enum ContentType {
20
+ JSON = "application/json",
21
+ FORM_URLENCODED = "application/x-www-form-urlencoded",
22
+ FORM_DATA = "multipart/form-data",
23
+ TEXT = "text/plain",
24
+ XML = "application/xml",
25
+ }
26
+
27
+ /**
28
+ * HTTP request configuration
29
+ */
30
+ export interface HttpRequestConfig {
31
+ url: string;
32
+ method?: HttpMethod;
33
+ headers?: Record<string, string>;
34
+ params?: Record<string, any>;
35
+ data?: any;
36
+ timeout?: number;
37
+ contentType?: ContentType;
38
+ responseType?: "json" | "text" | "blob" | "arraybuffer";
39
+ withCredentials?: boolean;
40
+ retryAttempts?: number;
41
+ retryDelay?: number;
42
+ cache?: boolean; // Enable/disable caching for this request
43
+ cacheTtl?: number; // Cache TTL in milliseconds
44
+ }
45
+
46
+ /**
47
+ * HTTP response structure
48
+ */
49
+ export interface HttpResponse<T = any> {
50
+ data: T;
51
+ status: number;
52
+ statusText: string;
53
+ headers: Record<string, string>;
54
+ config: HttpRequestConfig;
55
+ request?: any;
56
+ }
57
+
58
+ /**
59
+ * HTTP error structure
60
+ */
61
+ export interface HttpError extends Error {
62
+ config: HttpRequestConfig;
63
+ response?: HttpResponse;
64
+ status?: number;
65
+ statusText?: string;
66
+ isNetworkError?: boolean;
67
+ isTimeout?: boolean;
68
+ }
69
+
70
+ /**
71
+ * Error interceptor function
72
+ */
73
+ export type ErrorInterceptor = (error: any) => any;
74
+
75
+ /**
76
+ * HTTP provider interface
77
+ */
78
+ export interface HttpProviderInterface {
79
+ /**
80
+ * Make a GET request
81
+ */
82
+ get<T = any>(
83
+ url: string,
84
+ config?: Omit<HttpRequestConfig, "url" | "method">,
85
+ ): Promise<HttpResponse<T>>;
86
+
87
+ /**
88
+ * Make a POST request
89
+ */
90
+ post<T = any>(
91
+ url: string,
92
+ data?: any,
93
+ config?: Omit<HttpRequestConfig, "url" | "method" | "data">,
94
+ ): Promise<HttpResponse<T>>;
95
+
96
+ /**
97
+ * Make a PUT request
98
+ */
99
+ put<T = any>(
100
+ url: string,
101
+ data?: any,
102
+ config?: Omit<HttpRequestConfig, "url" | "method" | "data">,
103
+ ): Promise<HttpResponse<T>>;
104
+
105
+ /**
106
+ * Make a PATCH request
107
+ */
108
+ patch<T = any>(
109
+ url: string,
110
+ data?: any,
111
+ config?: Omit<HttpRequestConfig, "url" | "method" | "data">,
112
+ ): Promise<HttpResponse<T>>;
113
+
114
+ /**
115
+ * Make a DELETE request
116
+ */
117
+ delete<T = any>(
118
+ url: string,
119
+ config?: Omit<HttpRequestConfig, "url" | "method">,
120
+ ): Promise<HttpResponse<T>>;
121
+
122
+ /**
123
+ * Make a HEAD request
124
+ */
125
+ head<T = any>(
126
+ url: string,
127
+ config?: Omit<HttpRequestConfig, "url" | "method">,
128
+ ): Promise<HttpResponse<T>>;
129
+
130
+ /**
131
+ * Make an OPTIONS request
132
+ */
133
+ options<T = any>(
134
+ url: string,
135
+ config?: Omit<HttpRequestConfig, "url" | "method">,
136
+ ): Promise<HttpResponse<T>>;
137
+
138
+ /**
139
+ * Make a generic HTTP request
140
+ */
141
+ request<T = any>(config: HttpRequestConfig): Promise<HttpResponse<T>>;
142
+
143
+ /**
144
+ * Set global headers
145
+ */
146
+ setGlobalHeader(key: string, value: string): void;
147
+
148
+ /**
149
+ * Remove global header
150
+ */
151
+ removeGlobalHeader(key: string): void;
152
+
153
+ /**
154
+ * Get global headers
155
+ */
156
+ getGlobalHeaders(): Record<string, string>;
157
+
158
+ /**
159
+ * Set base URL for all requests
160
+ */
161
+ setBaseUrl(baseUrl: string): void;
162
+
163
+ /**
164
+ * Get current base URL
165
+ */
166
+ getBaseUrl(): string;
167
+
168
+ /**
169
+ * Set default timeout
170
+ */
171
+ setDefaultTimeout(timeout: number): void;
172
+
173
+ /**
174
+ * Add error interceptor
175
+ */
176
+ addErrorInterceptor(interceptor: ErrorInterceptor): number;
177
+
178
+ /**
179
+ * Remove error interceptor
180
+ */
181
+ removeErrorInterceptor(id: number): void;
182
+
183
+ /**
184
+ * Clear cache for a specific key or all cache
185
+ */
186
+ clearCache(key?: string): void;
187
+
188
+ /**
189
+ * Set authorization token
190
+ */
191
+ setAuthToken(token: string, type?: string): void;
192
+
193
+ /**
194
+ * Clear authorization token
195
+ */
196
+ clearAuthToken(): void;
197
+
198
+ /**
199
+ * Add request interceptor
200
+ */
201
+ addRequestInterceptor(
202
+ onFulfilled: (config: any) => any,
203
+ onRejected?: (error: any) => any,
204
+ ): number;
205
+
206
+ /**
207
+ * Remove request interceptor by id
208
+ */
209
+ removeRequestInterceptor(id: number): void;
210
+
211
+ /**
212
+ * Add response interceptor
213
+ */
214
+ addResponseInterceptor(
215
+ onFulfilled: (response: any) => any,
216
+ onRejected?: (error: any) => any,
217
+ ): number;
218
+
219
+ /**
220
+ * Remove response interceptor by id
221
+ */
222
+ removeResponseInterceptor(id: number): void;
223
+
224
+ /**
225
+ * Observable version for reactive programming
226
+ */
227
+ get$<T = any>(
228
+ url: string,
229
+ config?: Omit<HttpRequestConfig, "url" | "method">,
230
+ ): Observable<HttpResponse<T>>;
231
+ post$<T = any>(
232
+ url: string,
233
+ data?: any,
234
+ config?: Omit<HttpRequestConfig, "url" | "method" | "data">,
235
+ ): Observable<HttpResponse<T>>;
236
+ put$<T = any>(
237
+ url: string,
238
+ data?: any,
239
+ config?: Omit<HttpRequestConfig, "url" | "method" | "data">,
240
+ ): Observable<HttpResponse<T>>;
241
+ patch$<T = any>(
242
+ url: string,
243
+ data?: any,
244
+ config?: Omit<HttpRequestConfig, "url" | "method" | "data">,
245
+ ): Observable<HttpResponse<T>>;
246
+ delete$<T = any>(
247
+ url: string,
248
+ config?: Omit<HttpRequestConfig, "url" | "method">,
249
+ ): Observable<HttpResponse<T>>;
250
+ head$<T = any>(
251
+ url: string,
252
+ config?: Omit<HttpRequestConfig, "url" | "method">,
253
+ ): Observable<HttpResponse<T>>;
254
+ options$<T = any>(
255
+ url: string,
256
+ config?: Omit<HttpRequestConfig, "url" | "method">,
257
+ ): Observable<HttpResponse<T>>;
258
+ request$<T = any>(config: HttpRequestConfig): Observable<HttpResponse<T>>;
259
+ }
@@ -0,0 +1,27 @@
1
+ import { DynamicModule, Module } from "@nestjs/common";
2
+ import { AxiosInstance, AxiosRequestConfig } from "axios";
3
+
4
+ import { HttpProvider } from "./http.provider";
5
+ import { HTTP_PROVIDER } from "./http.token";
6
+ import { HttpImplementationAxiosModule } from "./implementations/axios/axios.http.module";
7
+
8
+ @Module({})
9
+ export class HttpModule {
10
+ /**
11
+ * Configure HttpModule with an Axios instance or AxiosRequestConfig.
12
+ * This will import the implementation-specific module (currently Axios).
13
+ */
14
+ static forRoot(config?: AxiosRequestConfig | AxiosInstance): DynamicModule {
15
+ return {
16
+ module: HttpModule,
17
+ imports: [HttpImplementationAxiosModule.forRoot(config)],
18
+ providers: [
19
+ {
20
+ provide: HTTP_PROVIDER,
21
+ useClass: HttpProvider,
22
+ },
23
+ ],
24
+ exports: [HTTP_PROVIDER],
25
+ };
26
+ }
27
+ }
@@ -0,0 +1,219 @@
1
+ import { Injectable, Inject } from "@nestjs/common";
2
+ import { Observable } from "rxjs";
3
+
4
+ import {
5
+ ErrorInterceptor,
6
+ HttpProviderInterface,
7
+ HttpRequestConfig,
8
+ HttpResponse,
9
+ } from "./http.interface";
10
+ import type { AxiosHttpProviderInterface } from "./implementations/axios/axios.http.provider";
11
+ import { HTTP_AXIOS_PROVIDER } from "./http.token";
12
+
13
+ @Injectable()
14
+ export class HttpProvider implements HttpProviderInterface {
15
+ constructor(
16
+ @Inject(HTTP_AXIOS_PROVIDER)
17
+ private readonly axiosHttpProvider: AxiosHttpProviderInterface,
18
+ ) {}
19
+
20
+ /**
21
+ * Expose underlying axios instance when available from the axios provider implementation.
22
+ * This is intentionally typed as unknown/any to avoid leaking implementation details.
23
+ */
24
+ getAxiosInstance(): any | undefined {
25
+ try {
26
+ // Some implementations expose `axiosInstance` or `instance`.
27
+ // Use brute-force access guarded in try/catch to avoid runtime errors.
28
+
29
+ const provider: any = this.axiosHttpProvider as any;
30
+ return provider?.axiosInstance ?? provider?.instance ?? undefined;
31
+ } catch (e) {
32
+ return undefined;
33
+ }
34
+ }
35
+
36
+ async get<T>(
37
+ url: string,
38
+ config?: HttpRequestConfig,
39
+ ): Promise<HttpResponse<T>> {
40
+ return this.axiosHttpProvider.get<T>(url, config);
41
+ }
42
+
43
+ get$<T>(
44
+ url: string,
45
+ config?: HttpRequestConfig,
46
+ ): Observable<HttpResponse<T>> {
47
+ return this.axiosHttpProvider.get$<T>(url, config);
48
+ }
49
+
50
+ async post<T>(
51
+ url: string,
52
+ data?: any,
53
+ config?: HttpRequestConfig,
54
+ ): Promise<HttpResponse<T>> {
55
+ return this.axiosHttpProvider.post<T>(url, data, config);
56
+ }
57
+
58
+ post$<T>(
59
+ url: string,
60
+ data?: any,
61
+ config?: HttpRequestConfig,
62
+ ): Observable<HttpResponse<T>> {
63
+ return this.axiosHttpProvider.post$<T>(url, data, config);
64
+ }
65
+
66
+ async put<T>(
67
+ url: string,
68
+ data?: any,
69
+ config?: HttpRequestConfig,
70
+ ): Promise<HttpResponse<T>> {
71
+ return this.axiosHttpProvider.put<T>(url, data, config);
72
+ }
73
+
74
+ put$<T>(
75
+ url: string,
76
+ data?: any,
77
+ config?: HttpRequestConfig,
78
+ ): Observable<HttpResponse<T>> {
79
+ return this.axiosHttpProvider.put$<T>(url, data, config);
80
+ }
81
+
82
+ async patch<T>(
83
+ url: string,
84
+ data?: any,
85
+ config?: HttpRequestConfig,
86
+ ): Promise<HttpResponse<T>> {
87
+ return this.axiosHttpProvider.patch<T>(url, data, config);
88
+ }
89
+
90
+ patch$<T>(
91
+ url: string,
92
+ data?: any,
93
+ config?: HttpRequestConfig,
94
+ ): Observable<HttpResponse<T>> {
95
+ return this.axiosHttpProvider.patch$<T>(url, data, config);
96
+ }
97
+
98
+ async delete<T>(
99
+ url: string,
100
+ config?: HttpRequestConfig,
101
+ ): Promise<HttpResponse<T>> {
102
+ return this.axiosHttpProvider.delete<T>(url, config);
103
+ }
104
+
105
+ delete$<T>(
106
+ url: string,
107
+ config?: HttpRequestConfig,
108
+ ): Observable<HttpResponse<T>> {
109
+ return this.axiosHttpProvider.delete$<T>(url, config);
110
+ }
111
+
112
+ async head<T>(
113
+ url: string,
114
+ config?: HttpRequestConfig,
115
+ ): Promise<HttpResponse<T>> {
116
+ return this.axiosHttpProvider.head<T>(url, config);
117
+ }
118
+
119
+ head$<T>(
120
+ url: string,
121
+ config?: HttpRequestConfig,
122
+ ): Observable<HttpResponse<T>> {
123
+ return this.axiosHttpProvider.head$<T>(url, config);
124
+ }
125
+
126
+ async options<T>(
127
+ url: string,
128
+ config?: HttpRequestConfig,
129
+ ): Promise<HttpResponse<T>> {
130
+ return this.axiosHttpProvider.options<T>(url, config);
131
+ }
132
+
133
+ options$<T>(
134
+ url: string,
135
+ config?: HttpRequestConfig,
136
+ ): Observable<HttpResponse<T>> {
137
+ return this.axiosHttpProvider.options$<T>(url, config);
138
+ }
139
+
140
+ async request<T>(config: HttpRequestConfig): Promise<HttpResponse<T>> {
141
+ return this.axiosHttpProvider.request<T>(config);
142
+ }
143
+
144
+ request$<T>(config: HttpRequestConfig): Observable<HttpResponse<T>> {
145
+ return this.axiosHttpProvider.request$<T>(config);
146
+ }
147
+
148
+ setGlobalHeader(key: string, value: string): void {
149
+ this.axiosHttpProvider.setGlobalHeader(key, value);
150
+ }
151
+
152
+ removeGlobalHeader(key: string): void {
153
+ this.axiosHttpProvider.removeGlobalHeader(key);
154
+ }
155
+
156
+ getGlobalHeaders(): Record<string, string> {
157
+ return this.axiosHttpProvider.getGlobalHeaders();
158
+ }
159
+
160
+ setBaseUrl(baseUrl: string): void {
161
+ this.axiosHttpProvider.setBaseUrl(baseUrl);
162
+ }
163
+
164
+ getBaseUrl(): string {
165
+ return this.axiosHttpProvider.getBaseUrl();
166
+ }
167
+
168
+ setDefaultTimeout(timeout: number): void {
169
+ this.axiosHttpProvider.setDefaultTimeout(timeout);
170
+ }
171
+
172
+ addErrorInterceptor(interceptor: ErrorInterceptor): number {
173
+ return this.axiosHttpProvider.addErrorInterceptor(interceptor);
174
+ }
175
+
176
+ removeErrorInterceptor(id: number): void {
177
+ this.axiosHttpProvider.removeErrorInterceptor(id);
178
+ }
179
+
180
+ clearCache(key?: string): void {
181
+ this.axiosHttpProvider.clearCache(key);
182
+ }
183
+
184
+ setAuthToken(token: string, type?: string): void {
185
+ this.axiosHttpProvider.setAuthToken(token, type);
186
+ }
187
+
188
+ clearAuthToken(): void {
189
+ this.axiosHttpProvider.clearAuthToken();
190
+ }
191
+
192
+ addRequestInterceptor(
193
+ onFulfilled: (config: any) => any,
194
+ onRejected?: (error: any) => any,
195
+ ): number {
196
+ return this.axiosHttpProvider.addRequestInterceptor(
197
+ onFulfilled,
198
+ onRejected,
199
+ );
200
+ }
201
+
202
+ removeRequestInterceptor(id: number): void {
203
+ this.axiosHttpProvider.removeRequestInterceptor(id);
204
+ }
205
+
206
+ addResponseInterceptor(
207
+ onFulfilled: (response: any) => any,
208
+ onRejected?: (error: any) => any,
209
+ ): number {
210
+ return this.axiosHttpProvider.addResponseInterceptor(
211
+ onFulfilled,
212
+ onRejected,
213
+ );
214
+ }
215
+
216
+ removeResponseInterceptor(id: number): void {
217
+ this.axiosHttpProvider.removeResponseInterceptor(id);
218
+ }
219
+ }
@@ -0,0 +1,3 @@
1
+ export const HTTP_AXIOS_CONNECTION = 'HTTP_AXIOS_CONNECTION';
2
+ export const HTTP_AXIOS_PROVIDER = 'HTTP_AXIOS_PROVIDER';
3
+ export const HTTP_PROVIDER = 'HTTP_PROVIDER';
@@ -0,0 +1,33 @@
1
+ import { DynamicModule, Module, Provider } from "@nestjs/common";
2
+ import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
3
+
4
+ import { HTTP_AXIOS_CONNECTION, HTTP_AXIOS_PROVIDER } from "../../http.token";
5
+ import { AxiosHttpProvider } from "./axios.http.provider";
6
+
7
+ @Module({})
8
+ export class HttpImplementationAxiosModule {
9
+ static forRoot(config?: AxiosRequestConfig | AxiosInstance): DynamicModule {
10
+ const axiosInstance: AxiosInstance =
11
+ config && (config as AxiosInstance).request
12
+ ? (config as AxiosInstance)
13
+ : axios.create(config as AxiosRequestConfig);
14
+
15
+ const providers: Provider[] = [
16
+ {
17
+ provide: HTTP_AXIOS_CONNECTION,
18
+ useValue: axiosInstance,
19
+ },
20
+ {
21
+ provide: HTTP_AXIOS_PROVIDER,
22
+ useFactory: (conn: AxiosInstance) => new AxiosHttpProvider(conn),
23
+ inject: [HTTP_AXIOS_CONNECTION],
24
+ },
25
+ ];
26
+
27
+ return {
28
+ module: HttpImplementationAxiosModule,
29
+ providers,
30
+ exports: [HTTP_AXIOS_CONNECTION, HTTP_AXIOS_PROVIDER],
31
+ };
32
+ }
33
+ }