Commit 0fd29c0b authored by Grant's avatar Grant
Browse files

Add Pixelfed API

parent 89656de4
Loading
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
{
  "name": "@sc07/fedi-testkit",
  "version": "1.0.3",
  "version": "1.0.4",
  "exports": {
    ".": {
      "default": "./dist/index.js",
@@ -12,12 +12,12 @@
  ],
  "packageManager": "yarn@4.12.0",
  "devDependencies": {
    "@types/node": "^25.0.0",
    "@types/node": "^25.0.9",
    "dotenv": "^17.2.3",
    "typescript": "^5.9.3"
  },
  "dependencies": {
    "puppeteer": "^24.33.0",
    "puppeteer": "^24.35.0",
    "tsx": "^4.21.0"
  },
  "scripts": {
+2 −0
Original line number Diff line number Diff line
@@ -5,10 +5,12 @@ import { LemmyAPI } from "./lemmy.api";
import { MastodonAPI } from "./mastodon.api";
import { MBinAPI } from "./mbin.api";
import { PiefedAPI } from "./piefed.api";
import { PixelfedAPI } from "./pixelfed.api";

export const API = {
  MASTODON: MastodonAPI,
  LEMMY: LemmyAPI,
  MBIN: MBinAPI,
  PIEFED: PiefedAPI,
  PIXELFED: PixelfedAPI,
} satisfies { [key in SOFTWARES]: new (software: Software) => ServiceAPI<any> };
+5 −5
Original line number Diff line number Diff line
@@ -2,8 +2,8 @@ import { Software } from "..";
import { IMessage, ServiceAPI } from "./abstract.api";

export class MastodonAPI implements ServiceAPI<IMastodonNotification> {
  private accessToken: string | null = null;
  constructor(private readonly software: Software) {}
  protected accessToken: string | null = null;
  constructor(protected readonly software: Software) {}

  async login() {
    if (this.accessToken) {
@@ -20,10 +20,10 @@ export class MastodonAPI implements ServiceAPI<IMastodonNotification> {
    this.accessToken = accessToken;
  }

  private authFetch(
  protected authFetch(
    endpoint: `/${string}`,
    method = "GET",
    accessToken = this.accessToken
    accessToken = this.accessToken,
  ) {
    return fetch(new URL(endpoint, this.software.host), {
      method,
@@ -69,7 +69,7 @@ export class MastodonAPI implements ServiceAPI<IMastodonNotification> {
    // return notifications from collection timeframe

    const page = await this.authFetch(
      this.makeRelative(this.nextNotificationPage) || "/api/v1/notifications"
      this.makeRelative(this.nextNotificationPage) || "/api/v1/notifications",
    );

    return await page.json();
+27 −0
Original line number Diff line number Diff line
import { MastodonAPI } from "./mastodon.api";

/**
 * The Pixelfed API is Mastodon compatible
 *
 * The only difference is the token check
 */
export class PixelfedAPI extends MastodonAPI {
  override async login() {
    if (this.accessToken) {
      console.warn("Mastodon#login called with accessToken");
      return;
    }

    const accessToken = this.software.auth;

    const api = await this.authFetch(
      `/api/v1/accounts/verify_credentials`,
      "GET",
      accessToken,
    );
    if (!api.ok) throw new Error("Access token provided is invalid");

    console.log("Acquired an access token");
    this.accessToken = accessToken;
  }
}
+7 −1
Original line number Diff line number Diff line
export const SOFTWARES = ["MASTODON", "LEMMY", "PIEFED", "MBIN"] as const;
export const SOFTWARES = [
  "MASTODON",
  "LEMMY",
  "PIEFED",
  "MBIN",
  "PIXELFED",
] as const;
export type SOFTWARES = (typeof SOFTWARES)[number];

const testkit_headful = process.env.TESTKIT_HEADFUL;
Loading