{"id":15091443,"url":"https://github.com/maxwellmatthis/swagger-to-typescript","last_synced_at":"2026-01-05T00:12:50.151Z","repository":{"id":158961279,"uuid":"416798279","full_name":"maxwellmatthis/swagger-to-typescript","owner":"maxwellmatthis","description":"Translate OpenAPI files to TypeScript","archived":false,"fork":false,"pushed_at":"2023-04-09T23:13:55.000Z","size":61,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-27T10:43:06.671Z","etag":null,"topics":["oasv3","open-api-specification","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/maxwellmatthis.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":"2021-10-13T15:27:14.000Z","updated_at":"2022-05-01T15:40:24.000Z","dependencies_parsed_at":"2023-06-15T00:45:23.012Z","dependency_job_id":null,"html_url":"https://github.com/maxwellmatthis/swagger-to-typescript","commit_stats":{"total_commits":13,"total_committers":1,"mean_commits":13.0,"dds":0.0,"last_synced_commit":"e530fa6a1d9e589593eba1071baf81a007031f2b"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxwellmatthis%2Fswagger-to-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxwellmatthis%2Fswagger-to-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxwellmatthis%2Fswagger-to-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxwellmatthis%2Fswagger-to-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxwellmatthis","download_url":"https://codeload.github.com/maxwellmatthis/swagger-to-typescript/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244945590,"owners_count":20536296,"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":["oasv3","open-api-specification","swagger"],"created_at":"2024-09-25T10:41:08.386Z","updated_at":"2026-01-05T00:12:50.113Z","avatar_url":"https://github.com/maxwellmatthis.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swagger-to-typescript\n\n## About\n\nThis program converts OpenAPI/swagger files into TypeScript code to make calling and receiving information from API endpoints easier and most of all type safe.\n\nI looked around for a while but couldn't find a tool that gave me clean, native (using fetch) and minimalistic output code. The code bases didn't look very clean either and were often not written in typescript and ES6 syntax, which made reading them quite difficult.\n\n## Usage\n\n```bash\nnode dist/index.js [options]\n```\n\n### Options\n\n- -s, --source \u0026lt;path-to-openapi-file\u0026gt;  The path to the openapi/swagger file that you want to convert to requests\n- -o, --output \u0026lt;path-to-api-file\u0026gt;      The path to the Typescript file that will contain the typed requests (default: \"./api.ts\")\n- -i, --inline                               If as many things as possible should be inlined or may be spread out over different interfaces and constants\n- -d, --dent \u0026lt;n+(tab/t/space/s)\u0026gt;       The amount of spaces or tabs with which to indent\n- -h, --help                                 Display help for the program\n\n## Limitations\n\nThis project is supposed to stay lightweight and readable. Due to this **it currently only supports `OpenAPIv3` format and the `application/json` media type** for requests being made and received. This is fine for my current project, since it uses the `application/json` media type for everything. Future versions of this project will contain support for more media types.\n\n### Example\n\nFirst define a swagger file:\n\n```yaml\n# openapi.yaml\nopenapi: 3.0.0\ninfo:\n  title: International Greeter\n  version: 2.5.1\nservers:\n  - url: https://greeting.example.com\n    description: The main production server\npaths:\n  /v1/hello:\n    post:\n      description: Returns hello in a random language\n      operationId: randomHello\n      responses:\n        200:\n          description: Hello in a random language along with the language\n          content:\n            application/json:\n              schema:\n                type: object\n                required:\n                  - language\n                  - text\n                properties:\n                  language:\n                    type: string\n                    example: German\n                  text:\n                    type: string\n                    example: Hallo\n  /v1/hello/{language}:\n    post:\n      description: Returns hello in the specified language\n      operationId: helloByLanguage\n      requestBody:\n        required: true\n        content:\n          application/json:\n            schema:\n              type: object\n              required:\n                - apiKey\n                - language\n              properties:\n                apiKey:\n                  type: string\n      responses:\n        200:\n          description: Hello in the specified language\n          content:\n            application/json:\n              schema:\n                type: object\n                required:\n                  - text\n                properties:\n                  text:\n                    type: string\n                    example: 你好\n        401:\n          description: apiKey has expired\n          content:\n            application/json:\n              schema:\n                $ref: '#/components/schemas/Error'\n        405:\n          description: Invalid language\n          content:\n            application/json:\n              schema:\n                $ref: '#/components/schemas/Error'\ncomponents:\n  schemas:\n    Error:\n      type: object\n      required:\n        - error\n      properties:\n        error:\n          type: object\n          required:\n            - message\n          properties:\n            message:\n              type: string\n```\n\nThen run the codegen:\n\n```bash\n# bash\nnode dist/index.js -s ./openapi.yaml -o ./api.ts\n```\n\nThis is the result:\n\n```typescript\n// api.ts\n\n/** The result of an accessor function */\nexport interface Res\u003cData\u003e { ok: boolean, data?: Data, networkError?: boolean };\n\nexport interface Error { error: { message: string } };\n\n/** The main production server */\nexport const mainServer: string = \"https://greeting.example.com\";\n\n/**\n * @route POST `/v1/hello`\n * @returns `Promise` of possible API responses\n */\nexport async function randomHello(): Promise\u003cRes\u003c{ language: string, text: string }\u003e\u003e {\n  try {\n    const r = await fetch(`${mainServer}/v1/hello`, {method: \"POST\"});\n    try {\n      return { ok: r.ok, data: await r.json() };\n    } catch (e) {\n      return { ok: r.ok };\n    }\n  } catch (e) {\n    console.error(e);\n    return { ok: false, networkError: true };\n  }\n}\n\n/**\n * @route POST `/v1/hello/{language}`\n * @returns `Promise` of possible API responses\n */\nexport async function helloByLanguage(\n  path: { language: string },\n  body: { apiKey: string }\n): Promise\u003cRes\u003c{ text: string } | Error\u003e\u003e {\n  try {\n    const r = await fetch(\n      `${mainServer}/v1/hello/${path.language}`,\n      {\n        method: \"POST\",\n        body: JSON.stringify(body),\n        headers: { \"Content-Type\": \"application/json\" }\n      }\n    );\n    try {\n      return { ok: r.ok, data: await r.json() };\n    } catch (e) {\n      return { ok: r.ok };\n    }\n  } catch (e) {\n    console.error(e);\n    return { ok: false, networkError: true };\n  }\n}\n```\n\nThe interfaces, constant and function can now be imported into any other ES module. It could look something like this:\n\n```typescript\nimport { apiKey } from \"./config\";\nimport { randomHello, helloByLanguage } from \"./api\";\n\nasync function printRandomHello() {\n  const res = await randomHello();\n  if (res.ok \u0026\u0026 res.data) {\n    console.log(`${res.data.language}: \"${res.data.text}\"`);\n  } else if (res.networkError) {\n    console.error(\"A network error occurred.\");\n  }\n}\n\nasync function printHelloByLanguage(language: string) {\n  const res = await helloByLanguage({ language }, { apiKey });\n  if (res.ok \u0026\u0026 res.data \u0026\u0026 \"text\" in res.data) {\n    console.log(res.data.text);\n  } else if (\"error\" in res.data) {\n    console.error(res.data.error.message);\n  } else if (res.networkError) {\n    console.error(\"A network error occurred.\");\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxwellmatthis%2Fswagger-to-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxwellmatthis%2Fswagger-to-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxwellmatthis%2Fswagger-to-typescript/lists"}