-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract API client logic to a separate class
- Loading branch information
Showing
2 changed files
with
100 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import type { z } from 'zod' | ||
import axios from 'axios' | ||
import type { AxiosInstance } from 'axios' | ||
import type { Logger } from './logging' | ||
|
||
export class ApiClient { | ||
protected logger: Logger | ||
protected axios: AxiosInstance | ||
|
||
constructor({ | ||
logger, | ||
axiosOptions, | ||
}: { | ||
logger: Logger | ||
axiosOptions?: Parameters<typeof axios.create>[0] | ||
}) { | ||
this.logger = logger | ||
this.axios = createAxiosWithLogging({ | ||
logger: logger, | ||
options: axiosOptions, | ||
}) | ||
} | ||
|
||
protected async request<S extends z.ZodSchema>(options: ( | ||
& Omit<Parameters<typeof axios.request>[0], 'method' | 'url' | 'params'> | ||
& { | ||
method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH' | ||
path: string | ||
queryParams?: any | ||
responseSchema: S | ||
} | ||
)): Promise<z.infer<S>> { | ||
const { | ||
responseSchema, | ||
path, | ||
queryParams, | ||
...rest | ||
} = options | ||
const response = await this.axios.request({ | ||
url: path, | ||
params: queryParams, | ||
...rest, | ||
}) | ||
return responseSchema.parse(response.data) | ||
} | ||
} | ||
|
||
function createAxiosWithLogging({ | ||
logger, | ||
options, | ||
}: { | ||
logger: Logger | ||
options?: Parameters<typeof axios.create>[0] | ||
}): AxiosInstance { | ||
const instance = axios.create(options) | ||
|
||
instance.interceptors.request.use( | ||
(config) => { | ||
logger.debug({ | ||
msg: 'API request initiated', | ||
config: { | ||
auth: config.auth, | ||
baseURL: config.baseURL, | ||
data: config.data, | ||
headers: config.headers, | ||
url: config.url, | ||
}, | ||
}) | ||
return config | ||
}, | ||
) | ||
|
||
instance.interceptors.response.use( | ||
(response) => { | ||
logger.debug({ | ||
msg: 'API request finished', | ||
response: response, | ||
}) | ||
return response | ||
}, | ||
(error) => { | ||
logger.error({ | ||
msg: 'API request failed', | ||
error: error, | ||
}) | ||
return Promise.reject(error) | ||
}, | ||
) | ||
|
||
return instance | ||
} |