Last active
April 14, 2025 13:50
-
-
Save nzvtrk/ebf494441e36200312faf82ce89de9f2 to your computer and use it in GitHub Desktop.
Revisions
-
nzvtrk revised this gist
Mar 29, 2021 . 1 changed file with 12 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ /* Basic example of saving cookie using axios in node.js and session's recreation after expiration. * We have to getting/saving cookie manually because WithCredential axios param use XHR and doesn't work in node.js * Also, this example supports parallel request and send only one create session request. * */ const BASE_URL = "https://google.com"; @@ -15,9 +15,9 @@ const createSession = async () => { password: "password" }; const resp = await axios.post(BASE_URL, authParams); const [cookie] = resp.headers["set-cookie"]; // getting cookie from request axiosInstance.defaults.headers.Cookie = cookie; // attaching cookie to axiosInstance for future requests return cookie; // return Promise<cookie> because func is async }; let isGetActiveSessionRequest = false; @@ -33,13 +33,13 @@ const clearQueue = () => { requestQueue = []; }; // registering axios interceptor which handle response's errors axiosInstance.interceptors.response.use(null, error => { console.error(error.message); //logging here const { response = {}, config: sourceConfig } = error; // checking if request failed cause Unauthorized if (response.status === 401) { // if this request is first we set isGetActiveSessionRequest flag to true and run createSession if (!isGetActiveSessionRequest) { @@ -61,16 +61,16 @@ axiosInstance.interceptors.response.use(null, error => { // we push new function to queue addRequestToQueue(cookie => { // function takes one param 'cookie' console.log("Retry with new session context %s request to %s", sourceConfig.method, sourceConfig.url); sourceConfig.headers.Cookie = cookie; // setting cookie to header resolve(axios(sourceConfig)); // and resolve promise with axios request by old config with cookie // we resolve exactly axios request - NOT axiosInstance's request because it could call recursion }); }); return retryRequest; } else { // if error is not related with Unauthorized we just reject promise return Promise.reject(error); } }); -
nzvtrk revised this gist
Sep 23, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,6 +50,7 @@ axiosInstance.interceptors.response.use(null, error => { callRequestsFromQueue(cookie); clearQueue(); // and clean queue }).catch(e => { isGetActiveSessionRequest = false; // Very important! console.error('Create session error %s', e.message); clearQueue(); }); -
nzvtrk revised this gist
Sep 13, 2019 . 1 changed file with 11 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ const createSession = async () => { password: "password" }; const resp = await axios.post(BASE_URL, authParams); const [cookie] = resp.headers["set-cookie"]; // get cookie from request axiosInstance.defaults.headers.Cookie = cookie; // attach cookie to axiosInstance for future requests return cookie; // return Promise<cookie> cause func is async }; @@ -24,15 +24,18 @@ let isGetActiveSessionRequest = false; let requestQueue = []; const callRequestsFromQueue = cookie => { requestQueue.forEach(sub => sub(cookie)); }; const addRequestToQueue = sub => { requestQueue.push(sub); }; const clearQueue = () => { requestQueue = []; }; // register axios interceptor which handle responses errors axiosInstance.interceptors.response.use(null, error => { console.error(error.message); //logging here const { response = {}, config: sourceConfig } = error; @@ -45,7 +48,10 @@ axiosInstance.interceptors.response.use(null, error => { // when createSession resolve with cookie value we run all request from queue with new cookie isGetActiveSessionRequest = false; callRequestsFromQueue(cookie); clearQueue(); // and clean queue }).catch(e => { console.error('Create session error %s', e.message); clearQueue(); }); } @@ -54,6 +60,7 @@ axiosInstance.interceptors.response.use(null, error => { // we push new function to queue addRequestToQueue(cookie => { // function takes one param 'cookie' console.log("Retry with new cookie %s request to %s", method, url); sourceConfig.headers.Cookie = cookie; // set cookie to header resolve(axios(sourceConfig)); // and resolve promise with axios request by old config with cookie // we resolve exactly axios request - NOT axiosInstance request cause it could call recursion -
nzvtrk revised this gist
Aug 20, 2019 . 1 changed file with 8 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,6 @@ const BASE_URL = "https://google.com"; // Init instance of axios which works with BASE_URL const axiosInstance = axios.create({ baseURL: BASE_URL }); const createSession = async () => { console.log("create session"); const authParams = { @@ -17,33 +16,32 @@ const createSession = async () => { }; const resp = await axios.post(BASE_URL, authParams); const cookie = resp.headers["set-cookie"][0]; // get cookie from request axiosInstance.defaults.headers.Cookie = cookie; // attach cookie to axiosInstance for future requests return cookie; // return Promise<cookie> cause func is async }; let isGetActiveSessionRequest = false; let requestQueue = []; const callRequestsFromQueue = cookie => { requestQueue.map(sub => sub(cookie)); }; const addRequestToQueue = sub => { requestQueue.push(sub); }; // register axios interceptor which handle responses errors axiosInstance.interceptors.response.use(null, error => { console.error(error); const { response = {}, config: sourceConfig } = error; // check if request failed cause Unauthorized if (response.status === 401) { // if this request is first we set isGetActiveSessionRequest flag to true and run createSession if (!isGetActiveSessionRequest) { isGetActiveSessionRequest = true; createSession().then(cookie => { // when createSession resolve with cookie value we run all request from queue with new cookie isGetActiveSessionRequest = false; callRequestsFromQueue(cookie); @@ -52,9 +50,9 @@ axiosInstance.interceptors.response.use(null, (error) => { } // and while isGetActiveSessionRequest equal true we create and return new promise const retryRequest = new Promise(resolve => { // we push new function to queue addRequestToQueue(cookie => { // function takes one param 'cookie' sourceConfig.headers.Cookie = cookie; // set cookie to header resolve(axios(sourceConfig)); // and resolve promise with axios request by old config with cookie -
nzvtrk revised this gist
Apr 5, 2019 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,6 @@ const createSession = async () => { const cookie = resp.headers["set-cookie"][0]; // get cookie from request axiosInstance.defaults.headers.Cookie = cookie; // attach cookie to axiosInstance for future requests return cookie; // return Promise<cookie> cause func is async }; let isGetActiveSessionRequest = false; -
nzvtrk revised this gist
Apr 5, 2019 . No changes.There are no files selected for viewing
-
nzvtrk created this gist
Apr 5, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ /* Basic example of save cookie using axios in node.js and recreate session if it expired. * Get/save cookie manually cause WithCredential axios param use XHR and not work in node.js * Supports parallel request and send only one create session request. * */ const BASE_URL = "https://google.com"; // Init instance of axios which works with BASE_URL const axiosInstance = axios.create({ baseURL: BASE_URL }); const createSession = async () => { console.log("create session"); const authParams = { username: "username", password: "password" }; const resp = await axios.post(BASE_URL, authParams); const cookie = resp.headers["set-cookie"][0]; // get cookie from request axiosInstance.defaults.headers.Cookie = cookie; // attach cookie to axiosInstance for future requests return cookie; // return Promise<cookie> cause func is async // NOTE - we get/save cookie manually cause WithCredential axios param use XHR and not work in node.js }; let isGetActiveSessionRequest = false; let requestQueue = []; const callRequestsFromQueue = (cookie) => { requestQueue.map((sub) => sub(cookie)); }; const addRequestToQueue = (sub) => { requestQueue.push(sub); }; // register axios interceptor which handle responses errors axiosInstance.interceptors.response.use(null, (error) => { console.error(error); const { response = {}, config: sourceConfig } = error; // check if request failed cause Unauthorized if (response.status === 401) { // if this request is first we set isGetActiveSessionRequest flag to true and run createSession if (!isGetActiveSessionRequest) { isGetActiveSessionRequest = true; createSession().then((cookie) => { // when createSession resolve with cookie value we run all request from queue with new cookie isGetActiveSessionRequest = false; callRequestsFromQueue(cookie); requestQueue = []; // and clean queue }); } // and while isGetActiveSessionRequest equal true we create and return new promise const retryRequest = new Promise((resolve) => { // we push new function to queue addRequestToQueue((cookie) => { // function takes one param 'cookie' sourceConfig.headers.Cookie = cookie; // set cookie to header resolve(axios(sourceConfig)); // and resolve promise with axios request by old config with cookie // we resolve exactly axios request - NOT axiosInstance request cause it could call recursion }); }); return retryRequest; } else { // if error is not related with Unauthorized we reject promise return Promise.reject(error); } });