From 7cb96eba0b47c259db9c67b349710c34014e1661 Mon Sep 17 00:00:00 2001 From: Anaswara T Rajan Date: Sat, 30 Jan 2021 22:19:25 +0530 Subject: [PATCH] Refactor serverAPI interface --- src/api/server.ts | 70 +++++++++++++++++++++++++-------------- src/utils/privateAuth.tsx | 2 +- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/api/server.ts b/src/api/server.ts index 79c9cb7..8fe398b 100644 --- a/src/api/server.ts +++ b/src/api/server.ts @@ -13,30 +13,19 @@ class ServerAPI { this.URL = process.env.NEXT_PUBLIC_SERVER_URL; this.domain = process.env.NEXT_PUBLIC_ORIGIN_DOMAIN; + this.headers = { + "Content-Type": "application/json" + } } httpMethod = async ( endpoint: string, - reqMethod: string, - token = "", - payload = "" + requestOptions: RequestInit, ): Promise => { - this.headers = { - "Content-Type": "application/json", - Authorization: `Bearer ${token}`, - }; - const requestOptions: RequestInit = { - mode: "cors", - credentials: "include", - method: reqMethod, - headers: this.headers, - }; - if (reqMethod !== "GET") { - requestOptions.body = payload; - } const res = await fetch(endpoint, requestOptions); const { status } = res; const responseData = await res.json(); + console.log(res) return { data: responseData, statusCode: status, @@ -47,7 +36,11 @@ class ServerAPI { pollid: string | string[] | null | undefined ): Promise => { const endpoint = `${this.URL}/poll/${pollid}`; - return this.httpMethod(endpoint, "GET"); + const requestOptions: RequestInit = { + method: "GET", + headers: this.headers, + }; + return this.httpMethod(endpoint, requestOptions); }; getPolls = (pollArgs: { @@ -56,7 +49,15 @@ class ServerAPI { }): Promise => { const { userID, token } = pollArgs; const endpoint = `${this.URL}/user/${userID}`; - return this.httpMethod(endpoint, "GET", token); + this.headers = { + Authorization: `Bearer ${token}`, + }; + const requestOptions: RequestInit = { + credentials: "include", + method: "GET", + headers: this.headers, + }; + return this.httpMethod(endpoint, requestOptions); }; createPoll = (pollArgs: { @@ -64,9 +65,17 @@ class ServerAPI { token: string; }): Promise => { const { poll, token } = pollArgs; - const payload = JSON.stringify(poll); const endpoint = `${this.URL}/user/poll`; - return this.httpMethod(endpoint, "POST", token, payload); + this.headers = { + Authorization: `Bearer ${token}`, + }; + const requestOptions: RequestInit = { + credentials: "include", + method: "POST", + headers: this.headers, + body: JSON.stringify(poll), + }; + return this.httpMethod(endpoint, requestOptions); }; markChoices = (voteArgs: { @@ -74,9 +83,13 @@ class ServerAPI { pollid: string; }): Promise => { const { newVote, pollid } = voteArgs; - const payload = JSON.stringify(newVote); const endpoint = `${this.URL}/poll/${pollid}`; - return this.httpMethod(endpoint, "PUT", payload); + const requestOptions: RequestInit = { + method: "PUT", + headers: this.headers, + body: JSON.stringify(newVote) + }; + return this.httpMethod(endpoint, requestOptions); }; markFinalChoice = (voteArgs: { @@ -85,9 +98,18 @@ class ServerAPI { token: string; }): Promise => { const { finalChoice, pollid, token } = voteArgs; - const payload = JSON.stringify(finalChoice); const endpoint = `${this.URL}/user/poll/${pollid}`; - return this.httpMethod(endpoint, "PUT", token, payload); + this.headers = { + Authorization: `Bearer ${token}`, + }; + const requestOptions: RequestInit = { + credentials: "include", + method: "PUT", + headers: this.headers, + body: JSON.stringify(finalChoice) + }; + console.log(requestOptions) + return this.httpMethod(endpoint, requestOptions); }; } diff --git a/src/utils/privateAuth.tsx b/src/utils/privateAuth.tsx index 520fa47..b2dfbbf 100644 --- a/src/utils/privateAuth.tsx +++ b/src/utils/privateAuth.tsx @@ -30,7 +30,7 @@ const privateAuthWrapper = (Component: NextPage) => { if (user) { user.getIdToken(true) .then((token) => { - // console.log(token) + console.log(token) dispatch( login({ displayName: user.displayName, username: user.email, token }) );