Skip to content

Commit

Permalink
feat: Migrate fetch to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
mizlan committed Feb 24, 2024
1 parent 616c7e8 commit e1da913
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions src/lib/fetch.js → src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import constants from '../constants';
* @param {string} method HTTP method to make the request, defaults to post
*/

const makeServerRequest = (data, endpoint, method = 'post') => {
const options = {
const makeServerRequest = (data: object, endpoint: string, method: string = 'post') => {
const options: RequestInit = {
method,
headers: {
'Content-Type': 'application/json; charset=utf-8',
Expand All @@ -32,7 +32,7 @@ const makeServerRequest = (data, endpoint, method = 'post') => {
return fetch(`${constants.SERVER_URL}/${endpoint}`, options);
};

export const createUser = (uid) => {
export const createUser = (uid: string) => {
console.error('creating user');
return makeServerRequest({ uid }, 'user/create', 'post');
};
Expand All @@ -51,30 +51,30 @@ export const createUser = (uid) => {
* }
* }
*/
export const getUserData = async (uid = '', includePrograms = false) => {
export const getUserData = async (
uid: string,
includePrograms = false,
): Promise<{ ok: boolean; data: object; error: string }> => {
const userDataEndpoint = `${constants.SERVER_URL}/user/get?uid=${uid}${
includePrograms ? '&programs=true' : ''
}`;

const options = {
const options: RequestInit = {
method: 'get',
mode: 'cors', // no-cors, cors, *same-origin
};

try {
const result = await fetch(userDataEndpoint, options);
const status = await result.status;
const status = result.status;
const ok = status === 200;
if (status === 404) {
await createUser(uid);
return getUserData(uid, includePrograms);
}
const data = ok ? await result.json() : {};
const error = !ok ? await result.text() : '';
return { ok, data, error };
} catch (err) {
await createUser(uid);
return getUserData(uid, includePrograms);
if (err instanceof Error) return { ok: false, data: {}, error: err.message };
else if (typeof err === 'string') return { ok: false, data: {}, error: err };
else return { ok: false, data: {}, error: 'Error encountered' };
}
};

Expand All @@ -85,7 +85,7 @@ export const getUserData = async (uid = '', includePrograms = false) => {
*/

// eslint-disable-next-line default-param-last
export const updatePrograms = (uid = '', programs) => {
export const updatePrograms = (uid: string = '', programs: object) => {
const endpoint = 'program/update';
return makeServerRequest({ uid, programs }, endpoint, 'put');
};
Expand All @@ -97,7 +97,7 @@ export const updatePrograms = (uid = '', programs) => {
*/

// eslint-disable-next-line default-param-last
export const updateUserData = (uid = '', userData) => {
export const updateUserData = (uid: string = '', userData: object) => {
const endpoint = 'user/update';
return makeServerRequest({ uid, ...userData }, endpoint, 'put');
};
Expand All @@ -107,18 +107,32 @@ export const updateUserData = (uid = '', userData) => {
* @param {Object} data required data to create program - might eventually become enumerated
*/

export const createSketch = (data) => {
const { uid, wid, ...rest } = data;
return makeServerRequest({ uid, wid, program: rest }, 'program/create');
interface Program {
uid: string;
thumbnail: string;
language: string;
name: string;
code: string;
}

export const createSketch = ({
uid,
wid,
program,
}: {
uid: string;
wid: string;
program: Program;
}) => {
return makeServerRequest({ uid, wid, program }, 'program/create');
};

/**
* deletes a sketch in Firestore with given docID
* @param {Object} data required data to delete program (uid, docID, name)
*/

export const deleteSketch = (data) => {
const { uid, name } = data;
export const deleteSketch = ({ uid, name }: { uid: string; name: string }) => {
return makeServerRequest({ uid, pid: name }, 'program/delete', 'delete');
};

Expand All @@ -127,7 +141,7 @@ export const deleteSketch = (data) => {
* @param {string} docID the key for the requested program in the top-level programs object
*/

export const getSketch = (docID) => {
export const getSketch = (docID: string) => {
const endpoint = `program/get?pid=${docID}`;
return makeServerRequest({}, endpoint, 'get');
};
Expand All @@ -136,16 +150,17 @@ export const getSketch = (docID) => {
* creates a new class
* @param {Object} data required data to create class {uid, name, thumbnail}
*/
export const createClass = (data) => makeServerRequest(data, 'class/create');
export const createClass = (data: { uid: string; name: string; thumbnail: string }) =>
makeServerRequest(data, 'class/create');

/**
* add a student to an existing class
* @param {Object} data student's uid and class's word id {uid, wid}
*/
export const joinClass = async (data) => {
export const joinClass = async (data: object) => {
console.error(data);
const result = await makeServerRequest(data, 'class/join', 'put');
const ok = await result.ok;
const ok = result.ok;
const { status } = result;
const classData = ok ? await result.json() : {};
return { ok, status, classData };
Expand All @@ -155,9 +170,9 @@ export const joinClass = async (data) => {
* remove a member from a class
* @param {Object} data member's uid and class's cid {uid, cid}
*/
export const leaveClass = async (data) => {
export const leaveClass = async (data: { uid: string; cid: string }) => {
const result = await makeServerRequest(data, 'class/leave', 'put');
const ok = await result.ok;
const ok = result.ok;
console.error(`ok: ${ok}`);
// let userData = await result.json();
const userData = {};
Expand All @@ -170,12 +185,16 @@ export const leaveClass = async (data) => {
* @param {boolean} withPrograms whether to include this class's sketches or not
* @param {boolean} withUserData whether to include student data
*/
export const getClass = async (data, withPrograms, withUserData) => {
export const getClass = async (
data: { uid: string; cid: string },
withPrograms: boolean,
withUserData: boolean,
) => {
const result = await makeServerRequest(
data,
`class/get?programs=${withPrograms}&userData=${withUserData}`,
);
const ok = await result.ok;
const ok = result.ok;
console.error(`response status: ${result.status}`);
const classData = await result.json();
return { ok, classData };
Expand Down

0 comments on commit e1da913

Please sign in to comment.