Kong / insomnia

The open-source, cross-platform API client for GraphQL, REST, WebSockets, SSE and gRPC. With Cloud, Local and Git storage.
https://insomnia.rest
Apache License 2.0
34.33k stars 1.93k forks source link

[Feature Request] Javascript JSON editor #1950

Open avin-kavish opened 4 years ago

avin-kavish commented 4 years ago

Is your feature request related to a problem? Please describe. I don't want to put double quotes around keys. I'd like to use node env intrinsic functions to build my JSON body. Currently, I have to be JSON compliant, which is not very human friendly. It's more machine friendly. Following are some inconvenient things to type.

{
  "arrayOfValues": [ "foo", "bar" ],
  "ttl": 300,
  "date": "2020-02-20-17:40z"
}

Describe the solution you'd like Allow javascript in the JSON body editor. For an example of the feature, create a free tier account with FaunaDB (no plug) and try their collection editor. With JS the above would be,

{
  arrayOfValues: Array(10).fill('foo'),
  ttl: 5 * 60,
  date: new Date().toISOString()
}

Describe alternatives you've considered There's no alternative to Javascript. Perhaps python, but not really.

avin-kavish commented 4 years ago

Or better yet, JS-YAML.

arrayOfValues: Array(10).fill('foo'),
ttl: 5 * 60,
date: new Date().toISOString()
avin-kavish commented 4 years ago

As for the implementation of this, I think things can be kept simple. You can parse the entire document as YAML, and apply eval on all values that are strings. If there are security concerns, you should be able to launch an isolated v8 context in electron.

arrayOfValues: eval(`Array(10).fill('foo')`),
ttl: eval(`5 * 60`),
date: eval(`new Date().toISOString()`)
ttmarek commented 4 years ago

I'm all for this! Also, this has been brought up a few times over the years:

@gschier if we were to extend the plugin API to allow for custom formats in the body selection dropdown then I would have no problem writing a plugin to achieve the rest of the functionality we want. This would be incredibly useful for me day-to-day.

My dream would be to be able to create a plugin where I could add an interface to save and set prepared body data. Kind of like a mini snippet manager for body data.

avin-kavish commented 4 years ago

Oh right, I guess what I saw was really close to JSON5. I think the feature request was never rejected but the thread went stale and was closed automatically.

tobske commented 4 years ago

We are using insomnia a lot at our company and for us it would be awesome to be able to define JS functions and have the response from another request as input for that JS function. This would allow to build great workflows by chaining several APIs, like in the following example:

You would then be able to define the response from the first API as an input for a custom JS function that would get only the IDs of the aircraft and join them in a comma separated list to then put them in a query parameter for the second API.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

strokirk commented 2 years ago

I'd really love this. Is it possible to build a plugin that could intercept the body and be allowed to mutate it? In that case Insomnia itself wouldn't have to know about how to parse json5, yaml, jsonnet, etc., and could see which plugins are most popular before incorporating them into the main client.