Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
Merge pull request #204 from zapier/convert-send-in-json
Browse files Browse the repository at this point in the history
zapier convert: Exclude input fields from bundle.inputData if send_in_json is false
  • Loading branch information
eliangcs authored Dec 25, 2017
2 parents 458bfb0 + 57e40ec commit abf396e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
20 changes: 19 additions & 1 deletion scaffold/convert/create.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,18 @@ const makeRequest = (z, bundle) => {
bundle._legacyUrl = '<%= URL %>';
bundle._legacyUrl = replaceVars(bundle._legacyUrl, bundle);

<% if (excludeFieldKeys) { %>
// Exclude create fields that uncheck "Send to Action Endpoint URL in JSON body"
// https://zapier.com/developer/documentation/v2/action-fields/#send-to-action-endpoint-url-in-json-body
<% excludeFieldKeys.forEach(fieldKey => { %>
delete bundle.inputData['<%= fieldKey %>'];
<% }); %>
<% } %>

const responsePromise = z.request({
url: bundle._legacyUrl
url: bundle._legacyUrl,
method: 'POST',
body: bundle.inputData
});
return responsePromise
.then((response) => {
Expand Down Expand Up @@ -102,6 +112,14 @@ const makeRequest = (z, bundle) => {
let url = '<%= URL %>';
url = replaceVars(url, bundle);

<% if (excludeFieldKeys) { %>
// Exclude create fields that uncheck "Send to Action Endpoint URL in JSON body"
// https://zapier.com/developer/documentation/v2/action-fields/#send-to-action-endpoint-url-in-json-body
<% excludeFieldKeys.forEach(fieldKey => { %>
delete bundle.inputData['<%= fieldKey %>'];
<% }); %>
<% } %>

const responsePromise = z.request({
url: url,
method: 'POST',
Expand Down
4 changes: 4 additions & 0 deletions scaffold/convert/simple-auth.template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<% if (TEST_TRIGGER_MODULE) { %>
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>');
<% } %>
<% if (hasGetConnectionLabelScripting) { %>
const getConnectionLabel = (z, bundle) => {
const scripting = require('./scripting');
Expand All @@ -14,7 +16,9 @@ const getConnectionLabel = (z, bundle) => {
const authentication = {
// TODO: just an example stub - you'll need to complete
type: '<%= TYPE %>',
<% if (TEST_TRIGGER_MODULE) { %>
test: testTrigger.operation.perform,
<% } %>
fields: [
<%= FIELDS %>
],
Expand Down
1 change: 1 addition & 0 deletions scripts/test-convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const appsToConvert = [
{ id: 82250, name: 'search-oauth' },
{ id: 82251, name: 'basic-api-header' },
{ id: 82460, name: 'custom-fields' },
{ id: 83073, name: 'send-in-json' },
{ id: 80444, name: 'custom-basic' },
{ id: 83342, name: 'replace-vars' }
// TODO: Add more apps that use different scripting methods, as we start to support them
Expand Down
11 changes: 11 additions & 0 deletions src/tests/utils/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ describe('convert render functions', () => {
field.should.not.have.property('helpText');
});

it('should escape single quotes in label', () => {
const wbKey = 'test_field';
const wbDef = {
label: "It's a test"
};

const string = convert.renderField(wbDef, wbKey);
const field = s2js(string);
field.label.should.eql("It's a test");
});

it('should escape multi-line help text', () => {
const wbKey = 'test_field';
const wbDef = {
Expand Down
19 changes: 16 additions & 3 deletions src/utils/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const renderField = (definition, key, indent = 0) => {

props.push(renderProp('key', quote(key)));
if (definition.label) {
props.push(renderProp('label', quote(definition.label)));
props.push(renderProp('label', quote(escapeSpecialChars(definition.label))));
}

if (definition.help_text) {
Expand Down Expand Up @@ -228,21 +228,26 @@ const getTestTriggerKey = definition => {
const renderAuthTemplate = (authType, definition) => {
const fields = renderFields(definition.auth_fields, 4);
const connectionLabel = _.get(definition, ['general', 'auth_label'], '');
const testTriggerKey = getTestTriggerKey(definition);
const { hasGetConnectionLabelScripting } = getAuthMetaData(definition);

if (authType === 'basic' && !_.isEmpty(definition.general.auth_mapping)) {
authType = 'custom';
}

const templateContext = {
TEST_TRIGGER_MODULE: `./triggers/${snakeCase(testTriggerKey)}`,
TYPE: authType,
FIELDS: fields,
CONNECTION_LABEL: connectionLabel,
hasGetConnectionLabelScripting
};

const testTriggerKey = getTestTriggerKey(definition);
if (testTriggerKey) {
templateContext.TEST_TRIGGER_MODULE = `./triggers/${snakeCase(testTriggerKey)}`;
} else {
templateContext.TEST_TRIGGER_MODULE = '';
}

const templateFile = path.join(TEMPLATE_DIR, '/simple-auth.template.js');
return renderTemplate(templateFile, templateContext);
};
Expand Down Expand Up @@ -709,6 +714,14 @@ const renderStep = (type, definition, key, legacyApp) => {
definition.custom_fields_result_url;
}

if (type === 'create' && !stepMeta.hasPreScripting && !stepMeta.hasFullScripting) {
// Exclude create fields that uncheck "Send to Action Endpoint URL in JSON body"
// https://zapier.com/developer/documentation/v2/action-fields/#send-to-action-endpoint-url-in-json-body
const fieldKeys = _.keys(definition.fields);
const excludeFieldKeys = _.filter(fieldKeys, k => !definition.fields[k].send_in_json);
templateContext.excludeFieldKeys = excludeFieldKeys || null;
}

const templateFile = path.join(TEMPLATE_DIR, `/${type}.template.js`);
return renderTemplate(templateFile, templateContext);
};
Expand Down

0 comments on commit abf396e

Please sign in to comment.