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 src/nodejs/jsasynchttpclient
import std/[asyncjs, jsfetch, uri]

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

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

  block:
    let url: Uri = parseUri("http://httpbin.org/delete")
    let content: cstring = await client.deleteContent(url)
    let response: Response = await client.delete(url)

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

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

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

discard example()

Types

JsAsyncHttpClient = ref object of JsRoot

Procs

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