oas_generator

Generate HTTP clients from Open API specs.


License
Other

Documentation

OAS Generator

Generate Gleam clients from Open API Specs.

Package Version Hex Docs

โœจ Features

  • ๐Ÿฆ„ Generates Gleam HTTP client code from OpenAPI 3.0+ specs
  • ๐Ÿ”’ Produces typesafe function signatures and records
  • ๐Ÿงฉ Compatible with gleam/httpc or gleam/fetch for use on front end or backend.
  • ๐Ÿ’พ Parses JSON responses into Gleam types

Doesn't support all uses of datatype composition for example AnyOf and OneOf.

For server side generation see this note

๐Ÿ› ๏ธ Usage

Install as a dev dependency.

gleam add --dev oas_generator@1

Add the utils dependency (not a dev dependency).

gleam add oas_generator_utils@1

Fetch the JSON version of your API specification.

An example for a petstore is available here

Create a module that will call the build function.

Adding the module to the test dir means it is not added to the published library. Support for a dev directory is coming later https://discord.com/channels/768594524158427167/1047099923897794590/threads/1365928769956610090

// test/petstore/dev
import gleam/io
import oas/generator
import snag

pub fn main() {
  case generator.build("./priv/petstore.openapi.json", ".", "petstore") {
    Ok(_) -> Nil
    Error(reason) -> io.print(snag.pretty_print(reason))
  }
}

Run the generator.

gleam run -m petstore/dev

Add functions to build your base request.

OAS Generator does not support authentication yet, you will need to add this yourself.

To handle this the generated code leaves the following as hooks to implement in the <project>.gleam file. The required functions are handle_errors and base_request. To see an example of these check out the netlify implementation here

Code above the // GENERATED ------------- line will be left alone.

OAS Generator uses Midas to compose request and response functions.

If you do not want to use Midas you can delete the top project file and use the functions in operations.gleam directly. Running with midas requires you to use a runner. Runners are available for node and the browser. It is also faily easy to write your own runner, if you wish to only handle the fetch effect

For further help reach out in the Gleam Discord

๐Ÿ“š Documentation

Further documentation can be found at https://hexdocs.pm/oas_generator.

๐Ÿงช Development

gleam test  # Run the tests

๐Ÿง‘โ€๐Ÿ’ป Credit

Created for Spotless, a code first integration platform.

๐Ÿ“œ Notes

never validate

Because the decoder requires a zero value, decoding of a never valid has to panic.

Server generation

I see no reason why server generation could not be added but it's not on my agenda to build a server side generator.