{"id":22322404,"url":"https://github.com/borolgs/openapi-ff","last_synced_at":"2025-03-26T05:11:16.203Z","repository":{"id":264456644,"uuid":"893407445","full_name":"borolgs/openapi-ff","owner":"borolgs","description":"Generate typed effects and queries from OpenAPI 3","archived":false,"fork":false,"pushed_at":"2024-12-05T18:29:34.000Z","size":41,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T06:46:53.573Z","etag":null,"topics":["effector","openapi","openapi-typescript","swagger"],"latest_commit_sha":null,"homepage":"","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/borolgs.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-11-24T11:21:23.000Z","updated_at":"2024-12-05T18:29:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"4671bea6-9d8c-4df7-a677-816e996c6986","html_url":"https://github.com/borolgs/openapi-ff","commit_stats":null,"previous_names":["borolgs/openapi-ff"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borolgs%2Fopenapi-ff","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borolgs%2Fopenapi-ff/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borolgs%2Fopenapi-ff/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borolgs%2Fopenapi-ff/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/borolgs","download_url":"https://codeload.github.com/borolgs/openapi-ff/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245591624,"owners_count":20640692,"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":["effector","openapi","openapi-typescript","swagger"],"created_at":"2024-12-04T01:07:15.258Z","updated_at":"2025-03-26T05:11:16.178Z","avatar_url":"https://github.com/borolgs.png","language":"TypeScript","readme":"# openapi-ff\n\nopenapi-ff is a type-safe tiny wrapper around [effector](https://effector.dev/) and [farfetched](https://ff.effector.dev/) to work with OpenAPI schema.\n\nIt works by using [openapi-fetch](https://openapi-ts.dev/openapi-fetch/) and [openapi-typescript](https://openapi-ts.dev/).\n\n## Setup\n\n```bash\npnpm i openapi-ff @farfetched/core effector openapi-fetch\npnpm i -D openapi-typescript typescript\n```\n\nNext, generate TypeScript types from your OpenAPI schema using openapi-typescript:\n\n```bash\nnpx openapi-typescript ./path/to/api/v1.yaml -o ./src/shared/api/schema.d.ts\n```\n\n## Usage\n\n```ts\nimport createFetchClient from \"openapi-fetch\";\nimport { createClient } from \"openapi-ff\";\nimport { createQuery } from \"@farfetched/core\";\nimport type { paths } from \"./schema\"; // generated by openapi-typescript\n\nexport const client = createFetchClient\u003cpaths\u003e({\n  baseUrl: \"https://myapi.dev/v1/\",\n});\nexport const { createApiEffect } = createClient(client);\n\nconst blogpostQuery = createQuery({\n  effect: createApiEffect(\"get\", \"/blogposts/{post_id}\"),\n});\n```\n\n```tsx\nimport { useUnit } from \"effector-react\";\n\nfunction Post() {\n  const { data: post, pending } = useUnit(blogpostQuery);\n\n  if (pending) {\n    return \u003cLoader /\u003e;\n  }\n\n  return (\n    \u003csection\u003e\n      \u003cp\u003e{post.title}\u003c/p\u003e\n    \u003c/section\u003e\n  );\n}\n```\n\nAdvanced Usage:\n\n```ts\nimport { chainRoute } from \"atomic-router\";\nimport { startChain } from \"@farfetched/atomic-router\";\nimport { isApiError } from \"openapi-ff\";\n\nconst getBlogpostFx = createApiEffect(\"get\", \"/blogposts/{post_id}\", {\n  mapParams: (args: { postId: string }) =\u003e ({ params: { path: { post_id: args.postId } } }),\n});\nconst blogpostQuery = createQuery({\n  effect: getBlogpostFx,\n  mapData: ({ result, params }) =\u003e ({ ...result, ...params }),\n  initialData: { body: \"-\", title: \"-\", postId: \"0\" },\n});\n\nexport const blogpostRoute = chainRoute({\n  route: routes.blogpost.item,\n  ...startChain(blogpostQuery),\n});\n\nconst apiError = sample({\n  clock: softwareQuery.finished.failure,\n  filter: isApiError,\n});\n```\n\n## Runtime Validation\n\n`openapi-ff` does not handle runtime validation, as `openapi-typescript` [does not support it](https://github.com/openapi-ts/openapi-typescript/issues/1420#issuecomment-1792909086).\n\n\u003e openapi-typescript by its design generates runtime-free static types, and only static types.\n\nHowever, `openapi-ff` allows adding a contract factory when creating a client and provides a corresponding method, `createApiEffectWithContract`:\n\n```ts\nconst { createApiEffectWithContract } = createClient(fetchClient, {\n  createContract(method, path) {\n    // ... create your own contract\n    return contract; // Contract\u003cunknown, unknown\u003e\n  },\n});\n\nconst query = createQuery({\n  ...createApiEffectWithContract(\"get\", \"/blogposts\"),\n});\n```\n\n### [typed-openapi](https://github.com/astahmer/typed-openapi) example\n\n```bash\nnpx typed-openapi path/to/api.yaml -o src/zod.ts -r zod # Generate zod schemas\npnpm install zod @farfetched/zod\n```\n\n```ts\nimport { EndpointByMethod } from \"./zod\";\nimport { zodContract } from \"@farfetched/zod\";\n\nconst { createApiEffectWithContract } = createClient(fetchClient, {\n  createContract(method, path) {\n    const response = (EndpointByMethod as any)[method][path]?.response;\n    if (!response) {\n      throw new Error(`Response schema for route \"${method} ${path}\" doesn't exist`);\n    }\n    return zodContract(response);\n  },\n});\n\nconst query = createQuery({\n  ...createApiEffectWithContract(\"get\", \"/blogposts\"),\n});\n```\n\n### [orval](https://orval.dev/) example\n\nAlternatively, you can simply add any contract to a query:\n\n```bash\npnpm install zod @farfetched/zod\nnpx orval --input path/to/api.yaml --output src/zod.ts --client zod --mode single\n```\n\n```ts\nimport { zodContract } from \"@farfetched/zod\";\nimport { getBlogpostsResponseItem } from \"./zod\";\n\nconst blogpostQuery = createQuery({\n  effect: createApiEffect(\"get\", \"/blogposts/{post_id}\"),\n  contract: zodContract(getBlogpostsResponseItem),\n});\n```\n\n## TODO\n\nAdd `createApiQuery`:\n\n```ts\ncreateApiQuery({\n  method: \"get\",\n  path: \"/blogposts/{post_id}\",\n  mapParams: (args: { postId: string }) =\u003e ({ params: { path: { post_id: args.postId } } }),\n  mapData: ({ result, params }) =\u003e ({ ...result, ...params }),\n  initialData: { body: \"-\", title: \"-\", postId: \"0\" },\n});\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborolgs%2Fopenapi-ff","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fborolgs%2Fopenapi-ff","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborolgs%2Fopenapi-ff/lists"}