This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,101 @@ | ||
import axios from 'axios'; | ||
import { getLS, removeLS } from '../LocalStorage/index'; | ||
import axios from "axios"; | ||
import { getLS, removeLS } from "../LocalStorage/index"; | ||
|
||
const API_URL = | ||
process.env.NEXT_PUBLIC_ENVIORNMENT === 'prod' | ||
? 'https://api.savemyform.tk' | ||
: 'https://dev-api.savemyform.tk'; | ||
process.env.NEXT_PUBLIC_ENVIORNMENT === "prod" | ||
? "https://api.savemyform.tk" | ||
: "https://dev-api.savemyform.tk"; | ||
|
||
const getAccessToken = () => { | ||
return getLS('secret'); | ||
return getLS("secret"); | ||
}; | ||
|
||
const getHeaders = (token) => { | ||
if (!token) token = getAccessToken(); | ||
if (token) { | ||
if (!token) token = getAccessToken(); | ||
if (token) { | ||
return { | ||
headers: { | ||
Accept: "application/json", | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}; | ||
} | ||
return { | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
}; | ||
} | ||
return { | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}; | ||
}; | ||
|
||
const post = async (endpoint, body, token = null, form = false) => { | ||
let options = getHeaders(token); | ||
if (form) { | ||
options.headers['Content-Type'] = 'multipart/form-data'; | ||
} | ||
try { | ||
const response = await axios.post(API_URL + endpoint, body, options); | ||
return response; | ||
} catch (err) { | ||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log('Wrong password'); | ||
removeLS('secret'); | ||
} else if (err?.response?.status === 404) { | ||
console.log('404 Error'); | ||
removeLS('secret'); | ||
let options = getHeaders(token); | ||
if (form) { | ||
options.headers["Content-Type"] = "multipart/form-data"; | ||
} | ||
try { | ||
const response = await axios.post(API_URL + endpoint, body, options); | ||
return response; | ||
} catch (err) { | ||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log("Wrong password"); | ||
removeLS("secret"); | ||
} else if (err?.response?.status === 404) { | ||
console.log("404 Error"); | ||
removeLS("secret"); | ||
} | ||
return err?.response?.data || err; | ||
} | ||
return err?.response?.data || err; | ||
} | ||
}; | ||
|
||
const get = async (endpoint, token = null) => { | ||
try { | ||
const response = await axios.get(API_URL + endpoint, getHeaders(token)); | ||
return response; | ||
} catch (err) { | ||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log('Wrong password'); | ||
removeLS('secret'); | ||
try { | ||
const response = await axios.get(API_URL + endpoint, getHeaders(token)); | ||
return response; | ||
} catch (err) { | ||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log("Wrong password"); | ||
removeLS("secret"); | ||
} | ||
return err?.response?.data || err; | ||
} | ||
return err?.response?.data || err; | ||
} | ||
}; | ||
|
||
const patch = async (endpoint, body, token = null) => { | ||
try { | ||
const response = await axios.patch( | ||
API_URL + endpoint, | ||
body, | ||
getHeaders(token), | ||
); | ||
return response.data; | ||
} catch (err) { | ||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log('Wrong password'); | ||
removeLS('secret'); | ||
try { | ||
const response = await axios.patch( | ||
API_URL + endpoint, | ||
body, | ||
getHeaders(token) | ||
); | ||
return response.data; | ||
} catch (err) { | ||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log("Wrong password"); | ||
removeLS("secret"); | ||
} | ||
return err?.response?.data || err; | ||
} | ||
return err?.response?.data || err; | ||
} | ||
}; | ||
|
||
const remove = async (endpoint, body, token = null) => { | ||
try { | ||
let conf = getHeaders(token); | ||
conf['data'] = body; | ||
const response = await axios.delete(API_URL + endpoint, conf); | ||
return response.data; | ||
} catch (err) { | ||
console.log(err); | ||
try { | ||
let conf = getHeaders(token); | ||
conf["data"] = body; | ||
const response = await axios.delete(API_URL + endpoint, conf); | ||
return response.data; | ||
} catch (err) { | ||
console.log(err); | ||
|
||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log('Wrong password'); | ||
removeLS('secret'); | ||
console.error(err?.response?.data || err); | ||
if (err?.response?.status === 401) { | ||
console.log("Wrong password"); | ||
removeLS("secret"); | ||
} | ||
return err?.response?.data || err; | ||
} | ||
return err?.response?.data || err; | ||
} | ||
}; | ||
|
||
export { getAccessToken, post, get, patch, remove, API_URL }; |
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters
This file contains 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 characters