Skip to content

Instantly share code, notes, and snippets.

@panzi
Last active September 30, 2021 19:12
Show Gist options
  • Save panzi/d84a468e0b52469d9d7fecd706220aa3 to your computer and use it in GitHub Desktop.
Save panzi/d84a468e0b52469d9d7fecd706220aa3 to your computer and use it in GitHub Desktop.
Some missing types in axios for NodeJS (types would be different in browser).
import axios from 'axios';
import { IncomingMessage } from 'http';
// browser version would use Uint8Array instead of Buffer and woudln't have stream/IncomingMessage
// since Buffer is a sub-class of Uint8Array you could use that in nodejs too, if you don't need any of the Buffer specific methods
declare module 'axios' {
interface AxiosInstance {
request(config: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
request(config: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
request(config: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
get(url: string, config?: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
get(url: string, config?: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
get(url: string, config?: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
delete(url: string, config?: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
delete(url: string, config?: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
delete(url: string, config?: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
head(url: string, config?: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
head(url: string, config?: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
head(url: string, config?: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
options(url: string, config?: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
options(url: string, config?: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
options(url: string, config?: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
post(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
post(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
post(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
put(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
put(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
put(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
patch(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'arraybuffer' }): Promise<AxiosResponse<Buffer>>;
patch(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'text' }): Promise<AxiosResponse<string>>;
patch(url: string, data?: any, config?: AxiosRequestConfig & { responseType: 'stream' }): Promise<AxiosResponse<IncomingMessage>>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment