{"_id":"@code6226/cf-workers-fetch-helpers","name":"@code6226/cf-workers-fetch-helpers","dist-tags":{"latest":"1.3.0"},"versions":{"1.3.0":{"name":"@code6226/cf-workers-fetch-helpers","private":false,"description":"A collection of chainable helpers to adapt the Fetch API","version":"1.3.0","license":"MIT","main":"dist/index.js","scripts":{"test:ci":"jest --ci","build":"tsc -p tsconfig.build.json --outDir dist/","clean":"rimraf dist/","clean:node":"rimraf package-lock.json node_modules/","lint":"prettier -c .","format":"npm run lint -- --write"},"author":{"name":"Greg Brimble","email":"greg@glenstack.com","url":"https://glenstack.com/"},"bugs":{"url":"https://github.com/glenstack/glenstack/tree/master/packages/cf-workers-fetch-helpers","email":"hello@glenstack.com"},"repository":{"type":"git","url":"git+https://github.com/glenstack/glenstack.git","directory":"packages/cf-workers-fetch-helpers"},"devDependencies":{"@types/jest":"26.0.13","@types/node":"14.10.1","cross-fetch":"3.0.6","jest":"26.4.2","prettier":"2.1.1","rimraf":"3.0.2","ts-jest":"26.3.0","typescript":"4.0.2"},"publishConfig":{"access":"public"},"dependencies":{"@cfworker/base64url":"^1.12.3"},"types":"./dist\\index.d.ts","gitHead":"c9a959a13a68779843946eaf7f3a91bb8b68e02d","homepage":"https://github.com/glenstack/glenstack#readme","_id":"@code6226/cf-workers-fetch-helpers@1.3.0","_nodeVersion":"16.14.2","_npmVersion":"8.5.0","dist":{"integrity":"sha512-I8YFlP7jL55yhPuylmKdnShpYXz0PQteeKrCLsgqGifW8aR9FFkdDsGrxBVNsXuA9GqVCQt5eKuiPXn3M2LW+Q==","shasum":"86891fa0554c167fb9c9af70435e3262f8b70220","tarball":"https://registry.npmjs.org/@code6226/cf-workers-fetch-helpers/-/cf-workers-fetch-helpers-1.3.0.tgz","fileCount":15,"unpackedSize":18893,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDsGu5FoP9H+OQZvZW2ZMXbXuE7iqBRjwYiaUtq+YNKOQIgGpeO/wKNdiQNWNUQkLR1ed6SnXcR8IycEB49TNdxkUU="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJibrBOACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpePg//b68fLt5b16qUzyn32TAvm9HAQRslyuluh3UNIHBX42zoExLz\r\nH5VnA+iKzJXIr6p+kudTwSnqXPePqvTW6RXmjFmBs0SbGSUHZsEi4n4K97Vr\r\n8aqTixOyYk/4CPnEwRceEi2zE1qBngVfqDg3j9IgpTxdJj3U3tGzVFlc5M5L\r\n+ImflJKkTwmib9aKm6btZCw8FtOpv0XUtAhTiofwUyIZA2u01lsqK9cAFrbM\r\nD2F051RL4AepLowVXJulz87nUfFYC7JgHejt+RmybJC4Aqrrc4q6SrgPVlap\r\nib8V6Co7qtQ/hjnJcgnYhlHvEq91jrU+oI4fxM9SyRR6ePPJiJHL7GfwkCJN\r\nsfP1oAgbMmae3krRUWyvcUaCpEPb3W5kaE7E5xN0FfjaJ4nHQXqFLMRFQ4s3\r\ncRCq6qs4ZUQAoT/W8TUKYe930VKEJ8GwHHo6G4pvdfPRHFapCmdsK+fZI7SY\r\n13H1QjFi+XH102edz9Qm08eM5cBYAA/qbZcwwKAUgjsBEWTCZRdUZZlA4TI0\r\nm64Sd5gjNOrNmWcoZDIiUvjRiPi6BY0Pn+GjbWKnNBiOYYvvFKGb2dnLMk6l\r\nJAOLoRP3FcVq9NZLvQCezjfcKC6i0FJ5wouMYDLIrbVLZyuqGTAe4q5/iyxD\r\niPmYnREbsSTFdgRWJqagykdL/Hc6hlU8lW8=\r\n=fB99\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"code6226","email":"code6226@gmail.com"},"directories":{},"maintainers":[{"name":"code6226","email":"code6226@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/cf-workers-fetch-helpers_1.3.0_1651421262787_0.5090590665642736"},"_hasShrinkwrap":false}},"time":{"created":"2022-05-01T16:07:42.726Z","1.3.0":"2022-05-01T16:07:42.963Z","modified":"2022-05-01T16:07:43.123Z"},"maintainers":[{"name":"code6226","email":"code6226@gmail.com"}],"description":"A collection of chainable helpers to adapt the Fetch API","homepage":"https://github.com/glenstack/glenstack#readme","repository":{"type":"git","url":"git+https://github.com/glenstack/glenstack.git","directory":"packages/cf-workers-fetch-helpers"},"author":{"name":"Greg Brimble","email":"greg@glenstack.com","url":"https://glenstack.com/"},"bugs":{"url":"https://github.com/glenstack/glenstack/tree/master/packages/cf-workers-fetch-helpers","email":"hello@glenstack.com"},"license":"MIT","readme":"# Cloudflare Workers Fetch Helpers\n\nA collection of chainable helpers to adapt the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch).\n\n### Installation\n\n```sh\nnpm install --save @glenstack/cf-workers-fetch-helpers\n```\n\n### Usage\n\nAll methods exported from this library can be chained together and have the following usage:\n\n```typescript\nimport {\n  fetchHelper,\n  otherFetchHelper,\n} from \"@glenstack/cf-workers-fetch-helpers\";\n\nconst fetch1 = fetchHelper(fetch, fetchHelperOptions); // `fetch` is the built-in fetch global\nconst fetch2 = otherFetchHelper(fetch1, otherFetchHelperOptions); // NOTE: `fetch1` is being chained here, such that `fetch2(request)` calls `fetch1(request)`, which calls `fetch(request)`\n\n(async () => {\n  const response = await fetch2(\"https://example.com\");\n})();\n```\n\nWhere:\n\n- `fetch`, `fetch1` and `fetch2` are all [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch) compatible functions,\n- `fetchHelper` and `otherFetchHelper` are fictional functions in this library (see below for the real ones),\n- `fetchHelperOptions` and `otherFetchHelperOptions` are the options for these fictional helper functions (again, the real functions and their options follow).\n\n### `alterURL`\n\nChanges the Request URL.\n\n#### Options Signature\n\n```typescript\ntype options =\n  | {\n      prepend?: string;\n      append?: string;\n    }\n  | {\n      mutate: (prevURL: string) => string;\n    };\n```\n\n#### Options\n\n| Option    | Notes                                                   |\n| --------- | ------------------------------------------------------- |\n| `prepend` | Prepends the URL with a given string.                   |\n| `append`  | Appends the URL with a given string.                    |\n| `mutate`  | A function that, when given a URL, returns the new URL. |\n\n#### Example Usage\n\n```typescript\nimport { alterURL } from \"@glenstack/cf-workers-fetch-helpers\";\n\nconst gitHubFetch = alterURL(fetch, { prepend: \"https://api.github.com\" });\n\n(async () => {\n  const response = await gitHubFetch(\"/meta\");\n})();\n```\n\n### `proxyHost`\n\nReplaces the host of a Request URL.\n\n#### Options Signature\n\n```typescript\ntype options = {\n  host: string;\n};\n```\n\n#### Options\n\n| Option | Notes                                       |\n| ------ | ------------------------------------------- |\n| `host` | The new host to replace in the Request URL. |\n\n#### Example Usage\n\n```typescript\nimport { proxyHost } from \"@glenstack/cf-workers-fetch-helpers\";\n\nconst proxiedFetch = proxyHost(fetch, { host: \"about.gitlab.com\" })(\n  async () => {\n    const response = await proxiedFetch(\"https://github.com/pricing\");\n  }\n)();\n```\n\n### `addHeaders`\n\nAdds headers to the Request.\n\n#### Options Signature\n\n```typescript\ntype options = {\n  headers: RequestInit[\"headers\"];\n};\n```\n\n#### Options\n\n| Option    | Notes                                                                                                                                                  |\n| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| `headers` | [A Headers object, an object literal, or an array of two-item arrays to set request’s headers.](https://fetch.spec.whatwg.org/#typedefdef-headersinit) |\n\n#### Example Usage\n\n```typescript\nimport { addHeaders } from \"@glenstack/cf-workers-fetch-helpers\";\n\nconst gitHubFetch = addHeaders(fetch, {\n  headers: { Authorization: \"Basic xyz\", \"User-Agent\": \"Awesome-Octocat-App\" },\n});\n\n(async () => {\n  const response = await gitHubFetch(\"/meta\");\n})();\n```\n\n### `authorization`\n\nAdds an Authorization header. The following types of authorization are supported:\n\n- Basic\n- Bearer\n\n#### Options Signature\n\n```typescript\ntype options =\n  | {\n      username?: string;\n      password?: string;\n    }\n  | {\n      bearere: string;\n    };\n```\n\n#### Options\n\n| Option     | Notes                                        |\n| ---------- | -------------------------------------------- |\n| `username` | Used in basic authorization.                 |\n| `password` | Used in basic authorization.                 |\n| `bearer`   | A bearer token used in bearer authorization. |\n\n#### Example Usage\n\n```typescript\nimport { authorization } from \"@glenstack/cf-workers-fetch-helpers\";\n\nconst gitHubFetch = authorization(fetch, { bearer: \"aToken\" });\n\n(async () => {\n  const response = await gitHubFetch(\"/meta\");\n})();\n```\n\n### `oauth2`\n\nA OAuth2 client which automatically refreshes tokens.\n\n#### Options Signature\n\n```typescript\ntype options = {\n  tokenRefreshed?: (options: {\n    accessToken?: string;\n    refreshToken: string;\n  }) => Promise<void>;\n  accessToken?: string;\n  authorizationHasExpired?: (response: Response) => Promise<boolean>;\n  refreshToken: string;\n  tokenEndpoint: string;\n  clientID: string;\n  clientSecret: string;\n  redirectURI?: string;\n  scope?: string;\n  refreshFetch?: typeof fetch;\n};\n```\n\n#### Options\n\n| Option                    | Notes                                                                                                                                        |\n| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |\n| `tokenRefreshed`          | A function called when a new token is generated. Useful if you wish to persist the latest valid tokens.                                      |\n| `accessToken`             | An Access Token.                                                                                                                             |\n| `authorizationHasExpired` | A function to evaluate if, given a Response, the Access Token is now invalid. Defaults to returning true if the Response status code is 401. |\n| `refreshToken`            | A valid Refresh Token.                                                                                                                       |\n| `tokenEndpoint`           | The URL of the authorization server which refreshes tokens.                                                                                  |\n| `clientID`                | The application client ID.                                                                                                                   |\n| `clientSecret`            | The application client secret.                                                                                                               |\n| `redirectURI`             | Although not in the specification, some authorization servers require a valid redirect URI when refreshing tokens.                           |\n| `scope`                   | The scope of the access token.                                                                                                               |\n| `refreshFetch`            | The fetch function to use when making calls to the authorization server. Defaults to the global fetch function.                              |\n\n#### Example Usage\n\n```typescript\nimport { oauth2 } from \"@glenstack/cf-workers-fetch-helpers\";\n\nconst gitHubFetch = oauth2(fetch, {\n  tokenRefreshed: async ({ accessToken, refreshToken }) => {\n    console.log(\"Tokens have been refreshed!\", { accessToken, refreshToken });\n  },\n  accessToken: \"anAccessToken\",\n  refreshToken: \"aRefreshToken\",\n  tokenEndpoint: \"https://github.com/login/oauth/access_token\",\n  clientID: \"anID\",\n  clientSecret: \"aSecret\",\n});\n\n(async () => {\n  const response = await gitHubFetch(\"https://api.github.com/meta\");\n})();\n```\n","readmeFilename":"README.md"}