mirror of
https://github.com/versia-pub/api.git
synced 2025-12-06 16:38:20 +01:00
feat(client): 🏷️ Export all types, refactor ResponseError better
This commit is contained in:
parent
dc352bc276
commit
218af68dcc
|
|
@ -3,6 +3,7 @@
|
|||
"name": "@lysand-org/client",
|
||||
"version": "0.0.0",
|
||||
"exports": {
|
||||
".": "./index.ts"
|
||||
".": "./index.ts",
|
||||
"./types": "./types.ts"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ type ConvertibleObject = {
|
|||
*/
|
||||
export interface Output<ReturnType> {
|
||||
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<ReturnType> extends Error {
|
||||
export class ResponseError<
|
||||
ReturnType = {
|
||||
error?: string;
|
||||
},
|
||||
> extends Error {
|
||||
constructor(
|
||||
public response: Output<ReturnType>,
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Output<Relationship>> {
|
||||
return this.getRelationships([id], options, extra).then((r) => ({
|
||||
...r,
|
||||
data: r.data[0],
|
||||
headers: r.headers,
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,10 @@
|
|||
".": {
|
||||
"import": "./index.ts",
|
||||
"default": "./index.ts"
|
||||
},
|
||||
"./types": {
|
||||
"import": "./types.ts",
|
||||
"default": "./types.ts"
|
||||
}
|
||||
},
|
||||
"funding": {
|
||||
|
|
|
|||
106
client/types.ts
Normal file
106
client/types.ts
Normal 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,
|
||||
};
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in a new issue