Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Apr 24, 2024
1 parent ffa7e2c commit fdfdcc4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
19 changes: 19 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<Response>} 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);
}
17 changes: 0 additions & 17 deletions response.js
Original file line number Diff line number Diff line change
@@ -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`:
Expand Down Expand Up @@ -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<Response>} 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);
}

0 comments on commit fdfdcc4

Please sign in to comment.