3 Commits
0.1.0 ... 0.2.1

5 changed files with 25 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.*.sw? .*.sw?
nimcache/ nimcache/
test/runner

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.1.0" version = "0.2.1"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "JDB Software's opinionated extensions and auth layer for Jester." description = "JDB Software's opinionated extensions and auth layer for Jester."
license = "MIT" license = "MIT"

View File

@ -1,4 +1,4 @@
import std/json, std/logging, std/strutils, std/sequtils import std/json, std/logging, std/options, std/strutils, std/sequtils
import jester, namespaced_logging import jester, namespaced_logging
import ./apierror import ./apierror
@ -29,8 +29,8 @@ template halt*(
break allRoutes break allRoutes
template sendJsonResp*( template sendJsonResp*(
code: HttpCode, body: JsonNode,
body: string = "", code: HttpCode = Http200,
knownOrigins: seq[string], knownOrigins: seq[string],
headersToSend: RawHeaders) = headersToSend: RawHeaders) =
## Immediately send a JSON response and stop processing the request. ## Immediately send a JSON response and stop processing the request.
@ -47,7 +47,7 @@ template sendJsonResp*(
"Access-Control-Allow-Headers": "Authorization,X-CSRF-TOKEN" "Access-Control-Allow-Headers": "Authorization,X-CSRF-TOKEN"
} }
else: else:
log().warn "Unrecognized Origin '" & reqOrigin & "', excluding CORS headers." log().debug "Unrecognized Origin '" & reqOrigin & "', excluding CORS headers."
@{:} @{:}
halt( halt(
@ -56,16 +56,29 @@ template sendJsonResp*(
"Content-Type": CONTENT_TYPE_JSON, "Content-Type": CONTENT_TYPE_JSON,
"Cache-Control": "no-cache" "Cache-Control": "no-cache"
}, },
body $body
) )
proc makeDataBody*(data: JsonNode): string = $(%*{"details":"","data":data }) proc makeDataBody*(
proc makeStatusBody*(details: string): string = $(%*{"details":details}) data: JsonNode,
nextOffset = none[int](),
totalItems = none[int](),
nextLink = none[string](),
prevLink = none[string]()): JsonNode =
result = %*{"details":"","data":data}
if nextOffset.isSome: result["nextOffset"] = %nextOffset.get
if totalItems.isSome: result["totalItems"] = %totalItems.get
if nextLink.isSome: result["next"] = %nextLink.get
if prevLink.isSome: result["prev"] = %prevLink.get
proc makeStatusBody*(details: string): JsonNode = %*{"details":details}
template sendErrorResp*(err: ref ApiError, knownOrigins: seq[string]): void = template sendErrorResp*(err: ref ApiError, knownOrigins: seq[string]): void =
log().debug err.respMsg & ( if err.msg.len > 0: ": " & err.msg else: "") log().debug err.respMsg & ( if err.msg.len > 0: ": " & err.msg else: "")
if not err.parent.isNil: log().debug " original exception: " & err.parent.msg if not err.parent.isNil: log().debug " original exception: " & err.parent.msg
sendJsonResp(err.respCode, makeStatusBody(err.respMsg), knownOrigins, @{:}) sendJsonResp(makeStatusBody(err.respMsg), err.respCode, knownOrigins, @{:})
## CORS support ## CORS support
template sendOptionsResp*( template sendOptionsResp*(
@ -85,7 +98,7 @@ template sendOptionsResp*(
"Access-Control-Allow-Headers": "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-TOKEN" "Access-Control-Allow-Headers": "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-CSRF-TOKEN"
} }
else: else:
log().warn "Unrecognized Origin '" & reqOrigin & "', excluding CORS headers." log().debug "Unrecognized Origin '" & reqOrigin & "', excluding CORS headers."
log().debug "Valid origins: " & knownOrigins.join(", ") log().debug "Valid origins: " & knownOrigins.join(", ")
@{:} @{:}

Binary file not shown.

View File

@ -1,3 +1,3 @@
import unittest import unittest
import ./tauth, ./tjson_util import ./tauth, ./tjsonutils