Skip to content

Commit

Permalink
Merge pull request #7 from takker99:option-t
Browse files Browse the repository at this point in the history
feat: Use `option-t` for `Result` type
  • Loading branch information
takker99 authored Jul 30, 2024
2 parents 423f485 + f29b5cf commit 33d0759
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 57 deletions.
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"update:commit": "deno task -q update --commit --pre-commit=fmt,lint",
"publish": "deno run --allow-env --allow-run=deno --allow-read --allow-write=deno.jsonc jsr:@david/publish-on-tag@0.1.x"
},
"imports": {},
"imports": { "result": "npm:option-t@^49.1.0/plain_result" },
"exports": "./mod.ts"
}
20 changes: 20 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 12 additions & 26 deletions error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isObject, type Result } from "./util.ts";
import { isObject } from "./util.ts";
import { createErr, createOk, type Result } from "result";

export class UnexpectedResponseError extends Error {
name = "UnexpectedResponseError";
Expand Down Expand Up @@ -85,13 +86,10 @@ export const checkResponse = async (
>
> => {
const text = await response.text();
if (response.ok) return { ok: true, value: text };
if (response.ok) return createOk(text);

if (response.status === 400) {
return {
ok: false,
value: { name: "BadRequestError", message: text },
};
return createErr({ name: "BadRequestError", message: text });
}

try {
Expand All @@ -107,30 +105,18 @@ export const checkResponse = async (

switch (response.status) {
case 401:
return {
ok: false,
value: { name: "UnauthorizedError", message: json.message },
};
return createErr({ name: "UnauthorizedError", message: json.message });
case 403:
return {
ok: false,
value: { name: "NotPrivilegeError", message: json.message },
};
return createErr({ name: "NotPrivilegeError", message: json.message });
case 404:
return {
ok: false,
value: { name: "NotFoundError", message: json.message },
};
return createErr({ name: "NotFoundError", message: json.message });
case 422:
return {
ok: false,
value: { name: "InvalidParameterError", message: json.message },
};
return createErr({
name: "InvalidParameterError",
message: json.message,
});
case 429:
return {
ok: false,
value: { name: "RateLimitError", message: json.message },
};
return createErr({ name: "RateLimitError", message: json.message });
default:
throw new UnexpectedResponseError({
status: response.status,
Expand Down
7 changes: 4 additions & 3 deletions getProfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type OAuthOptions, type Result, setDefaults } from "./util.ts";
import { type OAuthOptions, setDefaults } from "./util.ts";
import { checkResponse, type GyazoAPIError } from "./error.ts";
import { createOk, isErr, type Result, unwrapOk } from "result";

/** Gyazo account profile */
export interface Profile {
Expand All @@ -26,6 +27,6 @@ export const getProfile = async (
const res = await fetch(path);

const checked = await checkResponse(res);
if (!checked.ok) return checked;
return { ok: true, value: JSON.parse(checked.value) as Profile };
if (isErr(checked)) return checked;
return createOk(JSON.parse(unwrapOk(checked)) as Profile);
};
27 changes: 11 additions & 16 deletions image.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
type OAuthOptions,
type Result,
setDefaults,
type Timestamp,
} from "./util.ts";
import { type OAuthOptions, setDefaults, type Timestamp } from "./util.ts";
import { checkResponse, type GyazoAPIError } from "./error.ts";
import { createOk, isErr, type Result, unwrapOk } from "result";

/** Image data */
export interface Image {
Expand Down Expand Up @@ -71,9 +67,9 @@ export const getImage = async (
const res = await fetch(path);

const checked = await checkResponse(res);
if (!checked.ok) return checked;
if (isErr(checked)) return checked;

return { ok: true, value: JSON.parse(checked.value) };
return createOk(JSON.parse(unwrapOk(checked)) as Image);
};

/** the options for `getImages()` */
Expand Down Expand Up @@ -123,18 +119,15 @@ export const getImages = async (
const res = await fetch(path);

const checked = await checkResponse(res);
if (!checked.ok) return checked;
if (isErr(checked)) return checked;

const images: Image[] = JSON.parse(checked.value);
const images: Image[] = JSON.parse(unwrapOk(checked));
const count = parseInt(res.headers.get("X-Total-Count") ?? "0");
const currentPage = parseInt(res.headers.get("X-Current-Page") ?? "0");
const perPage = parseInt(res.headers.get("X-Per-Page") ?? "0");
const userType = res.headers.get("X-User-Type") ?? "";

return {
ok: true,
value: { images, count, page: currentPage, per: perPage, userType },
};
return createOk({ images, count, page: currentPage, per: perPage, userType });
};

/** delete an image
Expand All @@ -158,7 +151,9 @@ export const deleteImage = async (
const res = await fetch(path, { method: "DELETE" });

const checked = await checkResponse(res);
if (!checked.ok) return checked;
if (isErr(checked)) return checked;

return { ok: true, value: JSON.parse(checked.value) };
return createOk(
JSON.parse(unwrapOk(checked)) as Pick<Image, "image_id" | "type">,
);
};
12 changes: 4 additions & 8 deletions upload.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
type OAuthOptions,
type Result,
setDefaults,
type Timestamp,
} from "./util.ts";
import { type OAuthOptions, setDefaults, type Timestamp } from "./util.ts";
import { checkResponse, type GyazoAPIError } from "./error.ts";
import { createOk, isErr, type Result, unwrapOk } from "result";

/** metadata and access tokens */
export interface UploadInit extends OAuthOptions {
Expand Down Expand Up @@ -108,6 +104,6 @@ export const upload = async (
);

const checked = await checkResponse(res);
if (!checked.ok) return checked;
return { ok: true, value: JSON.parse(checked.value) };
if (isErr(checked)) return checked;
return createOk(JSON.parse(unwrapOk(checked)) as UploadResult);
};
3 changes: 0 additions & 3 deletions util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export type Timestamp = string;

/** 正常値と異常値を格納する型 */
export type Result<T, E> = { ok: true; value: T } | { ok: false; value: E };

/** networkからdataをとってくる処理
*
* interfaceは`fetch()`と同じ
Expand Down

0 comments on commit 33d0759

Please sign in to comment.