1- import { AuthOptions , Authentication , State , ClientType } from "./types" ;
1+ import {
2+ OAuthAppAuthOptions ,
3+ GitHubAppAuthOptions ,
4+ OAuthAppAuthentication ,
5+ GitHubAppAuthentication ,
6+ GitHubAppAuthenticationWithExpiration ,
7+ OAuthAppState ,
8+ GitHubAppState ,
9+ } from "./types" ;
210import { getAuthentication } from "./get-authentication" ;
311import {
412 checkToken ,
@@ -8,32 +16,48 @@ import {
816 resetToken ,
917} from "@octokit/oauth-methods" ;
1018
11- export async function auth < TClientType extends ClientType > (
12- state : State ,
13- options : AuthOptions = { }
14- ) : Promise < Authentication < TClientType > > {
19+ export async function auth (
20+ state : OAuthAppState ,
21+ options ?: OAuthAppAuthOptions
22+ ) : Promise < OAuthAppAuthentication > ;
23+
24+ export async function auth (
25+ state : GitHubAppState ,
26+ options ?: GitHubAppAuthOptions
27+ ) : Promise < GitHubAppAuthentication | GitHubAppAuthenticationWithExpiration > ;
28+
29+ export async function auth (
30+ state : OAuthAppState | GitHubAppState ,
31+ options : OAuthAppAuthOptions | GitHubAppAuthOptions = { }
32+ ) : Promise <
33+ | OAuthAppAuthentication
34+ | GitHubAppAuthentication
35+ | GitHubAppAuthenticationWithExpiration
36+ > {
1537 if ( ! state . authentication ) {
16- state . authentication = await getAuthentication ( state ) ;
38+ // This is what TS makes us do ¯\_(ツ)_/¯
39+ state . authentication =
40+ state . clientType === "oauth-app"
41+ ? await getAuthentication ( state )
42+ : await getAuthentication ( state ) ;
1743 }
1844
1945 if ( state . authentication . invalid ) {
2046 throw new Error ( "[@octokit/auth-oauth-user] Token is invalid" ) ;
2147 }
2248
23- const currentAuthentication = ( state . authentication as unknown ) as Authentication < TClientType > ;
49+ const currentAuthentication = state . authentication ;
2450
2551 // (auto) refresh for user-to-server tokens
2652 if ( "expiresAt" in currentAuthentication ) {
2753 if (
2854 options . type === "refresh" ||
29- // @ts -expect-error TBD
3055 new Date ( currentAuthentication . expiresAt ) < new Date ( )
3156 ) {
3257 const { authentication } = await refreshToken ( {
3358 clientType : "github-app" ,
3459 clientId : state . clientId ,
3560 clientSecret : state . clientSecret ,
36- // @ts -expect-error TBD
3761 refreshToken : currentAuthentication . refreshToken ,
3862 request : state . request ,
3963 } ) ;
@@ -77,6 +101,11 @@ export async function auth<TClientType extends ClientType>(
77101 // @ts -expect-error TBD
78102 ...authentication ,
79103 } ;
104+
105+ return state . authentication as
106+ | OAuthAppAuthentication
107+ | GitHubAppAuthentication
108+ | GitHubAppAuthenticationWithExpiration ;
80109 } catch ( error ) {
81110 // istanbul ignore else
82111 if ( error . status === 404 ) {
@@ -88,8 +117,6 @@ export async function auth<TClientType extends ClientType>(
88117
89118 throw error ;
90119 }
91-
92- return state . authentication as Authentication < TClientType > ;
93120 }
94121
95122 // invalidate
@@ -111,8 +138,8 @@ export async function auth<TClientType extends ClientType>(
111138 }
112139
113140 state . authentication . invalid = true ;
114- return state . authentication as Authentication < TClientType > ;
141+ return state . authentication ;
115142 }
116143
117- return state . authentication as Authentication < TClientType > ;
144+ return state . authentication ;
118145}
0 commit comments