oliyh / martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
MIT License
525 stars 42 forks source link

Applying interceptors to `bootstrap-openapi` #200

Closed dummaso closed 4 months ago

dummaso commented 4 months ago

I'm currently trying to initialize a martian client within a re-frame app using the following code

(ns app.example.martian
  (:require [martian.cljs-http :as mch]
            [martian.re-frame :as m]))

(m/init "https://petstore.swagger.io/v2/swagger.json"
        {:interceptors (concat [{:name ::cors
                                 :enter #(assoc-in % [:request :with-credentials?] false)}]
                               mch/default-interceptors)})

When initializing using an URL, this results in a CORS-error for the initial request which loads the API-documentation, seemingly ignoring the passed interceptors.

... The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. ...

However, when loading the API-documentation from a local file, the client is initialized without any problems and applies the provided::cors middleware to all future requests.

Is there any way to also apply the interceptors to the initial request or modify its http-headers in another way? Or am I just missing something? :D

Thanks a lot in advance!

oliyh commented 4 months ago

Hi,

It looks like init should be updated with an extra optional param which is load-opts which should allow you to specify the behaviour when loading the swagger json. This has existed for a while on cljs-http but it's not exposed by re-frame. I'll make the change and publish a snapshot.

Cheers

oliyh commented 4 months ago

Hello,

Have a go with 0.1.27-SNAPSHOT. There is an extra arg to init called load-opts. It is passed directly into cljs-http as you can see here: https://github.com/oliyh/martian/blob/master/cljs-http/src/martian/cljs_http.cljs#L32 so you can put any cljs-http params you want into it.

Cheers

dummaso commented 4 months ago

Seems to work now! Thank you for the quick support and for maintaining this awesome project :)

Have a nice weekend!

oliyh commented 4 months ago

Awesome, happy to help!