Skip to content

Commit bd1ae8b

Browse files
authored
net - feat: adding in get helper for fetch (#1293)
1 parent 35d49a3 commit bd1ae8b

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

packages/net/src/fetch.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,18 @@ export async function fetch(
6363
}) as UndiciResponse;
6464
}
6565

66+
/**
67+
* Perform a GET request to a URL with optional request options.
68+
* @param {string} url The URL to fetch.
69+
* @param {Omit<FetchOptions, 'method'>} options Optional request options. The `cache` property is required.
70+
* @returns {Promise<UndiciResponse>} The response from the fetch.
71+
*/
72+
export async function get(
73+
url: string,
74+
options: Omit<FetchOptions, "method">,
75+
): Promise<UndiciResponse> {
76+
return fetch(url, { ...options, method: "GET" });
77+
}
78+
6679
export type Response = UndiciResponse;
6780
export type { RequestInit as FetchRequestInit } from "undici";

packages/net/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,26 @@ export class CacheableNet extends Hookified {
5050

5151
return fetch(url, fetchOptions);
5252
}
53+
54+
/**
55+
* Perform a GET request to a URL with optional request options. Will use the cache that is already set in the instance.
56+
* @param {string} url The URL to fetch.
57+
* @param {Omit<FetchRequestInit, 'method'>} options Optional request options (method will be set to GET).
58+
* @returns {Promise<FetchResponse>} The response from the fetch.
59+
*/
60+
public async get(
61+
url: string,
62+
options?: Omit<FetchRequestInit, "method">,
63+
): Promise<FetchResponse> {
64+
return this.fetch(url, { ...options, method: "GET" });
65+
}
5366
}
5467

5568
export const Net = CacheableNet;
5669
export {
5770
type FetchOptions,
5871
type FetchRequestInit,
5972
fetch,
73+
get,
6074
type Response as FetchResponse,
6175
} from "./fetch.js";

packages/net/test/fetch.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from "node:process";
22
import { Cacheable } from "cacheable";
33
import { describe, expect, test } from "vitest";
4-
import { type FetchOptions, fetch } from "../src/fetch.js";
4+
import { type FetchOptions, fetch, get } from "../src/fetch.js";
55

66
const testUrl = process.env.TEST_URL ?? "https://mockhttp.org";
77
const testTimeout = 10_000; // 10 seconds
@@ -58,4 +58,40 @@ describe("Fetch", () => {
5858
},
5959
testTimeout,
6060
);
61+
62+
test(
63+
"should fetch data using get helper",
64+
async () => {
65+
const url = `${testUrl}/get`;
66+
const options = {
67+
cache: new Cacheable(),
68+
};
69+
const response = await get(url, options);
70+
expect(response).toBeDefined();
71+
expect(response.status).toBe(200);
72+
},
73+
testTimeout,
74+
);
75+
76+
test(
77+
"should cache data using get helper",
78+
async () => {
79+
const cache = new Cacheable({ stats: true });
80+
const url = `${testUrl}/get`;
81+
const options = {
82+
cache,
83+
};
84+
const response = await get(url, options);
85+
const response2 = await get(url, options);
86+
expect(response).toBeDefined();
87+
expect(response2).toBeDefined();
88+
expect(cache.stats).toBeDefined();
89+
expect(cache.stats.hits).toBe(1);
90+
// Verify that both responses have the same text content
91+
const text1 = await response.text();
92+
const text2 = await response2.text();
93+
expect(text1).toEqual(text2);
94+
},
95+
testTimeout,
96+
);
6197
});

packages/net/test/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type CacheableNetOptions,
88
type FetchOptions,
99
fetch,
10+
get,
1011
Net,
1112
} from "../src/index.js";
1213

@@ -79,4 +80,30 @@ describe("Cacheable Net", () => {
7980
},
8081
testTimeout,
8182
);
83+
84+
test(
85+
"should fetch data using CacheableNet get method",
86+
async () => {
87+
const net = new Net();
88+
const url = `${testUrl}/get`;
89+
const response = await net.get(url);
90+
expect(response).toBeDefined();
91+
expect(response.status).toBe(200);
92+
},
93+
testTimeout,
94+
);
95+
96+
test(
97+
"should fetch data using standalone get function",
98+
async () => {
99+
const url = `${testUrl}/get`;
100+
const options = {
101+
cache: new Cacheable(),
102+
};
103+
const response = await get(url, options);
104+
expect(response).toBeDefined();
105+
expect(response.status).toBe(200);
106+
},
107+
testTimeout,
108+
);
82109
});

0 commit comments

Comments
 (0)