API Design
Design and review predictable public APIs for TypeScript, JavaScript, React, and React Native libraries. Use when shaping exported functions, classes, hooks, options objects, event and listener APIs, error behavior, naming, cross-platform abstractions, or JS-only packages. Pair with build-nitro-modules when the library is backed by Nitro.
- Skill ID
- margelo/react-native-skills/api-design
- Publisher
- margelo
- Repository
- react-native-skills
- Installs
- 0
- Files
- 1
- Synced
- Sep 23, 2026
Open any RiverX project, open the Skills panel in the chat, and search for this identifier. The files are fetched from the source repository at install time.
margelo/react-native-skills/api-designInstalls these files- SKILL.md
What this skill tells the agent
API Design
Use this skill before implementation or when reviewing a public API surface. Target explicit types, stable semantics, and no irrelevant internals in the public contract.
If the library is a Nitro Module, use this skill for the public TypeScript and React API shape first, then use build-nitro-modules for Nitro-specific spec, native-state, and binding constraints. If the library is JS-only, React-only, or React Native JS-only, stay in this skill.
Workflow
- Sketch the user-facing TypeScript API before implementing internals.
- Write 2-3 realistic call-site examples, including error and cleanup paths.
- Check the surface against the rules below.
- Verify the exported TypeScript with the repo's typecheck/lint/docs tooling before treating the API as done.
- Implement only after the public shape is coherent and verified.
API Freshness
Before choosing public API shape, dependency APIs, platform capabilities, or implementation strategy, verify current official sources instead of relying on trained memory. Library, React, React Native, platform, and tooling APIs evolve quickly.
- Prefer official docs, source repositories, release notes, changelogs, package READMEs, and current package metadata.
- Look for
llms.txtorllms-full.txton official docs sites when available, and use those as compact current context. - Treat remembered API details as a starting hypothesis only. If current docs or source disagree, follow the current docs/source and mention the change when relevant.
- Avoid designing against stale blog posts, old snippets, or outdated trained assumptions when an official current source is available.
API Shape Rules
- Prefer a single source of truth. Do not split related state across booleans and dependent values when one typed value can express the state. Prefer
timeoutMs?: numberoverenableTimeout: booleanplustimeoutMs?: number. - Use option objects or named structs once a function has 3 or more parameters, parameters of the same primitive type, or values that are likely to grow.
- Keep APIs specific instead of accepting every possible input shape. A millisecond timeout should be a
number, notnumber | string | bigint | object | null. - Avoid giant "does everything" objects. Split by domain or lifecycle when responsibilities differ.
- Before simplifying or redesigning an API, inventory the workflows the feature is supposed to support. Do not silently drop a workflow, such as a live session API, because a one-shot path is easier to implement. Split workflows into separate APIs when needed.
- Prefer literal unions, discriminated unions, interfaces, and typed option groups when the valid states are known. In TypeScript libraries, prefer string literal unions over runtime
enums unless consumers need a runtime value. - Avoid untyped dictionaries, boolean clusters, stringly typed commands, and loosely shaped events when the valid states are known.
- Do not model a binary option as an optional two-case string union such as
'enabled' | 'disabled'. If omitted means "use the default" and provided means true/false, use an optional boolean and document the default. - Do not represent multiple object states as one interface full of optional fields. Use a discriminated union, inheritance, or separate variant interfaces so impossible field combinations are unrepresentable.
- Keep related fields together on the variant where they are required. If
barcodeandbarcodeTypeonly make sense together, both should be nonoptional onScannedBarcode, not optional on a genericScannedData. - If you create variant interfaces, use them in the actual public type. Do not define
RecognizedTextDataTypeandRecognizedBarcodeDataTypebut keeprecognizedDataTypes: RecognizedDataType[]whereRecognizedDataTypestill contains every variant field as optional. - Use
undefinedor optional fields for absence. Usenullonly when "explicit none" means something different from "not provided". - Prefer discriminated unions for state machines, loading states, and result variants.
- Model user intent separately from resolved state when negotiation is involved. For example, an ordered array of constraint objects can express priorities, while a resolved config object reports what the platform actually selected.
- For complex negotiation, prefer ranked constraints or preference objects over exposing a combinatorial support matrix. Let callers describe intent, resolve the closest working configuration internally, and expose the resolved configuration through a return value, callback, or explicit resolver method.
- Expose common presets as
as const satisfies Record<string, Type>objects. Keep the accepted type structural so users can provide their own values. - Do not expose ambient facts the caller already knows, such as a
platformfield that only repeatsPlatform.OS, unless the API can return data produced by a different platform than the current runtime. - Do not freeze today's platform support matrix into the type shape. Prefer runtime capability fields such as
availableTextTypes: []orsupportedFormats: []over separate platform-specific types or static exclusions. This lets newer native capabilities become available without redesigning the JS API. - Prefer one unified options object. Avoid
ios/androidoption bags and platform-prefixed methods unless the concepts are genuinely platform-only and cannot be described as a cross-platform capability or no-op. - Classify configuration fields as either requirements or preferences. Throw when a requirement cannot be met. Treat preferences as best-effort when the feature can still perform its core job, such as quality, guidance UI, high-frame-rate tracking, auto-zoom, or a wider scan area. Document best-effort fields and expose capabilities or resolved configuration when callers need to know what was applied.
- Split by stable semantic capability when capabilities have different options, results, or futures. For example, barcode scanning and text scanning may deserve separate
BarcodeScannerandTextScannerAPIs even when one native API happens to implement both today. A platform that lacks text scanning should failcreateTextScanner()or reportisTextScannerAvailable: false, not force text-specific fields to be nullable on a generic scanner used for barcode scanning. - Runtime availability should describe whether a stable capability exists today, not permanently restrict the API shape to the current platform matrix. If Android gains text scanning later, the existing
TextScannercapability should become available without changing barcode APIs or broadening nullable result types. - Keep capability discovery separate from object contracts. Use capability fields to decide whether a workflow can be created or which optional preferences may apply. Once a factory returns a specific session/resource object, its baseline methods should be guaranteed by construction; otherwise return a narrower type or fail creation instead of making callers check
can*before every normal method. - Splitting workflows should reduce runtime capability checks. Do not keep broad capability flags only to compensate for one oversized object. For example, a one-shot scanner and an app-owned live scanner can be separate APIs; if the live scanner implementation guarantees zoom, photo capture, or region control, those can be part of the live scanner contract instead of nullable properties plus
can*checks inherited from a one-shot backend. - Encode lifecycle transitions in the API object graph. If commands are valid only after
configure,connect,start, or another lifecycle transition, expose those commands on a handle returned by that transition, not on the parent object with "maybe active" checks. For example,session.configure(device, outputs)can return aControllerthat ownssetZoom(...)andfocusTo(...); theSessionowns graph configuration and start/stop.
More from margelo
Use this skill in a project
Open any RiverX project, search the Skills panel for margelo/react-native-skills/api-design, and install it in one click.
