-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
134 additions
and
32 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
packages/fetch-result/src/IncomingHttpResponseUnknownIssue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export class IncomingHttpResponseUnknownIssue<Body = unknown> { | ||
name = "IncomingHttpResponseUnknownIssue" as const; | ||
|
||
constructor( | ||
public method: string, | ||
public body: Body, | ||
public inner: Error, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export class Invalid<T> { | ||
protected constructor(public value: T) {} | ||
|
||
static of<T>(value: T) { | ||
return new Invalid(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { SensibleSet } from "~/src/SensibleSet"; | ||
import { Pair } from "~/src/Pair"; | ||
import { Result } from "@ollierelph/result4t"; | ||
import { Invalid } from "~/src/Invalid"; | ||
|
||
const valid = SensibleSet.of( | ||
Pair.of("Failed to fetch", "Chrome / Edge"), | ||
Pair.of("NetworkError when attempting to fetch resource.", "Firefox"), | ||
Pair.of("The Internet connection appears to be offline.", "Safari"), | ||
Pair.of("Load failed", "Safari"), | ||
); | ||
|
||
export class NetworkErrorMessage { | ||
protected constructor(public value: string) {} | ||
|
||
static of(value: string): Result<NetworkErrorMessage, Invalid<string>> { | ||
if (valid.contains((it) => it.first === value)) { | ||
return Result.success(new NetworkErrorMessage(value)); | ||
} | ||
return Result.failure(Invalid.of(value)); | ||
} | ||
|
||
toString() { | ||
return this.value; | ||
} | ||
|
||
equals(other: NetworkErrorMessage) { | ||
return this.value === other.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class OutgoingHttpRequestAborted { | ||
name = "OutgoingHttpRequestAborted" as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export class OutgoingHttpRequestFailed<Body = unknown> { | ||
name = "OutgoingHttpRequestFailed" as const; | ||
|
||
constructor( | ||
public method: string, | ||
public body: Body, | ||
public inner: Error, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export class Pair<First, Second> { | ||
protected constructor( | ||
public first: First, | ||
public second: Second, | ||
) {} | ||
|
||
static of<First, Second>(first: First, second: Second) { | ||
return new Pair(first, second); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { SensibleSet } from "~/src/SensibleSet"; | ||
import { Pair } from "~/src/Pair"; | ||
import { Result } from "@ollierelph/result4t"; | ||
import { Invalid } from "~/src/Invalid"; | ||
|
||
const valid = SensibleSet.of("The user aborted a request"); | ||
|
||
export class RequestAbortedErrorMessage { | ||
protected constructor(public value: string) {} | ||
|
||
static of( | ||
value: string, | ||
): Result<RequestAbortedErrorMessage, Invalid<string>> { | ||
if (valid.has(value)) { | ||
return Result.success(new RequestAbortedErrorMessage(value)); | ||
} | ||
return Result.failure(Invalid.of(value)); | ||
} | ||
|
||
toString() { | ||
return this.value; | ||
} | ||
|
||
equals(other: RequestAbortedErrorMessage) { | ||
return this.value === other.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export class SensibleSet<T> { | ||
protected constructor(private set: Set<T>) {} | ||
|
||
static of<T>(...args: T[]) { | ||
return new SensibleSet(new Set(args)); | ||
} | ||
|
||
map<Out>(predicate: (it: T) => Out) { | ||
return SensibleSet.of(...[...this.set].map(predicate)); | ||
} | ||
|
||
contains(predicate: (it: T) => boolean) { | ||
return [...this.set].reduce((agg, it) => { | ||
if (agg) { | ||
return agg; | ||
} | ||
return predicate(it); | ||
}, false); | ||
} | ||
|
||
has(it: T) { | ||
return this.set.has(it); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class UnexpectedIssueThrown { | ||
name = "UnexpectedIssueThrown" as const; | ||
|
||
constructor(inner: unknown) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters