oaspec/config
Types
One generation target: which spec to read, what to generate and where to write it.
load and load_all build it from an oaspec.yaml. To build one in
code, write the record and change fields with the record update syntax.
Examples
let cfg =
config.Config(
input: "openapi.yaml",
output_server: "./src/api",
output_client: "./src/api_client",
package: "api",
mode: config.Both,
validate: True,
include: config.Include(tags: [], paths: []),
handler_layout: config.SingleFile,
)
config.Config(..cfg, mode: config.Client)
pub type Config {
Config(
input: String,
output_server: String,
output_client: String,
package: String,
mode: GenerateMode,
validate: Bool,
include: Include,
handler_layout: HandlerLayout,
)
}
Constructors
-
Config( input: String, output_server: String, output_client: String, package: String, mode: GenerateMode, validate: Bool, include: Include, handler_layout: HandlerLayout, )Arguments
- input
-
Path of the OpenAPI document.
- output_server
-
Directory the server modules are written to; it must end in
package. - output_client
-
Directory the client modules are written to; it must end in
packageor<package>_client. - package
-
Module prefix of the generated code, such as
apiforimport api/types. - validate
-
Whether the generated server checks request values against the schema’s constraints before calling a handler.
A reason load or load_all could not build a Config.
pub type ConfigError {
FileNotFound(path: String)
CannotReadFile(path: String, reason: simplifile.FileError)
InvalidYaml(detail: String)
MissingField(field: String)
InvalidValue(field: String, detail: String)
}
Constructors
-
FileNotFound(path: String)No file exists at the path.
-
CannotReadFile(path: String, reason: simplifile.FileError)The file exists but could not be read.
-
InvalidYaml(detail: String)The file is not YAML, or the YAML does not parse.
-
MissingField(field: String)A required key, such as
input, is missing. -
InvalidValue(field: String, detail: String)A key has a value oaspec does not accept.
fieldis the key path, such asmodeortargets[1].package.
Which side of the API to generate.
pub type GenerateMode {
Server
Client
Both
}
Constructors
-
ServerTypes, decoders, router and handler stubs for a server.
-
ClientTypes, decoders and a client.
-
BothBoth of the above, into the two output directories.
How the user-owned server handler stubs are laid out on disk.
SingleFile (the default, handlers: single) writes every stub into
handlers.gleam. PerOperation (handlers: per_operation) writes
handlers.gleam with only the State type and one
handlers/<operation>.gleam per operation, so an operation added to
the spec gets its own stub on the next oaspec generate while the
files already written are left alone.
pub type HandlerLayout {
SingleFile
PerOperation
}
Constructors
-
SingleFile -
PerOperation
Subset filter for codegen. When non-empty, only
operations matching at least one of the listed tags or paths
are kept; the others are dropped before capability check, hoist,
and validate.
Path patterns may end in /** to match any path under a prefix
("/repos/**" matches /repos/foo, /repos/foo/bar, …);
otherwise the pattern is compared by exact string equality.
tags and paths combine with OR semantics — an operation is
kept if EITHER its tag list intersects Include.tags OR its path
matches one of Include.paths. With both lists empty (the
default), no filtering is applied.
pub type Include {
Include(tags: List(String), paths: List(String))
}
Constructors
-
Include(tags: List(String), paths: List(String))
Values
pub fn error_to_string(error: ConfigError) -> String
Returns a one-line description of a config error.
Examples
config.error_to_string(config.MissingField("input"))
// -> "Missing required config field: input"
pub fn load(path: String) -> Result(Config, ConfigError)
Reads an oaspec.yaml that declares one target.
Paths are resolved against the current directory. A config with more
than one entry under targets: is an InvalidValue error; use
load_all for those.
Examples
config.load("oaspec.yaml")
// -> Ok(Config(input: "openapi.yaml", package: "api", ..))
config.load("missing.yaml")
// -> Error(FileNotFound("missing.yaml"))
pub fn load_all(
path: String,
) -> Result(List(Config), ConfigError)
Reads an oaspec.yaml and returns one Config per target.
Without a targets: key the file is a single target. With one, each
entry gets its own package, output and include, and shares
input, mode, validate and handlers from the top level. When
validate is absent it defaults to True for server and both
and to False for client; mode defaults to both and the output
directory to ./src.
Examples
config.load_all("oaspec.yaml")
// -> Ok([Config(package: "github", ..), Config(package: "stripe", ..)])
config.load_all("oaspec.toml")
// -> Error(InvalidYaml("config files must be YAML; got '.toml' extension. ..."))