UNPKG

tsuite

Version:

A collection of useful utility functions, All fully typed and documented

82 lines (80 loc) 2.77 kB
/** * tsuite v0.7.2 * tijn.dev * @license MIT **/ import { tryCatch } from "typecatch"; //#region src/create-request.ts var Request = class { input; init; response; sendPromise; constructor(input, init) { this.input = input; this.init = init; } async get() { if (this.sendPromise) { await this.sendPromise; return; } this.sendPromise = (async () => { const fetchFunc = this.init?.event?.fetch || fetch; const res = await tryCatch(fetchFunc(this.input, this.init)); if (res.error) throw new Error(`Failed to fetch: ${res.error}`); if (!res.data?.ok) throw new Error(`HTTP error! status: ${res.data?.status} ${res.data?.statusText}`); this.response = res.data; })(); await this.sendPromise; } /** * Sends the request and returns the response as parsed JSON. * * @template T The expected type of the JSON response * @returns A promise that resolves to the parsed JSON data * @throws If the fetch request fails or if the JSON parsing fails */ async getJson() { await this.get(); if (!this.response) throw new Error("Request sending failed unexpectedly."); const parse = await tryCatch(this.response.json()); if (parse.error) throw new Error(`Failed to parse JSON: ${parse.error}`); return parse.data; } /** * Sends the request and returns the response as text. * * @returns A promise that resolves to a string * @throws If the fetch request fails */ async getText() { await this.get(); if (!this.response) throw new Error("Request sending failed unexpectedly."); const parse = await tryCatch(this.response.text()); if (parse.error) throw new Error(`Failed to parse text: ${parse.error}`); return parse.data; } }; /** * Creates a new Request instance for making fetch requests. * * @deprecated use effetch instead * * @param input The input for the fetch request. This can be a URL string or a RequestInfo object. * @param init An optional object containing custom settings for the fetch request. This can include properties like `method`, `headers`, `body`, etc. It can also include an `event` property with a `fetch` function for custom fetch implementations (e.g., for testing). * @returns A Request instance. **Note: You should not call this function without chaining either the `.getJson()` or `.getText()` method to retrieve the response data.** * @example * // Correct usage: * createRequest('https://api.example.com/data').getJson() * .then((data) => console.log(data)) * .catch((error) => console.error(error)); * * // Incorrect usage (will not make the request): * const myRequest = createRequest('https://api.example.com/data'); // This won't trigger the fetch */ function createRequest(input, init) { return new Request(input, init); } //#endregion export { createRequest };