From 39f27cd87fa3bc654c4a81561cf7286fb3d57f3f Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Fri, 26 Jul 2024 17:49:36 +0200 Subject: [PATCH] refactor(federation): :recycle: Refactor FederationRequester to be simpler --- client/LICENSE | 21 ++++++++ federation/LICENSE | 21 ++++++++ .../{http/index.test.ts => http.test.ts} | 4 +- federation/{http/index.ts => http.ts} | 4 +- federation/index.ts | 2 +- federation/requester/index.ts | 52 ++++++------------- federation/schemas.ts | 5 ++ .../{validator/index.ts => validator.ts} | 2 +- 8 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 client/LICENSE create mode 100644 federation/LICENSE rename federation/{http/index.test.ts => http.test.ts} (96%) rename federation/{http/index.ts => http.ts} (98%) rename federation/{validator/index.ts => validator.ts} (99%) diff --git a/client/LICENSE b/client/LICENSE new file mode 100644 index 0000000..32797b2 --- /dev/null +++ b/client/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Lysand + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/federation/LICENSE b/federation/LICENSE new file mode 100644 index 0000000..32797b2 --- /dev/null +++ b/federation/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Lysand + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/federation/http/index.test.ts b/federation/http.test.ts similarity index 96% rename from federation/http/index.test.ts rename to federation/http.test.ts index deaf372..c997a4a 100644 --- a/federation/http/index.test.ts +++ b/federation/http.test.ts @@ -1,6 +1,6 @@ import { beforeEach, describe, expect, jest, test } from "bun:test"; -import { RequestParserHandler } from "../http/index.ts"; -import { EntityValidator } from "../validator/index.ts"; +import { RequestParserHandler } from "./http.ts"; +import { EntityValidator } from "./validator.ts"; // Pulled from social.lysand.org const validUser = { diff --git a/federation/http/index.ts b/federation/http.ts similarity index 98% rename from federation/http/index.ts rename to federation/http.ts index 8d7ccac..f204e8b 100644 --- a/federation/http/index.ts +++ b/federation/http.ts @@ -10,8 +10,8 @@ import type { ServerMetadata, Undo, User, -} from "../schemas"; -import type { EntityValidator } from "../validator/index"; +} from "./schemas"; +import type { EntityValidator } from "./validator"; type MaybePromise = T | Promise; diff --git a/federation/index.ts b/federation/index.ts index 78c7dde..a2c9c24 100644 --- a/federation/index.ts +++ b/federation/index.ts @@ -13,7 +13,7 @@ import { type Output, ResponseError, } from "./requester/index"; -import { EntityValidator } from "./validator/index"; +import { EntityValidator } from "./validator"; export { EntityValidator, diff --git a/federation/requester/index.ts b/federation/requester/index.ts index 02e7710..6f64d43 100644 --- a/federation/requester/index.ts +++ b/federation/requester/index.ts @@ -39,12 +39,10 @@ export class ResponseError< /** * Class to handle requests to a remote server. - * @param serverUrl The URL of the server to send requests to. * @param signatureConstructor The constructor to sign requests with. * @param globalCatch A function to call when a request fails. * @example * const requester = new FederationRequester( - * new URL("https://example.com"), * new SignatureConstructor(privateKey, keyId), * ); * @@ -58,17 +56,12 @@ export class ResponseError< */ export class FederationRequester { constructor( - private serverUrl: URL, private signatureConstructor?: SignatureConstructor, public globalCatch: (error: ResponseError) => void = () => { // Do nothing by default }, ) {} - get url(): URL { - return this.serverUrl; - } - /** * Get the user's profile link from their username. * @param username The username to get the profile link for. @@ -84,7 +77,7 @@ export class FederationRequester { */ public async webFinger( username: string, - hostname = this.serverUrl.hostname, + hostname: string, contentType = "application/json", ): Promise { const result = await this.get( @@ -185,7 +178,7 @@ export class FederationRequester { headers.set("Accept", "application/json"); - const request = new Request(new URL(path, this.serverUrl).toString(), { + const request = new Request(path, { method, headers, body: body @@ -201,6 +194,12 @@ export class FederationRequester { : request; } + /** + * Make a GET request to a URL. + * @param path The path to make the request to. + * @param extra Any extra options to pass to the fetch function. + * @returns The data returned by the request. + */ public async get( path: string, extra?: RequestInit, @@ -213,19 +212,13 @@ export class FederationRequester { }); } - public static get( - url: string | URL, - extra?: RequestInit, - ): Promise> { - const urlUrl = new URL(url); - const requester = new FederationRequester(urlUrl); - - return requester.get( - urlUrl.pathname + urlUrl.search, - extra, - ); - } - + /** + * Make a POST request to a URL. + * @param path The path to make the request to. + * @param body The body of the request. + * @param extra Any extra options to pass to the fetch function. + * @returns The data returned by the request. + */ public async post( path: string, body: object, @@ -238,19 +231,4 @@ export class FederationRequester { throw e; }); } - - public static post( - url: string, - body: object, - extra?: RequestInit, - ): Promise> { - const urlUrl = new URL(url); - const requester = new FederationRequester(urlUrl); - - return requester.post( - urlUrl.pathname + urlUrl.search, - body, - extra, - ); - } } diff --git a/federation/schemas.ts b/federation/schemas.ts index 15d9122..a11ae30 100644 --- a/federation/schemas.ts +++ b/federation/schemas.ts @@ -1,3 +1,8 @@ +/** + * @file TypeScript type definitions + * @module federation/types + */ + import type { z } from "zod"; import type { ActionSchema, diff --git a/federation/validator/index.ts b/federation/validator.ts similarity index 99% rename from federation/validator/index.ts rename to federation/validator.ts index 5c7c527..cc0f8a5 100644 --- a/federation/validator/index.ts +++ b/federation/validator.ts @@ -22,7 +22,7 @@ import { UserSchema, VanityExtensionSchema, VisibilitySchema, -} from "../schemas/base"; +} from "./schemas/base"; // biome-ignore lint/suspicious/noExplicitAny: Used only as a base type type AnyZod = z.ZodType;