-
-
Save Rizarma/2afa277050f06561d8177f05be131f91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const base_url = 'http://demo.resthooks.org'; | |
const hook_path = '/api/v1/hooks/'; | |
const contacts_path = '/api/v1/contacts/'; | |
const subscribe = async (z, bundle) => { | |
const options = { | |
url: `${base_url + hook_path}`, | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
params: { api_key: bundle.inputData.api_key }, | |
body: z.JSON.stringify({ | |
event: 'contact.created', | |
target: bundle.targetUrl | |
}) | |
}; | |
try { | |
const response = await z.request(options); | |
return response; // return 201 with body '' | |
} catch (error) { | |
z.console.error(error); | |
} | |
}; | |
const unsubscribe = async (z, bundle) => { | |
const options = { | |
url: `${base_url + hook_path}`, | |
method: 'DELETE', | |
headers: { 'Content-Type': 'application/json' }, | |
params: { api_key: bundle.inputData.api_key }, | |
body: z.JSON.stringify({ | |
event: 'contact.created', | |
target: bundle.targetUrl | |
}) | |
}; | |
try { | |
const response = await z.request(options); | |
return response; // return 204 with body '' | |
} catch (error) { | |
z.console.error(error); | |
} | |
}; | |
const trigger = async (z, bundle) => { | |
const payload = bundle.cleanedRequest; | |
const data = { | |
id: payload.data.fields.user, | |
name: payload.data.fields.full_name, | |
email: payload.data.fields.email | |
}; | |
return [data]; | |
}; | |
const getSample = async (z, bundle) => { | |
const options = { | |
url: `${base_url + contacts_path}`, | |
method: 'GET', | |
params: { api_key: bundle.inputData.api_key } | |
}; | |
try { | |
const response = await z.request(options); | |
const json = await z.JSON.parse(response.content); | |
const contact_list_raw = json.objects; | |
const contact_list_filtered = []; | |
contact_list_raw.forEach(contact => { | |
contact_list_filtered.push({ | |
id: contact.id, | |
name: contact.full_name, | |
email: contact.email | |
}); | |
}); | |
return contact_list_filtered; | |
} catch (error) { | |
z.console.error(error); | |
} | |
}; | |
module.exports = { | |
key: 'rest_hook', | |
noun: 'Rest hook', | |
display: { | |
label: 'Contact Update', | |
description: 'Triggers when contact is created.' | |
}, | |
operation: { | |
type: 'hook', | |
inputFields: [ | |
{ | |
key: 'api_key', | |
label: 'API Key', | |
required: true, | |
type: 'string', | |
placeholder: 'c71849002f3c22b0b7a505a37da47d8fab86884d', | |
helpText: 'Signup to [http://demo.resthooks.org/](http://demo.resthooks.org/) to get your API Key.' | |
} | |
], | |
performSubscribe: subscribe, | |
performUnsubscribe: unsubscribe, | |
perform: trigger, | |
performList: getSample, | |
sample: { | |
id: 1, | |
name: 'John Doe', | |
email: 'johndoen@example.com' | |
}, | |
outputFields: [ | |
{ | |
key: 'id', | |
label: 'ID' | |
}, | |
{ | |
key: 'name', | |
label: 'Name' | |
}, | |
{ | |
key: 'email', | |
label: 'Email' | |
} | |
] | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment