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 be37376 commit ffa7e2c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
20 changes: 0 additions & 20 deletions path.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
/**
* Takes a url and `URL` properties and returns a new, merged `URL` object.
* ```js
* mergeUrl(new URL("https://example.com"))({ port: "8888", username: "joe" })
* ```
* @typedef {Partial<Omit<URL, "origin" | "searchParams">>} UrlProperties
* @param {string|URL} baseUrl
* @returns {(partialUrl:UrlProperties) => URL}
*/
export function mergeUrl(baseUrl) {
// NOT: Don't mutate the passed url object.
const url = new URL(baseUrl instanceof URL ? baseUrl.href : baseUrl);
return (urlOrProps) => {
Object.entries(urlOrProps).forEach(([key, value]) => {
/**@type {any}*/ (url)[key] = value;
});
return url;
};
}

/**
* importMetaResolve.
* @param {string} moduleUrl
Expand Down
17 changes: 17 additions & 0 deletions response.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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 @@ -36,3 +37,19 @@ 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);
}
20 changes: 20 additions & 0 deletions url.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ export function appendSearchParams(url) {
return urlObject;
};
}

/**
* Takes a url and `URL` properties and returns a new, merged `URL` object.
* ```js
* mergeUrl(new URL("https://example.com"))({ port: "8888", username: "joe" })
* ```
* @typedef {Partial<Omit<URL, "origin" | "searchParams">>} UrlProperties
* @param {string|URL} baseUrl
* @returns {(partialUrl:UrlProperties) => URL}
*/
export function mergeUrl(baseUrl) {
// NOT: Don't mutate the passed url object.
const url = new URL(baseUrl instanceof URL ? baseUrl.href : baseUrl);
return (urlOrProps) => {
Object.entries(urlOrProps).forEach(([key, value]) => {
/**@type {any}*/ (url)[key] = value;
});
return url;
};
}

0 comments on commit ffa7e2c

Please sign in to comment.