Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 78 additions & 14 deletions src/services/network.js
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 This could be simplified by using the ok Response attribute.

Suggested change
if (response.status < 200 || response.status >= 400) {
if (!response.ok) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its because we also need to handle 304s.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}