slumber icon indicating copy to clipboard operation
slumber copied to clipboard

Support file-esque chain in request body

Open Sammo98 opened this issue 10 months ago • 6 comments

Did you search for existing issues already? Yes

Is your feature request related to a problem? Please describe. I wanted to open this more as a discussion, I am aware we can do:

chains:
  big_file:
    source: !file
      path: Cargo.lock

  big_file: !request
    name: Big File
    method: POST
    url: "{{host}}/anything"
    body: "{{chains.big_file}}"

But I'm wondering if it would make sense for the following to be supported when you have large collections in order to keep the configuration concise:

  big_file: !request
    name: Big File
    method: POST
    url: "{{host}}/anything"
    body: !file Cargo.lock

For my use case, I would end up having to create a chain for every single request as our organisation has JSON requests for testing checked in to version control as json files, and parsing these into my slumber.yaml does not seem like a good direction to go.

Describe the solution you'd like Add a new body type variant for enabling simpler configuration to a json file

Describe alternatives you've considered Chains

Additional context This might just want to allow json, could also be bundled into the existing json! variant, but I imagine this would add unnecessary complexity.

Sammo98 avatar Feb 03 '25 14:02 Sammo98

@LucasPickering Let me know what you think, happy to continue work on the draft linked above if you think this is something that would be useful across the board :)

Sammo98 avatar Feb 03 '25 14:02 Sammo98

I totally agree, one of my gripes with the chain system is that it's very verbose, and there's no way to define chains inline. I've spent the past few months working on completely replacing the collection config system to eliminate YAML in favor of something that would make it easier to reuse recipe components, as well as make inline stuff like this easier. I've gone through a lot of variations of this, but right now I'm working on doing it in a simple JavaScript subset. In that world, your recipe would look something like:

big_file: {
  name: "Big File",
  method: "POST",
  url: ({ host }) => `${host}/anything`
  body: () => file('Cargo.lock')
}

What do you think of this? Do you think it would make your collections easier to write and maintain?

Unfortunately the downside of this is it's still a few months away at least. I'm going to leave this issue open since it's an active problem, but I don't really want to apply any short term fixes because it will take away from time working on the new system. So for now I think the answer is you'll have to keep using the verbose chains :(

LucasPickering avatar Feb 03 '25 16:02 LucasPickering

Oh wow a big rewrite. I've just been playing around with https://yamlscript.org/. Could this be a better solution than needing to maintain something more custom? Could be used as a preprocess and also allow arbitrary execution of functions to replace chains?

I can see the benefits to having something simpler of course and like the above example you've given, I just have concern over the maintenance burden it might lead to for you!

Sammo98 avatar Feb 04 '25 21:02 Sammo98

I'm surprised I didn't find YAMLscript in my many hours of research, but after looking at it I think the syntax is really opaque and hard to follow. My goal is to use something approachable and obvious, which is why I landed on JS, since it's so ubiquitous. It is a large upfront cost but I think in the long term it will make the tool more maintainable.

LucasPickering avatar Feb 06 '25 23:02 LucasPickering

Yeh I will admit that even getting it up and running and pre-processing the yaml in the way I wanted caused me some grief. Also adds a dependency to using slumber outside of cargo with the .so

Sammo98 avatar Feb 07 '25 08:02 Sammo98

You also raise a good point, there doesn't seem to be a simple config language that supports functions out the box, was surprised! Closest I could find was https://pkl-lang.org/index.html

Sammo98 avatar Feb 07 '25 10:02 Sammo98

Update on this: I've been working on an overhaul of the template language. Config is going to remain in YAML, but templates will support functions, eliminating the need for chains. So my example from above will instead look like:

big_file:
  type: request
  name: "Big File",
  method: "POST",
  url: "{{ host }}/anything"
  body: "{{ file('Cargo.lock') }}"

The implementation for this is mostly working but I'm still ironing out some final parts before I can release a public beta. I'll let you know when it's available though.

LucasPickering avatar Jul 14 '25 23:07 LucasPickering

The new template function syntax just released in 4.0 makes this much easier: https://slumber.lucaspickering.me/api/template_functions.html#file

You can now do:

requests:
  big_file:
    name: Big File
    method: POST
    url: "{{host}}/anything"
    body: "{{ file('Cargo.lock') }}"

and if you want to use it in multiple places you can still pull it out into the profile:

.profile_data:
  big_file: "{{ file('Cargo.lock') }}"

profiles:
  prd:
    data:
      $ref: "#/.profile_data"

requests:
  big_file1:
    name: Big File
    method: POST
    url: "{{host}}/anything"
    body: "{{ big_file }}"
  big_file2:
    name: Big File
    method: POST
    url: "{{host}}/anything"
    body: "{{ big_file }}"

LucasPickering avatar Sep 12 '25 14:09 LucasPickering