Skip to content

Commit

Permalink
Support passing the url as a URL instance as well as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
danbahrami committed Aug 31, 2024
1 parent ea57992 commit 01013d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const createClient = (options: ClientOptions = {}): Client => {
* use to build the public methods: `.get()`, `.post()`, `.request()` etc.
*/
const _createMethod = (getDefaultInit: () => RequestInit) => {
return (info: RequestInfo, init?: RequestInit & { json?: unknown }) => {
return (info: RequestInfo | URL, init?: RequestInit & { json?: unknown }) => {
const result = (async (): Promise<DecoratedResponse> => {
/**
* Combine the incoming RequestInit with the default RequestInit
Expand Down
17 changes: 9 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,70 +86,71 @@ export type ClientOptions = {
beforeSuccessResponse?: Modifiers["beforeSuccessResponse"];
beforeErrorResponse?: Modifiers["beforeErrorResponse"];
};
baseUrl?: string | URL;
};

export type Client = {
/**
* Perform an HTTP request using any HTTP method (defaults to GET)
*/
request: (
input: RequestInfo,
input: RequestInfo | URL,
init?: RequestInit & { json?: unknown }
) => DecoratedResponsePromise;

/**
* Perform a HTTP GET request
*/
get: (
input: RequestInfo,
input: RequestInfo | URL,
init?: Omit<RequestInit, "method"> & { json?: unknown }
) => DecoratedResponsePromise;

/**
* Perform a HTTP PUT request
*/
put: (
input: RequestInfo,
input: RequestInfo | URL,
init?: Omit<RequestInit, "method"> & { json?: unknown }
) => DecoratedResponsePromise;

/**
* Perform a HTTP POST request
*/
post: (
input: RequestInfo,
input: RequestInfo | URL,
init?: Omit<RequestInit, "method"> & { json?: unknown }
) => DecoratedResponsePromise;

/**
* Perform a HTTP PATCH request
*/
patch: (
input: RequestInfo,
input: RequestInfo | URL,
init?: Omit<RequestInit, "method"> & { json?: unknown }
) => DecoratedResponsePromise;

/**
* Perform a HTTP DELETE request
*/
delete: (
input: RequestInfo,
input: RequestInfo | URL,
init?: Omit<RequestInit, "method"> & { json?: unknown }
) => DecoratedResponsePromise;

/**
* Perform a HTTP OPTIONS request
*/
options: (
input: RequestInfo,
input: RequestInfo | URL,
init?: Omit<RequestInit, "method"> & { json?: unknown }
) => DecoratedResponsePromise;

/**
* Perform a HTTP HEAD request
*/
head: (
input: RequestInfo,
input: RequestInfo | URL,
init?: Omit<RequestInit, "method"> & { json?: unknown }
) => DecoratedResponsePromise;

Expand Down
23 changes: 23 additions & 0 deletions test/f.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ describe("f.get()", () => {
}),
});
});

test("You can pass a full Request instance", async () => {
nock(HOST)
.get("/api/user")
.reply(200, { firstName: "Shane", lastName: "MacGowan", age: 65 });

const request = new Request(HOST + "/api/user");
const response = await f.get(request);
expect(response).toBeInstanceOf(Response);
expect(response.status).toBe(200);
expect(response.statusText).toBe("OK");
});

test("You can pass a URL instance", async () => {
nock(HOST)
.get("/api/user")
.reply(200, { firstName: "Shane", lastName: "MacGowan", age: 65 });

const response = await f.get(new URL("/api/user", HOST));
expect(response).toBeInstanceOf(Response);
expect(response.status).toBe(200);
expect(response.statusText).toBe("OK");
});
});

describe("f.post()", () => {
Expand Down

0 comments on commit 01013d6

Please sign in to comment.