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

zapier convert: Generate create code based on different scripting method combinations #181

Merged
merged 3 commits into from
Nov 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 97 additions & 5 deletions scaffold/convert/create.template.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,108 @@
// "Create" stub created by 'zapier convert'. This is just a stub - you will need to edit!
<%
// Template for just _pre_write()
if (scripting && preScripting && !postScripting && !fullScripting) { %>
const makeRequest = (z, bundle) => {
const scripting = require('../scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= URL %>';

// Do a _pre_write() from scripting.
const preWriteEvent = {
name: 'create.pre',
key: '<%= KEY %>'
};
return legacyScriptingRunner.runEvent(preWriteEvent, z, bundle)
.then((preWriteResult) => z.request(preWriteResult))
.then((response) => z.JSON.parse(response.content));
};
<%
}

// Template for _pre_write() + _post_write()
if (scripting && preScripting && postScripting && !fullScripting) { %>
const makeRequest = (z, bundle) => {
const scripting = require('../scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= URL %>';

// Do a _pre_write() from scripting.
const preWriteEvent = {
name: 'create.pre',
key: '<%= KEY %>'
};
return legacyScriptingRunner.runEvent(preWriteEvent, z, bundle)
.then((preWriteResult) => z.request(preWriteResult))
.then((response) => {
// Do a _post_write() from scripting.
const postWriteEvent = {
name: 'create.post',
key: '<%= KEY %>',
response
};
return legacyScriptingRunner.runEvent(postWriteEvent, z, bundle);
});
};
<%
}

// Template for just _post_write()
if (scripting && !preScripting && postScripting && !fullScripting) { %>
const makeRequest = (z, bundle) => {
const scripting = require('../scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= URL %>';

const responsePromise = z.request({
url: bundle._legacyUrl
});
return responsePromise
.then((response) => {
// Do a _post_write() from scripting.
const postWriteEvent = {
name: 'create.post',
key: '<%= KEY %>',
response
};
return legacyScriptingRunner.runEvent(postWriteEvent, z, bundle);
});
};
<%
}

// Template for just _write()
if (scripting && fullScripting) { %>
const makeRequest = (z, bundle) => {
const scripting = require('../scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= URL %>';

// Do a _write() from scripting.
const fullWriteEvent = {
name: 'create.write',
key: '<%= KEY %>'
};
return legacyScriptingRunner.runEvent(fullWriteEvent, z, bundle);
};
<%
}

// If there's no scripting, it's even sweeter and simpler!
if (!scripting) { %>
const makeRequest = (z, bundle) => {
const responsePromise = z.request({
method: 'POST',
url: '<%= URL %>',
data: JSON.stringify({
EXAMPLE: bundle.inputData.EXAMPLE
})
method: 'POST',
body: bundle.inputData
});
return responsePromise
.then(response => JSON.parse(response.content));
.then(response => z.JSON.parse(response.content));
};
<% } %>

module.exports = {
key: '<%= KEY %>',
Expand Down