Skip to content

Commit

Permalink
Add jsheaders (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlospaco authored Dec 26, 2020
1 parent e942c64 commit 23be8e3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
convenience functions to change file permissions using Unix like octal file permissions.
- Added module `scripting` providing `withDir` to switch the directory temporarily. This
was previously only available in the `nimscript` module.


- Added `jsheaders` module for [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) for the JavaScript target.
13 changes: 11 additions & 2 deletions src/fusion/docutils.nim
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import std/[os,strformat,sugar,osproc]
import std/[os, strformat, sugar, osproc]
import std/private/globs


const blockList =
when not defined(js): ["nimcache", "htmldocs", "js"]
else: ["nimcache", "htmldocs"]

iterator findNimSrcFiles*(dir: string): string =
proc follow(a: PathEntry): bool =
a.path.lastPathPart notin ["nimcache", "htmldocs"]
a.path.lastPathPart notin blockList

for entry in walkDirRecFilter(dir, follow = follow):
if entry.path.splitFile.ext == ".nim" and entry.kind == pcFile:
yield entry.path


proc genCodeImportAll*(dir: string): string =
result = "{.warning[UnusedImport]: off.}\n"
for a in findNimSrcFiles(dir):
let s = "".dup(addQuoted(a))
result.add &"import {s}\n"


proc genDocs(dir: string, nim = "", args: seq[string]) =
let code = genCodeImportAll(dir)
let extra = quoteShellCommand(args)
Expand All @@ -22,6 +30,7 @@ proc genDocs(dir: string, nim = "", args: seq[string]) =
if ret.exitCode != 0:
doAssert false, ret.output & "\n" & code


when isMainModule:
let args = commandLineParams()
doAssert args.len >= 1
Expand Down
51 changes: 51 additions & 0 deletions src/fusion/js/jsheaders.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## - HTTP Headers for the JavaScript target: https://developer.mozilla.org/en-US/docs/Web/API/Headers
when not defined(js) and not defined(nimdoc):
{.fatal: "Module jsheaders is designed to be used with the JavaScript backend.".}

type Headers* = ref object of JsRoot ## HTTP Headers for the JavaScript target.

func newHeaders*(): Headers {.importjs: "new Headers()".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers

func add*(this: Headers; key: cstring; value: cstring) {.importjs: "#.append(#, #)".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/append

func delete*(this: Headers; key: cstring) {.importjs: "#.$1(#)".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/delete

func hasKey*(this: Headers; key: cstring): bool {.importjs: "#.has(#)".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/has

func keys*(this: Headers): seq[cstring] {.importjs: "Array.from(#.$1())".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/keys

func values*(this: Headers): seq[cstring] {.importjs: "Array.from(#.$1())".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/values

func entries*(this: Headers): seq[array[2, cstring]] {.importjs: "Array.from(#.$1())".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries

func `[]`*(this: Headers; key: cstring): cstring {.importjs: "#.get(#)".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/get

func `[]=`*(this: Headers; key: cstring; value: cstring) {.importjs: "#.set(#, #)".}
## https://developer.mozilla.org/en-US/docs/Web/API/Headers/set

func clear*(this: Headers) {.importjs:
"(() => { const header = #; Array.from(header.keys()).forEach((key) => header.delete(key)) })()".}
## Convenience func to delete all items from `Headers`.


runnableExamples:
if defined(fusionJsHeadersTests):
let header = newHeaders()
header.add("key", "value")
doAssert header.hasKey("key")
doAssert header.keys() == @["key".cstring]
doAssert header.values() == @["value".cstring]
doAssert header["key"] == "value".cstring
header["other"] = "another".cstring
doAssert header["other"] == "another".cstring
doAssert header.entries() == @[["key".cstring, "value"], ["other".cstring, "another"]]
header.delete("other")
doAssert header.entries() == @[]

0 comments on commit 23be8e3

Please sign in to comment.