zapier / zapier-platform

The SDK for you to build an integration on Zapier

Home Page:https://platform.zapier.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Cannot use z.cursor inside triggers code

xdmx opened this issue · comments

commented

Bug Description

I'm trying to integrate some triggers (both Rest and Polling for dynamic dropdowns) for an API that provides pagination through cursors and the links in the header (rfc 8288).

I see that the z object provides a cursor that stores the value for one hour, so I've tried with this cli example https://github.com/zapier/zapier-platform/blob/main/packages/cli/snippets/paging-cursor.js and adapted it a bit to return an array but I get the error Invalid cursor operation. 'z.cursor' can only be used in 'perform' methods of polling triggers or 'performList' methods of hook triggers, and when 'canPaginate' is 'true'. (the pagination checkbox is enabled)

Reproduction Steps

I've tried with a bunch of different attempts:

const perform = async (z, bundle) => {
  let cursor;

  // if fetching a page other than the first (first page is 0),
  // get the cursor stored after fetching the previous page.
  if (bundle.meta.page > 0) {
    cursor = await z.cursor.get();

    // if the previous page was the last one and cursor is empty/null,
    // return an empty array.
    if (!cursor) {
      return [];
    }
  }

  const response = await z.request(
    'https://5ae7ad3547436a00143e104d.mockapi.io/api/recipes',
    {
      // cursor typically is a param to pass along to the next request,
      // or the full URL for the next page of items.
      params: { cursor },
    }
  );

  // after fetching a page, set the returned cursor for the next page,
  // or an empty string if the cursor is null
  await z.cursor.set(response.nextPage ?? '');

  return response.items;
};

return perform(z, bundle).then(results => results);

let cursor;

// if fetching a page other than the first (first page is 0),
// get the cursor stored after fetching the previous page.
if (bundle.meta.page > 0) {
  cursor = await z.cursor.get();

  // if the previous page was the last one and cursor is empty/null,
  // return an empty array.
  if (!cursor) {
    return [];
  }
}

const response = await z.request(
  'https://5ae7ad3547436a00143e104d.mockapi.io/api/recipes',
  {
    // cursor typically is a param to pass along to the next request,
    // or the full URL for the next page of items.
    params: { cursor },
  }
);

// after fetching a page, set the returned cursor for the next page,
// or an empty string if the cursor is null
await z.cursor.set(response.nextPage ?? '');

return response.items;

Zapier Platform version

online editor

Node.js version

online editor

Your Operating System

No response

npm/yarn version

No response

App ID

No response

More Details

No response