-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor Mastodon specific logic with hook API
- Loading branch information
Showing
11 changed files
with
136 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import assert from "node:assert"; | ||
|
||
import { HttpHookMastodon } from "./hook-http-mastodon"; | ||
|
||
describe("hookHttpMastodon", () => { | ||
it("returns a new Request with method PATCH if the request is PUT and the URL ends with /api/v1/accounts/update_credentials", async () => { | ||
const request = new Request( | ||
"https://example.com/api/v1/accounts/update_credentials", | ||
{ method: "PUT" }, | ||
); | ||
const hook = new HttpHookMastodon(); | ||
const result = await hook.before(request); | ||
assert(result instanceof Request); | ||
expect(result).toBeInstanceOf(Request); | ||
expect(result.method).toBe("PATCH"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { type HttpHook } from "../../interfaces/hook"; | ||
|
||
export class HttpHookMastodon implements HttpHook { | ||
readonly type = "Http"; | ||
|
||
async before(request: Request): Promise<Request> { | ||
if ( | ||
request.method === "PUT" && | ||
request.url.toString().endsWith("/api/v1/accounts/update_credentials") | ||
) { | ||
return new Request(request, { method: "PATCH" }); | ||
} | ||
return request; | ||
} | ||
|
||
async after(response: Response): Promise<Response> { | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./hook-action-dispatcher-mastodon"; | ||
export * from "./hook-http-mastodon"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { type AnyAction } from "./action"; | ||
|
||
export type BeforeFn<Ctx extends unknown[], Result> = (...args: Ctx) => Result; | ||
|
||
export type AfterFn<Ctx extends unknown[], Result> = (...args: Ctx) => Result; | ||
|
||
export interface Hook< | ||
T, | ||
BCtx extends unknown[], | ||
BRes, | ||
ACtx extends unknown[], | ||
ARes, | ||
> { | ||
type: T; | ||
before: BeforeFn<BCtx, BRes>; | ||
after: AfterFn<ACtx, ARes>; | ||
} | ||
|
||
export type AnyHook = Hook<string, [unknown], unknown, [unknown], unknown>; | ||
|
||
export type HttpHook = Hook< | ||
"Http", | ||
[Request], | ||
Promise<Request>, | ||
[Response], | ||
Promise<Response> | ||
>; | ||
|
||
export type ActionDispatcherHook = Hook< | ||
"ActionDispatcher", | ||
[AnyAction], | ||
AnyAction, | ||
[unknown, AnyAction], | ||
unknown | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from "./action"; | ||
export * from "./config"; | ||
export * from "./hook"; | ||
export * from "./http"; | ||
export * from "./logger"; | ||
export * from "./serializer"; | ||
export * from "./config"; | ||
export * from "./action"; | ||
export * from "./ws"; |