{"id":25827184,"url":"https://github.com/superalibi/fetcher","last_synced_at":"2025-02-28T15:54:02.635Z","repository":{"id":247926258,"uuid":"823933030","full_name":"superAlibi/fetcher","owner":"superAlibi","description":"A simple encapsulation of a web API: fetch","archived":false,"fork":false,"pushed_at":"2024-09-17T15:23:58.000Z","size":32,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-18T18:30:19.442Z","etag":null,"topics":["fetch","httptools","xhr"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/superAlibi.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":"2024-07-04T03:24:33.000Z","updated_at":"2024-09-17T15:24:01.000Z","dependencies_parsed_at":"2024-09-17T18:21:01.105Z","dependency_job_id":null,"html_url":"https://github.com/superAlibi/fetcher","commit_stats":null,"previous_names":["superalibi/fetcher"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superAlibi%2Ffetcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superAlibi%2Ffetcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superAlibi%2Ffetcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superAlibi%2Ffetcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/superAlibi","download_url":"https://codeload.github.com/superAlibi/fetcher/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241176630,"owners_count":19922732,"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":["fetch","httptools","xhr"],"created_at":"2025-02-28T15:54:01.754Z","updated_at":"2025-02-28T15:54:02.628Z","avatar_url":"https://github.com/superAlibi.png","language":"TypeScript","readme":"# web api fetch 封装\n\n## 关于本包\n\n内部使用webapi fetch进行封装,参考了axios\n\n\n\n## 动机\n### 其一\n我不喜欢axios使用xhr,我更喜欢web api的标准,\n### 其二\n而不想关心内部实现,标准原生的就很好.不想关心什么兼容性\n\n### 其三\naxios有个比较讨厌的点,在拦截器中无法拦截处理非200的http状态码\n\n常见的http返回400系列或者500系列错误\n\n但是我希望拦截器可以拦截到非200的状态码,即使是403错误,服务器也有可能返回了正确的提示\n\n例如,服务器方返回http状态码:503,同时返回业务消息体\n```json\n{\n    \"description\": \"服务器升级维护,预计需要10分钟,请稍后重试\",\n    \"message\": \"服务器维护中\"\n}\n```\n以上消息,本来在拦截器都可以处理的问题,\n\n但是axios不经过拦截器,而是跳过了拦截器,直接返回到业务层报错,\n\n我tm...\n\n由此,决定自己封装一个fetch\n\n## 使用\n\n1. 基本使用\n\n```typescript\nimport {createFetcher} from 'jsr:@advance/fetcher'\nconst fetcher = createFetcher({\n    baseUrl: 'http://domain.exmaple',\n    headers: {\n        'Content-Type': 'application/json'\n    }\n})\nfetcher.get('/api/user/info').then(res =\u003e {\n    console.log(res)\n})\n```\n\n2. 拦截器\n\n可以直接在createFetcher中传入拦截器\n```typescript\nimport {createFetcher} from 'jsr:@advance/fetcher'\n\nconst fetcher = createFetcher({\n    baseUrl: 'http://domain.exmaple',\n    requestInterceptor:(reqinit:InterceptorConfig)=\u003e{\n        reqinit.headers['Authorization'] = 'Digest username=un'\n    }\n})\n\n\n```\n\n\n或者使用监听器的方式添加拦截器\n\n\n```typescript\nimport {createFetcher} from 'jsr:@advance/fetcher'\n\nconst fetcher = createFetcher({\n    baseUrl: 'http://domain.exmaple',\n})\n\nfetcher.addEventListener('request',(reqinit:InterceptorConfig)=\u003e{\n    reqinit.headers['Authorization'] = 'Digest username=un'\n})\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperalibi%2Ffetcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuperalibi%2Ffetcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperalibi%2Ffetcher/lists"}