Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

auth.gen.ts

48 lines1.0 KB

1// This file is auto-generated by @hey-api/openapi-ts
2​
3export type AuthToken = string | undefined
4​
5export interface Auth {
6 /**
7 * Which part of the request do we use to send the auth?
8 *
9 * @default 'header'
10 */
11 in?: 'header' | 'query' | 'cookie'
12 /**
13 * A unique identifier for the security scheme.
14 *
15 * Defined only when there are multiple security schemes whose `Auth`
16 * shape would otherwise be identical.
17 */
18 key?: string
19 /**
20 * Header or query parameter name.
21 *
22 * @default 'Authorization'
23 */
24 name?: string
25 scheme?: 'basic' | 'bearer'
26 type: 'apiKey' | 'http'
28​
29export const getAuthToken = async (
30 auth: Auth,
31 callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken,
32): Promise<string | undefined> => {
33 const token = typeof callback === 'function' ? await callback(auth) : callback
34​
35 if (!token) {
36 return
37 }
38​
39 if (auth.scheme === 'bearer') {
40 return `Bearer ${token}`
41 }
42​
43 if (auth.scheme === 'basic') {
44 return `Basic ${btoa(token)}`
45 }
46​
47 return token