Skip to content

Commit

Permalink
feat(cloud-storage): add postV2 function for use cloud-storage v2 api (
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheerego7 authored Sep 5, 2022
1 parent 456e906 commit 06cc405
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/flat-server-api/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const FLAT_SERVER_BASE_URL = `https://${process.env.FLAT_SERVER_DOMAIN}`;

export const FLAT_SERVER_BASE_URL_V1 = `https://${process.env.FLAT_SERVER_DOMAIN}/v1`;
export const FLAT_SERVER_BASE_URL_V2 = `https://${process.env.FLAT_SERVER_DOMAIN}/v2`;

export const FLAT_SERVER_LOGIN = {
AGORA_CALLBACK: `https://${process.env.FLAT_SERVER_DOMAIN}/v1/login/agora/callback`,
Expand Down
55 changes: 52 additions & 3 deletions packages/flat-server-api/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FLAT_SERVER_BASE_URL_V1, Status } from "./constants";
import { FLAT_SERVER_BASE_URL_V1, FLAT_SERVER_BASE_URL_V2, Status } from "./constants";
import { ServerRequestError, RequestErrorCode } from "./error";
import { v4 as uuidv4 } from "uuid";

let authToken = /* @__PURE__*/ localStorage.getItem("FlatAuthToken");

Expand Down Expand Up @@ -30,11 +31,24 @@ export async function requestFlatServer<TPayload, TResult>(
payload?: TPayload,
init?: Partial<RequestInit>,
token: string | null = authToken,
enableFlatServerV2?: boolean,
): Promise<FlatServerResponse<TResult>> {
const headers = new Headers(init?.headers);
headers.set("accept", "application/json, text/plain, */*");
headers.set("accept", "application/json, text/plain, */*, x-session-id, x-request-id");
const config: RequestInit = { method: "POST", ...init, headers };

if (!sessionStorage.getItem("sessionID")) {
sessionStorage.setItem("sessionID", uuidv4());
}

const sessionID = sessionStorage.getItem("sessionID");

if (sessionID) {
headers.set("x-session-id", sessionID);
}

headers.set("x-request-id", uuidv4());

if (payload) {
config.body = JSON.stringify(payload);
headers.set("content-type", "application/json");
Expand All @@ -44,7 +58,12 @@ export async function requestFlatServer<TPayload, TResult>(
headers.set("authorization", "Bearer " + token);
}

const response = await fetch(`${FLAT_SERVER_BASE_URL_V1}/${action}`, config);
const response = await fetch(
`${
enableFlatServerV2 === true ? FLAT_SERVER_BASE_URL_V2 : FLAT_SERVER_BASE_URL_V1
}/${action}`,
config,
);

if (!response.ok) {
// @TODO create a timeout error code
Expand Down Expand Up @@ -83,6 +102,36 @@ export async function post<TPayload, TResult>(
return res.data;
}

export async function postV2<TPayload, TResult>(
action: string,
payload?: TPayload,
init?: Partial<RequestInit>,
token?: string,
): Promise<TResult> {
const authorization = token || authToken;

if (!authorization) {
throw new ServerRequestError(RequestErrorCode.NeedLoginAgain);
}
const res = await requestFlatServer<TPayload, TResult>(
action,
payload,
init,
authorization,
true,
);

if (process.env.NODE_ENV !== "production") {
if (res.status !== Status.Success) {
throw new TypeError(
`[Flat API] ${action} returns unexpected processing status: ${res.status}`,
);
}
}

return res.data;
}

export async function postNotAuth<TPayload, TResult>(
action: string,
payload?: TPayload,
Expand Down

0 comments on commit 06cc405

Please sign in to comment.