-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PDE-4991 Define types for performBulk
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { BulkBundle, ZObject } from 'zapier-platform-core'; | ||
|
||
// You can optionally add the shape of the inputData in bundle, which will pass that | ||
// info down into the function and tests | ||
const performBulk = async ( | ||
z: ZObject, | ||
bundle: BulkBundle<{ genre: string; title: string; year: number }> | ||
) => { | ||
// Flatten the line items, preserving order | ||
const movies = bundle.bulk.reduce((result, { inputData }) => [ | ||
...result, | ||
{title: inputData.title, year: inputData.year}, | ||
], []); | ||
|
||
const response = await z.request({ | ||
method: 'POST', | ||
url: 'https://auth-json-server.zapier-staging.com/movies' | ||
body: { | ||
genre: bundle.groupedBy.genre, | ||
movies, | ||
}, | ||
}); | ||
|
||
// Create a matching result using the idempotency ID for each buffered bundle | ||
// The returned IDs will tell Zapier backend which items were successfully written. | ||
return bundle.bulk.reduce((result, {inputData, meta}, index) => { | ||
let error; | ||
let outputData; | ||
|
||
// assuming request order matches response | ||
if (response.data.length > index) { | ||
if (response.data[index].error) { | ||
error = response.data[index].error; | ||
} else { | ||
outputData = response.data[index]; | ||
} | ||
} | ||
|
||
result[meta.id] = { outputData, error }; | ||
return result; | ||
}, {}); | ||
}; | ||
|
||
export default { | ||
key: 'movies', | ||
noun: 'Movies', | ||
|
||
display: { | ||
label: 'Create Movies', | ||
description: 'Creates new movies in bulk.', | ||
}, | ||
|
||
operation: { | ||
bulk: { | ||
groupedBy: ['genre'], | ||
limit: 5, | ||
}, | ||
performBulk, | ||
inputFields: [ | ||
{ key: 'genre', type: 'string', required: true }, | ||
{ key: 'title', type: 'string' }, | ||
{ key: 'year', type: 'integer' }, | ||
], | ||
sample: { | ||
id: '1', | ||
title: 'example', | ||
genre: 'example genre', | ||
year: 2024, | ||
}, | ||
}, | ||
}; |
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
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
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