UNPKG

3.6 kBJavaScriptView Raw
1// get a list of <%= LOWER_NOUN %>s
2const 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// find a particular <%= LOWER_NOUN %> by name (or other search criteria)
13const 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// creates a new <%= LOWER_NOUN %>
24const 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 // json by default
30 }
31 });
32 return response.data
33};
34
35module.exports = {
36 // see here for a full list of available properties:
37 // https://github.com/zapier/zapier-platform/blob/master/packages/schema/docs/build/schema.md#resourceschema
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 // useful if your `searches` and `creates` return sparse objects
45 // get: {
46 // display: {
47 // label: 'Get <%= NOUN %>',
48 // description: 'Gets a <%= LOWER_NOUN %>.'
49 // },
50 // operation: {
51 // inputFields: [
52 // {key: 'id', required: true}
53 // ],
54 // perform: defineMe
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};