UNPKG

wretch

Version:

A tiny wrapper built around fetch with an intuitive syntax.

1 lines 3.03 kB
{"version":3,"file":"basicAuth.min.mjs","names":[],"sources":["../../../src/addons/basicAuth.ts"],"sourcesContent":["import type { ConfiguredMiddleware, Wretch, WretchAddon } from \"../types.js\"\n\nfunction utf8ToBase64(input: string) {\n const utf8Bytes = new TextEncoder().encode(input)\n return btoa(String.fromCharCode(...utf8Bytes))\n}\n\nexport interface BasicAuthAddon {\n /**\n * Sets the `Authorization` header to `Basic ` + <base64 encoded credentials>.\n * Additionally, allows using URLs with credentials in them.\n *\n * ```js\n * const user = \"user\"\n * const pass = \"pass\"\n *\n * // Automatically sets the Authorization header to \"Basic \" + <base64 encoded credentials>\n * wretch(\"...\").addon(BasicAuthAddon).basicAuth(user, pass).get()\n *\n * // Allows using URLs with credentials in them\n * wretch(`https://${user}:${pass}@...`).addon(BasicAuthAddon).get()\n * ```\n *\n * @param username - Username to use for basic auth\n * @param password - Password to use for basic auth\n */\n basicAuth<T extends BasicAuthAddon, C, R, E>(\n this: T & Wretch<T, C, R, E>,\n username: string,\n password: string\n ): this\n}\n\nconst makeBasicAuthMiddleware: () => ConfiguredMiddleware = () => next => (url, opts) => {\n let parsedUrl: URL | null\n try {\n parsedUrl = new URL(url)\n } catch {\n parsedUrl = null\n }\n\n if (parsedUrl?.username || parsedUrl?.password) {\n const basicAuthBase64 = utf8ToBase64(\n `${decodeURIComponent(parsedUrl.username)}:${decodeURIComponent(parsedUrl.password)}`,\n )\n opts.headers = {\n ...opts.headers,\n Authorization: `Basic ${basicAuthBase64}`,\n }\n parsedUrl.username = \"\"\n parsedUrl.password = \"\"\n url = parsedUrl.toString()\n }\n\n return next(url, opts)\n}\n\n\n/**\n * Adds the ability to use basic auth with the `Authorization` header.\n *\n * ```js\n * import BasicAuthAddon from \"wretch/addons/basicAuth\"\n *\n * wretch().addon(BasicAuthAddon)\n * ```\n */\nconst basicAuth: WretchAddon<BasicAuthAddon> = {\n beforeRequest(wretch) {\n return wretch.middlewares([makeBasicAuthMiddleware()])\n },\n wretch: {\n basicAuth(username, password) {\n const basicAuthBase64 = utf8ToBase64(`${username}:${password}`)\n return this.auth(`Basic ${basicAuthBase64}`)\n },\n },\n}\n\nexport default basicAuth\n"],"mappings":"AAEA,SAAS,EAAa,EAAe,CACnC,IAAM,EAAY,IAAI,aAAa,CAAC,OAAO,EAAM,CACjD,OAAO,KAAK,OAAO,aAAa,GAAG,EAAU,CAAC,CA6BhD,MAAM,MAA4D,IAAS,EAAK,IAAS,CACvF,IAAI,EACJ,GAAI,CACF,EAAY,IAAI,IAAI,EAAI,MAClB,CACN,EAAY,KAGd,GAAI,GAAW,UAAY,GAAW,SAAU,CAC9C,IAAM,EAAkB,EACtB,GAAG,mBAAmB,EAAU,SAAS,CAAC,GAAG,mBAAmB,EAAU,SAAS,GACpF,CACD,EAAK,QAAU,CACb,GAAG,EAAK,QACR,cAAe,SAAS,IACzB,CACD,EAAU,SAAW,GACrB,EAAU,SAAW,GACrB,EAAM,EAAU,UAAU,CAG5B,OAAO,EAAK,EAAK,EAAK,EAalB,EAAyC,CAC7C,cAAc,EAAQ,CACpB,OAAO,EAAO,YAAY,CAAC,GAAyB,CAAC,CAAC,EAExD,OAAQ,CACN,UAAU,EAAU,EAAU,CAC5B,IAAM,EAAkB,EAAa,GAAG,EAAS,GAAG,IAAW,CAC/D,OAAO,KAAK,KAAK,SAAS,IAAkB,EAE/C,CACF"}