From e0093fc0cdda83b3191af3511aed8ac6b18f5398 Mon Sep 17 00:00:00 2001 From: Tomas Zijdemans Date: Thu, 12 Sep 2024 11:16:07 +0200 Subject: [PATCH] Remove dependency on @std/status --- src/deps.ts | 5 ----- src/errors.ts | 3 +-- src/fetch.ts | 27 +++++++++++++++++++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/deps.ts b/src/deps.ts index f2748a0..1fc0935 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -2,11 +2,6 @@ export { retry, RetryError, } from "https://deno.land/std@0.224.0/async/retry.ts"; -export { - isServerErrorStatus, - isSuccessfulStatus, - STATUS_CODE, -} from "https://deno.land/std@0.224.0/http/status.ts"; export { parseMediaType } from "https://deno.land/std@0.224.0/media_types/parse_media_type.ts"; export { filterKeys } from "https://deno.land/std@0.224.0/collections/mod.ts"; /** diff --git a/src/errors.ts b/src/errors.ts index d8be452..0238c6d 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -1,5 +1,4 @@ import { RetryError } from "./deps.ts"; -import { STATUS_CODE } from "./deps.ts"; import type { SDKError } from "./types.ts"; /** @@ -44,7 +43,7 @@ export const parseError = ( } // Handle Forbidden status code - if (status === STATUS_CODE.Forbidden) { + if (status === 403) { return { ok: false, error: { diff --git a/src/fetch.ts b/src/fetch.ts index 813dee8..238dc64 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,8 +1,3 @@ -import { - isServerErrorStatus, - isSuccessfulStatus, - STATUS_CODE, -} from "./deps.ts"; import { parseMediaType } from "./deps.ts"; import { retry } from "./deps.ts"; import { parseError } from "./errors.ts"; @@ -52,7 +47,7 @@ export const fetchJSON = async ( /** * Check if the response is empty. */ - if (response.status === STATUS_CODE.NoContent) { + if (response.status === 204) { return { ok: true, data: {} as TOk }; } @@ -128,3 +123,23 @@ export const getMediaType = (response: Response): string | undefined => { return mediaType[0]; }; + +/** + * Checks if the given HTTP status code indicates a server error. + * + * @param {number} status - The HTTP status code to check. + * @returns {boolean} - Returns true if the status code is between 500 and 599, inclusive; otherwise, false. + */ +export function isServerErrorStatus(status: number): boolean { + return status >= 500 && status < 600; +} + +/** + * Checks if the given HTTP status code indicates a successful response. + * + * @param {number} status - The HTTP status code to check. + * @returns {boolean} - Returns true if the status code is between 200 and 299, inclusive; otherwise, false. + */ +export function isSuccessfulStatus(status: number): boolean { + return status >= 200 && status < 300; +}