summaryrefslogtreecommitdiffhomepage
path: root/packages/sdk/js/src/gen/core/auth.gen.ts
blob: bc7b230f4475a6a52271a6e6ac77437c244cea32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// This file is auto-generated by @hey-api/openapi-ts

export type AuthToken = string | undefined

export interface Auth {
  /**
   * Which part of the request do we use to send the auth?
   *
   * @default 'header'
   */
  in?: "header" | "query" | "cookie"
  /**
   * Header or query parameter name.
   *
   * @default 'Authorization'
   */
  name?: string
  scheme?: "basic" | "bearer"
  type: "apiKey" | "http"
}

export const getAuthToken = async (
  auth: Auth,
  callback: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken,
): Promise<string | undefined> => {
  const token = typeof callback === "function" ? await callback(auth) : callback

  if (!token) {
    return
  }

  if (auth.scheme === "bearer") {
    return `Bearer ${token}`
  }

  if (auth.scheme === "basic") {
    return `Basic ${btoa(token)}`
  }

  return token
}