diff --git a/src/lib/client/Rest/index.ts b/src/lib/rest/Rest.ts similarity index 85% rename from src/lib/client/Rest/index.ts rename to src/lib/rest/Rest.ts index 3192ac9..3a9c6da 100644 --- a/src/lib/client/Rest/index.ts +++ b/src/lib/rest/Rest.ts @@ -1,10 +1,11 @@ import AbortController from "abort-controller"; -import fetch, { Response } from "node-fetch"; -import { FailedToFetch } from "../../errors"; -import { Aborted } from "../../errors/HTTPErrors/Aborted"; -import { BaseClient } from "../BaseClient"; -import type { Imperial } from "../Imperial"; +import type { Response } from "node-fetch"; +import { FailedToFetch } from "../errors"; +import { Aborted } from "../errors/HTTPErrors/Aborted"; +import { BaseClient } from "../client/BaseClient"; +import type { Imperial } from "../client"; import { handleResponse } from "./responseHandler"; +import fetch from "../utils/fetch"; type Methods = "POST" | "GET" | "PATCH" | "DELETE"; @@ -26,7 +27,7 @@ export class Rest extends BaseClient { /** * Imperial's hostname */ - readonly hostname = "imperialb.in"; + readonly hostname = "staging.impb.in"; /** * Api Vesrion @@ -41,7 +42,7 @@ export class Rest extends BaseClient { /** * Regular Expression that is used to match against in functions */ - readonly hostnameCheckRegExp = /^(www\.)?imp(erial)?b(\.in|in.com)$/i; + readonly hostnameRe = new RegExp(`${this.hostname}`, "i"); // /^(www\.)?imp(erial)?b(\.in|in.com)$/i; async request(method: Methods, path: string, options: Options = {}): Promise { // default headers diff --git a/src/lib/client/Rest/responseHandler.ts b/src/lib/rest/responseHandler.ts similarity index 62% rename from src/lib/client/Rest/responseHandler.ts rename to src/lib/rest/responseHandler.ts index 8631626..685992e 100644 --- a/src/lib/client/Rest/responseHandler.ts +++ b/src/lib/rest/responseHandler.ts @@ -1,7 +1,7 @@ import type { Response } from "node-fetch"; import { URL } from "url"; import { noError, statusMessage } from "./statusCode"; -import { ImperialError } from "../../errors/ImperialError"; +import { ImperialError, NotAllowed, NotFound } from "../errors"; interface InternalResponse { success: boolean; @@ -24,7 +24,7 @@ export const handleResponse = async (response: Response): Pro } // remove not needed data - const { success, message, data } = json ?? {}; + const { success, data } = json ?? {}; // extract the status code const { status } = response; @@ -37,8 +37,22 @@ export const handleResponse = async (response: Response): Pro } // find an error message - const errorMsg = message ?? statusMessage(status) ?? `Status code ${status ?? null}`; + const { pathname: path } = new URL(response.url); - // throw an error - throw new ImperialError({ message: errorMsg, status, path: new URL(response.url).pathname }); + switch (status) { + case 401: { + throw new NotAllowed({ path }); + } + + case 404: { + throw new NotFound({ path }); + } + + default: { + const message = json?.message ?? statusMessage(status) ?? `Status: ${status ?? null}`; + + // throw an error + throw new ImperialError({ message, status, path }); + } + } }; diff --git a/src/lib/client/Rest/statusCode.ts b/src/lib/rest/statusCode.ts similarity index 100% rename from src/lib/client/Rest/statusCode.ts rename to src/lib/rest/statusCode.ts