![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
maioresomnis
Advanced tools
This lib is maintained for my needs, open-source for your inspiration
ts-interface-checker
. Checkers could be created using
dk-file-generatorblob
with customizable file nameurl
as functionomit_
are used in in url
creation but omitted in bodyAdd dk-request
to package.json and install.
import { AxiosError } from 'axios';
import { request, errors } from 'dk-request';
type TypeRequest = {
id: string;
}
type TypeResponse = {
data: string;
}
// possibly auto-generated
const validators = t.createCheckers({
TypeRequest: t.iface([], { id: 'string' }),
TypeResponse: t.iface([], { data: 'string' }),
});
request({
url: 'https://google.com/api/get-items',
// url: (requestParams) => `https://google.com/api/get-items/${requestParams.id}`,
apiName: 'getItems',
requestParams: { id: 'id' } as TypeRequest,
validatorRequest: validators.TypeRequest,
validatorResponse: validators.TypeResponse,
}).then((response: TypeResponse) => {
// response is validated and cleared, put it in store or use as you need
successHandler(response);
}).catch((error: Error | AxiosError) => {
// if it's validation error then
// error.name === errors.VALIDATION
// error.message smth. like
// 'validateRequest: request.id is missing for "getItems"'
// 'validateResponse: response.data is not a string for "getItems"'
errorHandler(error);
});
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 forgetexpose-headers
header in server response
FAQs
Unknown package
We found that maioresomnis demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.