Skip to content

Commit

Permalink
Emit timedata
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBastin committed Jan 27, 2021
1 parent 073d60b commit a6b099d
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import { DEFAULT_ORIGIN_LIST } from "./defaultOrigins";


axios.interceptors.request.use(
config => {
(config as any).timeData = { startTime: new Date().getTime() };
return config;
}
,
error => {
return Promise.reject(error);
}
)

axios.interceptors.response.use(
response => {
(response.config as any).timeData.endTime = new Date().getTime();
return response;
},
error => {
return Promise.reject(error);
}
)

let cancelSource = axios.CancelToken.source();

function errorToObject(e: any) {
Expand Down Expand Up @@ -32,7 +54,7 @@ interface PWChromeMessage<T> {
}

interface RecvRequestMessageData {
response: AxiosResponse<any> | null;
response: any;
error: any | null;
isBinary: boolean;
}
Expand Down Expand Up @@ -94,7 +116,8 @@ const handleSendRequestMessage = async (config: any) => {
status: r.status,
statusText: r.statusText,
headers: r.headers,
data: bufferToBase64(r.data)
data: bufferToBase64(r.data),
timeData: (r.config as any).timeData
},
isBinary: true,
error: null
Expand Down Expand Up @@ -132,7 +155,8 @@ const handleSendRequestMessage = async (config: any) => {
status: res.status,
statusText: res.statusText,
headers: res.headers,
data: res.data
data: res.data,
timeData: (res.config as any).timeData
},
isBinary: false,
error: null
Expand Down

0 comments on commit a6b099d

Please sign in to comment.