Skip to content

Commit dbe562c

Browse files
committed
feat: installationId strategy option
1 parent 4e914bf commit dbe562c

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

src/auth.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import { request } from "@octokit/request";
22

3-
import { StrategyOptionsWithDefaults, AuthOptions, Permissions } from "./types";
3+
import {
4+
Authentication,
5+
AuthOptions,
6+
Permissions,
7+
StrategyOptionsWithDefaults,
8+
WithInstallationId
9+
} from "./types";
410
import { isAppRoute } from "./is-app-route";
511
import { toTokenAuthentication } from "./to-token-authentication";
612
import { toAppAuthentication } from "./to-app-authentication";
713
import { get, set } from "./cache";
814

915
export async function auth(
1016
state: StrategyOptionsWithDefaults,
11-
options?: AuthOptions
17+
options: AuthOptions = {}
1218
) {
13-
if (options && !options.refresh) {
19+
// installationId is never 0, so a simple check here is fine
20+
const installationId = (options.installationId ||
21+
state.installationId) as number;
22+
23+
if (installationId && !options.refresh) {
1424
const result = get(state.cache, options);
1525
if (result) {
1626
const {
@@ -22,7 +32,7 @@ export async function auth(
2232
} = result;
2333

2434
return toTokenAuthentication(
25-
options.installationId,
35+
installationId,
2636
token,
2737
expiresAt,
2838
permissions,
@@ -34,7 +44,7 @@ export async function auth(
3444

3545
const appAuthentication = toAppAuthentication(state.id, state.privateKey);
3646

37-
if (!options || isAppRoute(options.url)) {
47+
if (isAppRoute(options) || !installationId) {
3848
return appAuthentication;
3949
}
4050

@@ -43,7 +53,7 @@ export async function auth(
4353
} = await state.request(
4454
"POST /app/installations/:installation_id/access_tokens",
4555
{
46-
installation_id: options.installationId,
56+
installation_id: installationId,
4757
repository_ids: options.repositoryIds,
4858
permissions: options.permissions,
4959
previews: ["machine-man"],
@@ -58,7 +68,7 @@ export async function auth(
5868
const {
5969
data: { permissions, single_file_name: singleFileName }
6070
} = await state.request("GET /app/installations/:installation_id", {
61-
installation_id: options.installationId,
71+
installation_id: options.installationId || state.installationId,
6272
previews: ["machine-man"],
6373
headers: appAuthentication.headers
6474
});
@@ -72,7 +82,7 @@ export async function auth(
7282
});
7383

7484
return toTokenAuthentication(
75-
options.installationId,
85+
installationId,
7686
token,
7787
expires_at,
7888
permissions,

src/cache.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// https://github.com/isaacs/node-lru-cache#readme
22
import LRU from "lru-cache";
33

4+
/* istanbul ignore next */
45
import { AuthOptions, Cache, CacheData, Permissions } from "./types";
56

67
export function getCache() {

src/is-app-route.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ function routeMatcher(paths: string[]) {
4747

4848
const REGEX = routeMatcher(PATHS);
4949

50-
export function isAppRoute(path?: string): Boolean {
51-
return !!path && REGEX.test(path);
50+
type Options = {
51+
url?: string;
52+
[key: string]: any;
53+
};
54+
55+
export function isAppRoute(options: Options): Boolean {
56+
return !!options.url && REGEX.test(options.url);
5257
}

src/types.ts

+48-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,52 @@ export type Cache =
88
set: (key: string, value: string) => any;
99
};
1010

11+
export interface AppAuthStrategy {
12+
(options?: StrategyOptions): AppAuth;
13+
}
14+
15+
export interface AppAuth {
16+
(options: AuthOptions): Promise<Authentication>;
17+
}
18+
19+
export type JWT = string;
20+
export type UTC_TIMESTAMP = string;
21+
22+
type AppAuthentication = {
23+
type: "app";
24+
token: JWT;
25+
appId: number;
26+
exporation: number;
27+
headers: {
28+
authorization: string;
29+
};
30+
query: {};
31+
};
32+
33+
type InstallationAccessTokenAuthentication = {
34+
type: "token";
35+
token: string;
36+
tokenType: "installation";
37+
expiresAt: UTC_TIMESTAMP;
38+
permissions: {
39+
[permission: string]: "read" | "write";
40+
};
41+
singleFileName?: string;
42+
repositoryIds?: number[];
43+
headers: {
44+
authorization: string;
45+
};
46+
query: {};
47+
};
48+
49+
export type Authentication =
50+
| AppAuthentication
51+
| InstallationAccessTokenAuthentication;
52+
1153
export type StrategyOptions = {
1254
id: number;
1355
privateKey: string;
56+
installationId?: number;
1457
request?: typeof request;
1558
cache?: Cache;
1659
};
@@ -25,7 +68,7 @@ export type Permissions = {
2568
};
2669

2770
export type AuthOptions = {
28-
installationId: number;
71+
installationId?: number;
2972
repositoryIds?: number[];
3073
permissions?: Permissions;
3174
url?: string;
@@ -39,3 +82,7 @@ export type CacheData = {
3982
repositoryIds?: number[];
4083
singleFileName?: string;
4184
};
85+
86+
export type WithInstallationId = {
87+
installationId: number;
88+
};

0 commit comments

Comments
 (0)