Shortcut multiple @get and @send

Hello,

I’m trying to create the following binding:

  const session = await stripe.checkout.sessions.create({
     ....
  });

I have the stripe type can achieve this via:

type checkout

@get
external checkout: stripe => checkout = "checkout"

type sessions

@get 
external sessions : checkout => sessions = "sessions"

@send
external create : (sessions, {..}) => Promise.t<string> = "create"

Usage:

let session = s->Stripe.checkout->Stripe.sessions->Stripe.create({ "key": "value"})

This works, but I’m not sure if this is idiomatic or not.
Can I not add the additional path to my @send somehow? type checkout and type sessions don’t really mean much in this case.

Perhaps you could use something like this?

@val
@scope("stripe.checkout.sessions")
external create : ({..}) => Promise.t<string> = "create"

Sorry, there is a bit more to it, it should be called on the stripe object:

type stripe

@module("stripe") @new
external make: string => stripe = "default"

@val
@scope("checkout.sessions")
external createCheckoutSession: (stripe, {..}) => Promise.t<string> = "create"
let s = Stripe.make(Common.stripe_private_key)
// Does not work yet
let session = s->Stripe.createCheckoutSession({ "key": "value"})

I see the issue. Maybe this will work?

@send
@scope(("checkout", "sessions"))
external createCheckoutSession: (stripe, {..}) => Promise.t<string> = "create"
2 Likes

Yes, that works!