Skip to content

Commit

Permalink
feat(types): optional refresh token for GitHub Apps (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
raferdev authored Jan 22, 2024
1 parent 9abb4a7 commit 01076fe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
24 changes: 11 additions & 13 deletions src/methods/create-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as OAuthAppAuth from "@octokit/auth-oauth-app";

import type { ClientType, State } from "../types.js";
import type {
ClientType,
GithubAppUserAuthenticationWithOptionalExpiration,
State,
} from "../types.js";
import { emitEvent } from "../emit-event.js";

export type CreateTokenWebFlowOptions = Omit<
Expand Down Expand Up @@ -61,12 +65,9 @@ export interface CreateTokenInterface<TClientType extends ClientType> {
// web flow
(options: CreateTokenWebFlowOptions): TClientType extends "oauth-app"
? Promise<{ authentication: OAuthAppAuth.OAuthAppUserAuthentication }>
: Promise<
| { authentication: OAuthAppAuth.GitHubAppUserAuthentication }
| {
authentication: OAuthAppAuth.GitHubAppUserAuthenticationWithExpiration;
}
>;
: Promise<{
authentication: GithubAppUserAuthenticationWithOptionalExpiration;
}>;

// device flow
(
Expand All @@ -75,10 +76,7 @@ export interface CreateTokenInterface<TClientType extends ClientType> {
: CreateTokenGitHubAppDeviceFlowOptions,
): TClientType extends "oauth-app"
? Promise<{ authentication: OAuthAppAuth.OAuthAppUserAuthentication }>
: Promise<
| { authentication: OAuthAppAuth.GitHubAppUserAuthentication }
| {
authentication: OAuthAppAuth.GitHubAppUserAuthenticationWithExpiration;
}
>;
: Promise<{
authentication: GithubAppUserAuthenticationWithOptionalExpiration;
}>;
}
9 changes: 5 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import { OAuthAppOctokit } from "./oauth-app-octokit";

export type ClientType = "oauth-app" | "github-app";
export type OAuthAppOctokitClassType = typeof OAuthAppOctokit;

export type GithubAppUserAuthenticationWithOptionalExpiration =
GitHubAppUserAuthentication &
Partial<GitHubAppUserAuthenticationWithExpiration>;
export type Scope = string;
export type ClientId = string;
export type ClientSecret = string;
export type Token = string;
export type EventName = "token" | "authorization";

export type ActionName =
| "created"
| "reset"
Expand Down Expand Up @@ -110,9 +113,7 @@ export type EventHandlerContext<TOptions extends Options<ClientType>> =
action: ActionName;
token: Token;
octokit: OctokitTypeFromOptions<TOptions>;
authentication?:
| GitHubAppUserAuthentication
| GitHubAppUserAuthenticationWithExpiration;
authentication?: GithubAppUserAuthenticationWithOptionalExpiration;
};
export type EventHandler<TOptions extends Options<ClientType>> = (
context: EventHandlerContext<TOptions>,
Expand Down
4 changes: 4 additions & 0 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export async function GitHubAppTest() {
// @ts-expect-error scopes are not used by GitHub Apps
result.scopes;

result.authentication.expiresAt;
result.authentication.refreshTokenExpiresAt;
result.authentication.refreshToken;

// @ts-expect-error scopes option not permitted for GitHub Apps
await githubApp.createToken({
onVerification() {},
Expand Down

0 comments on commit 01076fe

Please sign in to comment.