Skip to content

Commit

Permalink
PDE-4991 Define types for performBulk
Browse files Browse the repository at this point in the history
  • Loading branch information
kola-er committed May 14, 2024
1 parent 6b3570e commit f1955dc
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
71 changes: 71 additions & 0 deletions example-apps/typescript/src/creates/movies.ts
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,
},
},
};
2 changes: 2 additions & 0 deletions example-apps/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Bundle, HttpRequestOptions, ZObject } from 'zapier-platform-core';

import MovieCreate from './creates/movie';
import MoviesCreate from './creates/movies';
import MovieTrigger from './triggers/movie';
import { version as platformVersion } from 'zapier-platform-core';

Expand Down Expand Up @@ -29,5 +30,6 @@ export default {

creates: {
[MovieCreate.key]: MovieCreate,
[MoviesCreate.key]: MoviesCreate,
},
};
41 changes: 41 additions & 0 deletions example-apps/typescript/src/test/creates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,45 @@ describe('movie', () => {
year: 2020,
});
});

test('create movies in bulk', async () => {
const genre = 'Genre Test';
const idem1 = 'test-id-xxx';
const idem2 = 'test-id-yyy';
const idem3 = 'test-id-zzz';

const bundle = {
bulk: [
{
inputData: { genre, title: 'Title 1', year: 2020 },
meta: { id: idem1 },
},
{
inputData: { genre, title: 'Title 2', year: 2021 },
meta: { id: idem2 },
},
{
inputData: { genre, title: 'Title 3', year: 2022 },
meta: { id: idem3 },
},
],
groupedBy: { genre },
};

const result = await appTester(App.creates.movies.operation.perform, bundle);
expect(result).toMatchObject({
idem1: {
outputData: { genre, id: '1', title: 'Title 1', year: 2020 },
error: null,
},
idem2: {
outputData: { genre, id: '2', title: 'Title 2', year: 2021 },
error: null,
},
idem3: {
outputData: { genre, id: '3', title: 'Title 3', year: 2022 },
error: null,
},
});
});
});
28 changes: 28 additions & 0 deletions packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,31 @@ export interface ZObject {
delete: (key: string) => Promise<boolean>;
};
}

export interface BufferedBundle<InputData = { [x: string]: any }> {
inputData: InputData;
meta: {
id: string;
[x: string]: any;
};
}

export interface PerformBulkItem {
outputData: { [x: string]: any };
error?: string;
}

export interface BulkBundle<InputData = { [x: string]: any }> {
authData: { [x: string]: string };
bulk: BufferedBundle<InputData>[];
groupedBy: { [x: string]: string };
}

export interface PerformBulkResult {
[id: string]: PerformBulkItem;
}

export const performBulk: (
z: ZObject,
bundle: BulkBundle
) => Promise<PerformBulkResult>;

0 comments on commit f1955dc

Please sign in to comment.