-
Notifications
You must be signed in to change notification settings - Fork 4
Refactor Gist Web to use Fetch API #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,21 +1,85 @@ | ||||||
| import Gist from '../gist'; | ||||||
| import axios from 'axios'; | ||||||
| import { settings } from './settings'; | ||||||
| import { getUserToken } from "../managers/user-manager"; | ||||||
|
|
||||||
| export function UserNetworkInstance() { | ||||||
| var instance = axios.create({ | ||||||
| baseURL: settings.GIST_QUEUE_API_ENDPOINT[Gist.config.env], | ||||||
| timeout: 5000 | ||||||
| }); | ||||||
| if (settings.getQueueAPIVersion() === "3") { | ||||||
| instance.defaults.baseURL = settings.GIST_QUEUE_V3_API_ENDPOINT[Gist.config.env]; | ||||||
| } | ||||||
| instance.defaults.headers.common['X-CIO-Site-Id'] = Gist.config.siteId; | ||||||
| instance.defaults.headers.common['X-CIO-Client-Platform'] = 'web'; | ||||||
| var userToken = getUserToken(); | ||||||
| const baseURL = | ||||||
| settings.getQueueAPIVersion() === "3" | ||||||
| ? settings.GIST_QUEUE_V3_API_ENDPOINT[Gist.config.env] | ||||||
| : settings.GIST_QUEUE_API_ENDPOINT[Gist.config.env]; | ||||||
|
|
||||||
| const defaultHeaders = { | ||||||
| 'X-CIO-Site-Id': Gist.config.siteId, | ||||||
| 'X-CIO-Client-Platform': 'web', | ||||||
| }; | ||||||
|
|
||||||
| const userToken = getUserToken(); | ||||||
| if (userToken != null) { | ||||||
| instance.defaults.headers.common['X-Gist-Encoded-User-Token'] = btoa(userToken); | ||||||
| defaultHeaders['X-Gist-Encoded-User-Token'] = btoa(userToken); | ||||||
| } | ||||||
| return instance; | ||||||
| } | ||||||
|
|
||||||
| async function request(path, options = {}) { | ||||||
| const url = baseURL + path; | ||||||
| const controller = new AbortController(); | ||||||
| const timeout = setTimeout(() => controller.abort(), 5000); | ||||||
|
|
||||||
| try { | ||||||
| const response = await fetch(url, { | ||||||
| method: options.method || 'GET', | ||||||
| headers: { | ||||||
| ...defaultHeaders, | ||||||
| ...(options.headers || {}), | ||||||
| }, | ||||||
| body: options.body, | ||||||
| signal: controller.signal, | ||||||
| }); | ||||||
|
|
||||||
| clearTimeout(timeout); | ||||||
|
|
||||||
| const isJSON = response.headers.get("content-type")?.includes("application/json"); | ||||||
| const data = isJSON ? await response.json() : await response.text(); | ||||||
|
|
||||||
| const headersObj = Object.fromEntries(response.headers.entries()); | ||||||
|
|
||||||
| if (response.status < 200 || response.status >= 400) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 This could be simplified by using the
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its because we also need to handle 304s.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bernard is correct, we return a 304 Not Modified when users hit the cache for messages instead of going to Firestore. |
||||||
| throw { | ||||||
| response: { | ||||||
| status: response.status, | ||||||
| data, | ||||||
| headers: headersObj, | ||||||
| } | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| return { | ||||||
| status: response.status, | ||||||
| headers: headersObj, | ||||||
| data, | ||||||
| }; | ||||||
| } catch (err) { | ||||||
| clearTimeout(timeout); | ||||||
|
|
||||||
| if (!err.response) { | ||||||
| err.response = { | ||||||
| status: 0, | ||||||
| data: err.message || 'Unknown error', | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| throw err; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| request.post = (path, data = {}, config = {}) => { | ||||||
| return request(path, { | ||||||
| method: 'POST', | ||||||
| body: JSON.stringify(data), | ||||||
| headers: { | ||||||
| 'Content-Type': 'application/json', | ||||||
| ...(config.headers || {}) | ||||||
| } | ||||||
| }); | ||||||
| }; | ||||||
|
|
||||||
| return request; | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.