fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 39 lines 805 B view raw
1export type AuthToken = string | undefined; 2 3export interface Auth { 4 /** 5 * Which part of the request do we use to send the auth? 6 * 7 * @default 'header' 8 */ 9 in?: 'header' | 'query' | 'cookie'; 10 /** 11 * Header or query parameter name. 12 * 13 * @default 'Authorization' 14 */ 15 name?: string; 16 scheme?: 'basic' | 'bearer'; 17 type: 'apiKey' | 'http'; 18} 19 20export const getAuthToken = async ( 21 auth: Auth, 22 callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken, 23): Promise<string | undefined> => { 24 const token = typeof callback === 'function' ? await callback(auth) : callback; 25 26 if (!token) { 27 return; 28 } 29 30 if (auth.scheme === 'bearer') { 31 return `Bearer ${token}`; 32 } 33 34 if (auth.scheme === 'basic') { 35 return `Basic ${btoa(token)}`; 36 } 37 38 return token; 39};