1 |
|
2 | const performList = async (z, bundle) => {
|
3 | const response = await z.request({
|
4 | url: 'https://jsonplaceholder.typicode.com/posts',
|
5 | params: {
|
6 | order_by: 'id desc'
|
7 | }
|
8 | });
|
9 | return response.data
|
10 | };
|
11 |
|
12 |
|
13 | const performSearch = async (z, bundle) => {
|
14 | const response = await z.request({
|
15 | url: 'https://jsonplaceholder.typicode.com/posts',
|
16 | params: {
|
17 | name: bundle.inputData.name
|
18 | }
|
19 | });
|
20 | return response.data
|
21 | };
|
22 |
|
23 |
|
24 | const performCreate = async (z, bundle) => {
|
25 | const response = await z.request({
|
26 | method: 'POST',
|
27 | url: 'https://jsonplaceholder.typicode.com/posts',
|
28 | body: {
|
29 | name: bundle.inputData.name
|
30 | }
|
31 | });
|
32 | return response.data
|
33 | };
|
34 |
|
35 | module.exports = {
|
36 |
|
37 |
|
38 | key: '<%= KEY %>',
|
39 | noun: '<%= NOUN %>',
|
40 |
|
41 | <%= INCLUDE_INTRO_COMMENTS ? [
|
42 | '// If `get` is defined, it will be called after a `search` or `create`'
|
43 | ].join('\n ') : '' %>
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | list: {
|
59 | display: {
|
60 | label: 'New <%= NOUN %>',
|
61 | description: 'Lists the <%= LOWER_NOUN %>s.'
|
62 | },
|
63 | operation: {
|
64 | perform: performList,
|
65 | <%= INCLUDE_INTRO_COMMENTS ? [
|
66 | '// `inputFields` defines the fields a user could provide',
|
67 | '// Zapier will pass them in as `bundle.inputData` later. They\'re optional on triggers, but required on searches and creates.'
|
68 | ].join('\n ') : '' %>
|
69 | inputFields: []
|
70 | }
|
71 | },
|
72 |
|
73 | search: {
|
74 | display: {
|
75 | label: 'Find <%= NOUN %>',
|
76 | description: 'Finds a <%= LOWER_NOUN %> give.'
|
77 | },
|
78 | operation: {
|
79 | inputFields: [
|
80 | {key: 'name', required: true}
|
81 | ],
|
82 | perform: performSearch
|
83 | },
|
84 | },
|
85 |
|
86 | create: {
|
87 | display: {
|
88 | label: 'Create <%= NOUN %>',
|
89 | description: 'Creates a new <%= LOWER_NOUN %>.'
|
90 | },
|
91 | operation: {
|
92 | inputFields: [
|
93 | {key: 'name', required: true}
|
94 | ],
|
95 | perform: performCreate
|
96 | },
|
97 | },
|
98 |
|
99 | <%= INCLUDE_INTRO_COMMENTS ? [
|
100 | '// In cases where Zapier needs to show an example record to the user, but we are unable to get a live example',
|
101 | '// from the API, Zapier will fallback to this hard-coded sample. It should reflect the data structure of',
|
102 | '// returned records, and have obvious placeholder values that we can show to any user.',
|
103 | '// In this resource, the sample is reused across all methods'
|
104 | ].join('\n ') : '' %>
|
105 | sample: {
|
106 | id: 1,
|
107 | name: 'Test'
|
108 | },
|
109 |
|
110 | <%= INCLUDE_INTRO_COMMENTS ? [
|
111 | '// If fields are custom to each user (like spreadsheet columns), `outputFields` can create human labels',
|
112 | '// For a more complete example of using dynamic fields see',
|
113 | '// https://github.com/zapier/zapier-platform/tree/master/packages/cli#customdynamic-fields',
|
114 | '// Alternatively, a static field definition can be provided, to specify labels for the fields',
|
115 | '// In this resource, these output fields are reused across all resources'
|
116 | ].join('\n ') : '' %>
|
117 | outputFields: [
|
118 | {key: 'id', label: 'ID'},
|
119 | {key: 'name', label: 'Name'}
|
120 | ]
|
121 | };
|