Skip to content

Commit

Permalink
add fetch result impl
Browse files Browse the repository at this point in the history
  • Loading branch information
BBB committed Oct 9, 2023
1 parent d78af85 commit daa4232
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
69 changes: 61 additions & 8 deletions packages/fetch-result/src/fetchResult.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { URL } from "node:url";
import { TaskResult } from "@ollierelph/result4t";

interface OutgoingHttpInit {
method?: string;
url?: string;
Expand All @@ -8,6 +9,13 @@ interface OutgoingHttpInit {
headers?: HeadersInit;
}

const networkErrorMsgs = new Set([
"Failed to fetch", // Chrome & Edge
"NetworkError when attempting to fetch resource.", // Firefox
"The Internet connection appears to be offline.", // Safari
"Load failed", // Safari,
]);

interface ReadableStream<R = any> {}

declare var Blob: {
Expand Down Expand Up @@ -204,21 +212,66 @@ export interface OutgoingHttpConfig {
}

export type Fetch<Res extends IncomingHttpResponse = IncomingHttpResponse> = (
input: OutgoingHttpLocation,
input: OutgoingHttpLocation | OutgoingHttpInit,
init?: OutgoingHttpConfig | undefined,
) => Promise<Res>;

export class NotImplemented {
name = "NotImplemented" as const;
class UnexpectedIssueThrown {
name = "UnexpectedIssueThrown" as const;
constructor(inner: unknown) {}
}

type FetchFailure = NotImplemented;
class OutgoingHttpRequestAborted {
name = "OutgoingHttpRequestAborted" as const;
}
class IncomingHttpResponseUnknownIssue<Body = unknown> {
name = "IncomingHttpResponseUnknownIssue" as const;
constructor(
public method: string,
public body: Body,
public inner: Error,
) {}
}
class OutgoingHttpRequestFailed<Body = unknown> {
name = "OutgoingHttpRequestFailed" as const;
constructor(
public method: string,
public body: Body,
public inner: Error,
) {}
}
type FetchFailure =
| OutgoingHttpRequestAborted
| IncomingHttpResponseUnknownIssue
| OutgoingHttpRequestFailed
| UnexpectedIssueThrown;

export const fetchResult =
(fetch: Fetch) =>
<Res extends IncomingHttpResponse = IncomingHttpResponse>(
(
input: OutgoingHttpLocation | OutgoingHttpInit,
init?: OutgoingHttpConfig | undefined,
): TaskResult<Res, FetchFailure> => {
return TaskResult.failure(new NotImplemented());
): TaskResult<IncomingHttpResponse, FetchFailure> => {
return TaskResult.fromPromise(
() => fetch(input, init),
(err) => {
if (!(err instanceof Error)) {
return new UnexpectedIssueThrown(err);
}
if (err.message.includes("The user aborted a request")) {
return new OutgoingHttpRequestAborted();
}
if (networkErrorMsgs.has(err.message)) {
return new OutgoingHttpRequestFailed(
init?.method || "GET",
input.toString(),
err,
);
}
return new IncomingHttpResponseUnknownIssue(
init?.method || "GET",
input.toString(),
err,
);
},
);
};
9 changes: 4 additions & 5 deletions packages/fetch-result/test/fetchResult.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { expect, it } from "vitest";
import { fetchResult, NotImplemented } from "~/src/fetchResult";
import { fetchResult } from "~/src/fetchResult";
import { Response } from "undici";
import { Result } from "@ollierelph/result4t";

const underTest = fetchResult(async () => Response.error());
const underTest = (response: Response) => fetchResult(async () => response);
it("responds with an error", async () => {
expect(await underTest("/woo").run()).toEqual(
Result.failure(new NotImplemented()),
);
const res = Response.error();
expect(await underTest(res)("/woo").run()).toEqual(Result.failure(res));
});

0 comments on commit daa4232

Please sign in to comment.