{"id":28088971,"url":"https://github.com/moznion/openapi-fetch-gen","last_synced_at":"2025-05-13T12:52:53.991Z","repository":{"id":288395672,"uuid":"967921968","full_name":"moznion/openapi-fetch-gen","owner":"moznion","description":"Generate TypeScript API client from OpenAPI TypeScript interface definitions created by openapi-typescript (https://github.com/openapi-ts/openapi-typescript).","archived":false,"fork":false,"pushed_at":"2025-05-12T16:54:37.000Z","size":214,"stargazers_count":14,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-12T17:59:02.940Z","etag":null,"topics":["generator","openapi","openapi-fetch","openapi-typescript","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@moznion/openapi-fetch-gen","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/moznion.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,"zenodo":null}},"created_at":"2025-04-17T08:01:17.000Z","updated_at":"2025-05-12T16:53:57.000Z","dependencies_parsed_at":"2025-05-05T02:27:33.867Z","dependency_job_id":"07a1bc35-f896-4758-a67c-50fc91a7ea85","html_url":"https://github.com/moznion/openapi-fetch-gen","commit_stats":null,"previous_names":["moznion/openapi-fetch-gen"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fopenapi-fetch-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fopenapi-fetch-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fopenapi-fetch-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moznion%2Fopenapi-fetch-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moznion","download_url":"https://codeload.github.com/moznion/openapi-fetch-gen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253802642,"owners_count":21966838,"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":["generator","openapi","openapi-fetch","openapi-typescript","typescript"],"created_at":"2025-05-13T12:52:53.382Z","updated_at":"2025-05-13T12:52:53.960Z","avatar_url":"https://github.com/moznion.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# openapi-fetch-gen [![Run Tests](https://github.com/moznion/openapi-fetch-gen/actions/workflows/test.yml/badge.svg)](https://github.com/moznion/openapi-fetch-gen/actions/workflows/test.yml)\n\nGenerate TypeScript API client from OpenAPI TypeScript interface definitions created by [openapi-typescript](https://github.com/openapi-ts/openapi-typescript).\n\nThis tool takes TypeScript interface definitions generated by `openapi-typescript` and creates a fully typed API client using [openapi-fetch](https://github.com/openapi-ts/openapi-typescript/tree/main/packages/openapi-fetch).\n\n## How It Works\n\n1. Parse the TypeScript schema file generated by `openapi-typescript`\n2. Extract all API endpoints, their HTTP methods, and parameter structures from the schema\n3. Generate typed wrapper functions for each endpoint\n4. Export a fully-typed client that provides:\n   - A base client instance created with `createClient` from `openapi-fetch`\n   - Individual typed functions for each API endpoint\n   - Type helpers for parameters and responses\n\n## Installation\n\n```bash\nnpm install --save-dev @moznion/openapi-fetch-gen\n```\n\n## Usage\n\n### CLI\n\n```bash\nnpx openapi-fetch-gen --input ./schema.d.ts --output ./client.ts\n```\n\nOptions:\n\n```\n  -V, --version        output the version number\n  -i, --input \u003cpath\u003e   path to input OpenAPI TypeScript definition file\n  -o, --output \u003cpath\u003e  path to output generated client file (default: \"./client.ts\")\n  -h, --help           display help for command\n```\n\n### Example\n\nPlease refer to the [examples](./examples/).\n\n`schema.d.ts` is generated from `schema.yaml` by `openapi-typescript`, and `generated_client.ts` is generated by this tool according to the `schema.d.ts`.\n\nFYI, you can use the generated client as follows:\n\n```typescript\nimport { Client } from \"./generated_client\";\n\nasync function doSomething() {\n  const client = new Client({ baseUrl: \"https://api.example.com\" });\n  const users = await client.getUsers({\n    query: {\n      page: 1,\n      pageSize: 10,\n      membershipType: \"PREMIUM\",\n    },\n  });\n\n  for (const user of users.data?.items ?? []) {\n    console.log(`User: ${user.name}, Email: ${user.email}`);\n  }\n}\n```\n\n### Default HTTP Headers\n\nGenerated clients support a generic type for default HTTP headers.\n\nExample:\n\n```typescript\nexport class Client\u003cHT extends Record\u003cstring, string\u003e\u003e {\n  constructor(clientOptions: ClientOptions, defaultHeaders?: HT) {\n    this.client = createClient\u003cpaths\u003e(clientOptions);\n    this.defaultHeaders = defaultHeaders ?? ({} as HT);\n  }\n  ...\n}\n```\n\nYou can create a client instance with default headers like this:\n\n```typescript\nnew Client({}, {\"Authorization\": \"Bearer your-token\", \"Application-Version\": \"1.0.0\"});\n```\n\nWith this setup, endpoint methods that require these headers no longer need them to be explicitly passed each time.\n\nFor example, given the following schema:\n\n```typescript\n\"/users/bulk/{jobId}\": {\n  get: {\n    parameters: {\n      query?: never;\n      header: {\n        /** @description Authorization Header */\n        Authorization: string;\n        /** @description Application version */\n        \"Application-Version\": string;\n        /** @description Identifier of something */\n        \"Something-Id\": string;\n      };\n      path: {\n        /** @description Bulk import job identifier */\n        jobId: string;\n      };\n      cookie?: never;\n    };\n```\n\nThis tool generates an endpoint method using a type-level trick like this:\n\n```typescript\nasync getUsersBulkJobid(\n  params: [\n    Exclude\u003c\n      // Missed Header Keys for default headers\n      keyof {\n        Authorization: string;\n        \"Application-Version\": string;\n        \"Something-Id\": string;\n      },\n      Extract\u003c\n        // Provided header keys by default headers' keys\n        keyof HT,\n        keyof {\n          Authorization: string;\n          \"Application-Version\": string;\n          \"Something-Id\": string;\n        }\n      \u003e\n    \u003e,\n  ] extends [never]\n    ? {\n        header?: {\n          Authorization: string;\n          \"Application-Version\": string;\n          \"Something-Id\": string;\n        };\n        path: { jobId: string };\n      }\n    : {\n        header:\n          | (Pick\u003c\n              // Pick the header keys that are not in the default headers\n              {\n                Authorization: string;\n                \"Application-Version\": string;\n                \"Something-Id\": string;\n              },\n              Exclude\u003c\n                // Missed Header Keys for default headers\n                keyof {\n                  Authorization: string;\n                  \"Application-Version\": string;\n                  \"Something-Id\": string;\n                },\n                Extract\u003c\n                  // Provided header keys by default headers' keys\n                  keyof HT,\n                  keyof {\n                    Authorization: string;\n                    \"Application-Version\": string;\n                    \"Something-Id\": string;\n                  }\n                \u003e\n              \u003e\n            \u003e \u0026\n              Partial\u003c\n                // Disallow default headers' keys to be in the header param\n                Record\u003c\n                  Extract\u003c\n                    // Provided header keys by default headers' keys\n                    keyof HT,\n                    keyof {\n                      Authorization: string;\n                      \"Application-Version\": string;\n                      \"Something-Id\": string;\n                    }\n                  \u003e,\n                  never\n                \u003e\n              \u003e)\n          | {\n              Authorization: string;\n              \"Application-Version\": string;\n              \"Something-Id\": string;\n            };\n        path: { jobId: string };\n      },\n) {\n  return await this.client.GET(\"/users/bulk/{jobId}\", {\n    params: {\n      ...params,\n      header: { ...this.defaultHeaders, ...params.header } as {\n        Authorization: string;\n        \"Application-Version\": string;\n        \"Something-Id\": string;\n      },\n    },\n  });\n}\n```\n\nThis signature allows you to:\n\n- **Omit** the defaulted headers and only pass additional ones (here, `Something-Id`):\n\n```typescript\nclient.getUsersBulkJobid({header: {\"Something-Id\": \"foobar\"}, path: {jobId: \"123\"}});\n```\n\n- **Override** all headers, including the defaults:\n\n```typescript\nclient.getUsersBulkJobid({header: {\"Authorization\": \"foo\", \"Application-Version\": \"bar\", \"Something-Id\": \"foobar\"}, path: {jobId: \"123\"}});\n```\n\nIf your default headers already include **all** required headers for the endpoint (e.g. `{\"Authorization\": \"Bearer your-token\", \"Application-Version\": \"1.0.0\", \"Something-Id\": \"123\"}` as the second constructor argument), you can omit the `header` parameter entirely:\n\n```typescript\nclient.getUsersBulkJobid({path: {jobId: \"123\"}});\n```\n\nNOTE:\n\nIn this context, the \"default HTTP headers\" are different from the headers in `ClientOptions`.\nThe headers in `ClientOptions` are always sent implicitly, regardless of the header parameters specified in endpoint methods.\nIn contrast, the \"default HTTP headers\" mechanism is intended to reduce the need to repeatedly specify common header parameters\nin each endpoint method when their values are already known.\n\n## Articles\n\n- [openapi-fetch-gen – Generate TypeScript API client from OpenAPI TypeScript interface definitions created by openapi-typescript - DEV Community](https://dev.to/moznion/openapi-fetch-gen-generate-typescript-api-client-from-openapi-typescript-interface-definitions-kjd)\n\n## For developers\n\n### How to publish this packages\n\n```\n$ pnpm ship\n```\n\n## License\n\nMIT\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoznion%2Fopenapi-fetch-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoznion%2Fopenapi-fetch-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoznion%2Fopenapi-fetch-gen/lists"}