u/Prudent-Tangelo2640

▲ 1 r/reactnative+1 crossposts

I got tired of hardcoding API paths in my React Native app, so I built a CLI to fix it

Hey r/reactnative 👋 You know that feeling when you're 3 sprints deep into a project and your codebase is littered with stuff like this?

const res = await fetch('/roles/42/buttons/7', { method: 'GET' });

And then the backend team renames a route. And you have to grep through 40 files. Yeah. That. I got fed up enough to build something about it — a little CLI called typeoapi.

What it does

Point it at your OpenAPI spec (local file or a live URL) and it generates two TypeScript files automatically:

  • schema.ts — all your types for paths, components, operations, the whole schema
  • api.ts — the part I actually care about day to day:
  • A typed ApiRoute enum (no more magic strings)
  • A buildRoute() helper for path params
  • *_Payload types wired directly to each endpoint's request body

Using it is literally two questions

npx typeoapi
? OpenAPI schema source: https://api.example.com/openapi.json
? Output directory: ./src/api

No flags. No config files. Just two answers and you're done.

What you end up with

import { ApiRoute, buildRoute } from './api/api';
import type { Post_Gender_Payload } from './api/api';
// Static route — just use the enum
const res = await fetch(ApiRoute.PostGenders, {
method: 'POST',
body: JSON.stringify(payload satisfies PostGenderPayload),
});
// Dynamic route — path params handled safely
const url = buildRoute(ApiRoute.RolesRoleIdButtonsButtonId, {
role_id: 42,
button_id: 7,
});
// → '/roles/42/buttons/7'

If the backend renames a route, your TypeScript just breaks at compile time. Which is exactly what you want — catch it early, not in production.

The React Native specific bit

A lot of similar tools use const enum which Metro bundler chokes on. This one uses regular enum throughout, so it works out of the box with React Native — no extra babel plugins, no workarounds.

Other things worth knowing

Works with Swagger 2.0, OpenAPI 3.0 and 3.1

Accepts JSON or YAML, local file or remote URL

Auto-singularizes path segments (/genders/ → PostGenderPayload)

All types are zero runtime cost — fully erased at compile time

Built on top of the amazing openapi-typescript by @drwpow — huge credit to him

Try it

npx typeoapi

📦 npm: npmjs.com/package/typeoapi

GitHub: github.com/KarthiKeyan05046/openapi-ts-gen It's still early days so if something breaks or a use case isn't covered, open an issue — I'm actively working on it. And if you find it useful, a ⭐ on GitHub genuinely goes a long way. Happy to answer any questions below 🙌

reddit.com
u/Prudent-Tangelo2640 — 3 days ago