diff --git a/fetch.js b/fetch.js index 259fcb7..94a86da 100644 --- a/fetch.js +++ b/fetch.js @@ -1,3 +1,6 @@ +import { mergeUrl } from "./url.js"; +import { copyResponse } from "./response.js"; + /** * @typedef { string | number | boolean | null } JsonPrimitive * @typedef { { [member: string]: JsonValue } } JsonObject @@ -72,3 +75,19 @@ export const fetchForJson = fetchFor("json", true); export const fetchForText = fetchFor("text", true); export const fetchForUint8Array = fetchFor("uint8Array", true); export const fetchForBody = fetchFor("body", true); + +/** + * Fetches a resource based on the provided request, and optionally modifies + * the URL using given properties, then copies the response. + * + * @typedef {import("./url.js").UrlProperties} UrlProperties + * @param {Request} request - The original request object. + * @param {UrlProperties} [urlOrProps] - Optional properties to modify the request URL. + * @returns {Promise} The copied response. + */ +export async function fetchAndCopy(request, urlOrProps) { + const url = urlOrProps ? mergeUrl(request.url)(urlOrProps) : request.url; + const newRequest = new Request(url, request); + const response = await fetch(newRequest); + return copyResponse(response); +} diff --git a/response.js b/response.js index 47468be..7e35eb6 100644 --- a/response.js +++ b/response.js @@ -1,4 +1,3 @@ -import { mergeUrl } from "./url.js"; /** * Creates a new Response object by copying the body and headers from an existing Response object. * Difference between cloning and copying of a `Response`: @@ -37,19 +36,3 @@ export function getResponseBody(input) { return JSON.stringify(input); } } - -/** - * Fetches a resource based on the provided request, and optionally modifies - * the URL using given properties, then copies the response. - * - * @typedef {import("./url.js").UrlProperties} UrlProperties - * @param {Request} request - The original request object. - * @param {UrlProperties} [urlOrProps] - Optional properties to modify the request URL. - * @returns {Promise} The copied response. - */ -export async function fetchAndCopy(request, urlOrProps) { - const url = urlOrProps ? mergeUrl(request.url)(urlOrProps) : request.url; - const newRequest = new Request(url, request); - const response = await fetch(newRequest); - return copyResponse(response); -}