Skip to content

Commit

Permalink
Merge pull request #12 from haverstack/adele-stash
Browse files Browse the repository at this point in the history
Add new `disableRequest` option to perform `fetch` with a bare URL
  • Loading branch information
cuibonobo committed Dec 23, 2023
2 parents 9fd0852 + eb74460 commit b006311
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/buildFullPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ test("URLs can be built from a combination of base URLs and relative URLs", () =
expect(buildFullPath("http://example.com")).toBe("http://example.com");
expect(buildFullPath("", "http://example.com")).toBe("http://example.com");
expect(buildFullPath("/bar", "http://example.com/foo")).toBe("http://example.com/foo/bar");
expect(buildFullPath("/bar")).toBe("/bar");
});
18 changes: 18 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ test("Headers are passed to Axios", async () => {
expect(result.request.headers.get("Content-Type")).toBe("application/json");
});

test("Default adapter throws for relative URLs with no base", async () => {
try {
await fetchAdapter({ url: "/bar" });
throw new Error("Invalid request didn't throw");
} catch (e: unknown) {
const error = e as TypeError;
expect(error.message).toBe("Failed to parse URL from /bar");
}
});

test("Can disable Request object creation for custom adapters", async () => {
const myFetch = async (_: RequestInfo | URL) => new Response("Custom fetch!");
const customAdapter = createFetchAdapter({ fetch: myFetch, disableRequest: true });
const result = await customAdapter({ url: "/bar" });
expect(result.request).toBe("/bar");
expect(result.status).toBe(200);
});

test("Invalid request will throw an error", async () => {
// Disables fetch mock for the rest of this file
jest.restoreAllMocks();
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ type FetchFunction = (
type AdapterFunction = (config: AxiosRequestConfig) => Promise<AxiosResponse>;
interface FetchAdapterConfig {
fetch: FetchFunction;
disableRequest?: boolean;
}

export function createFetchAdapter(fetchConfig?: FetchAdapterConfig): AdapterFunction {
const adapterFetch = fetchConfig ? fetchConfig.fetch : undefined;
const disableRequest = fetchConfig && fetchConfig.disableRequest ? true : false;
async function axiosAdapter(config: AxiosRequestConfig): Promise<AxiosResponse> {
const request = createRequest(config);
const request = disableRequest && config.url ? config.url : createRequest(config);
const promiseChain = [getResponse(request, config, adapterFetch)];
let timer: NodeJS.Timeout | null = null;

Expand Down Expand Up @@ -93,7 +95,7 @@ const fetchAdapter = createFetchAdapter();
export default fetchAdapter;

async function getResponse(
request: Request,
request: Request | string | URL,
config: AxiosRequestConfig,
adapterFetch: FetchFunction = fetch
): Promise<(AxiosResponse & { ok: boolean }) | AxiosError> {
Expand Down Expand Up @@ -177,7 +179,7 @@ function createError(
message: string,
config: AxiosRequestConfig,
code: string,
request: Request,
request: Request | string | URL,
response?: AxiosResponse
): AxiosError {
const error = new Error(message) as AxiosError;
Expand Down

0 comments on commit b006311

Please sign in to comment.