refactor: 🚨 Make class methods that don't use this static

This commit is contained in:
Jesse Wierzbinski 2024-10-03 13:51:19 +02:00
parent 53688095cc
commit 835cdc3f18
No known key found for this signature in database
7 changed files with 47 additions and 40 deletions

View file

@ -113,7 +113,7 @@ export default apiRoute((app) =>
(error, message, app) =>
returnError(
context,
manager.processOAuth2Error(app),
OAuthManager.processOAuth2Error(app),
error,
message,
),

View file

@ -104,10 +104,9 @@ export class ImageConversionPreprocessor implements MediaPreprocessor {
fileName: string,
newExtension: string,
): string {
return this.extractFilenameFromPath(fileName).replace(
/\.[^/.]+$/,
`.${newExtension}`,
);
return ImageConversionPreprocessor.extractFilenameFromPath(
fileName,
).replace(/\.[^/.]+$/, `.${newExtension}`);
}
/**
@ -115,7 +114,7 @@ export class ImageConversionPreprocessor implements MediaPreprocessor {
* @param path - The path to extract the filename from.
* @returns The extracted filename.
*/
private extractFilenameFromPath(path: string): string {
private static extractFilenameFromPath(path: string): string {
const pathParts = path.split(/(?<!\\)\//);
return pathParts[pathParts.length - 1];
}

View file

@ -56,7 +56,7 @@ describe("PluginLoader", () => {
]);
// biome-ignore lint/complexity/useLiteralKeys: Private method
const directories = await pluginLoader["getDirectories"]("/some/path");
const directories = await PluginLoader["getDirectories"]("/some/path");
expect(directories).toEqual(["dir1", "dir2"]);
});
@ -65,7 +65,7 @@ describe("PluginLoader", () => {
const manifestFile =
// biome-ignore lint/complexity/useLiteralKeys: Private method
await pluginLoader["findManifestFile"]("/some/path");
await PluginLoader["findManifestFile"]("/some/path");
expect(manifestFile).toBe("manifest.json");
});
@ -73,7 +73,7 @@ describe("PluginLoader", () => {
mockReaddir.mockResolvedValue(["index.ts", "otherfile.txt"]);
// biome-ignore lint/complexity/useLiteralKeys: Private method
const hasEntrypoint = await pluginLoader["hasEntrypoint"]("/some/path");
const hasEntrypoint = await PluginLoader["hasEntrypoint"]("/some/path");
expect(hasEntrypoint).toBe(true);
});

View file

@ -18,7 +18,7 @@ export class PluginLoader {
* @param {string} dir - The directory to search.
* @returns {Promise<string[]>} - An array of directory names.
*/
private async getDirectories(dir: string): Promise<string[]> {
private static async getDirectories(dir: string): Promise<string[]> {
const files = await readdir(dir, { withFileTypes: true });
return files.filter((f) => f.isDirectory()).map((f) => f.name);
}
@ -28,7 +28,9 @@ export class PluginLoader {
* @param {string} dir - The directory to search.
* @returns {Promise<string | undefined>} - The manifest file name if found, otherwise undefined.
*/
private async findManifestFile(dir: string): Promise<string | undefined> {
private static async findManifestFile(
dir: string,
): Promise<string | undefined> {
const files = await readdir(dir);
return files.find((f) => f.match(/^manifest\.(json|json5|jsonc)$/));
}
@ -38,7 +40,7 @@ export class PluginLoader {
* @param {string} dir - The directory to search.
* @returns {Promise<boolean>} - True if the entrypoint file is found, otherwise false.
*/
private async hasEntrypoint(dir: string): Promise<boolean> {
private static async hasEntrypoint(dir: string): Promise<boolean> {
const files = await readdir(dir);
return files.includes("index.ts") || files.includes("index.js");
}
@ -79,16 +81,16 @@ export class PluginLoader {
* @returns {Promise<string[]>} - An array of plugin directories.
*/
public async findPlugins(dir: string): Promise<string[]> {
const directories = await this.getDirectories(dir);
const directories = await PluginLoader.getDirectories(dir);
const plugins: string[] = [];
for (const directory of directories) {
const manifestFile = await this.findManifestFile(
const manifestFile = await PluginLoader.findManifestFile(
`${dir}/${directory}`,
);
if (
manifestFile &&
(await this.hasEntrypoint(`${dir}/${directory}`))
(await PluginLoader.hasEntrypoint(`${dir}/${directory}`))
) {
plugins.push(directory);
}
@ -105,7 +107,9 @@ export class PluginLoader {
* @throws Will throw an error if the manifest file is missing or invalid.
*/
public async parseManifest(dir: string, plugin: string): Promise<Manifest> {
const manifestFile = await this.findManifestFile(`${dir}/${plugin}`);
const manifestFile = await PluginLoader.findManifestFile(
`${dir}/${plugin}`,
);
if (!manifestFile) {
throw new Error(`Plugin ${plugin} is missing a manifest file`);

View file

@ -149,7 +149,7 @@ export class SonicSearchManager {
* @param n Batch number
* @param batchSize Size of the batch
*/
private getNthDatabaseAccountBatch(
private static getNthDatabaseAccountBatch(
n: number,
batchSize = 1000,
): Promise<Record<string, string | Date>[]> {
@ -172,7 +172,7 @@ export class SonicSearchManager {
* @param n Batch number
* @param batchSize Size of the batch
*/
private getNthDatabaseStatusBatch(
private static getNthDatabaseStatusBatch(
n: number,
batchSize = 1000,
): Promise<Record<string, string | Date>[]> {
@ -221,7 +221,8 @@ export class SonicSearchManager {
const batchCount = Math.ceil(accountCount / batchSize);
for (let i = 0; i < batchCount; i++) {
const accounts = await this.getNthDatabaseAccountBatch(
const accounts =
await SonicSearchManager.getNthDatabaseAccountBatch(
i,
batchSize,
);
@ -252,7 +253,10 @@ export class SonicSearchManager {
const batchCount = Math.ceil(statusCount / batchSize);
for (let i = 0; i < batchCount; i++) {
const statuses = await this.getNthDatabaseStatusBatch(i, batchSize);
const statuses = await SonicSearchManager.getNthDatabaseStatusBatch(
i,
batchSize,
);
await Promise.all(
statuses.map((status) =>
this.ingestChannel.push(

View file

@ -33,7 +33,7 @@ export class OAuthManager {
this.issuer = found;
}
async getFlow(flowId: string) {
static async getFlow(flowId: string) {
return await db.query.OpenIdLoginFlows.findFirst({
where: (flow, { eq }) => eq(flow.id, flowId),
with: {
@ -42,13 +42,13 @@ export class OAuthManager {
});
}
async getAuthServer(issuerUrl: URL) {
static async getAuthServer(issuerUrl: URL) {
return await discoveryRequest(issuerUrl, {
algorithm: "oidc",
}).then((res) => processDiscoveryResponse(issuerUrl, res));
}
getParameters(
static getParameters(
authServer: AuthorizationServer,
issuer: (typeof config.oidc.providers)[0],
currentUrl: URL,
@ -64,7 +64,7 @@ export class OAuthManager {
);
}
async getOIDCResponse(
static async getOIDCResponse(
authServer: AuthorizationServer,
issuer: (typeof config.oidc.providers)[0],
redirectUri: string,
@ -83,7 +83,7 @@ export class OAuthManager {
);
}
async processOIDCResponse(
static async processOIDCResponse(
authServer: AuthorizationServer,
issuer: (typeof config.oidc.providers)[0],
oidcResponse: Response,
@ -98,7 +98,7 @@ export class OAuthManager {
);
}
async getUserInfo(
static async getUserInfo(
authServer: AuthorizationServer,
issuer: (typeof config.oidc.providers)[0],
accessToken: string,
@ -125,7 +125,7 @@ export class OAuthManager {
);
}
processOAuth2Error(
static processOAuth2Error(
application: InferInsertModel<typeof Applications> | null,
) {
return {
@ -212,7 +212,7 @@ export class OAuthManager {
app: Application | null,
) => Response,
) {
const flow = await this.getFlow(flowId);
const flow = await OAuthManager.getFlow(flowId);
if (!flow) {
return errorFn("invalid_request", "Invalid flow", null);
@ -220,9 +220,9 @@ export class OAuthManager {
const issuerUrl = new URL(this.issuer.url);
const authServer = await this.getAuthServer(issuerUrl);
const authServer = await OAuthManager.getAuthServer(issuerUrl);
const parameters = await this.getParameters(
const parameters = await OAuthManager.getParameters(
authServer,
this.issuer,
currentUrl,
@ -236,7 +236,7 @@ export class OAuthManager {
);
}
const oidcResponse = await this.getOIDCResponse(
const oidcResponse = await OAuthManager.getOIDCResponse(
authServer,
this.issuer,
redirectUrl.toString(),
@ -244,7 +244,7 @@ export class OAuthManager {
parameters,
);
const result = await this.processOIDCResponse(
const result = await OAuthManager.processOIDCResponse(
authServer,
this.issuer,
oidcResponse,
@ -265,7 +265,7 @@ export class OAuthManager {
// Validate `sub`
// Later, we'll use this to automatically set the user's data
const userInfo = await this.getUserInfo(
const userInfo = await OAuthManager.getUserInfo(
authServer,
this.issuer,
access_token,

View file

@ -76,7 +76,7 @@ export class Timeline<Type extends Note | User> {
switch (this.type) {
case TimelineType.Note:
linkHeader.push(
...(await this.fetchNoteLinkHeader(
...(await Timeline.fetchNoteLinkHeader(
objects as Note[],
urlWithoutQuery,
limit,
@ -85,7 +85,7 @@ export class Timeline<Type extends Note | User> {
break;
case TimelineType.User:
linkHeader.push(
...(await this.fetchUserLinkHeader(
...(await Timeline.fetchUserLinkHeader(
objects as User[],
urlWithoutQuery,
limit,
@ -98,7 +98,7 @@ export class Timeline<Type extends Note | User> {
return linkHeader.join(", ");
}
private async fetchNoteLinkHeader(
private static async fetchNoteLinkHeader(
notes: Note[],
urlWithoutQuery: string,
limit: number,
@ -126,7 +126,7 @@ export class Timeline<Type extends Note | User> {
return linkHeader;
}
private async fetchUserLinkHeader(
private static async fetchUserLinkHeader(
users: User[],
urlWithoutQuery: string,
limit: number,