feat(client): 🏷️ Export all types, refactor ResponseError better

This commit is contained in:
Jesse Wierzbinski 2024-06-19 12:38:51 -10:00
parent dc352bc276
commit 218af68dcc
No known key found for this signature in database
6 changed files with 156 additions and 37 deletions

View file

@ -3,6 +3,7 @@
"name": "@lysand-org/client", "name": "@lysand-org/client",
"version": "0.0.0", "version": "0.0.0",
"exports": { "exports": {
".": "./index.ts" ".": "./index.ts",
"./types": "./types.ts"
} }
} }

View file

@ -19,7 +19,8 @@ type ConvertibleObject = {
*/ */
export interface Output<ReturnType> { export interface Output<ReturnType> {
data: ReturnType; data: ReturnType;
headers: Headers; ok: boolean;
raw: Response;
} }
const objectToFormData = (obj: ConvertibleObject): FormData => { const objectToFormData = (obj: ConvertibleObject): FormData => {
@ -62,7 +63,11 @@ const objectToFormData = (obj: ConvertibleObject): FormData => {
* *
* Throws if the request returns invalid or unexpected data. * Throws if the request returns invalid or unexpected data.
*/ */
export class ResponseError<ReturnType> extends Error { export class ResponseError<
ReturnType = {
error?: string;
},
> extends Error {
constructor( constructor(
public response: Output<ReturnType>, public response: Output<ReturnType>,
message: string, message: string,
@ -96,12 +101,11 @@ export class BaseClient {
if (!result.ok) { if (!result.ok) {
const error = isJson ? await result.json() : await result.text(); const error = isJson ? await result.json() : await result.text();
throw new ResponseError<{ throw new ResponseError(
error?: string;
}>(
{ {
data: error, data: error,
headers: result.headers, ok: false,
raw: result,
}, },
`Request failed (${result.status}): ${ `Request failed (${result.status}): ${
error.error || error.message || result.statusText error.error || error.message || result.statusText
@ -111,7 +115,8 @@ export class BaseClient {
return { return {
data: isJson ? await result.json() : (await result.text()) || null, data: isJson ? await result.json() : (await result.text()) || null,
headers: result.headers, ok: true,
raw: result,
}; };
} }

View file

@ -1,30 +1,33 @@
import { OAuth2Client } from "@badgateway/oauth2-client"; import { OAuth2Client } from "@badgateway/oauth2-client";
import type { Account } from "../types/account"; import type {
import type { Activity } from "../types/activity"; Account,
import type { Announcement } from "../types/announcement"; Activity,
import type { Application, ApplicationData } from "../types/application"; Announcement,
import type { AsyncAttachment } from "../types/async_attachment"; Application,
import type { Attachment } from "../types/attachment"; ApplicationData,
import type { Context } from "../types/context"; AsyncAttachment,
import type { Conversation } from "../types/conversation"; Attachment,
import type { Emoji } from "../types/emoji"; Category,
import type { FeaturedTag } from "../types/featured_tag"; Context,
import type { ExtendedDescription, Instance } from "../types/instance"; Conversation,
import type { List } from "../types/list"; Emoji,
import type { LysandRole } from "../types/lysand"; ExtendedDescription,
import type { Marker } from "../types/marker"; FeaturedTag,
import type { Notification } from "../types/notification"; Instance,
import type { Poll } from "../types/poll"; List,
import type { Preferences } from "../types/preferences"; LysandRole,
import type { PushSubscription } from "../types/push_subscription"; Marker,
import type { Relationship } from "../types/relationship"; Poll,
import type { Category, Report } from "../types/report"; Preferences,
import type { Results } from "../types/results"; Relationship,
import type { ScheduledStatus } from "../types/scheduled_status"; Results,
import type { Status, StatusVisibility } from "../types/status"; ScheduledStatus,
import type { StatusSource } from "../types/status_source"; Status,
import type { Tag } from "../types/tag"; StatusSource,
import type { Token } from "../types/token"; StatusVisibility,
Tag,
Token,
} from "../types";
import { BaseClient, type Output } from "./base"; import { BaseClient, type Output } from "./base";
import { DEFAULT_SCOPE, NO_REDIRECT } from "./constants"; import { DEFAULT_SCOPE, NO_REDIRECT } from "./constants";
@ -1503,8 +1506,8 @@ export class LysandClient extends BaseClient {
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<Relationship>> { ): Promise<Output<Relationship>> {
return this.getRelationships([id], options, extra).then((r) => ({ return this.getRelationships([id], options, extra).then((r) => ({
...r,
data: r.data[0], data: r.data[0],
headers: r.headers,
})); }));
} }

View file

@ -41,6 +41,10 @@
".": { ".": {
"import": "./index.ts", "import": "./index.ts",
"default": "./index.ts" "default": "./index.ts"
},
"./types": {
"import": "./types.ts",
"default": "./types.ts"
} }
}, },
"funding": { "funding": {

106
client/types.ts Normal file
View file

@ -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,
};

View file

@ -1,7 +1,7 @@
export type LysandRole = { export type LysandRole = {
id: string; id: string;
name: string; name: string;
permissions: LysandRolePermissions[]; permissions: RolePermission[];
priority: number; priority: number;
description: string | null; description: string | null;
visible: boolean; visible: boolean;
@ -9,7 +9,7 @@ export type LysandRole = {
}; };
// Last updated: 2024-06-07 // Last updated: 2024-06-07
export enum LysandRolePermissions { export enum RolePermission {
ManageNotes = "notes", ManageNotes = "notes",
ManageOwnNotes = "owner:note", ManageOwnNotes = "owner:note",
ViewNotes = "read:note", ViewNotes = "read:note",