• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

elbywan / wretch / 18633918757

19 Oct 2025 05:43PM UTC coverage: 97.877% (+10.3%) from 87.552%
18633918757

Pull #273

github

web-flow
Merge dfa154830 into 028eb274f
Pull Request #273: V3

320 of 336 branches covered (95.24%)

Branch coverage included in aggregate %.

298 of 303 new or added lines in 13 files covered. (98.35%)

8 existing lines in 5 files now uncovered.

1662 of 1689 relevant lines covered (98.4%)

62.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.67
/src/addons/basicAuth.ts
1
import type { ConfiguredMiddleware, Wretch, WretchAddon } from "../types.js"
3✔
2

3✔
3
function utf8ToBase64(input: string) {
9✔
4
  const utf8Bytes = new TextEncoder().encode(input)
9✔
5
  return btoa(String.fromCharCode(...utf8Bytes))
9✔
6
}
9✔
7

3✔
8
export interface BasicAuthAddon {
3✔
9
  /**
3✔
10
   * Sets the `Authorization` header to `Basic ` + <base64 encoded credentials>.
3✔
11
   * Additionally, allows using URLs with credentials in them.
3✔
12
   *
3✔
13
   * ```js
3✔
14
   * const user = "user"
3✔
15
   * const pass = "pass"
3✔
16
   *
3✔
17
   * // Automatically sets the Authorization header to "Basic " + <base64 encoded credentials>
3✔
18
   * wretch("...").addon(BasicAuthAddon).basicAuth(user, pass).get()
3✔
19
   *
3✔
20
   * // Allows using URLs with credentials in them
3✔
21
   * wretch(`https://${user}:${pass}@...`).addon(BasicAuthAddon).get()
3✔
22
   * ```
3✔
23
   *
3✔
24
   * @param username - Username to use for basic auth
3✔
25
   * @param password - Password to use for basic auth
3✔
26
   */
3✔
27
  basicAuth<T extends BasicAuthAddon, C, R, E>(
3✔
28
    this: T & Wretch<T, C, R, E>,
3✔
29
    username: string,
3✔
30
    password: string
3✔
31
  ): this
3✔
32
}
3✔
33

3✔
34
const makeBasicAuthMiddleware: () => ConfiguredMiddleware = () => next => (url, opts) => {
3✔
35
  let parsedUrl: URL | null
9✔
36
  try {
9✔
37
    parsedUrl = new URL(url)
9✔
38
  } catch {
9!
39
    parsedUrl = null
×
UNCOV
40
  }
×
41

9✔
42
  if (parsedUrl?.username || parsedUrl?.password) {
9✔
43
    const basicAuthBase64 = utf8ToBase64(
3✔
44
      `${decodeURIComponent(parsedUrl.username)}:${decodeURIComponent(parsedUrl.password)}`,
3✔
45
    )
3✔
46
    opts.headers = {
3✔
47
      ...opts.headers,
3✔
48
      Authorization: `Basic ${basicAuthBase64}`,
3✔
49
    }
3✔
50
    parsedUrl.username = ""
3✔
51
    parsedUrl.password = ""
3✔
52
    url = parsedUrl.toString()
3✔
53
  }
3✔
54

9✔
55
  return next(url, opts)
9✔
56
}
9✔
57

3✔
58

3✔
59
/**
3✔
60
 * Adds the ability to use basic auth with the `Authorization` header.
3✔
61
 *
3✔
62
 * ```js
3✔
63
 * import BasicAuthAddon from "wretch/addons/basicAuth"
3✔
64
 *
3✔
65
 * wretch().addon(BasicAuthAddon)
3✔
66
 * ```
3✔
67
 */
3✔
68
const basicAuth: WretchAddon<BasicAuthAddon> = {
3✔
69
  beforeRequest(wretch) {
3✔
70
    return wretch.middlewares([makeBasicAuthMiddleware()])
9✔
71
  },
9✔
72
  wretch: {
3✔
73
    basicAuth(username, password) {
3✔
74
      const basicAuthBase64 = utf8ToBase64(`${username}:${password}`)
6✔
75
      return this.auth(`Basic ${basicAuthBase64}`)
6✔
76
    },
6✔
77
  },
3✔
78
}
3✔
79

3✔
80
export default basicAuth
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc