{"id":23767359,"url":"https://github.com/ayu-sh-kr/dota-rest","last_synced_at":"2026-02-17T02:35:51.036Z","repository":{"id":270134345,"uuid":"909427101","full_name":"ayu-sh-kr/dota-rest","owner":"ayu-sh-kr","description":"Fluent API based RestClient to perform http - requests using the fetch api","archived":false,"fork":false,"pushed_at":"2025-04-13T11:51:20.000Z","size":128,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-01T21:52:00.885Z","etag":null,"topics":["fetch","fetch-api","http","http-client","rest-client"],"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/ayu-sh-kr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2024-12-28T17:09:09.000Z","updated_at":"2025-04-13T11:50:31.000Z","dependencies_parsed_at":"2025-04-12T03:00:44.513Z","dependency_job_id":null,"html_url":"https://github.com/ayu-sh-kr/dota-rest","commit_stats":null,"previous_names":["ayu-sh-kr/dota-rest"],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/ayu-sh-kr/dota-rest","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayu-sh-kr%2Fdota-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayu-sh-kr%2Fdota-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayu-sh-kr%2Fdota-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayu-sh-kr%2Fdota-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ayu-sh-kr","download_url":"https://codeload.github.com/ayu-sh-kr/dota-rest/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ayu-sh-kr%2Fdota-rest/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29531028,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T00:57:22.232Z","status":"online","status_checked_at":"2026-02-17T02:00:08.105Z","response_time":100,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","fetch-api","http","http-client","rest-client"],"created_at":"2025-01-01T00:37:12.387Z","updated_at":"2026-02-17T02:35:50.993Z","avatar_url":"https://github.com/ayu-sh-kr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Project Documentation\n\n## Overview\n\nThis project provides a `RestClient` class for creating and configuring HTTP clients to make RESTful API requests. It includes methods for various HTTP methods such as GET, POST, PUT, PATCH, and DELETE.\n\n## Installation\n\nTo install the project dependencies, run:\n\n```bash\nnpm install @ayu-sh-kr/dota-rest\n```\n\n## Usage\n\n### Initializing the RestClient\n\nTo create and configure an instance of `RestClient`, use the `RestClientBuilder`:\n\n```typescript\nimport {RestClient} from \"@ayu-sh-kr/dota-rest\";\n\nconst client = RestClient.builder()\n    .baseUrl('https://api.example.com')\n    .defaultHeaders({'Authorization': 'Bearer token'})\n    .build();\n```\n\n\u003e After version \u003e= 1.1.0 use the `RestClient.builder()` to create the instance of `RestClient` instead of using the create method.\n\n### Performing Requests\n\n#### GET Request\n\nTo perform a GET request:\n\n```typescript\nconst response = await client.get\u003cUser\u003e()\n    .uri('/users/1')\n    .retrieve()\n    .toEntity();\n\nconsole.log(response.data);\n```\n\n#### POST Request\n\nTo perform a POST request:\n\n```typescript\nconst response = await client.post\u003cUser\u003e()\n    .uri('/users')\n    .body({ name: 'John Doe', email: 'john.doe@example.com' })\n    .retrieve()\n    .toEntity();\n\nconsole.log(response.data);\n```\n\n#### PUT Request\n\nTo perform a PUT request:\n\n```typescript\nconst response = await client.put\u003cUser\u003e()\n    .uri('/users/1')\n    .body({ name: 'John Doe', email: 'john.doe@example.com' })\n    .retrieve()\n    .toEntity();\n\nconsole.log(response.data);\n```\n\n#### PATCH Request\n\nTo perform a PATCH request:\n\n```typescript\nconst response = await client.patch\u003cUser\u003e()\n    .uri('/users/1')\n    .body({ email: 'john.newemail@example.com' })\n    .retrieve()\n    .toEntity();\n\nconsole.log(response.data);\n```\n\n#### DELETE Request\n\nTo perform a DELETE request:\n\n```typescript\nconst response = await client.delete\u003cvoid\u003e()\n    .uri('/users/1')\n    .retrieve()\n    .toVoid();\n\nconsole.log('User deleted');\n```\n\n#### Resolving request to response\nTo get the `Response` object of the fetch request and taking action as per your requirement.\n\n```typescript\nconst response = await client.get\u003cUser\u003e()\n    .url('/users/1')\n    .retreive()\n    .toResponse()\n\nconsole.log(response.status)\nconst data = await response.json();\nconsole.log(data)\n```\n\n#### Creating `RestClient` with timeout\nBy default the client have a timout of 10s but it can be changed during the build of `RestClient` or \nduring making the request.\n\nAdding timeout during client buildup.\n```typescript\nimport {RestClient} from \"@ayu-sh-kr/rest\";\n\nconst restClient = RestClient.builder()\n    .baseUrl('http://localhost:8080')\n    .defaultHeaders({'Authorization': 'Bearer kh.....'})\n    .timeout(5000)\n    .build();\n```\n\nAdding timeout during client setup.\n```typescript\nconst response = await client.get\u003cUser\u003e()\n    .url('/users/1')\n    .timeout(5000)\n    .retreive()\n    .toResponse()\n```\n\n#### Response Handler\n`RestClient` provide ability to set response handler which can be used to process response before resolving to\n`toEntity`, `toVoid` or `toReponse`. Response handler is function with one parameter `response` object should be \nused to check the response for required `status` and prepare the error if the result is not favourable.\n\nCurrent implementation of response handler return void to keep it simple and provide bare minimum feature to user\nin order to reduce duplicate code error handling where same logic is implemented across the code.\n\n##### Request processing without response handling\nHere is an example of how to perform request without using a response handler:\n```typescript\nimport { RestClient } from \"@ayu-sh-kr/dota-rest\";\n\n// Create a RestClient instance without a response handler\nconst client = RestClient.builder()\n    .baseUrl('https://api.example.com')\n    .defaultHeaders({ 'Authorization': 'Bearer token' })\n    .build();\n\n// Perform a GET request without the response handler\nconst response = await client.get\u003cUser\u003e()\n    .uri('/users/1')\n    .retrieve()\n    .toEntity();\n\nif (response.status !== 200) {\n    throw new Error(`Request failed with status code ${response.status}`);\n}\n\nconsole.log(response.data);\n```\n\n##### Request processing with a response handler\nHere is an example of how to use a response handler to process the response:\n```typescript\nimport { RestClient } from \"@ayu-sh-kr/dota-rest\";\n\n// Define the response handler\nconst responseHandler = (response: Response) =\u003e {\n    if (response.status !== 200) {\n        throw new Error(`Request failed with status code ${response.status}`);\n    }\n};\n\n// Create a RestClient instance with the response handler\nconst client = RestClient.builder()\n    .baseUrl('https://api.example.com')\n    .defaultHeaders({ 'Authorization': 'Bearer token' })\n    .handler(responseHandler)\n    .build();\n\n// Perform a GET request with the response handler\nconst response = await client.get\u003cUser\u003e()\n    .uri('/users/1')\n    .retrieve()\n    .toEntity();\n\nconsole.log(response.data);\n```\n\nUsing a response handler simplifies the error handling logic by centralizing the response processing in a single function, reducing duplicate code and making the request handling more consistent.\n\n#### ResponseConverter\nUse a converter to process the json data and its mapping to the required object. The converter is a function that takes the json object and returns the data in the required format.\n\nHere is an example of using a converter with the `RestClient` for a GET request, along with a dummy type:\n\nFirst, define the dummy type:\n\n```typescript\ninterface User {\n    id: number;\n    name: string;\n    email: string;\n}\n```\n\nNext, create a converter function that processes the JSON data and maps it to the `User` type:\n\n```typescript\nconst userConverter = (data: any): User =\u003e {\n    return {\n        id: data.id,\n        name: data.name,\n        email: data.email\n    };\n};\n```\n\nNow, use the `RestClient` to perform a GET request with the converter:\n\n```typescript\nimport { RestClient } from \"@ayu-sh-kr/dota-rest\";\n\n// Create a RestClient instance\nconst client = RestClient.builder()\n    .baseUrl('https://api.example.com')\n    .defaultHeaders({ 'Authorization': 'Bearer token' })\n    .build();\n\n// Perform a GET request with the converter\nconst response = await client.get\u003cUser\u003e()\n    .uri('/users/1')\n    .converter(userConverter)\n    .retrieve()\n    .toEntity();\n\nconsole.log(response.data);\n```\n\nIn this example, the `userConverter` function is used to transform the response data into the `User` type. The `RestClient` instance is configured to use this converter when performing the GET request.\n\nThe example taken is in its simplest form, we can do more than just mapping the field to it's type. Instead we may\nrequired to return an object with additional fields or perform some operation on the data before returning it.\n\nHere is an example of a converter function that maps the response data to a `User` class and checks for data types, converting them as necessary:\n\nFirst, define the `User` class:\n\n```typescript\nclass User {\n    id: number;\n    name: string;\n    email: string;\n\n    constructor(id: number, name: string, email: string) {\n        this.id = id;\n        this.name = name;\n        this.email = email;\n    }\n}\n```\n\nNext, create the converter function:\n\n```typescript\nconst userConverter = (data: any): User =\u003e {\n    const id = typeof data.id === 'number' ? data.id : parseInt(data.id, 10);\n    const name = typeof data.name === 'string' ? data.name : String(data.name);\n    const email = typeof data.email === 'string' ? data.email : String(data.email);\n\n    return new User(id, name, email);\n};\n```\n\nThis converter function ensures that the `id` is a number, and `name` and `email` are strings before creating a new `User` instance.\n\n## Conclusion\n\nThis documentation covers the basic usage of the `RestClient` class for making HTTP requests. \nFor more advanced usage and customization, refer to the class and method documentation in the source code.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayu-sh-kr%2Fdota-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fayu-sh-kr%2Fdota-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fayu-sh-kr%2Fdota-rest/lists"}