import { HttpResponse } from "node.http.jsig"

type JSONSchema<containingType: S> : Object
type Statsd : {
    increment: (Array<String>) => void,
    timing: (Array<String>, Number) => void
}
type MikealRequest : (opts: {
    url: String,
    method: String,
    headers: Object,
    timeout: Number,
    json: true | Any
}, cb: (Error, HttpResponse & {
    body: Any
}) => void) => void

type HandlerOptions : {
    requestSchema: JSONSchema,
    responseSchema: JSONSchema,
    resource: String
}

type TypedRequest : {
    url: String,
    method?: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" |
        "DELETE" | "TRACE" | "PATCH",
    query?: Object<String, String>,
    headers?: Object<String, String>,
    body?: Any
}

type TypedResponse : {
    httpVersion: String,
    statusCode: Number,
    headers: Object<String, String>
    body?: Any
}

typed-request-client : ({
    clientName: String,
    statsd: Statsd,
    request?: MikealRequest,
    now?: () => Number
}) => (
    typedRequest: TypedRequest,
    opts: HandlerOptions,
    cb: (Error, TypedResponse) => void
) => void

type ValidationError : Error & {
    message: String,
    type: 'ValidationError',
    errors: Array<Error>
}

type Result<E <: Error, O> => {
    type: 'ok',
    ok: O,
    error: null
} | {
    type: 'error',
    ok: null,
    error: E
}

typed-request-client/validate-shape : (
    shape: T, schema: JSONSchema<S>
) => Result<ValidationError, S <: T>
