fastly/fastly-js

Getting TypeError: Cannot read properties of undefined during authentication before purging

jindalshivali opened this issue · 2 comments

I'm getting error on test envs - Cannot read properties of undefined (reading 'ApiClient') when I try to run purging cache APIs on the code below:

import Fastly from "fastly";
Fastly.ApiClient.instance.authenticate("FASTLY_API_TOKEN");
const apiInstance = new Fastly.PurgeApi();

Locally, I get a similar error - TypeError: Cannot read properties of undefined (reading 'instance') on the authenticate line.

I referred to https://developer.fastly.com/reference/api/purging/ for documentation on purging cache for Fastly version 3.0.0-beta3. This is when I upgraded from Fastly 2.2.1 to 3.x since there's no documentation available on purging for 2.x available anymore and we needed one to start purging cache by url.

Can anyone help with this?? I would really appreciate a quick response.

This was resolved on test envs by replacing import statement with require. Though that still does not work on local (M1 Mac node version 16.17.1).

But the purgeKey call works but the purgeurl call still fails.

async purgeUrl (url) {
    const options = {
      cached_url: url
    };
    try {
      const response = await apiInstance.purgeSingleUrl(options);
      console.log(response);
    } catch (error) {
      throw new PurgeUrlRequestFailed(url, error);
    }
  }
ERROR	PurgeUrlRequestFailed: Request to purge url test-service.qatest.com/?x=abc&y=css&specificity=search-widget has failed for the following reason: {}
    at _.purgeUrl (/var/task/fastly-cache.js:37:169639)
    at w.bustCache (/var/task/fastly-cache.js:37:170035)
    at /var/task/fastly-cache.js:37:172844
    at Array.forEach (<anonymous>)
    at Runtime.U [as handler] (/var/task/fastly-cache.js:37:172676)
    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1028:29)

The API call to purge url work through postman and not through the client. I checked, I couldn't find the POST call to purge url in the client (which should have been there according to the documentation). I could only find the POST call to purge for tags/keys.

Anyways, I am working around this by using fetch instead of this client for now to unblock myself.