{"id":20046407,"url":"https://github.com/aneldev/dyna-fetch","last_synced_at":"2025-09-08T05:36:30.320Z","repository":{"id":26311511,"uuid":"105926837","full_name":"aneldev/dyna-fetch","owner":"aneldev","description":"Universal fetch","archived":false,"fork":false,"pushed_at":"2022-12-07T13:37:43.000Z","size":868,"stargazers_count":0,"open_issues_count":15,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T23:15:00.664Z","etag":null,"topics":["fetch","retry","timeout","universal"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/aneldev.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2017-10-05T18:41:52.000Z","updated_at":"2021-02-15T16:01:55.000Z","dependencies_parsed_at":"2022-08-28T20:50:10.165Z","dependency_job_id":null,"html_url":"https://github.com/aneldev/dyna-fetch","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aneldev%2Fdyna-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aneldev","download_url":"https://codeload.github.com/aneldev/dyna-fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241476435,"owners_count":19968916,"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","retry","timeout","universal"],"created_at":"2024-11-13T11:23:39.851Z","updated_at":"2025-03-02T07:48:51.187Z","avatar_url":"https://github.com/aneldev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About\n\n`dyna-fetch` wrapps the [axios](https://github.com/axios/axios) and adds some sugar on it like:\n\n- pre-flight to bypass CORS\n- timeout\n- cancel\n- retry\n- abort\n\nWritten in Typescript, it is universal and runs everywhere browser/nodeJs/.\n\n# Usage\n```\nimport {dynaFetch} from 'dyna-fetch';\n\nconst myRequest = dynaFetch({\n    url: 'https://example.com/api?client=3002',\n    requestConfig: {        // (optional)\n        data: {},           // (optional) Request body\n    },\n    preFlight: true,        // (optional) try to bypass CORS\n    timeout: 20000,         // (optional) wait for max 20 seconds\n    retryMaxTimes: 3,       // (optional) retry max 3 times\n    retryTimeout: 1000,     // (optional) wait for 1sec for each retry\n    retryRandomFactor: 1,   // (optional) timeout factor\n    onRetry: () =\u003e console.log('retrying...'), // (optional) \n});\n\nmyRequest\n    .then((response: AxiosResponse) =\u003e {\n        // ...\n    })\n    .catch((error: IError | AxiosError) =\u003e {\n        // ...\n    });\n\n// later you can abort it, rejects the promise\nmyRequest.abort();\n\n// later you can cancel it, rejects the promise\nmyRequest.cancel('Fetch is canceled');\n\n```\n\n# Arguments\n\n```\ninterface IDynaFetchConfig {\n  url: string;\n  requestConfig?: AxiosRequestConfig;         // help: https://github.com/axios/axios#axios-api\n  preFlight?: boolean;                        // default: false, skip CORS with pre-flight OPTIONS request (the server should support this)\n  retry?: (error: AxiosError) =\u003e boolean;     // default: () =\u003e true; Validate the error. Return true to retry or false to return the error.\n  cancelOnRetry?: boolean;                    // default: false; if true, in case of retry timeout the current request will be XHR canceled.\n  retryMaxTimes?: number;                     // default: 0\n  retryTimeout?: number;                      // default: 0, in ms\n  retryRandomFactor?: number;                 // default is 1, finalTimeout = retryTimeout * random(0, timeoutRandomFactor)\n  onRetry?: () =\u003e void;                       // callback called on each retry\n}\n```\n\n# dynaFetch API\n\nThe `dynaFetch()` returns a promise object together with a small API object.\n\nTS the type of th result is `IDynaFetchHandler`.\n\n```\n{\n  abort: () =\u003e void;\n  cancel: (message?: string) =\u003e void;\n}\n```\n\n## Abort\n\nDuring the execution of the request, you can abort the request.\n\n**Note**: The request is not a cancelled! The abort just swallows the response and rejects the promise.\n\n## Cancel\n\nCancel the XHR request.\n\nCancel rejects the promise.\n\n# Errors\n\nIn case of a rejected request, if the error has to do with features of the `dyna-fetch`, the `IError` will be returned.\n\nOtherwise, the `axios`'s error will be returned.\n\n## IError\n\n```\ninterface IError {\n  code?: number | string; // code\n  section?: string;       // section of the application (dynaFetch in this case)\n  message?: string;       // a meaningful dev/debug message\n  error?: any;            // nested error\n}\n```\n\n## error.code 5017\n\nIn case of timeout.\n\n## error.code 5019\n\nIn case of `abort()` call.\n\n# Change log\n\n## v1.0.0\n\n- Working with [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch)\n- Retry feature\n\n## v2.0.0\n\n- Working with [axios](https://github.com/axios/axios)\n- Pre-flight requests with CORS\n- Retry with random factor\n\n## v3.0.0\n\n- New prop `retry?: (error: AxiosError) =\u003e boolean;`. Validate the error to retry or not.\n- The returned error is not an IError that wraps the AxiosError but the AxiosError directly. Still the IError returned on 5017 \u0026 5019.\n\n## v3.1.0\n\n- Cancel request on demand\n- Cancel request on retry\n\n## v3.1.2\n\n- Cancel request on retry only then the `cancelOnRetry: true` config prop\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faneldev%2Fdyna-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faneldev%2Fdyna-fetch/lists"}