Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"compare-versions": "^6.1.1",
"lemmy-js-client-v0": "npm:lemmy-js-client@0.20.0-alpha.18",
"lemmy-js-client-v1": "npm:lemmy-js-client@1.0.0-dont-return-actions.1",
"lemmy-js-client-v1": "npm:lemmy-js-client@1.0.0-image-uploads-request.0",
"openapi-fetch": "^0.14.0",
"zod": "^3.25.67"
},
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

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

41 changes: 8 additions & 33 deletions src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export abstract class BaseClient {

abstract featurePost(
payload: {
feature_type: "Community" | "Local";
feature_type: "community" | "local";
featured: boolean;
post_id: number;
},
Expand Down Expand Up @@ -142,7 +142,7 @@ export abstract class BaseClient {
): Promise<types.ListModlogResponse>;

abstract getNotifications(
payload: types.GetPersonMentions,
payload: types.GetNotifications,
options?: RequestOptions,
): Promise<types.ListNotificationsResponse>;

Expand All @@ -151,11 +151,6 @@ export abstract class BaseClient {
options?: RequestOptions,
): Promise<types.GetPersonDetailsResponse>;

abstract getPersonMentions(
payload: types.GetPersonMentions,
options?: RequestOptions,
): Promise<types.ListPersonMentionsResponse>;

abstract getPost(
payload: types.GetPost,
options?: RequestOptions,
Expand All @@ -166,21 +161,11 @@ export abstract class BaseClient {
options?: RequestOptions,
): Promise<types.ListPostsResponse>;

abstract getPrivateMessages(
payload: types.GetPrivateMessages,
options?: RequestOptions,
): Promise<types.ListPrivateMessagesResponse>;

abstract getRandomCommunity(
payload: { type_: types.ListingType },
options?: RequestOptions,
): Promise<{ community_view: types.CommunityView }>;

abstract getReplies(
payload: types.GetReplies,
options?: RequestOptions,
): Promise<types.ListRepliesResponse>;

abstract getSite(options?: RequestOptions): Promise<types.GetSiteResponse>;

abstract getSiteMetadata(
Expand All @@ -193,12 +178,12 @@ export abstract class BaseClient {
): Promise<types.GetUnreadCountResponse>;

abstract likeComment(
payload: { comment_id: number; score: number },
payload: { comment_id: number; is_upvote?: boolean },
options?: RequestOptions,
): Promise<{ comment_view: types.CommentView }>;

abstract likePost(
payload: { post_id: number; score: number },
payload: { is_upvote?: boolean; post_id: number },
options?: RequestOptions,
): Promise<{ post_view: types.PostView }>;

Expand All @@ -218,7 +203,7 @@ export abstract class BaseClient {
): Promise<types.ListPersonContentResponse>;

abstract listPersonLiked(
payload: types.PageParams & { type: types.LikeType },
payload: types.PageParams & { like_type: types.LikeType },
options?: RequestOptions,
): Promise<types.ListPersonLikedResponse>;

Expand Down Expand Up @@ -255,13 +240,8 @@ export abstract class BaseClient {

abstract markAllAsRead(options?: RequestOptions): Promise<void>;

abstract markCommentReplyAsRead(
payload: { comment_reply_id: number; read: boolean },
options?: RequestOptions,
): Promise<void>;

abstract markPersonMentionAsRead(
payload: { person_mention_id: number; read: boolean },
abstract markNotificationAsRead(
payload: { notification: types.Notification; read: boolean },
options?: RequestOptions,
): Promise<void>;

Expand All @@ -270,11 +250,6 @@ export abstract class BaseClient {
options?: RequestOptions,
): Promise<void>;

abstract markPrivateMessageAsRead(
payload: { private_message_id: number; read: boolean },
options?: RequestOptions,
): Promise<void>;

abstract register(
payload: types.Register,
options?: RequestOptions,
Expand Down Expand Up @@ -328,7 +303,7 @@ export abstract class BaseClient {
): Promise<types.ListSearchResponse>;

abstract uploadImage(
payload: { file: File },
payload: { image: File },
options?: RequestOptions,
): Promise<types.UploadImageResponse>;
}
37 changes: 3 additions & 34 deletions src/SafeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,6 @@ export default function buildSafeClient(_Client: AnyClient): AnyClient {
return schemas.GetPersonDetailsResponse.parse(response);
}

async getPersonMentions(
...params: Parameters<BaseClient["getPersonMentions"]>
) {
const response = await super.getPersonMentions(...params);
return schemas.ListPersonMentionsResponse.parse(response);
}

async getPost(...params: Parameters<BaseClient["getPost"]>) {
const { post_view } = await super.getPost(...params);
return { post_view: schemas.PostView.parse(post_view) };
Expand All @@ -183,25 +176,13 @@ export default function buildSafeClient(_Client: AnyClient): AnyClient {
return schemas.ListPostsResponse.parse(response);
}

async getPrivateMessages(
...params: Parameters<BaseClient["getPrivateMessages"]>
) {
const response = await super.getPrivateMessages(...params);
return schemas.ListPrivateMessagesResponse.parse(response);
}

async getRandomCommunity(
...params: Parameters<BaseClient["getRandomCommunity"]>
) {
const { community_view } = await super.getRandomCommunity(...params);
return { community_view: schemas.CommunityView.parse(community_view) };
}

async getReplies(...params: Parameters<BaseClient["getReplies"]>) {
const response = await super.getReplies(...params);
return schemas.ListRepliesResponse.parse(response);
}

async getSite(...params: Parameters<BaseClient["getSite"]>) {
const response = await super.getSite(...params);
return schemas.GetSiteResponse.parse(response);
Expand Down Expand Up @@ -294,28 +275,16 @@ export default function buildSafeClient(_Client: AnyClient): AnyClient {
return super.markAllAsRead(...params);
}

async markCommentReplyAsRead(
...params: Parameters<BaseClient["markCommentReplyAsRead"]>
) {
return super.markCommentReplyAsRead(...params);
}

async markPersonMentionAsRead(
...params: Parameters<BaseClient["markPersonMentionAsRead"]>
async markNotificationAsRead(
...params: Parameters<BaseClient["markNotificationAsRead"]>
) {
return super.markPersonMentionAsRead(...params);
return super.markNotificationAsRead(...params);
}

async markPostAsRead(...params: Parameters<BaseClient["markPostAsRead"]>) {
return super.markPostAsRead(...params);
}

async markPrivateMessageAsRead(
...params: Parameters<BaseClient["markPrivateMessageAsRead"]>
) {
return super.markPrivateMessageAsRead(...params);
}

async register(...params: Parameters<BaseClient["register"]>) {
const response = await super.register(...params);
return schemas.LoginResponse.parse(response);
Expand Down
43 changes: 5 additions & 38 deletions src/ThreadiverseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { satisfies } from "compare-versions";
import { BaseClient, BaseClientOptions, ProviderInfo } from "./BaseClient";
import { UnsupportedSoftwareError } from "./errors";
import LemmyV0Client from "./providers/lemmyv0";
// import LemmyV1Client from "./providers/lemmyv1";
import LemmyV1Client from "./providers/lemmyv1";
import PiefedClient from "./providers/piefed";
import { Nodeinfo21Payload, resolveSoftware } from "./wellknown";

Expand All @@ -16,7 +16,7 @@ export default class ThreadiverseClient implements BaseClient {
* Important: First match wins.
*/
static get supportedSoftware() {
return [LemmyV0Client, PiefedClient] as const;
return [LemmyV1Client, LemmyV0Client, PiefedClient] as const;
}
get software(): ProviderInfo {
if (
Expand Down Expand Up @@ -208,13 +208,6 @@ export default class ThreadiverseClient implements BaseClient {
return client.getPersonDetails(...params);
}

async getPersonMentions(
...params: Parameters<BaseClient["getPersonMentions"]>
) {
const client = await this.ensureClient();
return client.getPersonMentions(...params);
}

async getPost(...params: Parameters<BaseClient["getPost"]>) {
const client = await this.ensureClient();
return client.getPost(...params);
Expand All @@ -225,25 +218,13 @@ export default class ThreadiverseClient implements BaseClient {
return client.getPosts(...params);
}

async getPrivateMessages(
...params: Parameters<BaseClient["getPrivateMessages"]>
) {
const client = await this.ensureClient();
return client.getPrivateMessages(...params);
}

async getRandomCommunity(
...params: Parameters<BaseClient["getRandomCommunity"]>
) {
const client = await this.ensureClient();
return client.getRandomCommunity(...params);
}

async getReplies(...params: Parameters<BaseClient["getReplies"]>) {
const client = await this.ensureClient();
return client.getReplies(...params);
}

async getSite(...params: Parameters<BaseClient["getSite"]>) {
const client = await this.ensureClient();
return client.getSite(...params);
Expand Down Expand Up @@ -339,32 +320,18 @@ export default class ThreadiverseClient implements BaseClient {
return client.markAllAsRead(...params);
}

async markCommentReplyAsRead(
...params: Parameters<BaseClient["markCommentReplyAsRead"]>
) {
const client = await this.ensureClient();
return client.markCommentReplyAsRead(...params);
}

async markPersonMentionAsRead(
...params: Parameters<BaseClient["markPersonMentionAsRead"]>
async markNotificationAsRead(
...params: Parameters<BaseClient["markNotificationAsRead"]>
) {
const client = await this.ensureClient();
return client.markPersonMentionAsRead(...params);
return client.markNotificationAsRead(...params);
}

async markPostAsRead(...params: Parameters<BaseClient["markPostAsRead"]>) {
const client = await this.ensureClient();
return client.markPostAsRead(...params);
}

async markPrivateMessageAsRead(
...params: Parameters<BaseClient["markPrivateMessageAsRead"]>
) {
const client = await this.ensureClient();
return client.markPrivateMessageAsRead(...params);
}

async register(...params: Parameters<BaseClient["register"]>) {
const client = await this.ensureClient();
return client.register(...params);
Expand Down
51 changes: 43 additions & 8 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,57 @@ export class InvalidPayloadError extends FediverseError {
}
}

/**
* Thrown when a fediverse server returns an error response.
*
* Generic — use this when you don't care which software emitted it (e.g.,
* mapping `error.code` or `error.status` to a toast). Software-specific
* subclasses (`LemmyResponseError`, `PiefedResponseError`) extend this; check
* with `instanceof` when business logic depends on the software.
*
* The error code (e.g. "incorrect_login", "too_many_requests") is exposed on
* both `.code` (preferred) and `.message` (for legacy `error.message ===`
* checks). HTTP status is on `.status` when known. The original underlying
* error (e.g. lemmy-js-client's `LemmyError`) is attached as `.cause`.
*/
export class ResponseError extends FediverseError {
status: number;
code: string;
status?: number;

constructor(status: number, message?: string) {
super(message ?? `${status}`);
this.status = status;
constructor(code: string, options?: { cause?: unknown; status?: number }) {
super(
code,
options?.cause !== undefined ? { cause: options.cause } : undefined,
);
this.name = "ResponseError";
this.code = code;
this.status = options?.status;
}
}

/** Thrown when a Lemmy (v0 or v1) server returns an error response. */
export class LemmyResponseError extends ResponseError {
constructor(code: string, options?: { cause?: unknown; status?: number }) {
super(code, options);
this.name = "LemmyResponseError";
}
}

/** Thrown when a PieFed server returns an error response. */
export class PiefedResponseError extends ResponseError {
response: PiefedErrorResponse;
response?: PiefedErrorResponse;

constructor(status: number, payload: PiefedErrorResponse) {
super(status, payload.message);
this.response = payload;
constructor(
code: string,
options?: {
cause?: unknown;
response?: PiefedErrorResponse;
status?: number;
},
) {
super(code, options);
this.name = "PiefedResponseError";
this.response = options?.response;
}
}

Expand Down
Loading
Loading