AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
222 stars 20 forks source link

Nodejs parsing for query params #249

Closed natalie-o-perret closed 6 months ago

natalie-o-perret commented 6 months ago

๐Ÿ™‹โ€โ™€๏ธ Hello, it's again wee me!

Context

I have to deal some API (not mine), and the query params can pretty verbose I found only two ways to handle this case but none of them make things conveninent from my point of view:

Workarounds

Workaround 1: Full verbose variables

### Get Whatever data
# @name getWhateverData
# @forceRef getExchangeToken
@id1=21720000
@id2=21100000
@id3=21100100
@id4=21300000
@id5=21200000
@id6=21302000
@id7=21310000
@id8=24020500
@id9=24020598
@id10=24020599
@id11=24020000
@id12=24020300
@id13=12590000
@id14=12110000
GET {{apiHost}}/api/v1/entities
    ?id={{id1}}
    &id={{id2}}
    &id={{id3}}
    &id={{id4}}
    &id={{id5}}
    &id={{id6}}
    &id={{id7}}
    &id={{id8}}
    &id={{id9}}
    &id={{id10}}
    &id={{id11}}
    &id={{id12}}
    &id={{id13}}
    &id={{id14}}
Authorization: Bearer {{getExchangeToken.access_token}}

But as you can see, it's super verbose and time-consuming to type all the ids๐Ÿ˜•

Workaround 2: Nodejs script

### Get Whatever data
# @name getWhateverData
# @forceRef getExchangeToken
GET {{apiHost}}/api/v1/entities?{{[21720000, 21100000, 21100100, 21300000, 21200000, 21302000, 21310000, 24020500, 24020598].map(x => `entityId=${x}`).join('&')}}
Authorization: Bearer {{getExchangeToken.access_token}}

So this works, but I see 2 main caveats:

Proposal

### Get Whatever data
# @name getWhateverData
# @forceRef getExchangeToken
@ids=[21720000, 21100000, 21100100, 21300000, 21200000, 21302000, 21310000, 24020500, 24020598]
GET {{apiHost}}/api/v1/entities
    ?{{{{ids}}.map(x => `id=${x}`).join('&')}}
Authorization: Bearer {{getExchangeToken.access_token}}

But it doesn't seem to work ๐Ÿค”

If something similar is already available, please feel free to point me towards that solution.

Also would love some support for multiple lines variable definitions, e.g.,

@ids=[
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]

Wdyt?

AnWeber commented 6 months ago

using NodeJS Script should work.

{{ 
  exports.ids=[
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]
}}
GET {{apiHost}}/api/v1/entities
    ?{{ids.map(x => `id=${x}`).join('&')}}

You could rename your apiHost variable to host and just write GET /api/...

Multiple line variable definitions are really hard to parse without explicit end separator. Is a empty line or comment a end of variable definition or just some formatting modification. This parsing would be unstable. I would just use NodeJS Scripts for this use case.

natalie-o-perret commented 6 months ago

Hey there @AnWeber, thanks for being responsive, you're amazing!

So about what you just suggested:

using NodeJS Script should work.

{{ 
  exports.ids=[
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]
}}
GET {{apiHost}}/api/v1/entities
    ?{{ids.map(x => `id=${x}`).join('&')}}

It kinda works, but there is some hiccup:

It works if the ? query param prefix is one the same line as the host stuff, i.e.,

GET {{apiHost}}/api/v1/entities?{{ids.map(x => `id=${x}`).join('&')}}

leading effectively to (when evaluated by httpyac):

GET https://my-api.com/api/v1/entities?id=21720000&id=21100000&id=21100100&id=21300000&id=21200000&id=21302000&id=21310000&id=24020500&id=24020598

That being said, when it's on new line:

GET {{apiHost}}/api/v1/entities
    ?{{ids.map(x => `id=${x}`).join('&')}}

It ends being interpreted:

GET https://my-api.com/api/v1/entities
accept-encoding: gzip, deflate, br
accept: */*
content-length: 3458
user-agent: httpyac

    ?id=21720000&id=21100000&id=21100100&id=21300000&id=21200000&id=21302000&id=21310000&id=24020500&id=24020598
Authorization: Bearer my-amazingly-long-bearer-token

It's like the extra line messing things which oddly-enough isn't the case with the "Workaround 1: Full verbose variables" I mentioned in my original post, do you know what that might be the case?

AnWeber commented 6 months ago

Yes, I just noticed that too, the parser for the URL is probably not quite right. I'll have to have a look at the regex. One more addition, I also have a helper method that makes it easier to create query params, but it can only create arrays with the next release. I haven't needed it yet and am currently adding it.

{{ 
const {utils} = require("httpyac");
  exports.q=utils.toQueryParams({ id: [
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]})
}}
GET http://postman-echo.com/get?{{q}}
natalie-o-perret commented 6 months ago

Yes, I just noticed that too, the parser for the URL is probably not quite right. I'll have to have a look at the regex. One more addition, I also have a helper method that makes it easier to create query params, but it can only create arrays with the next release. I haven't needed it yet and am currently adding it.

{{ 
const {utils} = require("httpyac");
  exports.q=utils.toQueryParams({ id: [
  21720000, 
  21100000, 
  21100100, 
  21300000, 
  21200000, 
  21302000, 
  21310000, 
  24020500, 
  24020598
]})
}}
GET http://postman-echo.com/get?{{q}}

Nice! Can't wait ๐Ÿชฉ๐Ÿ’ƒ!

AnWeber commented 6 months ago

I released a update containing these fixes.