Skip to content

Commit c6d2923

Browse files
committed
feat: add support for authentication using @octokit/auth strategies
1 parent ce5ecee commit c6d2923

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ export class Octokit {
5151
}
5252

5353
if (options.auth) {
54-
requestDefaults.headers.authorization = withAuthorizationPrefix(
55-
options.auth
56-
);
54+
if (typeof options.auth === "string") {
55+
requestDefaults.headers.authorization = withAuthorizationPrefix(
56+
options.auth
57+
);
58+
} else {
59+
// @ts-ignore
60+
hook.wrap("request", options.auth.hook);
61+
}
5762
}
5863

5964
this.request = request.defaults(requestDefaults);

src/types.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { Agent } from "http";
22

3+
import { request as Request } from "@octokit/request";
34
import { Octokit } from ".";
45

56
export type OctokitOptions = {
6-
auth?: string;
7+
auth?: string | AutenticationHook;
78
request?: OctokitRequestOptions;
89
[option: string]: any;
910
};
1011

12+
interface AutenticationHook {
13+
(options?: any): any;
14+
15+
hook: (
16+
request: typeof Request,
17+
options: Endpoint
18+
) => ReturnType<typeof Request>;
19+
}
20+
1121
export type Plugin = (octokit: Octokit, options?: OctokitOptions) => void;
1222

1323
// TODO: deduplicate
@@ -60,6 +70,21 @@ export type Parameters = {
6070
[parameter: string]: any;
6171
};
6272

73+
/**
74+
* Relative or absolute URL. Examples: `'/orgs/:org'`, `https://example.com/foo/bar`
75+
*/
76+
export type Url = string;
77+
78+
/**
79+
* Request method
80+
*/
81+
export type Method = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";
82+
83+
export type Endpoint = Parameters & {
84+
method: Method;
85+
url: Url;
86+
};
87+
6388
export type RequestHeaders = {
6489
/**
6590
* Avoid setting `accept`, use `mediaFormat.{format|previews}` instead.

0 commit comments

Comments
 (0)