Skip to content

Commit 1da21ee

Browse files
committed
Create compatibility with TS 3.9.7
1 parent c0ffc0a commit 1da21ee

File tree

8 files changed

+321
-13
lines changed

8 files changed

+321
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"tslint-config-vtex": "^2.1.0",
122122
"tslint-eslint-rules": "^5.4.0",
123123
"typemoq": "^2.1.0",
124-
"typescript": "4.9.5",
124+
"typescript": "3.9.7",
125125
"typescript-json-schema": "^0.65.1"
126126
}
127127
}

src/HttpClient/middlewares/request/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AxiosRequestConfig } from 'axios'
2-
import buildFullPath from 'axios/lib/core/buildFullPath'
2+
import buildFullPath from '../../../utils/buildFullPath'
33
import { Limit } from 'p-limit'
44
import { stringify } from 'qs'
55
import { toLower } from 'ramda'

src/HttpClient/middlewares/request/setupAxios/interceptors/tracing/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
1+
import { AxiosError, AxiosInstance, AxiosResponse, AxiosRequestConfig } from 'axios'
22
import { FORMAT_HTTP_HEADERS, Span } from 'opentracing'
33
import { createSpanReference, ErrorReport } from '../../../../../../tracing'
44
import { SpanReferenceTypes } from '../../../../../../tracing/spanReference/SpanReferenceTypes'
@@ -9,7 +9,8 @@ interface AxiosRequestTracingContext extends MiddlewaresTracingContext {
99
requestSpan?: Span
1010
}
1111

12-
interface TraceableAxiosRequestConfig extends InternalAxiosRequestConfig {
12+
// Modificado para usar AxiosRequestConfig em vez de InternalAxiosRequestConfig
13+
interface TraceableAxiosRequestConfig extends AxiosRequestConfig {
1314
tracing?: AxiosRequestTracingContext
1415
}
1516

@@ -31,7 +32,6 @@ const preRequestInterceptor = (http: AxiosInstance) => (
3132
}
3233

3334
const { tracer, rootSpan, requestSpanNameSuffix } = config.tracing
34-
3535
const spanName = requestSpanNameSuffix ? `${requestSpanPrefix}:${requestSpanNameSuffix}` : requestSpanPrefix
3636

3737
const span = rootSpan
@@ -44,6 +44,7 @@ const preRequestInterceptor = (http: AxiosInstance) => (
4444

4545
config.tracing.requestSpan = span
4646
tracer.inject(span, FORMAT_HTTP_HEADERS, config.headers)
47+
4748
return config
4849
}
4950

@@ -55,6 +56,7 @@ const onResponseSuccess = (response: TraceableAxiosResponse): TraceableAxiosResp
5556
const requestSpan = response.config.tracing?.requestSpan
5657
injectResponseInfoOnSpan(requestSpan, response)
5758
requestSpan?.finish()
59+
5860
return response
5961
}
6062

@@ -67,17 +69,26 @@ const onResponseError = (err: ExtendedAxiosError) => {
6769
injectResponseInfoOnSpan(requestSpan, err.response)
6870
ErrorReport.create({ originalError: err }).injectOnSpan(requestSpan, err.config.tracing.logger)
6971
requestSpan.finish()
72+
7073
return Promise.reject(err)
7174
}
7275

7376
export const addTracingPreRequestInterceptor = (http: AxiosInstance) => {
74-
const requestTracingInterceptor = http.interceptors.request.use(preRequestInterceptor(http), undefined)
77+
// Usando type assertion para compatibilidade com TypeScript 3.9.7
78+
const requestTracingInterceptor = http.interceptors.request.use(
79+
preRequestInterceptor(http) as any,
80+
undefined
81+
)
7582

7683
return { requestTracingInterceptor }
7784
}
7885

7986
export const addTracingResponseInterceptor = (http: AxiosInstance) => {
80-
const responseTracingInterceptor = http.interceptors.response.use(onResponseSuccess, onResponseError)
87+
// Usando type assertion para compatibilidade com TypeScript 3.9.7
88+
const responseTracingInterceptor = http.interceptors.response.use(
89+
onResponseSuccess as any,
90+
onResponseError as any
91+
)
8192

8293
return { responseTracingInterceptor }
8394
}

src/HttpClient/middlewares/request/setupAxios/interceptors/tracing/spanSetup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
2-
import buildFullPath from 'axios/lib/core/buildFullPath'
2+
import buildFullPath from '../../../../../../utils/buildFullPath'
33
import { Span } from 'opentracing'
44
import { ROUTER_CACHE_HEADER } from '../../../../../../constants'
55
import { CustomHttpTags, OpentracingTags } from '../../../../../../tracing/Tags'

src/axios.d.ts

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
declare module 'axios/index' {}
2+
3+
declare module 'axios' {
4+
export interface AxiosTransformer {
5+
(data: any, headers?: any): any;
6+
}
7+
8+
export interface AxiosAdapter {
9+
(config: AxiosRequestConfig): AxiosPromise<any>;
10+
}
11+
12+
export interface AxiosBasicCredentials {
13+
username: string;
14+
password: string;
15+
}
16+
17+
export interface AxiosProxyConfig {
18+
host: string;
19+
port: number;
20+
auth?: {
21+
username: string;
22+
password: string;
23+
};
24+
protocol?: string;
25+
}
26+
27+
export type Method =
28+
| 'get' | 'GET'
29+
| 'delete' | 'DELETE'
30+
| 'head' | 'HEAD'
31+
| 'options' | 'OPTIONS'
32+
| 'post' | 'POST'
33+
| 'put' | 'PUT'
34+
| 'patch' | 'PATCH'
35+
| 'link' | 'LINK'
36+
| 'unlink' | 'UNLINK';
37+
38+
export type ResponseType =
39+
| 'arraybuffer'
40+
| 'blob'
41+
| 'document'
42+
| 'json'
43+
| 'text'
44+
| 'stream';
45+
46+
// Tipos de cabeçalho
47+
export type RawAxiosRequestHeaders = Record<string, any>;
48+
export type RawAxiosResponseHeaders = Record<string, any>;
49+
export interface AxiosHeaders extends Record<string, any> {
50+
set(headerName: string, value: string): AxiosHeaders;
51+
get(headerName: string): string | undefined;
52+
delete(headerName: string): boolean;
53+
clear(): AxiosHeaders;
54+
toJSON(): Record<string, any>;
55+
[key: string]: any;
56+
}
57+
58+
export interface AxiosRequestConfig {
59+
url?: string;
60+
method?: Method;
61+
baseURL?: string;
62+
transformRequest?: AxiosTransformer | AxiosTransformer[];
63+
transformResponse?: AxiosTransformer | AxiosTransformer[];
64+
headers?: RawAxiosRequestHeaders;
65+
params?: any;
66+
paramsSerializer?: (params: any) => string;
67+
data?: any;
68+
timeout?: number;
69+
timeoutErrorMessage?: string;
70+
withCredentials?: boolean;
71+
adapter?: AxiosAdapter;
72+
auth?: AxiosBasicCredentials;
73+
responseType?: ResponseType;
74+
responseEncoding?: string;
75+
xsrfCookieName?: string;
76+
xsrfHeaderName?: string;
77+
onUploadProgress?: (progressEvent: any) => void;
78+
onDownloadProgress?: (progressEvent: any) => void;
79+
maxContentLength?: number;
80+
maxBodyLength?: number;
81+
validateStatus?: (status: number) => boolean;
82+
maxRedirects?: number;
83+
socketPath?: string | null;
84+
httpAgent?: any;
85+
httpsAgent?: any;
86+
proxy?: AxiosProxyConfig | false;
87+
cancelToken?: CancelToken;
88+
decompress?: boolean;
89+
transitional?: any;
90+
signal?: any;
91+
insecureHTTPParser?: boolean;
92+
[key: string]: any; // Para permitir qualquer propriedade adicional
93+
}
94+
95+
export interface InternalAxiosRequestConfig extends AxiosRequestConfig {
96+
headers: AxiosHeaders | RawAxiosRequestHeaders;
97+
[key: string]: any; // Para permitir propriedades adicionais como 'tracing'
98+
}
99+
100+
export interface AxiosResponse<T = any> {
101+
data: T;
102+
status: number;
103+
statusText: string;
104+
headers: RawAxiosResponseHeaders;
105+
config: AxiosRequestConfig;
106+
request?: any;
107+
}
108+
109+
// Define AxiosError como uma classe para fazer instanceof funcionar
110+
export class AxiosError<T = any> extends Error {
111+
config: AxiosRequestConfig;
112+
code?: string;
113+
request?: any;
114+
response?: AxiosResponse<T>;
115+
isAxiosError: boolean;
116+
status?: number;
117+
118+
constructor(
119+
message?: string,
120+
code?: string,
121+
config?: AxiosRequestConfig,
122+
request?: any,
123+
response?: AxiosResponse<T>
124+
);
125+
126+
toJSON(): object;
127+
128+
// Métodos estáticos para criar instâncias de AxiosError
129+
static from<T = any>(
130+
error: Error,
131+
code?: string,
132+
config?: AxiosRequestConfig,
133+
request?: any,
134+
response?: AxiosResponse<T>,
135+
customProps?: Record<string, any>
136+
): AxiosError<T>;
137+
}
138+
139+
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
140+
}
141+
142+
export interface CancelStatic {
143+
new (message?: string): Cancel;
144+
}
145+
146+
export interface Cancel {
147+
message: string;
148+
}
149+
150+
export interface Canceler {
151+
(message?: string): void;
152+
}
153+
154+
export interface CancelTokenStatic {
155+
new (executor: (cancel: Canceler) => void): CancelToken;
156+
source(): CancelTokenSource;
157+
}
158+
159+
export interface CancelToken {
160+
promise: Promise<Cancel>;
161+
reason?: Cancel;
162+
throwIfRequested(): void;
163+
}
164+
165+
export interface CancelTokenSource {
166+
token: CancelToken;
167+
cancel: Canceler;
168+
}
169+
170+
export interface AxiosInterceptorManager<V> {
171+
use(onFulfilled?: (value: V) => V | Promise<V>, onRejected?: (error: any) => any): number;
172+
eject(id: number): void;
173+
clear(): void;
174+
}
175+
176+
export interface AxiosInstance {
177+
(config: AxiosRequestConfig): AxiosPromise;
178+
(url: string, config?: AxiosRequestConfig): AxiosPromise;
179+
defaults: AxiosRequestConfig;
180+
interceptors: {
181+
request: AxiosInterceptorManager<AxiosRequestConfig>;
182+
response: AxiosInterceptorManager<AxiosResponse>;
183+
};
184+
getUri(config?: AxiosRequestConfig): string;
185+
request<T = any, R = AxiosResponse<T>>(config: AxiosRequestConfig): Promise<R>;
186+
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
187+
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
188+
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
189+
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
190+
post<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
191+
put<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
192+
patch<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;
193+
}
194+
195+
export interface AxiosStatic extends AxiosInstance {
196+
create(config?: AxiosRequestConfig): AxiosInstance;
197+
Cancel: CancelStatic;
198+
CancelToken: CancelTokenStatic;
199+
isCancel(value: any): boolean;
200+
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
201+
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
202+
isAxiosError(payload: any): payload is AxiosError;
203+
}
204+
205+
const axios: AxiosStatic;
206+
207+
export const AxiosError: {
208+
new <T = any>(
209+
message?: string,
210+
code?: string,
211+
config?: AxiosRequestConfig,
212+
request?: any,
213+
response?: AxiosResponse<T>
214+
): AxiosError<T>;
215+
readonly prototype: AxiosError;
216+
readonly ERR_NETWORK: string;
217+
readonly ERR_BAD_REQUEST: string;
218+
readonly TIMEOUT_ERROR_CODE: string;
219+
readonly ERR_BAD_RESPONSE: string;
220+
readonly ERR_FR_TOO_MANY_REDIRECTS: string;
221+
readonly ERR_DEPRECATED: string;
222+
readonly ERR_BAD_OPTION_VALUE: string;
223+
readonly ERR_CANCELED: string;
224+
readonly ECONNABORTED: string;
225+
readonly ETIMEDOUT: string;
226+
from<T = any>(
227+
error: Error,
228+
code?: string,
229+
config?: AxiosRequestConfig,
230+
request?: any,
231+
response?: AxiosResponse<T>,
232+
customProps?: Record<string, any>
233+
): AxiosError<T>;
234+
};
235+
236+
export default axios;
237+
}

src/utils/buildFullPath.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// This module is part of the Axios library.
2+
// It is used to build a full URL by combining a base URL with a relative URL.
3+
4+
// It is originally not intended to be used directly, but rather as a utility function within the Axios library.
5+
// Since Axios 1.0, this function is not exported by default, and the team does not recommend using it directly,
6+
// as it may not be available in future versions.
7+
8+
// The function takes a base URL and a relative URL as input, and returns the full URL.
9+
// It handles both absolute and relative URLs, and ensures that the resulting URL is properly formatted.
10+
// It is used internally by Axios to construct the full URL for HTTP requests.
11+
12+
/**
13+
* Creates a new URL by combining the baseURL with the requestedURL,
14+
* only when the requestedURL is not already an absolute URL.
15+
* If the requestURL is absolute, this function returns the requestedURL untouched.
16+
*
17+
* @param {string} baseURL The base URL
18+
* @param {string} requestedURL Absolute or relative URL to combine
19+
*
20+
* @returns {string} The combined full path
21+
*/
22+
export default function(baseURL?: string, requestedURL?: string, allowAbsoluteUrls?: boolean): string | undefined {
23+
let isRelativeUrl = !isAbsoluteURL(requestedURL);
24+
if (baseURL && (isRelativeUrl || allowAbsoluteUrls == false)) {
25+
return combineURLs(baseURL, requestedURL);
26+
}
27+
return requestedURL;
28+
}
29+
30+
/**
31+
* Determines whether the specified URL is absolute
32+
*
33+
* @param {string} url The URL to test
34+
*
35+
* @returns {boolean} True if the specified URL is absolute, otherwise false
36+
*/
37+
function isAbsoluteURL(url?: string): boolean {
38+
// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
39+
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
40+
// by any combination of letters, digits, plus, period, or hyphen.
41+
return !!url && /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
42+
}
43+
44+
/**
45+
* Creates a new URL by combining the specified URLs
46+
*
47+
* @param {string} baseURL The base URL
48+
* @param {string} relativeURL The relative URL
49+
*
50+
* @returns {string} The combined URL
51+
*/
52+
function combineURLs(baseURL?: string, relativeURL?: string): string | undefined {
53+
return relativeURL && baseURL
54+
? baseURL.replace(/\/?\/$/, '') + '/' + relativeURL.replace(/^\/+/, '')
55+
: baseURL;
56+
}

0 commit comments

Comments
 (0)