@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
64 lines • 2.17 kB
JavaScript
import { arch, type } from 'node:os';
import axios from 'axios';
import { highlight } from 'cli-highlight';
import FormData from 'form-data';
import { logger } from './logger.js';
/**
* This is used to set a request id, so the request and response can be matched in the logs.
*/
const id = Symbol('id');
/**
* An {@link axios} request interceptor to add support for {@link form-data}.
*
* @param config The axios request configuration.
* @returns The config with additional `multipart/form-data` headers if appropriate.
*/
export function formData(config) {
if (config.data instanceof FormData) {
Object.assign(config.headers, config.data.getHeaders());
}
return config;
}
/**
* An {@link axios} request interceptor to log requests.
*
* @param config The axios request configuration.
* @returns The original config.
*/
export function requestLogger(config) {
const time = Date.now();
logger.verbose(`> ${time} ${highlight(`${config.method?.toUpperCase()} ${axios.getUri(config)} HTTP/1.1`, {
language: 'http',
})}`);
// eslint-disable-next-line no-param-reassign
config[id] = time;
return config;
}
/**
* An {@link axios} response interceptor to log responses.
*
* @param response The axios response.
* @returns The original response.
*/
export function responseLogger(response) {
logger.verbose(`< ${response.config[id]} ${highlight(`HTTP/1.1 ${response.status} ${response.statusText}`, {
language: 'http',
})}`);
return response;
}
/**
* Configure the default Axios instance.
*
* This applies the interceptors in this modules and sets the appropriate user agent string.
*
* @param name A PascalCase representation of the client.
* @param version The version of the client to represent.
*/
export function configureAxios(name, version) {
const ua = `${name}/${version} (${type()} ${arch()}; Node ${process.version})`;
axios.defaults.headers.common['user-agent'] = ua;
axios.interceptors.request.use(formData);
axios.interceptors.request.use(requestLogger);
axios.interceptors.response.use(responseLogger);
}
//# sourceMappingURL=interceptors.js.map