Skip to content

Instantly share code, notes, and snippets.

@zsnmwy
Last active May 10, 2021 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsnmwy/8e2db52a46a8ec4518dbd3a85fdc7836 to your computer and use it in GitHub Desktop.
Save zsnmwy/8e2db52a46a8ec4518dbd3a85fdc7836 to your computer and use it in GitHub Desktop.
// ENV Node
import axios, {
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
} from "axios";
declare module "axios" {
interface AxiosInstance {
(config: AxiosRequestConfig): AxiosPromise;
(url: string, config?: AxiosRequestConfig): AxiosPromise;
defaults: AxiosRequestConfig;
interceptors: {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
getUri(config?: AxiosRequestConfig): string;
request<T = any, R = AxiosResponse<T>>(
config: AxiosRequestConfig
): Promise<R>;
get<T = any, R = AxiosResponse<T>>(
url: string,
config?: AxiosRequestConfig
): Promise<R>;
delete<T = any, R = AxiosResponse<T>>(
url: string,
config?: AxiosRequestConfig
): Promise<R>;
head<T = any, R = AxiosResponse<T>>(
url: string,
config?: AxiosRequestConfig
): Promise<R>;
options<T = any, R = AxiosResponse<T>>(
url: string,
config?: AxiosRequestConfig
): Promise<R>;
post<T = any, R = AxiosResponse<T>>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<R>;
put<T = any, R = AxiosResponse<T>>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<R>;
patch<T = any, R = AxiosResponse<T>>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<R>;
$request<T = any>(config: AxiosRequestConfig): Promise<T>;
$get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
$delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
$head<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
$options<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
$post<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<T>;
$put<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<T>;
$patch<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<T>;
}
}
const instance: AxiosInstance = axios.create();
for (const method of [
"request",
"delete",
"get",
"head",
"options",
"post",
"put",
"patch",
]) {
instance["$" + method] = function () {
return this[method]
.apply(this, arguments)
.then((res: AxiosResponse) => res && res.data);
};
}
instance.interceptors.request.use((config: AxiosRequestConfig) => {
let { url } = config;
console.log("start url",url) // https://www.baidu.com/s?wd=qwe&rsv_spt=1
const { method } = config;
const tmpUrl = new URL(url);
const { pathname, searchParams } = tmpUrl;
const re = /\/s/;
const appKey = "123";
// Check Path
if (!re.test(pathname)) return config;
// Check Method
if (method === "get") {
// Check Key
if (searchParams.get("appKey")) return config;
// Set Key
searchParams.set("appKey", appKey);
// Replace original url
url = tmpUrl.toString();
}
console.log("end url", url) // https://www.baidu.com/s?wd=qwe&rsv_spt=1&appKey=123
return config;
});
(async () => {
const data = await instance.$get("https://www.baidu.com/s?wd=qwe&rsv_spt=1");
console.log("data \n", data);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment