Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: usage of axios [PHX-2738] #370

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
"fast-copy": "^2.1.7",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"p-throttle": "^4.1.1",
"qs": "^6.11.2"
"p-throttle": "^4.1.1"
},
"devDependencies": {
"@babel/cli": "^7.12.8",
Expand All @@ -66,7 +65,6 @@
"@types/jest": "^29.2.2",
"@types/lodash.isplainobject": "^4.0.6",
"@types/lodash.isstring": "^4.0.6",
"@types/qs": "^6.9.5",
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"axios": "^1.4.0",
Expand Down
17 changes: 6 additions & 11 deletions src/create-http-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AxiosRequestHeaders, AxiosStatic } from 'axios'
import { AxiosRequestHeaders } from 'axios'
import type { AxiosStatic } from 'axios'
import copy from 'fast-copy'
import qs from 'qs'
import asyncToken from './async-token'

import rateLimitRetry from './rate-limit'
Expand All @@ -16,7 +16,7 @@ const HOST_REGEX = /^(?!\w+:\/\/)([^\s:]+\.?[^\s:]+)(?::(\d+))?(?!:)$/
* @private
* @param {AxiosStatic} axios - Axios library
* @param {CreateHttpClientParams} options - Initialization parameters for the HTTP client
* @return {ContentfulAxiosInstance} Initialized axios instance
* @return {AxiosInstance} Initialized axios instance
*/
export default function createHttpClient(
axios: AxiosStatic,
Expand All @@ -41,7 +41,6 @@ export default function createHttpClient(
httpsAgent: false as const,
timeout: 30000,
throttle: 0,
proxy: false as const,
basePath: '',
adapter: undefined,
maxContentLength: 1073741824, // 1GB
Expand Down Expand Up @@ -90,11 +89,6 @@ export default function createHttpClient(
headers: config.headers,
httpAgent: config.httpAgent,
httpsAgent: config.httpsAgent,
paramsSerializer: {
serialize: (params) => {
return qs.stringify(params, { arrayFormat: 'repeat' })
},
},
proxy: config.proxy,
timeout: config.timeout,
adapter: config.adapter,
Expand All @@ -106,6 +100,7 @@ export default function createHttpClient(
requestLogger: config.requestLogger,
retryOnError: config.retryOnError,
}

const instance = axios.create(axiosOptions) as AxiosInstance
instance.httpClientParams = options

Expand All @@ -116,8 +111,8 @@ export default function createHttpClient(
* and the version of the library comes from different places depending
* on whether it's a browser build or a node.js build.
* @private
* @param {CreateHttpClientParams} httpClientParams - Initialization parameters for the HTTP client
* @return {ContentfulAxiosInstance} Initialized axios instance
* @param {CreateHttpClientParams} newParams - Initialization parameters for the HTTP client
* @return {AxiosInstance} Initialized axios instance
*/
instance.cloneWithNewParams = function (
newParams: Partial<CreateHttpClientParams>
Expand Down
3 changes: 1 addition & 2 deletions src/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import isPlainObject from 'lodash.isplainobject'
import type { AxiosError } from 'axios'
import type { ContentfulErrorData } from './types'

/**
Expand All @@ -10,7 +9,7 @@ import type { ContentfulErrorData } from './types'
* and the expected error codes.
* @private
*/
export default function errorHandler(errorResponse: AxiosError<ContentfulErrorData>): never {
export default function errorHandler(errorResponse: any): never {
const { config, response } = errorResponse
let errorName

Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AxiosRequestHeaders, InternalAxiosRequestConfig } from 'axios'
import { AxiosHeaderValue, AxiosRequestHeaders, InternalAxiosRequestConfig } from 'axios'

import type {
AxiosInstance as OriginalAxiosInstance,
Expand Down Expand Up @@ -64,7 +64,7 @@ export type CreateHttpClientParams = {
logHandler?: DefaultOptions['logHandler']

/** Optional additional headers */
headers?: AxiosRequestHeaders
headers?: AxiosRequestHeaders | Record<string, AxiosHeaderValue>

defaultHostname?: string

Expand Down Expand Up @@ -122,7 +122,7 @@ export type ContentfulErrorData = {
statusText?: string
requestId?: string
message: string
details: Record<string, unknown>
request?: Record<string, unknown>
details: Record<string, any>
request?: Record<string, any>
sys?: { id?: string }
}