// An API's documentation read from its OpenAPI file: a list of the operations, and a mock
// server that answers each request with the operation it would reach.
//
//   quillo run openapi.qwe      then open http://localhost:8080/docs

use "openapi.qwer"

let SPEC = "openapi_sample.yaml"

route GET /docs {
    let info = openapi_info(SPEC)?
    let ops = openapi_operations(SPEC)?
    {
        api: `${info.title} ${info.version}`,
        operations: ops | map((o) => `${o.method} ${o.path}: ${o.operation_id}${o.deprecated ? " (deprecated)" : ""}`),
        list_params: (find(ops, (o) => o.operation_id == "listPets") ?? { parameters: [] }).parameters | map((p) => `${p.name}: ${p.type}`),
    }
}

// The mock: what the API would do with this request.
route ANY /mock/*rest {
    let m = openapi_match(SPEC, request.method, `/${rest}`)?
    if m == null { reply 404 { error: `no operation for ${request.method} /${rest}` } }
    { operation: m.operation_id, params: m.params, answers: m.operation.responses }
}
