-
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
1 parent
4ef71f6
commit df736d7
Showing
4 changed files
with
458 additions
and
388 deletions.
There are no files selected for viewing
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,20 @@ | ||
export class ApiError { | ||
public readonly kind: ApiErrorKind; | ||
public readonly status?: number; | ||
public readonly message?: string; | ||
|
||
private constructor(kind: ApiErrorKind, status?: number, message?: string) { | ||
this.kind = kind; | ||
this.status = status; | ||
this.message = message; | ||
} | ||
|
||
static request(status: number, message: string): ApiError { | ||
return new ApiError(ApiErrorKind.Request, status, message); | ||
} | ||
|
||
} | ||
|
||
export enum ApiErrorKind { | ||
Request, | ||
} |
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,36 @@ | ||
import {Result} from "@/scripts/core/result"; | ||
import {ApiError} from "@/scripts/core/error"; | ||
|
||
export async function fetch1(input: RequestInfo | URL, init?: RequestInit): Promise<Result<Response, ApiError>> { | ||
if (init) { | ||
init.credentials = "include"; | ||
if (init.headers) { | ||
init.headers = { | ||
...init.headers, | ||
'Access-Control-Allow-Credentials': "true", | ||
} | ||
} else { | ||
init.headers = { | ||
'Access-Control-Allow-Credentials': "true", | ||
}; | ||
} | ||
} else { | ||
init = { | ||
credentials: "include", | ||
headers: { | ||
'Access-Control-Allow-Credentials': "true", | ||
}, | ||
}; | ||
} | ||
|
||
try { | ||
const r = await fetch(input, init); | ||
if (!r.ok) { | ||
return Result.err(ApiError.request(r.status, await r.text())); | ||
} | ||
|
||
return Result.ok(r); | ||
} catch (error: unknown) { | ||
return Result.err(ApiError.request(-1, error.toString())) | ||
} | ||
} |
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,66 @@ | ||
export class Result<T, E> { | ||
private readonly state: ResultState; | ||
private readonly ok?: T; | ||
private readonly err?: E; | ||
|
||
private constructor(state: ResultState, ok?: T, err?: E) { | ||
this.state = state; | ||
this.ok = ok; | ||
this.err = err; | ||
} | ||
|
||
public static ok<T, E>(ok: T): Result<T, E> { | ||
return new Result<T, E>(ResultState.OK, ok, undefined); | ||
} | ||
|
||
public static err<T, E>(err: E): Result<T, E> { | ||
return new Result<T, E>(ResultState.ERROR, undefined, err); | ||
} | ||
|
||
public isOk(): boolean { | ||
return this.state == ResultState.OK; | ||
} | ||
|
||
public isErr(): boolean { | ||
return this.state == ResultState.ERROR; | ||
} | ||
|
||
public unwrap(): T { | ||
if (this.isOk()) { | ||
return this.ok!; | ||
} | ||
|
||
console.error("Tried to unwrap Result while it was an error"); | ||
return undefined!; | ||
} | ||
|
||
public unwrapErr(): E { | ||
if (this.isErr()) { | ||
return this.err!; | ||
} | ||
|
||
console.error("Tried to unwrap error Result while it was ok"); | ||
return undefined!; | ||
} | ||
|
||
public map<T1>(f: (t: T) => T1): Result<T1, E> { | ||
if (this.isOk()) { | ||
return Result.ok(f(this.unwrap())); | ||
} else { | ||
return Result.err(this.unwrapErr()); | ||
} | ||
} | ||
|
||
public async map1<T1>(f: (t: T) => Promise<T1>): Promise<Result<T1, E>> { | ||
if (this.isOk()) { | ||
return Result.ok(await f(this.unwrap())); | ||
} else { | ||
return Result.err(this.unwrapErr()); | ||
} | ||
} | ||
} | ||
|
||
export enum ResultState { | ||
OK, | ||
ERROR | ||
} |
Oops, something went wrong.