Generate type-safe ReScript clients from OpenAPI specifications.
OpenAPI is the de facto standard for describing REST APIs. This tool generates:
-
Type definitions - Records, variants, aliases from schemas
-
Runtime validators - Using
rescript-schemafor safe JSON parsing -
HTTP clients - Pluggable HTTP layer (fetch by default, bring your own)
-
Operation aliases - Multiple ways to call the same endpoint
cargo install rescript-openapiOr build from source:
git clone https://github.com/hyperpolymath/rescript-openapi
cd rescript-openapi
cargo build --releaserescript-openapi generate -i openapi.yaml -o src/apiThis generates:
-
ApiTypes.res- All type definitions -
ApiSchema.res- rescript-schema validators -
ApiClient.res- HTTP client with fetch
| Flag | Description | Default |
|---|---|---|
|
Path to OpenAPI spec (JSON/YAML) |
Required |
|
Output directory |
|
|
Module name prefix |
|
|
Generate rescript-schema validators |
|
|
Generate HTTP client |
|
/** A user in the system */
type user = {
id: string,
@as("full_name") fullName: string,
email: string,
role: option<userRole>,
}
type userRole = [
| #Admin
| #User
| #Guest
]module S = RescriptSchema
let userSchema: S.t<user> = S.object(s => ({
id: s.field("id", S.string),
fullName: s.field("full_name", S.string),
email: s.field("email", S.string),
role: s.fieldOr("role", S.option(userRoleSchema), None),
}: user))
let parseUser = (json: Js.Json.t): user => {
S.parseJsonOrThrow(json, userSchema)
}
let serializeUser = (value: user): Js.Json.t => {
S.reverseConvertToJsonOrThrow(value, userSchema)
}// Default client using fetch
module Client = Make(FetchClient)
// Usage
let config = makeConfig(~baseUrl="https://api.example.com", ())
let result = await Client.getUser(config, ~id="123", ())
switch result {
| Ok(user) => Console.log(user.fullName)
| Error(err) => Console.error(err.message)
}// Implement the HttpClient signature
module AxiosClient: HttpClient = {
let request = async (req: httpRequest) => {
// Use axios, ky, got, or any HTTP library
let response = await Axios.request({
method: req.method->methodToString,
url: req.url,
headers: req.headers,
data: req.body,
})
Ok(response.data)
}
}
// Create client with custom HTTP
module Api = Make(AxiosClient)Add to your rescript.json:
{
"bs-dependencies": [
"@rescript/core",
"rescript-schema",
"@glennsl/rescript-fetch"
]
}Install with npm:
npm install @rescript/core rescript-schema @glennsl/rescript-fetch-
❏ OpenAPI 3.1 support
-
❏
oneOf/anyOfdiscriminated unions -
❏ File upload/download
-
❏ Streaming responses
-
❏ WebSocket support
-
❏ GraphQL schema generation
This tool is part of the ReScript Full Stack ecosystem.