MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
947 stars 278 forks source link

collectionListing is not working #629

Closed iDonib closed 8 months ago

iDonib commented 1 year ago

Below is the error after calling collectionListing.*()


HTTPError: Response code 404 (Not Found)
    at Request.<anonymous> (/home/donib/iDonib/virtual-gravity/my-shopify-app/node_modules/got/dist/source/as-promise/index.js:118:42)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1692872191919,
    socket: 1692872191931,
    lookup: 1692872191981,
    connect: 1692872192042,
    secureConnect: 1692872192130,
    upload: 1692872192132,
    response: 1692872192521,
    end: 1692872192557,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 12,
      dns: 50,
      tcp: 61,
      tls: 88,
      request: 2,
      firstByte: 389,
      download: 36,
      total: 638
    }
  }
}
lpinca commented 8 months ago

I'm closing this due to inactivity.

50bbx commented 5 months ago

Hey, I have the same issue.

Steps to reproduce:

  1. pnpm init
  2. pnpm i shopify-api-node@latest
  3. Create a private app on your Shopify store with this Admin API access scopes: read_products, read_product_listings and get your credentials
  4. Run this script using node index.js inserting credentials created in step 3
// index.js
const Shopify = require('shopify-api-node');

const shopify = new Shopify({
  shopName: 'your-shop',
  apiKey: 'your-key',
  password: 'your-password',
  apiVersion: '2024-04'
});

const main = async () => {
  let params = { limit: 250 };
  const page = await shopify.collectionListing.list(params);

  console.log(page);
};

main();

The error will be:

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

HTTPError: Response code 404 (Not Found)

I suspected that this is because the collectionListing URL does not exist anymore in newest Shopify API versions, but that is not true. Here's what i get if i put a console.log inside buildUrl function in mixins/base.js file:

buildUrl(id, query) {
    id || id === 0 || (id = '');

    let pathname = '/admin';

    if (this.shopify.options.apiVersion) {
      pathname += `/api/${this.shopify.options.apiVersion}`;
    }

    pathname += `/${this.name}/${id}`;
    pathname = pathname.replace(/\/+/g, '/').replace(/\/$/, '') + '.json';

    const url = { pathname, ...this.shopify.baseUrl };

    if (query) {
      url.search = '?' + qs.stringify(query, { arrayFormat: 'brackets' });
    }

    console.log(url)

    return url;
  }
{
  pathname: '/admin/api/2024-04/collection_listings.json',
  hostname: 'mystore.myshopify.com',
  protocol: 'https:',
  search: '?limit=250'
}

And that's a real endpoint as Shopify Docs say, but it really returns 404.

I dug a bit deeper and I found out the issue. It really doesn't make ANY sense and it's not shopify-api-node's fault at all: turning on unauthenticated_read_product_listings on Storefront API access scopes on the private app makes everything work as expected. I have really no idea why, since we are making admin calls and Storefront API calls are not involved. That looks a lot like a workaround and I am not sure it is the right solution, but I wanted to share with you.