summaryrefslogtreecommitdiff
path: root/client/simple/src/js/loader.ts
blob: ed374a2d8982ded7b151e0b7c8c5b40b51953d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SPDX-License-Identifier: AGPL-3.0-or-later

import type { Plugin } from "./Plugin.ts";
import { type EndpointsKeys, endpoint } from "./toolkit.ts";

type Options =
  | {
      on: "global";
    }
  | {
      on: "endpoint";
      where: EndpointsKeys[];
    };

export const load = <T extends Plugin>(instance: () => Promise<T>, options: Options): void => {
  if (!check(options)) return;

  void instance();
};

const check = (options: Options): boolean => {
  // biome-ignore lint/style/useDefaultSwitchClause: options is typed
  switch (options.on) {
    case "global": {
      return true;
    }
    case "endpoint": {
      if (!options.where.includes(endpoint)) {
        // not on the expected endpoint
        return false;
      }

      return true;
    }
  }
};