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

fix: inconsistent targeting key #16

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,9 @@ jobs:
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
tags:
- ${{ needs.release-please.outputs.release_tag_name }}
- latest

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down
2 changes: 1 addition & 1 deletion flags.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"defaultVariant": "on",
"targeting": {
"if": [{ "==": [{ "var": "size" }, "sm"]}, "on", "off" ]
"if": [{ "==": [{ "var": "size" }, "sm"]}, "on", null ]
}
},
"use-distributed-db": {
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/toggle-shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spec:
# - var: size
# - sm
# - 'on'
# - 'off'
# -
---
# Flags for our backend application
apiVersion: core.openfeature.dev/v1beta1
Expand Down
22 changes: 10 additions & 12 deletions src/app/checkout/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"use client";

import { useMemo, useReducer } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import Image from "next/image";
import { ArrowLeft, ArrowRight, Minus, Plus, X } from "lucide-react";
import { useCart } from "@/hooks/use-cart";
import Header from "@/components/Header";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { tanstackMetaToHeader } from "@/libs/open-feature/evaluation-context";
import { useCart } from "@/hooks/use-cart";
import { setTargetingKeyHeader } from "@/libs/open-feature/evaluation-context";
import { TARGETING_KEY } from "@/libs/targeting-key";
import { useFlag, useTrack } from "@openfeature/react-sdk";
import { useMutation } from "@tanstack/react-query";
import { ArrowLeft, ArrowRight, Minus, Plus, X } from "lucide-react";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useMemo, useReducer } from "react";

export default function Checkout() {
const { cartItems, removeFromCart, updateQuantity } = useCart();
Expand Down Expand Up @@ -169,16 +170,13 @@ function CheckoutForm() {
const router = useRouter();
const { cartItems, clearCart } = useCart();

const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: () => {
return fetch("/api/orders", {
method: "POST",
headers: {
"Content-Type": "application/json",
...tanstackMetaToHeader(
queryClient.getDefaultOptions().mutations?.meta
),
...setTargetingKeyHeader(TARGETING_KEY),
},
body: JSON.stringify({
items: cartItems,
Expand Down
8 changes: 3 additions & 5 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Inter } from "next/font/google";
import { CartProvider } from "@/providers/cart";
import { ReactQueryProvider } from "@/providers/react-query";
import { OpenFeatureProvider } from "@/providers/open-feature";
import { v4 } from "uuid";
import { TARGETING_KEY } from "@/libs/targeting-key";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -12,8 +12,6 @@ export const metadata = {
description: "If it can toggle, you'll find it here!",
};

const targetingKey = v4();

export default function RootLayout({
children,
}: {
Expand All @@ -22,8 +20,8 @@ export default function RootLayout({
return (
<html lang="en">
<body className={inter.className}>
<ReactQueryProvider targetingKey={targetingKey}>
<OpenFeatureProvider context={{ targetingKey }}>
<ReactQueryProvider>
<OpenFeatureProvider context={{ targetingKey: TARGETING_KEY }}>
<CartProvider>{children}</CartProvider>
</OpenFeatureProvider>
</ReactQueryProvider>
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/use-products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { useSuspenseQuery } from "@tanstack/react-query";
import type { Product } from "@/types/product";
import { getBaseUrl } from "@/libs/url";
import { tanstackMetaToHeader } from "@/libs/open-feature/evaluation-context";
import { setTargetingKeyHeader } from "@/libs/open-feature/evaluation-context";
import { TARGETING_KEY } from "@/libs/targeting-key";

export function useProducts() {
const { data } = useSuspenseQuery({
Expand All @@ -12,7 +13,7 @@ export function useProducts() {
console.log("fetching products");
const res = await fetch(getBaseUrl() + "/api/products", {
cache: "no-store",
headers: tanstackMetaToHeader(meta),
headers: setTargetingKeyHeader(TARGETING_KEY),
});
if (!res.ok) {
throw new Error("Failed to fetch products");
Expand All @@ -30,7 +31,7 @@ export function useProduct(id: string) {
console.log(`fetching product ${id}`);
const res = await fetch(getBaseUrl() + `/api/products/${id}`, {
cache: "no-store",
headers: tanstackMetaToHeader(meta),
headers: setTargetingKeyHeader(TARGETING_KEY),
});
if (!res.ok) {
throw new Error("Failed to fetch products");
Expand Down
14 changes: 5 additions & 9 deletions src/libs/open-feature/evaluation-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import type { EvaluationContext } from "@openfeature/core";

const TARGETING_KEY_HEADER = "x-targeting-key";

export function tanstackMetaToHeader(
meta?: EvaluationContext
export function setTargetingKeyHeader(
targetingKey: string
): Record<string, string> {
if (meta && meta.targetingKey) {
return {
[TARGETING_KEY_HEADER]: meta.targetingKey,
};
}

return {};
return {
[TARGETING_KEY_HEADER]: targetingKey,
};
}

export function headerToEvaluationContext(
Expand Down
5 changes: 5 additions & 0 deletions src/libs/targeting-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use client'

import { v4 } from "uuid";

export const TARGETING_KEY = v4();
21 changes: 1 addition & 20 deletions src/providers/react-query.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
"use client";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useMemo } from "react";
import type { EvaluationContext } from "@openfeature/react-sdk";

declare module "@tanstack/react-query" {
interface Register {
queryMeta: EvaluationContext;
mutationMeta: EvaluationContext;
}
}

export function ReactQueryProvider({
targetingKey,
children,
}: {
targetingKey: string;
children: React.ReactNode;
}) {
const queryClient = useMemo(() => {
return new QueryClient({
defaultOptions: {
queries: { meta: { targetingKey } },
mutations: { meta: { targetingKey } },
},
});
}, [targetingKey]);

return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
<QueryClientProvider client={new QueryClient()}>{children}</QueryClientProvider>
);
}
Loading