Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 16 additions & 33 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,31 @@ import tseslint from "typescript-eslint";

export default [
pluginJs.configs.recommended,
...tseslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: true,
},
},
},
{
languageOptions: {
parser: tseslint.parser,
},
},
// For some reason this has to be in its own block
{
ignores: ["putTypesInIndex.js", "dist/*", "docs/*"],
ignores: [
"putTypesInIndex.js",
"dist/*",
"docs/*",
"eslint.config.mjs",
"tsoa_build",
],
},
{
files: ["src/**/*"],
rules: {
"@typescript-eslint/no-empty-interface": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-object-type": 0,
"arrow-body-style": 0,
curly: 0,
"eol-last": 0,
eqeqeq: 0,
"func-style": 0,
"import/no-duplicates": 0,
"max-statements": 0,
"max-params": 0,
"new-cap": 0,
"no-console": 0,
"no-duplicate-imports": 0,
"no-extra-parens": 0,
"no-return-assign": 0,
"no-throw-literal": 1,
"no-trailing-spaces": 0,
"no-unused-expressions": 0,
"no-useless-constructor": 0,
"no-useless-escape": 0,
"no-var": 0,
"prefer-const": 0,
"prefer-rest-params": 0,
"quote-props": 0,
"unicorn/filename-case": 0,
},
rules: {},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe its not finding any because rules here is empty?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this line makes no difference.

},
];
40 changes: 27 additions & 13 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ import { CreatePostWarning } from "./types/CreatePostWarning";
import { CreateCommentWarning } from "./types/CreateCommentWarning";
import { GetMultiCommunity } from "./types/GetMultiCommunity";
import { ListMultiCommunities } from "./types/ListMultiCommunities";
import { UserSettingsBackup } from "./types/UserSettingsBackup";

enum HttpType {
Get = "GET",
Expand All @@ -240,7 +241,7 @@ type RequestOptions = Pick<RequestInit, "signal">;
export class LemmyHttp extends Controller {
#apiUrl: string;
#headers: { [key: string]: string } = {};
#fetchFunction: typeof fetch = fetch.bind(globalThis);
#fetchFunction = fetch.bind(globalThis) as typeof fetch;

/**
* Generates a new instance of LemmyHttp.
Expand Down Expand Up @@ -386,7 +387,10 @@ export class LemmyHttp extends Controller {
@Security("bearerAuth")
@Post("/account/settings/import")
@Tags("Account")
async importSettings(@Body() form: any, @Inject() options?: RequestOptions) {
async importSettings(
@Body() form: UserSettingsBackup,
@Inject() options?: RequestOptions,
) {
return this.#wrapper<object, SuccessResponse>(
HttpType.Post,
"/account/settings/import",
Expand Down Expand Up @@ -671,7 +675,7 @@ export class LemmyHttp extends Controller {
@Get("/account/unread_counts")
@Tags("Account")
async getUnreadCounts(@Inject() options?: RequestOptions) {
return this.#wrapper<{}, UnreadCountsResponse>(
return this.#wrapper<object, UnreadCountsResponse>(
HttpType.Get,
"/account/unread_counts",
{},
Expand Down Expand Up @@ -2876,7 +2880,7 @@ export class LemmyHttp extends Controller {
body: formData as unknown as BodyInit,
headers: this.#headers,
});
return response.json();
return response.json() as ResponseType;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised this needs an as cast

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one actually throws an error: Unsafe return of a value of type Promise @typescript-eslint/no-unsafe-return

}

async #uploadWithQuery<QueryType extends object, ResponseType>(
Expand Down Expand Up @@ -2918,8 +2922,7 @@ export class LemmyHttp extends Controller {
});
}

let json: any | undefined;

let json: unknown;
try {
json = await response.json();
} catch {
Expand All @@ -2930,14 +2933,15 @@ export class LemmyHttp extends Controller {
console.error(
`Request error while calling ${type_} ${endpoint} with ${JSON.stringify(form)}`,
);
let err = new LemmyError(
json.error ?? response.statusText,
const json2 = json as LemmyErrorDummy;
const err = new LemmyError(
json2.error ?? response.statusText,
response.status,
json.message,
json2.message ?? "",
);
throw err;
} else {
return json;
return json as ResponseType;
}
}

Expand All @@ -2949,6 +2953,11 @@ export class LemmyHttp extends Controller {
}
}

interface LemmyErrorDummy {
error: string;
message?: string;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add this because LemmyErrorType doesnt always have message, and so TS throws an error.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that accurate? shouldn't it be then json2.message ?? ""

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems your right. Strange that there is no lint error.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then you should be able to use LemmyErrorType ? its basically the same definition, every option has error but some have message?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it throws this error:

src/http.ts:2941:15 - error TS2339: Property 'message' does not exist on type 'LemmyErrorType'.
Property 'message' does not exist on type '{ error: "block_keyword_too_short"; }'.

2941         json2.message,
~~~~~~~


Found 1 error in src/http.ts:2941

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make message in LemmyError optional, then you shouldn't need this dummy type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not working: Property 'message' does not exist on type '{ error: "block_keyword_too_short"; }'


function encodeGetParams<BodyType extends object>(p: BodyType): string {
return Object.entries(p)
.filter(kv => kv[1] !== undefined && kv[1] !== null)
Expand All @@ -2957,7 +2966,7 @@ function encodeGetParams<BodyType extends object>(p: BodyType): string {
}

function createFormData(image: File | Buffer): FormData {
let formData = new FormData();
const formData = new FormData();

if (image instanceof File) {
formData.append("images[]", image);
Expand All @@ -2983,9 +2992,14 @@ export class LemmyError extends Error {
name: string;
status: number;
message: string;
cause: any;
cause: unknown;

constructor(name: string, status: number, message: string = "", cause?: any) {
constructor(
name: string,
status: number,
message: string = "",
cause?: unknown,
) {
super();
this.name = name;
this.message = message;
Expand Down
1 change: 1 addition & 0 deletions src/other_types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-object-type */

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors about the empty interface definitions below. It suggests using export type ListMediaI = ListMedia which passes lint, but not sure if its correct.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TSOA fails with type aliases and @queries decorator. So it wouldnt work.

But I have PR to fix that and then you can get rid of these below

lukeautry/tsoa#1841

import { AdminListUsers } from "./types/AdminListUsers";
import { CommunityIdQuery } from "./types/CommunityIdQuery";
import { DeleteImageParams } from "./types/DeleteImageParams";
Expand Down
8 changes: 6 additions & 2 deletions tsconfig.json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably needs prettier, and the woodpecker prettier lint should also check this file.

@Nutomic Nutomic Mar 23, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, wasnt running prettier in CI at all.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
"experimentalDecorators": true,
"strictNullChecks": true,
"moduleResolution": "Node",
"esModuleInterop": true
"esModuleInterop": true,
"noFallthroughCasesInSwitch": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
"exclude": ["node_modules", "dist","tsoa_build/routes.ts"]
}