about summary refs log tree commit diff stats
path: root/src/io/response.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/response.nim')
-rw-r--r--src/io/response.nim50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/io/response.nim b/src/io/response.nim
new file mode 100644
index 00000000..7d0f789a
--- /dev/null
+++ b/src/io/response.nim
@@ -0,0 +1,50 @@
+import streams
+
+import bindings/quickjs
+import io/promise
+import io/request
+import js/javascript
+import types/url
+
+type
+  Response* = ref object
+    fd*: int
+    body*: Stream
+    bodyUsed* {.jsget.}: bool
+    res* {.jsget.}: int
+    contenttype* {.jsget.}: string
+    status* {.jsget.}: int
+    headers* {.jsget.}: Headers
+    redirect*: Request
+    url*: URL #TODO should be urllist?
+    unregisterFun*: proc()
+    bodyRead*: Promise[string]
+
+proc newResponse*(res: int, request: Request, fd = -1, stream: Stream = nil):
+    Response =
+  return Response(
+    res: res,
+    url: request.url,
+    body: stream,
+    bodyRead: Promise[string](),
+    fd: fd
+  )
+
+#TODO: this should be a property of body
+proc close*(response: Response) {.jsfunc.} =
+  response.bodyUsed = true
+  if response.unregisterFun != nil:
+    response.unregisterFun()
+  if response.body != nil:
+    response.body.close()
+
+proc text*(response: Response): Promise[string] {.jsfunc.} =
+  return response.bodyRead
+
+proc json(ctx: JSContext, this: Response): Promise[JSValue] {.jsfunc.} =
+  return this.text().then(proc(s: string): JSValue =
+    return JS_ParseJSON(ctx, cstring(s), cast[csize_t](s.len),
+      cstring"<input>"))
+
+proc addResponseModule*(ctx: JSContext) =
+  ctx.registerType(Response)