{"id":18307791,"url":"https://github.com/exceptionless/fetchclient","last_synced_at":"2025-04-05T17:32:02.281Z","repository":{"id":230612722,"uuid":"779786771","full_name":"exceptionless/FetchClient","owner":"exceptionless","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-10T02:23:31.000Z","size":104,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-21T08:05:51.069Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/exceptionless.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-30T19:36:57.000Z","updated_at":"2025-01-10T02:23:34.000Z","dependencies_parsed_at":"2024-05-09T06:26:33.193Z","dependency_job_id":"dc74bc54-59be-4bf1-8964-f682cb0a3dfd","html_url":"https://github.com/exceptionless/FetchClient","commit_stats":null,"previous_names":["exceptionless/fetchclient"],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exceptionless%2FFetchClient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exceptionless%2FFetchClient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exceptionless%2FFetchClient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/exceptionless%2FFetchClient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/exceptionless","download_url":"https://codeload.github.com/exceptionless/FetchClient/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247374904,"owners_count":20928907,"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-11-05T16:05:29.714Z","updated_at":"2025-04-05T17:32:02.266Z","avatar_url":"https://github.com/exceptionless.png","language":"TypeScript","readme":"\u003c!-- deno-fmt-ignore-file --\u003e\n# FetchClient [![CI](https://github.com/exceptionless/fetchclient/workflows/CI/badge.svg)](https://github.com/exceptionless/fetchclient/actions?query=workflow%3ACI) [![NPM](https://img.shields.io/npm/v/%40exceptionless%2Ffetchclient)](https://www.npmjs.com/package/@exceptionless/fetchclient) [![JSR](https://jsr.io/badges/@exceptionless/fetchclient)](https://jsr.io/@exceptionless/fetchclient)\n\nFetchClient is a library that makes it easier to use the fetch API for JSON APIs. It provides the following features:\n\n* [Makes fetch easier to use for JSON APIs](#typed-response)\n* [Automatic model validation](#model-validator)\n* [Caching](#caching)\n* [Middleware](#middleware)\n* [Problem Details](https://www.rfc-editor.org/rfc/rfc9457.html) support\n* Option to parse dates in responses\n\n## Install\n\n```shell\nnpm install --save @exceptionless/fetchclient\n```\n\n## Docs\n\n[API Documentation](https://jsr.io/@exceptionless/fetchclient/doc)\n\n## Usage\n\n### Typed Response\n\n```ts\nimport { FetchClient } from '@exceptionless/fetchclient';\n\ntype Products = {\n  products: Array\u003c{ id: number; name: string }\u003e;\n};\n\nconst client = new FetchClient();\nconst response = await client.getJSON\u003cProducts\u003e(\n  `https://dummyjson.com/products/search?q=iphone\u0026limit=10`,\n);\n\nconst products = response.data;\n```\n\n### Typed Response Using a Function\n\n```ts\nimport { getJSON } from '@exceptionless/fetchclient';\n\ntype Products = {\n  products: Array\u003c{ id: number; name: string }\u003e;\n};\n\nconst response = await getJSON\u003cProducts\u003e(\n  `https://dummyjson.com/products/search?q=iphone\u0026limit=10`,\n);\n\nconst products = response.data;\n```\n\n### Model Validator\n\n```ts\nimport { FetchClient, setModelValidator } from '@exceptionless/fetchclient';\n\nsetModelValidator(async (data: object | null) =\u003e {\n  // use zod or any other validator\n  const problem = new ProblemDetails();\n  const d = data as { password: string };\n  if (d?.password?.length \u003c 6) {\n    problem.errors.password = [\n      \"Password must be longer than or equal to 6 characters.\",\n    ];\n  }\n  return problem;\n});\n\nconst client = new FetchClient();\nconst data = {\n  email: \"test@test\",\n  password: \"test\",\n};\n\nconst response = await client.postJSON(\n  \"https://jsonplaceholder.typicode.com/todos/1\",\n  data,\n);\n\nif (!response.ok) {\n  // check response problem\n  console.log(response.problem.detail);\n}\n```\n\n### Caching\n\n```ts\nimport { FetchClient } from '@exceptionless/fetchclient';\n\ntype Todo = { userId: number; id: number; title: string; completed: boolean };\n\nconst client = new FetchClient();\nconst response = await client.getJSON\u003cTodo\u003e(\n  `https://jsonplaceholder.typicode.com/todos/1`,\n  {\n    cacheKey: [\"todos\", \"1\"],\n    cacheDuration: 1000 * 60, // expires in 1 minute\n  }\n);\n\n// invalidate programmatically\nclient.cache.delete([\"todos\", \"1\"]);\n```\n\n### Middleware\n\n```ts\nimport { FetchClient, useMiddleware } from '@exceptionless/fetchclient';\n\ntype Products = {\n  products: Array\u003c{ id: number; name: string }\u003e;\n};\n\nuseMiddleware(async (ctx, next) =\u003e {\n  console.log('starting request')\n  await next();\n  console.log('completed request')\n});\n\nconst client = new FetchClient();\nconst response = await client.getJSON\u003cProducts\u003e(\n  `https://dummyjson.com/products/search?q=iphone\u0026limit=10`,\n);\n```\n\nAlso, take a look at the tests:\n\n[FetchClient Tests](src/FetchClient.test.ts)\n\n## Contributing\n\nRun tests:\n\n```shell\ndeno run test\n```\n\nLint code:\n\n```shell\ndeno lint\n```\n\nFormat code:\n\n```shell\ndeno fmt\n```\n\nType check code:\n\n```shell\ndeno run check\n```\n\n## License\n\nMIT © [Exceptionless](https://exceptionless.com)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexceptionless%2Ffetchclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexceptionless%2Ffetchclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexceptionless%2Ffetchclient/lists"}