Fork me on GitHub
#clojurescript
<
2023-09-13
>
stagmoose03:09:12

After require ing a npm package, is it possible to inspect all objs or methods behind the namespace? btw, why [cljs.repl/dir](https://cljs.github.io/api/cljs.repl/dir) is not working in my cljs repl

cljs꞉dudee.core꞉> 
cljs.repl
#js {:print_doc #object[cljs$repl$print_doc], :Error__GT_map #object[cljs$repl$Error__GT_map], :ex_triage #object[cljs$repl$ex_triage], :ex_str #object[cljs$repl$ex_str], :error__GT_str #object[cljs$repl$error__GT_str]}
cljs꞉dudee.core꞉> 
cljs.repl/dir
nil
; ------ WARNING - :undeclared-var -----------------------------------------------
;  Resource: <eval>:1:1
;  Can't take value of macro cljs.repl/dir
; --------------------------------------------------------------------------------

thheller06:09:51

As the error says, dir is a macro. so you cannot "get" it like a regular var

thheller06:09:04

you can still use it though. so (dir cljs.core) or whatever will work just fine

thheller06:09:37

but dir will not work for npm packages, as dir only supports cljs namespaces

thheller06:09:25

you can use (require '["whatever-npm" :as x]) (js/console.log x) or (js/console.dir x)

👍 1
stagmoose09:09:01

Thanks for your help!

Frank Henard20:09:37

I want to interop with this in cljs:

const jwksClient = require('jwks-rsa');

const client = jwksClient({
  jwksUri: '',
  requestHeaders: {}, // Optional
  timeout: 30000 // Defaults to 30s
});
I'm using this in cljs (and variations like it):
(ns my-ns
  (:require [jwks-rsa]))

(comment
  (jwks-rsa #js {:jwksUri "blahblah"})
  )
It doesn't like me calling jwks-rsa directly. Any ideas what I should be doing differently?

Frank Henard20:09:55

I've also tried

...(:require [jwks-rsa :as jwks-client])

(jwks-client ...)
(js/jwks-client ...)
(jwks-client/jwks-client ...)
(new jwks-client ...)

Frank Henard20:09:59

Thank you @U4EFBUCUE It turns out I needed

[jwks-rsa :refer [JwksClient]]

(new JwksClient ...)

bcoop71323:09:00

Hey everyone, what are some ways of converting a clojure form into a javascript string inside a clojure app? I've looked into the cljs comipler as well as squint/cherry but haven't seen an obvious answer.

bcoop71323:09:21

for example:

(defn double [n] (+ n n))

(defn handler [_]
  {:status 200,
   :headers {"content-type" "text/javascript"}
   :body (clj->js double)})
what could provide the clj->js function?

Sam Ferrell00:09:20

Unless you literally want to compile double as cljs? I'm not sure if thats feasible

bcoop71300:09:25

Thanks for the reply. I'd expect the string output to be something similar to function double(n) {n + n} or at least something that behaves similarly inside of a js interpreter

Nasiru Ibrahim02:09:34

I think you'd need to write a simple interpreter for that (if there's no way out) I wrote a simple interpreter sometime ago (not complete though) that can interpret some clj forms and run them. This can be transformed to JS forms of its that important and Maybe be helpful for you (if you don't mind) https://github.com/naxiwaynez10/hw6