src/nodejs/jsasynchttpclient

  • Async HttpClient for JavaScript targets implemented as sugar on top of std/jsfetch

Example: cmd: -d:nimExperimentalJsfetch -d:nimExperimentalAsyncjsThen -r:off

import std/[asyncjs, jsfetch, uri]

proc example(): Future[void] {.async.} =
  let client = newJsAsyncHttpClient()
  const data = """{"key": "value"}"""

  block:
    let content = await client.getContent("http://nim-lang.org")
    let response = await client.get("http://nim-lang.org")

  block:
    let content = await client.deleteContent("http://httpbin.org/delete")
    let response = await client.delete("http://httpbin.org/delete")

  block:
    let content = await client.postContent("http://httpbin.org/post", data)
    let response = await client.post("http://httpbin.org/post", data)

  block:
    let content = await client.putContent("http://httpbin.org/put", data)
    let response = await client.put("http://httpbin.org/put", data)

  block:
    let content = await client.patchContent("http://httpbin.org/patch", data)
    let response = await client.patch("http://httpbin.org/patch", data)

discard example()

Types

JsAsyncHttpClient = ref object of JsRoot

Procs

proc delete(self: JsAsyncHttpClient; url: string or cstring): Future[Response] {.
    codegenDecl: "async function $2($3)".}
proc deleteContent(self: JsAsyncHttpClient; url: string or cstring): Future[
    cstring] {.codegenDecl: "async function $2($3)".}
proc get(self: JsAsyncHttpClient; url: string or cstring): Future[Response] {.
    codegenDecl: "async function $2($3)".}
proc getContent(self: JsAsyncHttpClient; url: string or cstring): Future[cstring] {.
    codegenDecl: "async function $2($3)".}
proc head(self: JsAsyncHttpClient; url: string or cstring): Future[Response] {.
    codegenDecl: "async function $2($3)".}
func newJsAsyncHttpClient(): JsAsyncHttpClient {....raises: [], tags: [].}
proc patch(self: JsAsyncHttpClient; url: string or cstring; body = ""): Future[
    Response] {.codegenDecl: "async function $2($3)".}
proc patchContent(self: JsAsyncHttpClient; url: string or cstring; body = ""): Future[
    cstring] {.codegenDecl: "async function $2($3)".}
proc post(self: JsAsyncHttpClient; url: string or cstring; body = ""): Future[
    Response] {.codegenDecl: "async function $2($3)".}
proc postContent(self: JsAsyncHttpClient; url: string or cstring; body = ""): Future[
    cstring] {.codegenDecl: "async function $2($3)".}
proc put(self: JsAsyncHttpClient; url: string or cstring; body = ""): Future[
    Response] {.codegenDecl: "async function $2($3)".}
proc putContent(self: JsAsyncHttpClient; url: string or cstring; body = ""): Future[
    cstring] {.codegenDecl: "async function $2($3)".}