Emanuele Saccomandi 9baf4988f3
Zapier generator (#15997)
* First version of Zapier Generator

* fixed zapier codegen

* added zapier templates

* added zapier sample

* added zapier doc

* added zapier generator form data management

* added samples generation

* updated docs

* fixed zapier api template

* fixed zapier samples export

* added zapier readme template

* fixed zapier readme template

* added petstore readme

* cleaned zapier generator

* updated samples

* fixed zapier enum label

* cleaned code

* updated samples

* improved zapier search actions

* updated samples

---------

Co-authored-by: Mauro Valota <maurovalota@fattureincloud.it>
Co-authored-by: William Cheng <wing328hk@gmail.com>
2023-07-09 21:52:04 +08:00

371 lines
13 KiB
JavaScript

const samples = require('../samples/PetApi');
const ApiResponse = require('../models/ApiResponse');
const Pet = require('../models/Pet');
const utils = require('../utils/utils');
const FormData = require('form-data');
module.exports = {
addPet: {
key: 'addPet',
noun: 'Pet',
display: {
label: 'addPet',
description: '',
hidden: false,
},
operation: {
inputFields: [
...Pet.fields(),
],
outputFields: [
...Pet.fields('', false),
],
perform: async (z, bundle) => {
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet'),
method: 'POST',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Content-Type': 'application/json, application/xml',
'Accept': 'application/xml, application/json',
},
params: {
},
body: {
...Pet.mapping(bundle),
},
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: samples['PetSample']
}
},
deletePet: {
key: 'deletePet',
noun: 'Pet',
display: {
label: 'deletePet',
description: '',
hidden: false,
},
operation: {
inputFields: [
{
key: 'petId',
label: 'Pet id to delete',
type: '',
required: true,
},
{
key: 'api_key',
label: '',
type: 'string',
},
],
outputFields: [
],
perform: async (z, bundle) => {
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet/{petId}'),
method: 'DELETE',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Content-Type': '',
'Accept': '',
},
params: {
},
body: {
},
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: { data: {} }
}
},
findPetsByStatus: {
key: 'findPetsByStatus',
noun: 'Pet',
display: {
label: 'findPetsByStatus',
description: 'Multiple status values can be provided with comma separated strings',
hidden: false,
},
operation: {
inputFields: [
...string.fields(),
],
outputFields: [
...array.fields('', false),
],
perform: async (z, bundle) => {
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet/findByStatus'),
method: 'GET',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Content-Type': '',
'Accept': 'application/xml, application/json',
},
params: {
'status': bundle.inputData?.['status'],
},
body: {
},
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: samples['PetSample']
}
},
findPetsByTags: {
key: 'findPetsByTags',
noun: 'Pet',
display: {
label: 'findPetsByTags',
description: 'Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.',
hidden: false,
},
operation: {
inputFields: [
...string.fields(),
],
outputFields: [
...array.fields('', false),
],
perform: async (z, bundle) => {
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet/findByTags'),
method: 'GET',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Content-Type': '',
'Accept': 'application/xml, application/json',
},
params: {
'tags': bundle.inputData?.['tags'],
},
body: {
},
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: samples['PetSample']
}
},
getPetById: {
key: 'getPetById',
noun: 'Pet',
display: {
label: 'getPetById',
description: 'Returns a single pet',
hidden: false,
},
operation: {
inputFields: [
{
key: 'petId',
label: 'ID of pet to return',
type: '',
required: true,
},
],
outputFields: [
...Pet.fields('', false),
],
perform: async (z, bundle) => {
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet/{petId}'),
method: 'GET',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Content-Type': '',
'Accept': 'application/xml, application/json',
},
params: {
},
body: {
},
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: samples['PetSample']
}
},
updatePet: {
key: 'updatePet',
noun: 'Pet',
display: {
label: 'updatePet',
description: '',
hidden: false,
},
operation: {
inputFields: [
...Pet.fields(),
],
outputFields: [
...Pet.fields('', false),
],
perform: async (z, bundle) => {
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet'),
method: 'PUT',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Content-Type': 'application/json, application/xml',
'Accept': 'application/xml, application/json',
},
params: {
},
body: {
...Pet.mapping(bundle),
},
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: samples['PetSample']
}
},
updatePetWithForm: {
key: 'updatePetWithForm',
noun: 'Pet',
display: {
label: 'updatePetWithForm',
description: '',
hidden: false,
},
operation: {
inputFields: [
{
key: 'petId',
label: 'ID of pet that needs to be updated',
type: '',
required: true,
},
{
key: 'name',
label: 'Updated name of the pet',
type: 'string',
},
{
key: 'status',
label: 'Updated status of the pet',
type: 'string',
},
],
outputFields: [
],
perform: async (z, bundle) => {
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet/{petId}'),
method: 'POST',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '',
},
params: {
},
body: {
},
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: { data: {} }
}
},
uploadFile: {
key: 'uploadFile',
noun: 'Pet',
display: {
label: 'uploadFile',
description: '',
hidden: false,
},
operation: {
inputFields: [
{
key: 'petId',
label: 'ID of pet to update',
type: '',
required: true,
},
{
key: 'additionalMetadata',
label: 'Additional data to pass to server',
type: 'string',
},
{
key: 'file',
label: 'file to upload',
type: 'file',
},
],
outputFields: [
...ApiResponse.fields('', false),
],
perform: async (z, bundle) => {
const formData = new FormData()
formData.append('additionalMetadata', bundle.inputData?.['additionalMetadata'])
const filename = bundle.inputData?.['filename'] || bundle.inputData?.['file'].split('/').slice(-1)[0]
formData.append('file', (await (await z.request({url: bundle.inputData?.['file'], method: 'GET', raw: true})).buffer()), { filename: filename })
const options = {
url: utils.replacePathParameters('http://petstore.swagger.io/v2/pet/{petId}/uploadImage'),
method: 'POST',
removeMissingValuesFrom: { params: true, body: true },
headers: {
'Authorization': 'Bearer {{bundle.authData.access_token}}',
'Accept': 'application/json',
},
params: {
},
body: formData,
}
return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
return results;
})
},
sample: samples['ApiResponseSample']
}
},
}