{"id":17869246,"url":"https://github.com/itpixelz/ngx-http-retry","last_synced_at":"2025-03-21T11:32:20.012Z","repository":{"id":258208996,"uuid":"874473614","full_name":"itpixelz/ngx-http-retry","owner":"itpixelz","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-17T23:56:10.000Z","size":122,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-20T10:52:14.811Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/itpixelz.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-10-17T22:25:38.000Z","updated_at":"2024-10-18T18:01:15.000Z","dependencies_parsed_at":"2024-10-18T07:34:12.419Z","dependency_job_id":null,"html_url":"https://github.com/itpixelz/ngx-http-retry","commit_stats":null,"previous_names":["itpixelz/ngx-http-retry"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itpixelz%2Fngx-http-retry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itpixelz%2Fngx-http-retry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itpixelz%2Fngx-http-retry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itpixelz%2Fngx-http-retry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/itpixelz","download_url":"https://codeload.github.com/itpixelz/ngx-http-retry/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221814843,"owners_count":16885071,"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":[],"created_at":"2024-10-28T10:01:13.726Z","updated_at":"2024-10-28T10:02:34.358Z","avatar_url":"https://github.com/itpixelz.png","language":"TypeScript","funding_links":[],"categories":["Architecture and Advanced Topics"],"sub_categories":["HTTP"],"readme":"# NgxHttpRetryService\n\n[![npm version](https://img.shields.io/npm/v/ngx-http-retry.svg)](https://www.npmjs.com/package/ngx-http-retry)\n[![npm downloads](https://img.shields.io/npm/dm/ngx-http-retry.svg)](https://www.npmjs.com/package/ngx-http-retry)\n[![license](https://img.shields.io/npm/l/ngx-http-retry.svg)](LICENSE)\n\nAn Angular service that provides HTTP methods (`GET`, `POST`, `PUT`, `DELETE`) with built-in retry logic using RxJS's `retry` operator.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Importing the Service](#importing-the-service)\n  - [Available Methods](#available-methods)\n    - [`get`](#get)\n    - [`post`](#post)\n    - [`put`](#put)\n    - [`delete`](#delete)\n  - [Customizing Retries](#customizing-retries)\n- [Retry Strategy](#retry-strategy)\n  - [Non-Retryable Errors](#non-retryable-errors)\n  - [Exponential Backoff](#exponential-backoff)\n  - [Custom Retry Conditions](#custom-retry-conditions)\n- [Error Handling](#error-handling)\n- [Extending the Service](#extending-the-service)\n- [FAQs](#faqs)\n- [Contributing](#contributing)\n- [License](#license)\n- [Author](#author)\n- [Changelog](#changelog)\n\n## Installation\n\nInstall the package via npm:\n\n```bash\nnpm install ngx-http-retry\n```\n\n## Usage\n\n### Importing the Service\n\nImport `NgxHttpRetryService` into your Angular component or service:\n\n```typescript\nimport { NgxHttpRetryService } from 'ngx-http-retry';\n\n@Component({\n  // ...\n})\nexport class YourComponent {\n  constructor(private httpRetryService: NgxHttpRetryService) {}\n}\n```\n\n### Available Methods\n\nThe service provides the following methods, which mirror Angular's `HttpClient` methods but include retry logic:\n\n- `get\u003cT\u003e(url: string, options?: any, retries?: number, delayMs?: number): Observable\u003cT\u003e`\n- `post\u003cT\u003e(url: string, body: any, options?: any, retries?: number, delayMs?: number): Observable\u003cT\u003e`\n- `put\u003cT\u003e(url: string, body: any, options?: any, retries?: number, delayMs?: number): Observable\u003cT\u003e`\n- `delete\u003cT\u003e(url: string, options?: any, retries?: number, delayMs?: number): Observable\u003cT\u003e`\n\n#### `get`\n\nPerforms a GET request with retry logic.\n\n**Parameters:**\n\n- `url: string` - The endpoint URL.\n- `options?: any` - Optional HTTP options.\n- `retries?: number` - Number of retry attempts (default: `3`).\n- `delayMs?: number` - Delay between retries in milliseconds (default: `1000`).\n\n**Returns:**\n\n- `Observable\u003cT\u003e` - An observable of the response.\n\n**Example:**\n\n```typescript\nthis.httpRetryService.get\u003cUser[]\u003e('https://api.example.com/users')\n  .subscribe(\n    users =\u003e console.log(users),\n    error =\u003e console.error('Request failed', error)\n  );\n```\n\n#### `post`\n\nPerforms a POST request with retry logic.\n\n**Parameters:**\n\n- `url: string` - The endpoint URL.\n- `body: any` - The payload to send.\n- `options?: any` - Optional HTTP options.\n- `retries?: number` - Number of retry attempts (default: `3`).\n- `delayMs?: number` - Delay between retries in milliseconds (default: `1000`).\n\n**Returns:**\n\n- `Observable\u003cT\u003e` - An observable of the response.\n\n**Example:**\n\n```typescript\nconst payload = { name: 'John Doe', email: 'john@example.com' };\nthis.httpRetryService.post\u003cUser\u003e('https://api.example.com/users', payload)\n  .subscribe(\n    user =\u003e console.log('User created:', user),\n    error =\u003e console.error('Request failed', error)\n  );\n```\n\n#### `put`\n\nPerforms a PUT request with retry logic.\n\n**Parameters:**\n\n- `url: string` - The endpoint URL.\n- `body: any` - The payload to update.\n- `options?: any` - Optional HTTP options.\n- `retries?: number` - Number of retry attempts (default: `3`).\n- `delayMs?: number` - Delay between retries in milliseconds (default: `1000`).\n\n**Returns:**\n\n- `Observable\u003cT\u003e` - An observable of the response.\n\n**Example:**\n\n```typescript\nconst updatedData = { name: 'Jane Doe' };\nthis.httpRetryService.put\u003cUser\u003e('https://api.example.com/users/1', updatedData)\n  .subscribe(\n    user =\u003e console.log('User updated:', user),\n    error =\u003e console.error('Request failed', error)\n  );\n```\n\n#### `delete`\n\nPerforms a DELETE request with retry logic.\n\n**Parameters:**\n\n- `url: string` - The endpoint URL.\n- `options?: any` - Optional HTTP options.\n- `retries?: number` - Number of retry attempts (default: `3`).\n- `delayMs?: number` - Delay between retries in milliseconds (default: `1000`).\n\n**Returns:**\n\n- `Observable\u003cT\u003e` - An observable of the response.\n\n**Example:**\n\n```typescript\nthis.httpRetryService.delete\u003cvoid\u003e('https://api.example.com/users/1')\n  .subscribe(\n    () =\u003e console.log('User deleted'),\n    error =\u003e console.error('Request failed', error)\n  );\n```\n\n### Customizing Retries\n\nCustomize the number of retries and delay between retries:\n\n```typescript\nthis.httpRetryService.get\u003cUser[]\u003e('https://api.example.com/users', {}, 5, 2000)\n  .subscribe(\n    users =\u003e console.log(users),\n    error =\u003e console.error('Request failed after retries', error)\n  );\n```\n\n- **Retries:** `5`\n- **Delay:** `2000` milliseconds\n\n## Retry Strategy\n\nThe retry logic uses RxJS's `retry` operator with a custom strategy.\n\n```typescript\nprivate retryStrategy\u003cT\u003e(retries: number, delayMs: number) {\n  return retry\u003cT\u003e({\n    count: retries,\n    delay: (error: HttpErrorResponse, retryCount: number) =\u003e {\n      if (this.isNonRetryableError(error)) {\n        throw error;\n      } else {\n        console.warn(`Retry attempt #${retryCount}`);\n        return timer(delayMs);\n      }\n    },\n  });\n}\n```\n\n### Non-Retryable Errors\n\nBy default, the following HTTP errors are **not retried**:\n\n- Client-side errors (`4xx`), except for `408 Request Timeout`.\n\n```typescript\nprivate isNonRetryableError(error: HttpErrorResponse): boolean {\n  return error.status \u003e= 400 \u0026\u0026 error.status \u003c 500 \u0026\u0026 error.status !== 408;\n}\n```\n\n### Exponential Backoff\n\nTo implement exponential backoff, modify the `retryStrategy`:\n\n```typescript\nprivate retryStrategy\u003cT\u003e(retries: number, delayMs: number) {\n  return retry\u003cT\u003e({\n    count: retries,\n    delay: (error: HttpErrorResponse, retryCount: number) =\u003e {\n      if (this.isNonRetryableError(error)) {\n        throw error;\n      } else {\n        const backoffDelay = delayMs * Math.pow(2, retryCount - 1);\n        console.warn(`Retry attempt #${retryCount} after ${backoffDelay}ms`);\n        return timer(backoffDelay);\n      }\n    },\n  });\n}\n```\n\n### Custom Retry Conditions\n\nCustomize retry conditions based on specific error codes or responses:\n\n```typescript\nprivate retryStrategy\u003cT\u003e(retries: number, delayMs: number) {\n  return retry\u003cT\u003e({\n    count: retries,\n    delay: (error: HttpErrorResponse, retryCount: number) =\u003e {\n      if (this.isNonRetryableError(error) || retryCount \u003e retries) {\n        throw error;\n      } else if (error.status === 500) {\n        // Immediate retry for server errors\n        return timer(0);\n      } else {\n        return timer(delayMs);\n      }\n    },\n  });\n}\n```\n\n## Error Handling\n\nHandle errors where you consume the service:\n\n```typescript\nthis.httpRetryService.get\u003cUser[]\u003e('https://api.example.com/users')\n  .subscribe(\n    users =\u003e {\n      // Successful response\n    },\n    error =\u003e {\n      // Error after all retries\n      console.error('Request failed', error);\n    }\n  );\n```\n\n## Extending the Service\n\nFor extensive customization, extend `NgxHttpRetryService`:\n\n```typescript\n@Injectable({\n  providedIn: 'root',\n})\nexport class CustomHttpRetryService extends NgxHttpRetryService {\n  protected isNonRetryableError(error: HttpErrorResponse): boolean {\n    // Custom logic (e.g., don't retry on 404)\n    return error.status === 404;\n  }\n}\n```\n\nUse `CustomHttpRetryService` in your components:\n\n```typescript\nconstructor(private httpRetryService: CustomHttpRetryService) {}\n```\n\n## FAQs\n\n### Does this service support Angular Universal (Server-Side Rendering)?\n\nYes, it is compatible with Angular Universal.\n\n### How is this different from using `HttpClient` directly?\n\n`NgxHttpRetryService` wraps `HttpClient` methods and adds configurable retry logic.\n\n### Can I use this service for file uploads or downloads?\n\nYes, any HTTP request supported by `HttpClient` can be used.\n\n### Does it support HTTP interceptors?\n\nYes, any interceptors configured with `HttpClient` are applied.\n\n## Contributing\n\nContributions are welcome! Please submit issues or pull requests on [GitHub](https://github.com/itpixelz/ngx-http-retry).\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n## Author\n\n[Umar Draz](https://github.com/itpixelz)\n\n## Changelog\n\n### v1.0.0\n\n- Initial release with GET, POST, PUT, DELETE methods and retry logic.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitpixelz%2Fngx-http-retry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fitpixelz%2Fngx-http-retry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitpixelz%2Fngx-http-retry/lists"}