Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xdmx opened this issue Dec 22, 2023 · 0 comments
Open

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

xdmx opened this issue Dec 22, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@xdmx
Copy link

xdmx commented Dec 22, 2023

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

@xdmx xdmx added the bug Something isn't working label Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant