Skip to content

Commit

Permalink
Update packes & add core scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasDeBruijn committed Sep 18, 2024
1 parent 4ef71f6 commit df736d7
Show file tree
Hide file tree
Showing 4 changed files with 458 additions and 388 deletions.
20 changes: 20 additions & 0 deletions src/scripts/core/error.ts
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,
}
36 changes: 36 additions & 0 deletions src/scripts/core/fetch1.ts
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()))
}
}
66 changes: 66 additions & 0 deletions src/scripts/core/result.ts
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
}
Loading

0 comments on commit df736d7

Please sign in to comment.