Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/net/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,18 @@ export async function fetch(
}) as UndiciResponse;
}

/**
* Perform a GET request to a URL with optional request options.
* @param {string} url The URL to fetch.
* @param {Omit<FetchOptions, 'method'>} options Optional request options. The `cache` property is required.
* @returns {Promise<UndiciResponse>} The response from the fetch.
*/
export async function get(
url: string,
options: Omit<FetchOptions, "method">,
): Promise<UndiciResponse> {
return fetch(url, { ...options, method: "GET" });
}

export type Response = UndiciResponse;
export type { RequestInit as FetchRequestInit } from "undici";
14 changes: 14 additions & 0 deletions packages/net/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,26 @@ export class CacheableNet extends Hookified {

return fetch(url, fetchOptions);
}

/**
* Perform a GET request to a URL with optional request options. Will use the cache that is already set in the instance.
* @param {string} url The URL to fetch.
* @param {Omit<FetchRequestInit, 'method'>} options Optional request options (method will be set to GET).
* @returns {Promise<FetchResponse>} The response from the fetch.
*/
public async get(
url: string,
options?: Omit<FetchRequestInit, "method">,
): Promise<FetchResponse> {
return this.fetch(url, { ...options, method: "GET" });
}
}

export const Net = CacheableNet;
export {
type FetchOptions,
type FetchRequestInit,
fetch,
get,
type Response as FetchResponse,
} from "./fetch.js";
38 changes: 37 additions & 1 deletion packages/net/test/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from "node:process";
import { Cacheable } from "cacheable";
import { describe, expect, test } from "vitest";
import { type FetchOptions, fetch } from "../src/fetch.js";
import { type FetchOptions, fetch, get } from "../src/fetch.js";

const testUrl = process.env.TEST_URL ?? "https://mockhttp.org";
const testTimeout = 10_000; // 10 seconds
Expand Down Expand Up @@ -58,4 +58,40 @@ describe("Fetch", () => {
},
testTimeout,
);

test(
"should fetch data using get helper",
async () => {
const url = `${testUrl}/get`;
const options = {
cache: new Cacheable(),
};
const response = await get(url, options);
expect(response).toBeDefined();
expect(response.status).toBe(200);
},
testTimeout,
);

test(
"should cache data using get helper",
async () => {
const cache = new Cacheable({ stats: true });
const url = `${testUrl}/get`;
const options = {
cache,
};
const response = await get(url, options);
const response2 = await get(url, options);
expect(response).toBeDefined();
expect(response2).toBeDefined();
expect(cache.stats).toBeDefined();
expect(cache.stats.hits).toBe(1);
// Verify that both responses have the same text content
const text1 = await response.text();
const text2 = await response2.text();
expect(text1).toEqual(text2);
},
testTimeout,
);
});
27 changes: 27 additions & 0 deletions packages/net/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type CacheableNetOptions,
type FetchOptions,
fetch,
get,
Net,
} from "../src/index.js";

Expand Down Expand Up @@ -79,4 +80,30 @@ describe("Cacheable Net", () => {
},
testTimeout,
);

test(
"should fetch data using CacheableNet get method",
async () => {
const net = new Net();
const url = `${testUrl}/get`;
const response = await net.get(url);
expect(response).toBeDefined();
expect(response.status).toBe(200);
},
testTimeout,
);

test(
"should fetch data using standalone get function",
async () => {
const url = `${testUrl}/get`;
const options = {
cache: new Cacheable(),
};
const response = await get(url, options);
expect(response).toBeDefined();
expect(response.status).toBe(200);
},
testTimeout,
);
});