Skip to content

CLI and API ​

CLI ​

oasis-gen <SPEC> [-o <FILE>] [--lang <LANG>]
ArgumentMeaning
SPECAn OpenAPI document as a JSON or YAML file, or - for stdin
-o, --output FILEWrite to FILE instead of stdout
-l, --lang LANGThe target language; currently only typescript (the default)
-h, --helpPrint help
-V, --versionPrint the version
  • oasis-gen follows $refs to other local files. It resolves them relative to the referencing document, or relative to the current directory when the document comes from stdin.
  • If an error occurs, such as an invalid document, oasis-gen prints error: ... to stderr and exits with status 1.
  • The output starts with a Generated by oasis-gen ... Do not edit. line.

Requirements ​

  • Node.js 22 or later, for both the CLI and the generated client. The client also runs in browsers, tested in Chromium, Firefox, and WebKit, and in Bun.
  • TypeScript 5.0 or later. The generated file needs lib ES2022 or later, plus either the dom lib or @types/node for fetch, Request, and Response.
  • The file uses only erasable TypeScript syntax, so it also runs directly under Node's type stripping, which is on by default since Node.js 22.18.

The examples on this site import ./api.ts. This import requires allowImportingTsExtensions (Vite's templates set it) or Node's type stripping. Otherwise, import ./api.js or ./api, whichever your module resolution expects.

Installation ​

The npm package includes prebuilt binaries for macOS (arm64, x64), Linux (arm64, x64; statically linked, so they work with both glibc and musl), and Windows (arm64, x64). npm installs only the binary for your platform. Archives are also attached to each GitHub release. To build from source instead, run:

sh
cargo install --git https://github.com/22mb/oasis

API ​

The generated file exports the following:

ExportDescription
createClient(options?)Creates a client; see Calling operations
auth(credentials)Middleware that sends credentials as the document's security specifies; see Authentication
DecodeErrorThrown for a body that does not match its Content-Type; see Errors
Schema types, such as Pet and AccountRequest, and enum values, such as KindSee Schemas
pathsEvery operation's inputs, responses and the statuses each response covers, by path and method
schemas, componentsThe document's schemas by name; see Schemas
ApiResponse<Operation>The result type of a call, from an operation in paths
Client<Paths>, ClientOptions, RequestOptionsThe client, its options, and the fetch options of a call
Middleware, MiddlewareContext, OperationInfoMiddleware and the information it receives; see Middleware
Credential, SecurityRequirement, SecuritySchemeNameThe types that auth uses
ServerSentEventOne item of a text/event-stream response: { data, event?, id?, retry? }

ClientOptions ​

OptionTypeDescription
baseUrlstringReplaces every server in the document
headersHeadersInit, or a function that returns it, possibly asyncSent with every request
middlewareMiddleware[]Wraps every request, outermost first
fetch(request: Request) => Promise<Response>Called instead of the global fetch

Middleware ​

ts
type Middleware = (
  request: Request,
  next: (request: Request) => Promise<Response>,
  context: { operation: OperationInfo },
) => Promise<Response>;
OperationInfo fieldTypeDescription
methodstringThe HTTP method, as used in the call
pathstringThe path template, such as /pets/{petId}
operationIdstring, optionalThe operationId, when the document has one
securitySecurityRequirement[]The security requirements that apply, in document order

Credential ​

ts
type Credential =
  | string
  | ((
      scopes: string[],
      context: MiddlewareContext,
    ) => string | undefined | Promise<string | undefined>);

undefined or "" means that the credential is unavailable. auth also accepts undefined in place of a Credential.

Typing results ​

ApiResponse takes an operation from paths:

ts
import type { 
ApiResponse
, paths } from "./api.ts";
type
GetPet
=
ApiResponse
<paths["/pets/{petId}"]["GET"]>;