-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.ts
175 lines (157 loc) · 4.99 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
import { VERSION } from "./version";
import { addEventHandler } from "./add-event-handler";
import { OAuthAppOctokit } from "./oauth-app-octokit";
import {
getUserOctokitWithState,
GetUserOctokitWithStateInterface,
} from "./methods/get-user-octokit";
import {
GetWebFlowAuthorizationUrlInterface,
getWebFlowAuthorizationUrlWithState,
} from "./methods/get-web-flow-authorization-url";
import {
CreateTokenInterface,
createTokenWithState,
} from "./methods/create-token";
import {
CheckTokenInterface,
checkTokenWithState,
} from "./methods/check-token";
import {
ResetTokenInterface,
resetTokenWithState,
} from "./methods/reset-token";
import {
RefreshTokenInterface,
refreshTokenWithState,
} from "./methods/refresh-token";
import {
ScopeTokenInterface,
scopeTokenWithState,
} from "./methods/scope-token";
import {
DeleteTokenInterface,
deleteTokenWithState,
} from "./methods/delete-token";
import {
DeleteAuthorizationInterface,
deleteAuthorizationWithState,
} from "./methods/delete-authorization";
import type {
AddEventHandler,
ClientType,
ClientTypeFromOptions,
ConstructorOptions,
OctokitTypeFromOptions,
Options,
State,
} from "./types";
// types required by external handlers (aws-lambda, etc)
export type {
HandlerOptions,
OctokitRequest,
OctokitResponse,
} from "./middleware/types";
// generic handlers
export { handleRequest } from "./middleware/handle-request";
export { createNodeMiddleware } from "./middleware/node/index";
export {
createCloudflareHandler,
createWebWorkerHandler,
} from "./middleware/web-worker/index";
export { createAWSLambdaAPIGatewayV2Handler } from "./middleware/aws-lambda/api-gateway-v2";
type Constructor<T> = new (...args: any[]) => T;
export class OAuthApp<
TOptions extends Options<ClientType> = Options<"oauth-app">
> {
static VERSION = VERSION;
static defaults<
TDefaults extends Options<ClientType>,
S extends Constructor<OAuthApp<TDefaults>>
>(this: S, defaults: TDefaults) {
const OAuthAppWithDefaults = class extends this {
constructor(...args: any[]) {
super({
...defaults,
...args[0],
});
}
};
return OAuthAppWithDefaults as typeof OAuthAppWithDefaults & typeof this;
}
constructor(options: ConstructorOptions<TOptions>) {
const Octokit = options.Octokit || OAuthAppOctokit;
this.type = (options.clientType ||
"oauth-app") as ClientTypeFromOptions<TOptions>;
const octokit = new Octokit({
authStrategy: createOAuthAppAuth,
auth: {
clientType: this.type,
clientId: options.clientId,
clientSecret: options.clientSecret,
},
});
const state: State = {
clientType: this.type,
clientId: options.clientId,
clientSecret: options.clientSecret,
// @ts-expect-error defaultScopes not permitted for GitHub Apps
defaultScopes: options.defaultScopes || [],
allowSignup: options.allowSignup,
baseUrl: options.baseUrl,
redirectUrl: options.redirectUrl,
log: options.log,
Octokit,
octokit,
eventHandlers: {},
};
this.on = addEventHandler.bind(null, state) as AddEventHandler<TOptions>;
// @ts-expect-error TODO: figure this out
this.octokit = octokit;
this.getUserOctokit = getUserOctokitWithState.bind(null, state);
this.getWebFlowAuthorizationUrl = getWebFlowAuthorizationUrlWithState.bind(
null,
state
) as GetWebFlowAuthorizationUrlInterface<ClientTypeFromOptions<TOptions>>;
this.createToken = createTokenWithState.bind(
null,
state
) as CreateTokenInterface<ClientTypeFromOptions<TOptions>>;
this.checkToken = checkTokenWithState.bind(
null,
state
) as CheckTokenInterface<ClientTypeFromOptions<TOptions>>;
this.resetToken = resetTokenWithState.bind(
null,
state
) as ResetTokenInterface<ClientTypeFromOptions<TOptions>>;
this.refreshToken = refreshTokenWithState.bind(
null,
state
) as RefreshTokenInterface;
this.scopeToken = scopeTokenWithState.bind(
null,
state
) as ScopeTokenInterface;
this.deleteToken = deleteTokenWithState.bind(null, state);
this.deleteAuthorization = deleteAuthorizationWithState.bind(null, state);
}
// assigned during constructor
type: ClientTypeFromOptions<TOptions>;
on: AddEventHandler<TOptions>;
octokit: OctokitTypeFromOptions<TOptions>;
getUserOctokit: GetUserOctokitWithStateInterface<
ClientTypeFromOptions<TOptions>
>;
getWebFlowAuthorizationUrl: GetWebFlowAuthorizationUrlInterface<
ClientTypeFromOptions<TOptions>
>;
createToken: CreateTokenInterface<ClientTypeFromOptions<TOptions>>;
checkToken: CheckTokenInterface<ClientTypeFromOptions<TOptions>>;
resetToken: ResetTokenInterface<ClientTypeFromOptions<TOptions>>;
refreshToken: RefreshTokenInterface;
scopeToken: ScopeTokenInterface;
deleteToken: DeleteTokenInterface;
deleteAuthorization: DeleteAuthorizationInterface;
}