Fork me on GitHub
#clojurescript
<
2022-10-19
>
Rambabu Patina05:10:10

Hi, How to use npm modules correctly in clojurescript for the below code

import axios from 'axios';
import { aws4Interceptor } from "aws4-axios";

const client = axios.create({ baseURL: BASE_UEL, headers: {appkey: 'test'}});
client.interceptors.request.use(aws4Interceptor({
        region: AWS_REGION,
        service: "execute-api",
    },
    {
        accessKeyId: AWS_ACCESS_KEY_ID,
        secretAccessKey: AWS_SECRET_ACCESS_KEY,
    }
));
(PATH, JSON.stringify(body))

Rambabu Patina06:10:58

Thanks @U2FRKM4TW for you quick response. I use shadow-js and could able to import. But on runtime getting issues such as TypeError: Cannot read property 'url' of undefined

Rambabu Patina06:10:04

Here is my code

["axios" :as axios]
["aws4-axios" :refer [aws4Interceptor]]
...

(p/let [instance (->
                   (axios/create
                     {:baseURL js/process.env.BASE_URL
                      :headers {:appkey (:key event)}}))
        interseptor (aws4Interceptor
                      {:region js/process.env.AWS_REGION
                       :service 'execute-api'}
                      {:accessKeyId js/process.env.AWS_ACCESS_KEY_ID
                       :secretAccessKey js/process.env.AWS_SECRET_ACCESS_KEY})]
(.use (.. instance -interceptors -request) (interseptor))
)

p-himik06:10:17

> Cannot read property url Your code doesn't mention the url property anywhere. Can you provide a full stack trace of that error? Also, note that you still have to pass JS objects to JS functions that expect them - you can't just shove CLJS data in there. Meaning, all those {...} you pass to axios functions should actually be #js {...}. Including the nested ones, because #js is shallow.

Rambabu Patina06:10:30

Now I am passing js objects. The error stack is

>  (node:50334) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'url' of undefined
>      at /Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:93:33
>      at step (/Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:33:23)
>      at Object.next (/Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:14:53)
>      at /Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:8:71
>      at new Promise (<anonymous>)
>      at __awaiter (/Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:4:12)
>      at /Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:88:39
>      at $server$utils$register_with_ac [as register_with_ac] (/Users/rambabupatina/sde/development/app-folder/.shadow-cljs/builds/functions/dev/out/cljs-runtime/app/server/utils.cljs:48:49)
>      at switch__40487__auto__ (/Users/rambabupatina/sde/development/app-folder/.shadow-cljs/builds/functions/dev/out/cljs-runtime/app/server/installations.cljs:42:11)
>      at /Users/rambabupatina/sde/development/app-folder/.shadow-cljs/builds/functions/dev/out/cljs-runtime/app/server/installations.cljs:42:11

p-himik06:10:57

And what is going on at app/server/utils.cljs:48:49?

Rambabu Patina06:10:16

The line (.use (.. instance -interceptors -request) (interseptor)) Changing to (.use (.. instance -interceptors -request) interseptor) resolved that issue. Thanks for narrowing down the issue. But now I got another issue now

>  (node:53573) UnhandledPromiseRejectionWarning: Error: No URL present in request config, unable to sign request
>      at /Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:94:31
>      at step (/Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:33:23)
>      at Object.next (/Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:14:53)
>      at /Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:8:71
>      at new Promise (<anonymous>)
>      at __awaiter (/Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:4:12)
>      at /Users/rambabupatina/sde/development/app-folder/functions/node_modules/aws4-axios/dist/interceptor.js:88:39
>      at processTicksAndRejections (internal/process/task_queues.js:95:5)
>  (Use `node --trace-warnings ...` to show where the warning was created)

Rambabu Patina06:10:44

Though I am passing url

p-himik06:10:28

> Changing to [...] resolved that issue. Nice! > But now I got another issue As you can imagine, debugging someone else's code via a text chat is a logistical nightmare. :) NodeJS has debugging capabilities. Just add a breakpoint in that place, relaunch the app in the debugging mode, and poke around.

Rambabu Patina06:10:34

I agree. Sure I will try that. Thanks for awesome inputs!

👍 1
sirwobin12:10:28

In ns1 I have (def ^:dynamic sentinel true) and in ns2 I use (binding [ns1/sentinel false] ...) where the body inside the binding calls promises. The binding isn't effective when functions inside the promise run. Doesn't seem to work with-redefs either. Am I missing some magic?

thheller12:10:47

both don't work in async contexts

sirwobin12:10:35

ah, so much to learn in cljs land coming from jvm. Thank you

thheller12:10:04

no difference on the the JVM. clojure has bound-fn to carry over bindings, cljs does not

👍 1
Joshua Suskalo19:10:42

The bit-count function only works with integer-ish numbers with 32 bits or less, but since JS numbers are doubles that means you get ~51 bits of integer precision, and that means that bit-count is poorly-behaved on large integer-ish numbers. Is this something that was explicitly considered? With bit-count being a cljs-only function I'd think that it would be designed specifically for JS types, but it appears to be designed with C integers in mind since that's where the algorithm used is most often defined.

Joshua Suskalo19:10:59

Makes sense! That's the answer I was looking for

souenzzo21:10:51

I'm trying to migrate from cljsbuild to shadow-cljs I can't find where is the "main" function/namespace in cljsbuild. And I have no idea which namespace is the main one.

thheller21:10:17

the "main" namespace is the namespace where your code "initializes", basically where code runs when the JS is loaded

thheller21:10:09

usually if you followed some templates a .core namespace, or maybe a .main or .app

thheller21:10:16

whatever naming pattern you followed in the code 😛

souenzzo08:10:32

it is a big codebase. In some cljsbuild configs, there is a :main function. In others, it does not exists. I can't understand how cljsbuild works without a main

thheller17:10:59

if no :main is set it'll just compile all the .cljs files it finds

souenzzo07:10:06

found it. I noticed that on prod build it was using some extra classpath src/../prod and inside this classpath there is a ...bootstrap namespace, that calls (main/start) funciton at top-level. thanks!