fork of hey-api/openapi-ts because I need some additional things
at main 809 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 = 25 typeof callback === 'function' ? await callback(auth) : callback; 26 27 if (!token) { 28 return; 29 } 30 31 if (auth.scheme === 'bearer') { 32 return `Bearer ${token}`; 33 } 34 35 if (auth.scheme === 'basic') { 36 return `Basic ${btoa(token)}`; 37 } 38 39 return token; 40};