diff --git a/changelog.md b/changelog.md index 7ceb6a55..1c4cfb90 100644 --- a/changelog.md +++ b/changelog.md @@ -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. diff --git a/src/fusion/docutils.nim b/src/fusion/docutils.nim index 78bd1025..f7cf7bd1 100644 --- a/src/fusion/docutils.nim +++ b/src/fusion/docutils.nim @@ -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) @@ -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 diff --git a/src/fusion/js/jsheaders.nim b/src/fusion/js/jsheaders.nim new file mode 100644 index 00000000..203489c5 --- /dev/null +++ b/src/fusion/js/jsheaders.nim @@ -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() == @[]