2022-06-02 09:45:00 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import std/typetraits
|
|
|
|
import
|
|
|
|
chronicles,
|
|
|
|
stew/results,
|
|
|
|
presto/common
|
|
|
|
import "."/serdes
|
|
|
|
|
|
|
|
|
|
|
|
const MIMETYPE_JSON* = MediaType.init("application/json")
|
2022-06-10 11:30:51 +00:00
|
|
|
const MIMETYPE_TEXT* = MediaType.init("text/plain")
|
2022-06-02 09:45:00 +00:00
|
|
|
|
|
|
|
proc jsonResponse*(t: typedesc[RestApiResponse], data: auto, status: HttpCode = Http200): SerdesResult[RestApiResponse] =
|
|
|
|
let encoded = ?encodeIntoJsonBytes(data)
|
|
|
|
ok(RestApiResponse.response(encoded, status, $MIMETYPE_JSON))
|
|
|
|
|
|
|
|
proc internalServerError*(t: typedesc[RestApiResponse]): RestApiResponse =
|
2022-06-10 11:30:51 +00:00
|
|
|
RestApiResponse.error(Http500)
|
|
|
|
|
|
|
|
proc ok*(t: typedesc[RestApiResponse]): RestApiResponse =
|
|
|
|
RestApiResponse.response("OK", status=Http200, contentType="text/plain")
|
|
|
|
|
|
|
|
proc badRequest*(t: typedesc[RestApiResponse]): RestApiResponse =
|
|
|
|
RestApiResponse.error(Http400)
|
|
|
|
|
|
|
|
|
|
|
|
proc notFound*(t: typedesc[RestApiResponse]): RestApiResponse =
|
|
|
|
RestApiResponse.error(Http404)
|