{"id":20619673,"url":"https://github.com/mybuilder/fetch-oauth2","last_synced_at":"2025-04-15T11:55:18.652Z","repository":{"id":2849503,"uuid":"47626699","full_name":"mybuilder/fetch-oauth2","owner":"mybuilder","description":"Fetch JS wrapper to allow OAuth2 authenticated requests","archived":false,"fork":false,"pushed_at":"2024-12-10T04:22:53.000Z","size":61,"stargazers_count":3,"open_issues_count":46,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-15T11:55:14.928Z","etag":null,"topics":["javascript","oauth"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mybuilder.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-08T14:20:18.000Z","updated_at":"2021-09-25T18:29:19.000Z","dependencies_parsed_at":"2023-01-13T12:07:37.461Z","dependency_job_id":"e63e1547-9660-4b7d-8581-590022ee861b","html_url":"https://github.com/mybuilder/fetch-oauth2","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":0.05882352941176472,"last_synced_commit":"a4b8c1db5bf73a7aedceef46473c43b1e3717eef"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mybuilder%2Ffetch-oauth2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mybuilder%2Ffetch-oauth2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mybuilder%2Ffetch-oauth2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mybuilder%2Ffetch-oauth2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mybuilder","download_url":"https://codeload.github.com/mybuilder/fetch-oauth2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067775,"owners_count":21207395,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","oauth"],"created_at":"2024-11-16T12:12:15.175Z","updated_at":"2025-04-15T11:55:18.631Z","avatar_url":"https://github.com/mybuilder.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Fetch JS wrapper to allow OAuth2 authenticated requests\n=======================================================\n\nBased on a fetch.js, and allows creating a authenticated request to an api with oauth2 `access_token`. In addition to handling OAuth2 tokens it prevents race conditions between request when token needs to be generated or fetched.\n\n[![Build Status](https://travis-ci.org/mybuilder/fetch-oauth2.svg?branch=master)](https://travis-ci.org/mybuilder/fetch-oauth2)\n\n## Install from npm\n```\nnpm install fetch-oauth2 --save\n```\n\n### Token storage\n\nStorage takes 3 functions, and all of them are optional and must return a `Promise`, but you should pass in at least one of them it depends on the application.\n\n```javascript\nimport {tokenStorage} from 'fetch-oauth2';\n\nconst storage = tokenStorage({initialToken, fetchToken, generateToken});\n```\n\n* `fetchToken` is an equivalent to a http `GET`, and can `reject` the promise when no token is found\n* `generateToken` is an equivalent to a http `POST`, and should generate a new token\n\n### Request\n\n```javascript\nimport {fetchWithMiddleware, middleware} from 'fetch-oauth2';\n\nconst oauth2Fetch = fetchWithMiddleware(middleware.authorisationChallengeHandler(storage), middleware.setOAuth2Authorization(storage));\n\noauth2Fetch('http://httpbin.org/get')\n    .then(response =\u003e /**/)\n    .catch(error =\u003e /**/)\n```\n\n#### setOAuth2Authorization middleware\n\nHandles adding the `Authorization: Bearer abc123` header to the request.\n\n#### authorisationChallengeHandler middleware\n\nHandles responses with expired and invalid token's. When the response is 401, this hook will generate a new token and retry the request using the generated token.\n\nYou can optionally pass in a function that tests for the authentication challenge.\n\n```javascript\nauthorisationChallengeHandler(storage, (response) =\u003e Response.resolve(true))\n```\n\n### A simple url rewrite\n\nAssuming that the api uri's are relative then its easy to add a simple middleware to add the host.\n\n```javascript\nfunction addHostToUrl(next) {\n    return configPromise =\u003e {\n        return next(configPromise.then(config =\u003e config.updateUri(uri =\u003e 'http://httpbin.org' + uri)));\n    }\n}\n\nconst oauth2Fetch = fetchWithMiddleware(addHostToUrl, ...);\n\noauth2Fetch('/get')\n```\n\n## Contributing\n\nThe simplest why is to run test through npm.\n\n`npm test`\n\nIf you would like to use wallaby.js then you need to start the test server.\n\n`npm start`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmybuilder%2Ffetch-oauth2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmybuilder%2Ffetch-oauth2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmybuilder%2Ffetch-oauth2/lists"}