/
requires-app-auth.ts
52 lines (45 loc) · 1.48 KB
/
requires-app-auth.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
const PATHS = [
"/app",
"/app/installations",
"/app/installations/:installation_id",
"/app/installations/:installation_id",
"/app/installations/:installation_id/access_tokens",
"/orgs/:org/installation",
"/orgs/:org/installation",
"/repos/:owner/:repo/installation",
"/repos/:owner/:repo/installation",
"/users/:username/installation",
"/users/:username/installation",
];
// CREDIT: Simon Grondin (https://github.com/SGrondin)
// https://github.com/octokit/plugin-throttling.js/blob/45c5d7f13b8af448a9dbca468d9c9150a73b3948/lib/route-matcher.js
function routeMatcher(paths: string[]) {
// EXAMPLE. For the following paths:
/* [
"/orgs/:org/invitations",
"/repos/:owner/:repo/collaborators/:username"
] */
const regexes = paths.map((p) =>
p
.split("/")
.map((c) => (c.startsWith(":") ? "(?:.+?)" : c))
.join("/")
);
// 'regexes' would contain:
/* [
'/orgs/(?:.+?)/invitations',
'/repos/(?:.+?)/(?:.+?)/collaborators/(?:.+?)'
] */
const regex = `^(?:${regexes.map((r) => `(?:${r})`).join("|")})[^/]*$`;
// 'regex' would contain:
/*
^(?:(?:\/orgs\/(?:.+?)\/invitations)|(?:\/repos\/(?:.+?)\/(?:.+?)\/collaborators\/(?:.+?)))[^\/]*$
It may look scary, but paste it into https://www.debuggex.com/
and it will make a lot more sense!
*/
return new RegExp(regex, "i");
}
const REGEX = routeMatcher(PATHS);
export function requiresAppAuth(url: string | undefined): Boolean {
return !!url && REGEX.test(url);
}