/
load.ts
76 lines (66 loc) 路 1.71 KB
/
load.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Deprecation } from "deprecation";
import { Router } from "express";
import { Probot } from "./index";
import { Application } from "./application";
import { ApplicationFunction, ApplicationFunctionOptions } from "./types";
import { getRouter } from "./get-router";
type DeprecatedKey =
| "auth"
| "load"
| "log"
| "on"
| "receive"
| "route"
| "router";
const DEPRECATED_APP_KEYS: DeprecatedKey[] = [
"auth",
"load",
"log",
"on",
"receive",
"route",
"router",
];
let didDeprecate = false;
function bindMethod(app: Probot | Application, key: keyof Application) {
return typeof app[key] === "function" ? app[key].bind(app) : app[key];
}
/**
* Loads an ApplicationFunction into the current Application
* @param appFn - Probot application function to load
*/
export function load(
app: Application | Probot,
router: Router | null,
appFn: ApplicationFunction | ApplicationFunction[]
) {
const deprecatedApp = DEPRECATED_APP_KEYS.reduce(
(api: Record<string, unknown>, key: DeprecatedKey) => {
Object.defineProperty(api, key, {
get() {
if (didDeprecate) return bindMethod(app, key);
app.log.warn(
new Deprecation(
'[probot] "(app) => {}" is deprecated. Use "({ app }) => {}" instead'
)
);
didDeprecate = true;
return bindMethod(app, key);
},
});
return api;
},
{}
);
if (Array.isArray(appFn)) {
appFn.forEach((fn) => load(app, router, fn));
} else {
appFn(
(Object.assign(deprecatedApp, {
app,
getRouter: getRouter.bind(null, router || app.router),
}) as unknown) as ApplicationFunctionOptions
);
}
return app;
}