contentful / node-apps-toolkit

A collection of helpers and utilities for creating NodeJS Contentful Apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The method 'getManagementToken' does not cache token

william-segel opened this issue · comments

Hi! I've been using the node-apps-toolkit for my Node 16.13.0 application and have been trying to track down this HTTP 429 problem that has been plaguing requests to Contentful across the application and I think I've found the culprit.

The problem

When using the "getManagementToken" method multiple times it will never retrieve cached tokens. This has led to plenty of 429 HTTP responses since the library always retrieves a fresh token from Contentful.

I have been putting breakpoints in the generated code found in the module and following the execution where I discovered that the token does get saved to a NodeCache instance but then gets thrown away in the factory method createGetManagementToken. I would assume that the parameter existingCache is supposed to be used somehow to retain the NodeCache instance but it seems like the parameter remains undefined during the entire runtime.

Sample

Here is a sample of code the code used. The application is written in TypeScript and I'm using version 2.0.0 of node-apps-toolkit

import { getManagementToken } from '@contentful/node-apps-toolkit';
import { GetManagementTokenOptions } from '@contentful/node-apps-toolkit/lib/keys/get-management-token';

...
async function getToken() {
      const options: GetManagementTokenOptions = {
            appInstallationId: appid,
            spaceId: spaceid,
            environmentId: environmentid,
            reuseToken: true
      };

      const appAccessToken = await getManagementToken(privateKey, options)
            .catch(async (error) => { /*...handles HTTP 429 error*/ }
      return appAccessToken
}

Hey William, thanks for raising this issue. We'll look into it and hopefully get a fix merged in the near future.

Hi @Jwhiles, thanks for reaching back! Just solved the caching issue.
We used the createGetManagementToken method along with the default logger and HTTP client from createLogger and createHttpClient respectively, then we put our own instance of NodeCache for the existingCache parameter.

Here is a sample of the solution:

import { createGetManagementToken, GetManagementTokenOptions } from '@contentful/node-apps-toolkit/lib/keys/get-management-token';
import { createHttpClient, createLogger } from '@contentful/node-apps-toolkit/lib/utils';

const logger = createLogger({ namespace: 'debug' });
const httpClient = createHttpClient();
const tokenCache = new NodeCache();

...
async function getToken() {
      ...
      const appAccessToken = await createGetManagementToken(
            logger,
            httpClient,
            tokenCache
      )(privateKey, options)
      return appAccessToken
}

From there we can control what happens with the cache. Maybe this was the intended use if we wanted to use the cache? If that is the case then sorry for making such a fuzz about nothing.

🎉 This issue has been resolved in version 2.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Hi William,
We have just integrated a fix that adds a default cache to the function if it is not present. When the new version is released with this change, can you check again if it fixed your problem as well?

I will close this issue now, but feel free to reopen if there are any further issues.