{"id":27200892,"url":"https://github.com/koloer-blus/etmo-utils","last_synced_at":"2025-04-09T21:45:44.586Z","repository":{"id":41055338,"uuid":"508142357","full_name":"koloer-blus/etmo-utils","owner":"koloer-blus","description":"✔Etmo is a collection of front-end general functional solutions！","archived":false,"fork":false,"pushed_at":"2022-07-10T02:34:25.000Z","size":92,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-25T02:41:36.924Z","etag":null,"topics":["axios","etmo","handler","http","javascript","localstorage","request","sessionstorage","storage","typescipt"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/etmo-utils","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/koloer-blus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-28T03:34:30.000Z","updated_at":"2023-03-07T10:53:44.000Z","dependencies_parsed_at":"2022-09-20T22:00:17.416Z","dependency_job_id":null,"html_url":"https://github.com/koloer-blus/etmo-utils","commit_stats":null,"previous_names":["baiziyu-fe/etmo-utils"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koloer-blus%2Fetmo-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koloer-blus%2Fetmo-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koloer-blus%2Fetmo-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koloer-blus%2Fetmo-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koloer-blus","download_url":"https://codeload.github.com/koloer-blus/etmo-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119439,"owners_count":21050754,"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":["axios","etmo","handler","http","javascript","localstorage","request","sessionstorage","storage","typescipt"],"created_at":"2025-04-09T21:45:43.765Z","updated_at":"2025-04-09T21:45:44.569Z","avatar_url":"https://github.com/koloer-blus.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# etmo-utils\n\n✔`etmo-utils` is a collection of front-end universal functional solutions!\n\n[中文文档](./README_ZH.md)\n\n## Install\n\n```shell\nnpm install etmo-utils\n\nyarn add etmo-utils\n```\n\n## main module\n\n### Request\n\n`Request` is mainly the `Request` class encapsulated by `Axios`.\n\n`Request` class parameters not only support the configuration parameters that come with `Axios`, but also support incoming request interception function (`requestCallback`), response interception function (`responseCallback`) and `GET` request parameter processing function (`queryParamsCallback` ).\n\n#### Initialize\n\n1. Use the default configuration directly\n\nThis will execute `const request = new Request({})` by default:\n\n````js\nimport {request} from 'etmo-utils';\nrequest.get('http://test/api');\n````\n\n2. Requires custom configuration\n\nHere you need to use the `Request` class:\n\n````js\nimport {Request} from 'etmo-utils';\nconst request = new Request({\n  requestCallback: ...,\n  responseCallback: ...,\n  queryParamsCallback: ...,\n  baseUrl: ...,\n  ...\n});\n````\n\n#### Interceptor\n\nThe incoming interceptor function `requestCallback/responseCallback` accepts a `request/response instance` as a parameter, and intercepts and handles errors by using the `use` method of the parameter.\n\nFor example, the following is the writing method of the request interceptor:\n\n````js\nconst requestCallback = (request) =\u003e {\n      request.use(\n        function (config: AxiosRequestConfig) {\n          // Do something before request is sent\n          return config;\n        },\n        function (error: AxiosError) {\n          // Do something with request error\n          return Promise.reject(error);\n        },\n      );\n    };\n````\n\n\n#### request method\n\nCurrently only `get`, `post`, `put`, `delete` are supported:\n\n- `get(url[, params[, config]])`\n- `post(url[, params[, config]])`\n- `put(url[, params[, config]])`\n- `delete(url[, params[, config]])`\n\n#### cancel request\n\nCurrently `etmo-utils` supports two methods of canceling requests that `Axios` natively supports:\n\n- `controller.abort`\n- `source.cancelToken`\n\nThese two ways are simpler in `etmo-utils`:\n\n````js\nconst req = new Request({});\n// source is used to cancel the request by cancelToken\nconst source = req.cancel()\n//abort is mainly applicable to the cancellation of fetch requests, mainly using AbortController\nconst controller = req.abort();\n````\n\n### Storage\n\n`Storage` mainly encapsulates operations related to front-end browser storage `sessionStorage` and `localStorage`, and supports setting prefix and expiration time.\n\nIn `Storage`, the content related to `Ltg` represents `localStorage`, and the content related to `Stg` represents `sessionStorage`.\n\n#### start\n\n1. Use `storage` directly\n\n````js\nimport {storage} from 'etmo-utils';\nstorage.get('test');\n````\n\n2. `new Storage()` custom\n\n`Storage` supports passing a parameter as a prefix for all storage information `key`:\n\n````js\nimport {Storage} from 'etmo-utils';\nexport const storage = new Storage('prefix');\n````\n\n#### store/delete/retrieve\n\n1. `localStorage`\n\n- `setLtg(key, value[, expired])`\n- `delLtg(key)`\n- `getLtg(key)`\n\n\n2. `sessionStorage`\n\n- `setStg(key, value[, expired])`\n- `delStg(key)`\n- `getStg(key)`\n\n#### Get browser `Storage` storage space (available in some browsers)\n\n- `getStorageUsedSize`\n\nReturns a result with a `Promise`\n\n#### Does it expire?\n\nUsed to determine whether the set value has expired:\n\n- `isOutPeriod({key, value, expired})`\n\n### Handler\n\nGeneric error handling based on `try...catch...`:\n\n#### how to use\n\nFirst, you need to pass in the handler functions in the `success`, `error`, and `finally` states:\n\n- `successHandler: (value) =\u003e void`\n- `errorHandler: (error) =\u003e void`\n- `finalHandler: () =\u003e void`\n\nwhere `errorHandler` and `finalHandler` are optional parameters.\n\n````js\nimport {Handler} from 'etmo-utils';\n\nconst temp = new Handler((v) =\u003e console.info(v));\n\ntemp.use(() =\u003e {\n  ....\n})\n\n````\n\n## Precautions\n\n- When the `Request` class cancels the request, only the `Fetch` request can use the `AbortController`\n- `Storage` will not be actively deleted after the value expires, and will only be deleted or other operations when the value is retrieved next time","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoloer-blus%2Fetmo-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoloer-blus%2Fetmo-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoloer-blus%2Fetmo-utils/lists"}