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

docs: correct node cache example #5084

Merged
merged 3 commits into from
Feb 14, 2025
Merged

Conversation

hammerOwen
Copy link
Contributor

Thanks for submitting a PR! Please check the boxes below:

  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

The cache implementation for node needs to return a Flags object not a JSON object.

How did you test this code?

No testing this is only documentation update

@hammerOwen hammerOwen requested a review from a team as a code owner February 7, 2025 16:27
@hammerOwen hammerOwen requested review from zachaysan and removed request for a team February 7, 2025 16:27
Copy link

vercel bot commented Feb 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 14, 2025 10:05am

Copy link

vercel bot commented Feb 7, 2025

@hammerOwen is attempting to deploy a commit to the Flagsmith Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added the docs Documentation updates label Feb 7, 2025
@matthewelwell matthewelwell requested review from rolodato and tiagoapolo and removed request for zachaysan February 10, 2025 09:55
Copy link
Member

@rolodato rolodato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hammerOwen, thanks for sending this over. You're right that this example is not correct, but I think neither is your suggested change:

image

There are several things wrong with the original example, which I'm surprised we didn't notice before. This looks like a correct example to me:

import { Flagsmith, Flags } from 'flagsmith-nodejs';
import type { BaseOfflineHandler, EnvironmentModel, FlagsmithCache } from 'flagsmith-nodejs';
import * as redis from 'redis';

const redisClient = redis.createClient({
    url: 'localhost:6379',
});

const redisFlagsmithCache = {
    async get(key: string): Promise<Flags | undefined> {
        const cachedValue = await redisClient.get(key);
        if (cachedValue) {
            return new Flags(JSON.parse(cachedValue));
        }
    },
    async set(key: string, value: Flags): Promise<void> {
        await redisClient.set(key, JSON.stringify(value), { EX: 60 });
    },
} satisfies FlagsmithCache;

Another change is that we're now using import instead of import type for Flags, since you can't instantiate classes imported with import type.

Let me know if that looks correct to you as well, and if it does add it to this PR and we'll get it merged.

@hammerOwen
Copy link
Contributor Author

Updated the example with your corrections.

@rolodato rolodato merged commit 0821532 into Flagsmith:main Feb 14, 2025
5 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants