nwaku/waku/waku_api/rest/responses.nim
Ivan FB fd6a71cdd7
chore: Bump dependencies for v0.31.0 (#2885)
* bump_dependencies.md: add nim-results dependency
* change imports stew/results to results
* switching to Nim 2.0.8
* waku.nimble: reflect the requirement nim 1.6.0 to 2.0.8
  Adding --mm:refc as nim 2.0 enables a new garbage collector that we're
  not yet ready to support
* adapt waku code to Nim 2.0
* gcsafe adaptations because Nim 2.0 is more strict
2024-07-09 13:14:28 +02:00

46 lines
1.6 KiB
Nim

{.push raises: [].}
import std/typetraits, results, chronicles, presto/common
import ./serdes
const MIMETYPE_JSON* = MediaType.init("application/json")
const MIMETYPE_TEXT* = MediaType.init("text/plain")
proc ok*(t: typedesc[RestApiResponse]): RestApiResponse =
RestApiResponse.response("OK", Http200, $MIMETYPE_TEXT)
proc internalServerError*(
t: typedesc[RestApiResponse], msg: string = ""
): RestApiResponse =
RestApiResponse.error(Http500, msg, $MIMETYPE_TEXT)
proc serviceUnavailable*(
t: typedesc[RestApiResponse], msg: string = ""
): RestApiResponse =
RestApiResponse.error(Http503, msg, $MIMETYPE_TEXT)
proc badRequest*(t: typedesc[RestApiResponse], msg: string = ""): RestApiResponse =
RestApiResponse.error(Http400, msg, $MIMETYPE_TEXT)
proc notFound*(t: typedesc[RestApiResponse], msg: string = ""): RestApiResponse =
RestApiResponse.error(Http404, msg, $MIMETYPE_TEXT)
proc preconditionFailed*(
t: typedesc[RestApiResponse], msg: string = ""
): RestApiResponse =
RestApiResponse.error(Http412, msg, $MIMETYPE_TEXT)
proc tooManyRequests*(t: typedesc[RestApiResponse], msg: string = ""): RestApiResponse =
RestApiResponse.error(Http429, msg, $MIMETYPE_TEXT)
proc jsonResponse*(
t: typedesc[RestApiResponse], data: auto, status: HttpCode = Http200
): SerdesResult[RestApiResponse] =
let encoded = ?encodeIntoJsonBytes(data)
ok(RestApiResponse.response(encoded, status, $MIMETYPE_JSON))
proc textResponse*(
t: typedesc[RestApiResponse], data: string, status: HttpCode = Http200
): RestApiResponse =
RestApiResponse.response(data, status, $MIMETYPE_TEXT)