{"id":20420339,"url":"https://github.com/deepraining/see-fetch","last_synced_at":"2025-03-05T04:26:58.278Z","repository":{"id":65493720,"uuid":"134049391","full_name":"deepraining/see-fetch","owner":"deepraining","description":"一个 window.fetch 封装器, 能够对响应 Json 对象进行重构, 预处理请求对象, 后置处理响应数据等. A window.fetch wrapper, with response refactoring, pre handling, post handling, etc.","archived":false,"fork":false,"pushed_at":"2019-11-25T03:39:12.000Z","size":64,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-16T20:01:49.551Z","etag":null,"topics":["fetch","handle","json","refactor","response","wrapper"],"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/deepraining.png","metadata":{"files":{"readme":"README.en.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-19T09:45:59.000Z","updated_at":"2020-03-09T03:53:28.000Z","dependencies_parsed_at":"2023-01-25T21:15:17.278Z","dependency_job_id":null,"html_url":"https://github.com/deepraining/see-fetch","commit_stats":null,"previous_names":["senntyou/see-fetch"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fsee-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fsee-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fsee-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deepraining%2Fsee-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deepraining","download_url":"https://codeload.github.com/deepraining/see-fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241768287,"owners_count":20017116,"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","handle","json","refactor","response","wrapper"],"created_at":"2024-11-15T06:42:31.002Z","updated_at":"2025-03-05T04:26:58.256Z","avatar_url":"https://github.com/deepraining.png","language":"JavaScript","readme":"# see-fetch\n\n[中文文档](./README.md)\n\nA `window.fetch` wrapper, with response refactoring, pre handling, post handling, etc.\n\n## requirements\n\n- [json-refactor](https://github.com/senntyou/json-refactor)\n\n## related\n\n- [see-ajax](https://github.com/senntyou/see-ajax)\n\n## note\n\nOnly for `json` response.\n\n## quick start\n\n```\nnpm install see-fetch --save\n```\n\n```\nimport seeFetch from 'see-fetch';\n\n// configure application\nseeFetch.config(name, {\n  method,\n  stringify,\n  settings,\n  url,\n  req,\n  refactor,\n  pre,\n  post,\n  implement,\n});\n\n// make a request\nseeFetch(name, params).then(result =\u003e { ... });\n```\n\n## config options\n\n### `method`: which http method to use\n\n- `type`: `string`\n- `default`: `get`\n\n```\n'post/put/delete'\n```\n\n### `stringify`: whether to stringify request params\n\n- `type`: `bool`\n- `default`: `false`\n\nIf `true`, the server will receive string, but not `key-value` pairs.\n\nIf `GET` method, request params will not stringify at any time.\n\n### `settings`: extra fetch options\n\n- `type`: `map`\n- `default`: `{}`\n\n### `url`: url to request\n\n- `type`: `string`\n- `default`: empty string\n\n### `req/requestKeys`: keys mapping of request params\n\n- `type`: `map`\n- `default`: `{}`\n\n```\n{sourceKey: 'newKey'}\n```\n\n### `refactor/responseRefactor`: rules to refactor response using [json-refactor](https://github.com/senntyou/json-refactor)\n\n- `type`: `map`\n- `default`: `{}`\n\n```\nrefactor: rules\n```\n\n- `rules`: see [json-refactor](https://github.com/senntyou/json-refactor)\n\n### `pre/preHandle`: more handling to request params\n\n- `type`: `function`\n\n```\n(params, name) =\u003e {... modify params, or return a new params ...}\n```\n\n### `post/postHandle`: more handling to response data\n\n- `type`: `function`\n\n```\n(result, params, name) =\u003e {... modify result, or return a new result }\n```\n\n### `implement`: custom implementing instead of `fetch`\n\n- `type`: `function`\n\n```\n(cb, params) =\u003e { ... cb(result), or return a Promise }\n```\n\nSometimes, you have to not use `fetch`, but other ways, like html templates.\n\n## api\n\n### `seeFetch.config`: configure application\n\n```\n// one\nseeFetch.config(name, options);\n\n// multiple\nseeFetch.config({\n  name1: options1,\n  name2: options2,\n  ...\n});\n```\n\n### `seeFetch.setEnv`: set current environment(index to get config options)\n\n```\nseeFetch.setEnv(0/1/2/3);\n```\n\nIf you need multiple environments supports, you can configure all config options by array, and then set a env.\n\nIf you don't set an environment, 0 will be the default.\n\n```\nseeFetch.config(name, {\n  method: [method1, method2, ...],\n  stringify: [stringify1, stringify2, ...],\n  settings: [settings1, settings2, ...],\n  url: [url1, url2, ...],\n  req: [req1, req2, ...],\n  refactor: [refactor1, refactor2, ...],\n  pre: [pre1, pre2, ...],\n  post: [post1, post2, ...],\n  implement: [implement1, implement2, ...],\n});\n\nseeFetch.setEnv(0); // method1, stringify1, url1, ...\nseeFetch.setEnv(1); // method2, stringify2, url2, ...\n```\n\n### `seeFetch.getEnv`: get current environment\n\n```\nconst env = seeFetch.getEnv(); // 0/1/2/3\n```\n\n### `seeFetch`: make a request\n\n```\nseeFetch(name, params).then(result =\u003e { ... });\n```\n\n- `name`: defined request name\n  - `note`: `common` is a special request name, and it will apply to all requests\n- `params`: request params\n  - `type`: `map`\n  - `example`: `{a: 1, b: '2'}`\n- `result`: handled response data. But if response's status code is `3XX, 4XX, 5XX`, `result` will be like: `{error: true, response: Response}`\n  - `error`: mark response having an error, and you can customize it by `seeFetch.set({errorField: 'yourErrorField'})`\n  - `response`: original [Response Object](https://developer.mozilla.org/zh-CN/docs/Web/API/Response)\n\n### `seeFetch.set`: set custom config\n\n```\nseeFetch.set({\n  errorField: 'error',\n  debug: !0,\n  disableCache: !0,\n  disableCacheField: '_',\n});\n```\n\n- `errorField`: `type: string` `default: error` configure your own error field\n- `debug`: `type: bool` `default: true` whether in debug mode\n- `disableCache`: `type: bool` `default: true` disable request cache for `GET, HEAD` methods\n- `disableCacheField`: `type: string` `default: _` field name for appending timestamp to original url when `disableCache` is `true`\n\n## handlers sequences while processing\n\n1. `method`: check which http method to use, default is `GET`\n2. `stringify`: check whether to stringify request params\n3. `settings`: check extra fetch settings\n4. `url`: get request url\n5. `req`: get real request params\n6. `pre`: more handling before send a request\n   1. `common`: common handling, if have\n   2. `name`: named handling\n7. `implement`: if have, `see-fetch` will not send a `fetch`\n8. `refactor`: refactoring response data\n   1. `common`: common handling, if have\n   2. `name`: named handling\n9. `post`: more handling after refactoring response data\n   1. `common`: common handling, if have\n   2. `name`: named handling\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepraining%2Fsee-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeepraining%2Fsee-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeepraining%2Fsee-fetch/lists"}