Request utility with validations based on Axios & ts-interface-checker
This lib is maintained for my needs, open-source for your inspiration
Features
- validates request and response by
ts-interface-checker
. Checkers could be created using
dk-file-generator - omits extraneous params from response using dk-checker-remove-extraneous
- supports mocks
- supports file downloads if response type is
blob
with customizable file name - supports FormData requests
- supports
url
as function - supports custom headers
- works in Node.js
- request params starting with
omit_
are used in in url
creation but omitted in body
Installation
Add dk-request
to package.json and install.
Usage
import { AxiosError } from 'axios';
import { request, errors } from 'dk-request';
type TypeRequest = {
id: string;
}
type TypeResponse = {
data: string;
}
const validators = t.createCheckers({
TypeRequest: t.iface([], { id: 'string' }),
TypeResponse: t.iface([], { data: 'string' }),
});
request({
url: 'https://google.com/api/get-items',
apiName: 'getItems',
requestParams: { id: 'id' } as TypeRequest,
validatorRequest: validators.TypeRequest,
validatorResponse: validators.TypeResponse,
}).then((response: TypeResponse) => {
successHandler(response);
}).catch((error: Error | AxiosError) => {
errorHandler(error);
});
Params
url
- string | ((request: any) => string)
- full url
apiName
- string
- just for logging
requestParams
- Record<string, any> & { formData?: any; downloadAsFile?: boolean }
If you need to send FormData include it in formData
. Other params will not be sent, but
could still be used in url
function.
If you need to download file passed as blob
by backend use downloadAsFile: true
, this
way response validation will be omitted.
mock
(optional) - TypeResponse
, there will be no actual request to API, but validations are
applied
method
(optional, default POST
) - 'GET' | 'POST' | 'PUT' | 'DELETE'
headers
(optional) - Record<string, any>
extraneousLogger
(optional) - logger for dk-checker-remove-extraneous
validatorRequest
(optional) - Checker
validatorResponse
(optional) - Checker
disableCredentials
(optional) - boolean
- restrict or allow sending of cookies along with request
omitResponseValidation
(optional) - boolean
- restrict or allow response validation
afterRequestInterceptor
(optional) - (response: AxiosResponse) => Promise<void>
- a method for
performing some manipulations with response, like checking response.headers
(ex. server sends
JWT token in headers)
afterRequestInterceptor: (axiosResponse) => {
const newToken = axiosResponse.headers.authorization;
if (newToken) setTokenToStore(newToken);
return Promise.resolve();
}
downloadFileNameGetter
(optional) - (response: AxiosResponse) => string
- a method for
defining custom downloaded file name, ex.
downloadFileNameGetter: (axiosResponse) => {
return (
axiosResponse.headers['content-disposition']?.split('filename=')?.[1]?.replaceAll('"', '') ||
'result.csv'
);
}
For access to response headers like content-disposition
don't forget expose-headers
header
in server response