From 218af68dcc1a210ea59455e64a764a8c861954e9 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 19 Jun 2024 12:38:51 -1000 Subject: [PATCH] feat(client): :label: Export all types, refactor ResponseError better --- client/jsr.jsonc | 3 +- client/lysand/base.ts | 19 ++++--- client/lysand/lysand.ts | 57 +++++++++++---------- client/package.json | 4 ++ client/types.ts | 106 ++++++++++++++++++++++++++++++++++++++++ client/types/lysand.ts | 4 +- 6 files changed, 156 insertions(+), 37 deletions(-) create mode 100644 client/types.ts diff --git a/client/jsr.jsonc b/client/jsr.jsonc index c0f866f..9aa740d 100644 --- a/client/jsr.jsonc +++ b/client/jsr.jsonc @@ -3,6 +3,7 @@ "name": "@lysand-org/client", "version": "0.0.0", "exports": { - ".": "./index.ts" + ".": "./index.ts", + "./types": "./types.ts" } } diff --git a/client/lysand/base.ts b/client/lysand/base.ts index 3750af2..dcbd297 100644 --- a/client/lysand/base.ts +++ b/client/lysand/base.ts @@ -19,7 +19,8 @@ type ConvertibleObject = { */ export interface Output { data: ReturnType; - headers: Headers; + ok: boolean; + raw: Response; } const objectToFormData = (obj: ConvertibleObject): FormData => { @@ -62,7 +63,11 @@ const objectToFormData = (obj: ConvertibleObject): FormData => { * * Throws if the request returns invalid or unexpected data. */ -export class ResponseError extends Error { +export class ResponseError< + ReturnType = { + error?: string; + }, +> extends Error { constructor( public response: Output, message: string, @@ -96,12 +101,11 @@ export class BaseClient { if (!result.ok) { const error = isJson ? await result.json() : await result.text(); - throw new ResponseError<{ - error?: string; - }>( + throw new ResponseError( { data: error, - headers: result.headers, + ok: false, + raw: result, }, `Request failed (${result.status}): ${ error.error || error.message || result.statusText @@ -111,7 +115,8 @@ export class BaseClient { return { data: isJson ? await result.json() : (await result.text()) || null, - headers: result.headers, + ok: true, + raw: result, }; } diff --git a/client/lysand/lysand.ts b/client/lysand/lysand.ts index 944cce1..f1a2678 100644 --- a/client/lysand/lysand.ts +++ b/client/lysand/lysand.ts @@ -1,30 +1,33 @@ import { OAuth2Client } from "@badgateway/oauth2-client"; -import type { Account } from "../types/account"; -import type { Activity } from "../types/activity"; -import type { Announcement } from "../types/announcement"; -import type { Application, ApplicationData } from "../types/application"; -import type { AsyncAttachment } from "../types/async_attachment"; -import type { Attachment } from "../types/attachment"; -import type { Context } from "../types/context"; -import type { Conversation } from "../types/conversation"; -import type { Emoji } from "../types/emoji"; -import type { FeaturedTag } from "../types/featured_tag"; -import type { ExtendedDescription, Instance } from "../types/instance"; -import type { List } from "../types/list"; -import type { LysandRole } from "../types/lysand"; -import type { Marker } from "../types/marker"; -import type { Notification } from "../types/notification"; -import type { Poll } from "../types/poll"; -import type { Preferences } from "../types/preferences"; -import type { PushSubscription } from "../types/push_subscription"; -import type { Relationship } from "../types/relationship"; -import type { Category, Report } from "../types/report"; -import type { Results } from "../types/results"; -import type { ScheduledStatus } from "../types/scheduled_status"; -import type { Status, StatusVisibility } from "../types/status"; -import type { StatusSource } from "../types/status_source"; -import type { Tag } from "../types/tag"; -import type { Token } from "../types/token"; +import type { + Account, + Activity, + Announcement, + Application, + ApplicationData, + AsyncAttachment, + Attachment, + Category, + Context, + Conversation, + Emoji, + ExtendedDescription, + FeaturedTag, + Instance, + List, + LysandRole, + Marker, + Poll, + Preferences, + Relationship, + Results, + ScheduledStatus, + Status, + StatusSource, + StatusVisibility, + Tag, + Token, +} from "../types"; import { BaseClient, type Output } from "./base"; import { DEFAULT_SCOPE, NO_REDIRECT } from "./constants"; @@ -1503,8 +1506,8 @@ export class LysandClient extends BaseClient { extra?: RequestInit, ): Promise> { return this.getRelationships([id], options, extra).then((r) => ({ + ...r, data: r.data[0], - headers: r.headers, })); } diff --git a/client/package.json b/client/package.json index dbb608b..6112b31 100644 --- a/client/package.json +++ b/client/package.json @@ -41,6 +41,10 @@ ".": { "import": "./index.ts", "default": "./index.ts" + }, + "./types": { + "import": "./types.ts", + "default": "./types.ts" } }, "funding": { diff --git a/client/types.ts b/client/types.ts new file mode 100644 index 0000000..212e432 --- /dev/null +++ b/client/types.ts @@ -0,0 +1,106 @@ +import type { Account } from "./types/account"; +import type { Activity } from "./types/activity"; +import type { + Announcement, + AnnouncementAccount, + AnnouncementReaction, + AnnouncementStatus, +} from "./types/announcement"; +import type { Application, ApplicationData } from "./types/application"; +import type { AsyncAttachment } from "./types/async_attachment"; +import type { Attachment, Focus, Meta, Sub } from "./types/attachment"; +import type { Card } from "./types/card"; +import type { Context } from "./types/context"; +import type { Conversation } from "./types/conversation"; +import type { Emoji } from "./types/emoji"; +import type { FeaturedTag } from "./types/featured_tag"; +import type { Field } from "./types/field"; +import type { Filter, FilterContext } from "./types/filter"; +import type { FollowRequest } from "./types/follow_request"; +import type { History } from "./types/history"; +import type { IdentityProof } from "./types/identity_proof"; +import type { + ExtendedDescription, + Instance, + InstanceRule, +} from "./types/instance"; +import type { List, RepliesPolicy } from "./types/list"; +import type { LysandRole, RolePermission } from "./types/lysand"; +import type { Marker } from "./types/marker"; +import type { Mention } from "./types/mention"; +import type { Notification, NotificationType } from "./types/notification"; +import type { Poll, PollOption } from "./types/poll"; +import type { Preferences } from "./types/preferences"; +import type { Alerts, PushSubscription } from "./types/push_subscription"; +import type { Reaction } from "./types/reaction"; +import type { Relationship } from "./types/relationship"; +import type { Category, Report } from "./types/report"; +import type { Results } from "./types/results"; +import type { ScheduledStatus } from "./types/scheduled_status"; +import type { Source } from "./types/source"; +import type { Stats } from "./types/stats"; +import type { Status, StatusTag, StatusVisibility } from "./types/status"; +import type { StatusParams } from "./types/status_params"; +import type { StatusSource } from "./types/status_source"; +import type { Tag } from "./types/tag"; +import type { Token } from "./types/token"; +import type { URLs } from "./types/urls"; + +export type { + Account, + Activity, + Alerts, + Announcement, + AnnouncementAccount, + AnnouncementReaction, + AnnouncementStatus, + Application, + ApplicationData, + AsyncAttachment, + Attachment, + Card, + Category, + Context, + Conversation, + Emoji, + ExtendedDescription, + FeaturedTag, + Field, + Filter, + FilterContext, + Focus, + FollowRequest, + History, + IdentityProof, + Instance, + InstanceRule, + List, + LysandRole, + Marker, + Mention, + Meta, + Notification, + NotificationType, + Poll, + PollOption, + Preferences, + PushSubscription, + Reaction, + Relationship, + RepliesPolicy, + Report, + Results, + RolePermission, + ScheduledStatus, + Source, + Stats, + Status, + StatusParams, + StatusSource, + StatusTag, + StatusVisibility, + Sub, + Tag, + Token, + URLs, +}; diff --git a/client/types/lysand.ts b/client/types/lysand.ts index 1a84629..1439137 100644 --- a/client/types/lysand.ts +++ b/client/types/lysand.ts @@ -1,7 +1,7 @@ export type LysandRole = { id: string; name: string; - permissions: LysandRolePermissions[]; + permissions: RolePermission[]; priority: number; description: string | null; visible: boolean; @@ -9,7 +9,7 @@ export type LysandRole = { }; // Last updated: 2024-06-07 -export enum LysandRolePermissions { +export enum RolePermission { ManageNotes = "notes", ManageOwnNotes = "owner:note", ViewNotes = "read:note",