Skip to content

Commit

Permalink
chore(rest): move to root of lib
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Nov 1, 2021
1 parent fad56db commit b995910
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/lib/client/Rest/index.ts → src/lib/rest/Rest.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -26,7 +27,7 @@ export class Rest extends BaseClient {
/**
* Imperial's hostname
*/
readonly hostname = "imperialb.in";
readonly hostname = "staging.impb.in";

/**
* Api Vesrion
Expand All @@ -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<T extends unknown>(method: Methods, path: string, options: Options = {}): Promise<T> {
// default headers
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> {
success: boolean;
Expand All @@ -24,7 +24,7 @@ export const handleResponse = async <T extends unknown>(response: Response): Pro
}

// remove not needed data
const { success, message, data } = json ?? {};
const { success, data } = json ?? {};

// extract the status code
const { status } = response;
Expand All @@ -37,8 +37,22 @@ export const handleResponse = async <T extends unknown>(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 });
}
}
};
File renamed without changes.

0 comments on commit b995910

Please sign in to comment.