{"id":43682447,"url":"https://github.com/cocreators-ee/apity","last_synced_at":"2026-02-05T02:05:33.481Z","repository":{"id":65380560,"uuid":"591235475","full_name":"cocreators-ee/apity","owner":"cocreators-ee","description":"A typed fetch client for openapi-typescript for use with SvelteKit","archived":false,"fork":false,"pushed_at":"2025-04-10T08:05:20.000Z","size":549,"stargazers_count":58,"open_issues_count":11,"forks_count":3,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-25T17:49:56.063Z","etag":null,"topics":["api","fetch","openapi","openapi3","svelte","sveltekit","ts","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"ajaishankar/openapi-typescript-fetch","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cocreators-ee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","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},"funding":{"custom":["https://www.buymeacoffee.com/cocreators"]}},"created_at":"2023-01-20T08:55:39.000Z","updated_at":"2025-06-29T14:03:38.000Z","dependencies_parsed_at":"2025-04-10T09:22:49.624Z","dependency_job_id":"ee02fc41-4722-42a9-b850-15754b70dcf8","html_url":"https://github.com/cocreators-ee/apity","commit_stats":null,"previous_names":["cocreators-ee/openapi-typescript-fetch-svelte"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/cocreators-ee/apity","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocreators-ee%2Fapity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocreators-ee%2Fapity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocreators-ee%2Fapity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocreators-ee%2Fapity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cocreators-ee","download_url":"https://codeload.github.com/cocreators-ee/apity/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocreators-ee%2Fapity/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29107301,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T00:52:08.035Z","status":"online","status_checked_at":"2026-02-05T02:00:07.839Z","response_time":65,"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":["api","fetch","openapi","openapi3","svelte","sveltekit","ts","typescript"],"created_at":"2026-02-05T02:05:32.531Z","updated_at":"2026-02-05T02:05:33.467Z","avatar_url":"https://github.com/cocreators-ee.png","language":"TypeScript","funding_links":["https://www.buymeacoffee.com/cocreators"],"categories":[],"sub_categories":[],"readme":"# 📘️ apity - Typed API client for Svelte and SvelteKit\n\nA typed fetch client for [openapi-typescript](https://github.com/drwpow/openapi-typescript) compatible with SvelteKit's custom `fetch`\n\n## Installation\n\n```bash\nnpm install @cocreators-ee/apity\n```\n\nOr\n\n```bash\npnpm add @cocreators-ee/apity\n```\n\n## Features\n\n- [x] Support of JSON request and responses from [OpenAPI 3.0](https://swagger.io/specification)\n- [x] Support of `{#await}` syntax in Svelte templates\n- [x] Compatibility with SvelteKit's `fetch` in `load` functions\n- [x] Request reloading\n- [x] Configuration of default [fetch options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters)\n\nOn the roadmap:\n\n- [ ] Caching of subsequent requests with the same URL and parameters\n\n### Live demo\n\nApity has a [live demo](https://apity-demo.vercel.app/) with code samples.\nAlso you can read an introductionary [blog post](https://dev.to/fbjorn/a-typed-http-client-for-sveltekit-88b).\n\n## Usage\n\n### Generate typescript definition from schema\n\nBefore working with the library, you need to generate an API spec using [openapi-typescript](https://www.npmjs.com/package/openapi-typescript):\n\n```bash\nnpx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.json --output src/petstore.ts\n\n🚀 https://petstore3.swagger.io/api/v3/openapi.json → file:./src/petstore.ts [870ms]\n```\n\n### Using Apity\n\nConfigure Apity instance and generate functions for making API calls:\n\n```ts\n// File: api.ts\n\nimport { Apity } from '@cocreators-ee/apity'\nimport type { paths } from 'src/petstore'\n\nconst apity = Apity.for\u003cpaths\u003e()\n\n// global configuration\napity.configure({\n  // Base URL to your API\n  baseUrl: 'https://petstore.swagger.io/v2',\n  // RequestInit options, e.g. default headers\n  init: {\n    // mode: 'cors'\n    // headers: {}\n  },\n})\n\n// create fetch operations\nexport const findPetsByStatus = apity\n  .path('/pet/findByStatus')\n  .method('get')\n  .create()\nexport const addPet = apity.path('/pet').method('post').create()\n```\n\nEach API call is represented as a request object that has the following properties:\n\n```typescript\ntype ApiRequest\u003cR = any\u003e = {\n  // Svelte store containing the response of the API call.\n  readonly resp: Writable\u003cApiResponse\u003cR\u003e | undefined\u003e\n\n  // Svelte store that contains a promise for an API call.\n  // If you reload the request using reload() function, this store will be updated.\n  readonly ready: Writable\u003cundefined | Promise\u003cApiResponse\u003cR\u003e\u003e\u003e\n\n  // Function that reloads the request with the same parameters.\n  reload: () =\u003e Promise\u003cApiResponse\u003cR\u003e\u003e\n\n  // Promise for the API call.\n  // Useful for server code and places where you can't use the `ready` store.\n  result: Promise\u003cApiResponse\u003cR\u003e\u003e\n}\n```\n\nEach response is a Svelte store returning either an `undefined`, or the following object:\n\n```ts\ntype SuccessfulResp\u003cR\u003e = {\n  ok: true\n  // Typed object for a successful request. Built from the OpenAPI spec\n  data: R\n  // HTTP status code\n  status: number\n}\n\ntype FailedResp = {\n  ok: false\n  data: any\n  // HTTP status code\n  status: number\n}\n\ntype ApiResponse\u003cR\u003e = SuccessfulResp\u003cR\u003e | FailedResp\n```\n\n### Error handling\n\nThere are certain conditions under which an API request could throw an exception without\nactually reaching the desired server, for example, unpredictable network issues. For such\ncases, the api response will contain a status set to a negative number, indicating that\nan exception was thrown.\n\n```js\n{\n  ok: false,\n  status: -1,\n  data: undefined,\n}\n```\n\n### Using Apity with await syntax in templates\n\nAssuming you've created an `src/api.ts` from [using Apity](#using-apity) section:\n\n```svelte\n\u003cscript lang=\"ts\"\u003e\n  import { findPetByStatus } from 'src/api.ts'\n  const request = findPetByStatus({ status: 'sold' })\n  const petsReady = request.ready\n\u003c/script\u003e\n\n\u003cdiv\u003e\n  {#await $petsReady}\n    \u003cp\u003eLoading..\u003c/p\u003e\n  {:then resp}\n    {#if resp.ok}\n      {#each resp.data as pet}\n        \u003cp\u003e{pet.name}\u003c/p\u003e\n      {/each}\n    {:else}\n      \u003cp\u003eError while loading pets\u003c/p\u003e\n    {/if}\n  {/await}\n\n  \u003cbutton on:click={() =\u003e {request.reload()}}\u003e\n    Reload pets\n  \u003c/button\u003e\n\u003c/div\u003e\n```\n\n### Subscribing to response store\n\nAssuming you've created an `src/api.ts` from [using Apity](#using-apity) section:\n\n```svelte\n\u003cscript lang=\"ts\"\u003e\n  import { findPetByStatus } from 'src/api.ts'\n  const request = findPetByStatus({ status: 'sold' })\n  let names = []\n\n  request.resp.subscribe(resp =\u003e {\n    if (resp.ok) {\n      names = resp.data.map(pet =\u003e pet.name)\n    }\n  })\n\u003c/script\u003e\n\n\u003cdiv\u003e\n  {#each names as name}\n    \u003cp\u003e{name}\u003c/p\u003e\n  {/each}\n\u003c/div\u003e\n```\n\n### Using in load functions\n\nFetch operations support SvelteKit's [load](https://kit.svelte.dev/docs/load#making-fetch-requests) function from `+page.ts` and `+page.server.ts`.\n\nAssuming you've created an `src/api.ts` from [using Apity](#using-apity) section:\n\n```ts\nimport { findPetByStatus } from 'src/api.ts'\n\nexport async function load({ fetch }) {\n  const request = findPetByStatus({ status: 'sold' })\n  const resp = await request.result\n  if (resp.ok) {\n    return { pets: resp.data, error: undefined }\n  } else {\n    return { pets: [], error: 'Failed to load pets' }\n  }\n}\n```\n\n# Financial support\n\nThis project has been made possible thanks to [Cocreators](https://cocreators.ee). You can help us continue our open source work by supporting us on [Buy me a coffee](https://www.buymeacoffee.com/cocreators).\n\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/cocreators)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocreators-ee%2Fapity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcocreators-ee%2Fapity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocreators-ee%2Fapity/lists"}