-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BREAKING CHANGE(core): Introduce response.data with support for form-urlencoded and custom parsing #211
Conversation
6ca1e9d
to
36c160c
Compare
@szchenghuang a heads-up that I'd like to include this in v10 as well. It's the last piece of the puzzle that makes |
@FokkeZB we're getting v10 ready to go out the door - do you have a timeline on this? |
@xavdid I've got it almost finished. I was prioritizing https://github.com/zapier/zapier/pull/39898 over this one, but I now realize that we won't need to switch visual builder apps immediately, so this should come first as we don't want to have another breaking change soon after. I'll work on this this afternoon and also prioritize writing docs on both with lots of examples. |
2984e7b
to
16dcf3b
Compare
553bb70
to
439a596
Compare
@FokkeZB quick question: looks like |
@eliangcs it would be breaking for CLI apps that currently change |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self-review of changes
if (response.data === undefined) { | ||
throw new Error( | ||
'Response needs to be JSON, form-urlencoded or parsed in middleware.' | ||
); | ||
} | ||
return createJSONtool().parse(resp.content); | ||
return response.data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows afterResponse
to do any custom parsing and override response.data
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self-review added tests
@@ -616,4 +617,66 @@ describe('create-app', () => { | |||
); | |||
}); | |||
}); | |||
|
|||
describe('response.content parsing', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds test to ensure that form-mode/shorthand request still work with both JSON and form-urlencoded. We didn't have any tests for it AFAIK :(
@@ -529,3 +529,151 @@ describe('http logResponse after middleware', () => { | |||
}); | |||
}); | |||
}); | |||
|
|||
describe('http prepareResponse', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds tests for prepareResponse
for which AFAIK we didn't have any.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self-review docs & examples
@@ -17,7 +15,7 @@ const getRequestToken = async (z, bundle) => { | |||
oauth_callback: bundle.inputData.redirect_uri, | |||
}, | |||
}); | |||
return querystring.parse(response.content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trello is a great example of an app that can now use response.data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks awesome! Thanks for putting this all together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good except for the changes in legacy-scripting-runner. We'll want legacy-scripting-runner to be backward compatible with older versions of core.
Definitions:
FunctionSchema
RequestSchema
Commits
I've split the work up in 3 commits:
Changes
prepareContentResponse
now parses.content
to.data
if its eitherapplication/x-www-form-urlencoded
or can be parsed as JSON.prepareContentResponse
still sets.json
but it should be considered deprecated.executeHttpRequest
now uses.data
instead of (again) parsing.content
asapplication/x-www-form-urlencoded
or JSON.executeHttpRequest
now throws a helpful error instead ofSyntaxError
if.data
is not set and thus notapplication/x-www-form-urlencoded
, JSON or set inafterResponse
.See the first commit.
Benefits
application/x-www-form-urlencoded
, just like form-mode requests.JSON.stringify()
their custom-parsed.content
back to the same property for form-mode to work..data
property allows us to later add support for types like XML.See the below Examples.
Examples
These examples all use
afterResponse
. The code-mode ones could also handle this within the function of course, but this makes it easier to compare between code and form-mode.Form-mode request with form response
Already and still works out of the box. It just now relies on
response.data
set byprepareResponse
instead of parsingresponse.content
just before returning it.This means it may be a breaking change for CLI apps that were doing custom parsing in
afterResponse
as the Form-mode request with XML response example.Code-mode request with form response
Before
After
Code-mode request with JSON response
You can still use
response.json
but we encourage switching toresponse.data
.Before
After
Form-mode request with XML response
Note that both before and after currently are only possible in CLI apps, but once https://github.com/zapier/zapier/pull/39899 makes the Middleware tab public for all, this can also be used in visual builder apps.
Before
As we defaulted to parse
response.content
as JSON:After
As we now use
response.data
:Code-mode request with XML response
Before
After: