tsuite
Version:
A collection of useful utility functions, All fully typed and documented
50 lines • 1.91 kB
TypeScript
//#region src/create-request.d.ts
type FetchInput = RequestInfo | URL;
type FetchInit = RequestInit & {
event?: {
fetch: typeof fetch;
};
};
declare class Request {
input: FetchInput;
init?: FetchInit;
private response;
private sendPromise;
constructor(input: FetchInput, init?: FetchInit);
private get;
/**
* 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
*/
getJson<T>(): Promise<T>;
/**
* Sends the request and returns the response as text.
*
* @returns A promise that resolves to a string
* @throws If the fetch request fails
*/
getText(): Promise<string>;
}
/**
* 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
*/
declare function createRequest(input: FetchInput, init?: FetchInit): Request;
//#endregion
export { createRequest };