@zuplo/zudoku-plugin-monetization 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,54 +1,14 @@
1
- import * as zudoku0 from "zudoku";
2
- import * as zudoku_icons0 from "zudoku/icons";
3
- import * as react_jsx_runtime0 from "react/jsx-runtime";
4
- import * as react0 from "react";
5
- import * as zudoku_plugins0 from "zudoku/plugins";
1
+ import { ApiIdentityPlugin, CommonPlugin, NavigationPlugin, ProfileMenuPlugin } from "zudoku";
2
+ import { TransformConfigPlugin } from "zudoku/plugins";
6
3
 
7
4
  //#region src/ZuploMonetizationPlugin.d.ts
8
5
  type ZudokuMonetizationPluginOptions = {
9
- environmentName: string;
6
+ environmentName?: string;
10
7
  pricing?: {
11
8
  subtitle?: string;
12
9
  title?: string;
13
10
  };
14
11
  };
15
- declare const zuploMonetizationPlugin: (options: ZudokuMonetizationPluginOptions) => {
16
- transformConfig: (config: zudoku0.ZudokuConfig) => {
17
- slots: {
18
- "head-navigation-start": () => react_jsx_runtime0.JSX.Element;
19
- };
20
- };
21
- getIdentities: (context: zudoku0.ZudokuContext) => Promise<{
22
- label: string;
23
- id: string;
24
- authorizeRequest: (request: Request) => Promise<Request>;
25
- authorizationFields: {
26
- headers: string[];
27
- };
28
- }[]>;
29
- getProfileMenuItems: () => {
30
- label: string;
31
- path: string;
32
- icon: react0.ForwardRefExoticComponent<Omit<zudoku_icons0.LucideProps, "ref"> & react0.RefAttributes<SVGSVGElement>>;
33
- }[];
34
- getRoutes: () => ({
35
- Component: () => react_jsx_runtime0.JSX.Element;
36
- handle: {
37
- layout: string;
38
- };
39
- children: {
40
- path: string;
41
- element: react_jsx_runtime0.JSX.Element;
42
- }[];
43
- } | {
44
- Component: () => react_jsx_runtime0.JSX.Element;
45
- children: {
46
- path: string;
47
- element: react_jsx_runtime0.JSX.Element;
48
- }[];
49
- handle?: undefined;
50
- })[];
51
- getProtectedRoutes: () => string[];
52
- } & zudoku_plugins0.TransformConfigPlugin;
12
+ declare const zuploMonetizationPlugin: (options?: ZudokuMonetizationPluginOptions | undefined) => NavigationPlugin & ProfileMenuPlugin & ApiIdentityPlugin & CommonPlugin & TransformConfigPlugin;
53
13
  //#endregion
54
14
  export { zuploMonetizationPlugin };
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { cn, createPlugin, joinUrl } from "zudoku";
2
+ import { Button, ClientOnly, Heading } from "zudoku/components";
2
3
  import { AlertTriangleIcon, ArrowLeftIcon, ArrowUpIcon, CheckIcon, ClockIcon, InfoIcon, LockIcon, RefreshCwIcon, ShieldIcon, StarsIcon, Trash2Icon } from "zudoku/icons";
3
4
  import { Link, Outlet, useNavigate, useParams, useSearchParams } from "zudoku/router";
4
- import { Button, ClientOnly, Heading } from "zudoku/components";
5
5
  import { useAuth, useZudoku } from "zudoku/hooks";
6
6
  import { QueryClient, QueryClientProvider, useMutation, useQuery, useSuspenseQuery } from "zudoku/react-query";
7
7
  import { Alert, AlertDescription, AlertTitle } from "zudoku/ui/Alert";
@@ -235,19 +235,19 @@ const formatBillingCycle = (duration) => {
235
235
  if (duration.includes(" ")) return `every ${duration}`;
236
236
  return `every ${duration}`;
237
237
  };
238
- const CheckoutConfirmPage = ({ environmentName }) => {
238
+ const CheckoutConfirmPage = () => {
239
239
  const [search] = useSearchParams();
240
240
  const planId = search.get("plan");
241
241
  const zudoku = useZudoku();
242
242
  const navigate = useNavigate();
243
- const { data: plans } = usePlans(environmentName);
243
+ const { data: plans } = usePlans(zudoku.env.ZUPLO_PUBLIC_DEPLOYMENT_NAME ?? "");
244
244
  const selectedPlan = plans?.items?.find((plan) => plan.id === planId);
245
245
  const rateCards = selectedPlan?.phases.at(-1)?.rateCards;
246
246
  const { quotas, features } = categorizeRateCards(rateCards ?? []);
247
247
  const price = selectedPlan ? getPriceFromPlan(selectedPlan) : null;
248
248
  const billingCycle = selectedPlan?.billingCadence ? formatDuration(selectedPlan.billingCadence) : null;
249
249
  const createSubscriptionMutation = useMutation({
250
- mutationFn: createMutationFn(`/v3/zudoku-metering/${environmentName}/subscriptions`, zudoku, {
250
+ mutationFn: createMutationFn(`/v3/zudoku-metering/${zudoku.env.ZUPLO_PUBLIC_DEPLOYMENT_NAME}/subscriptions`, zudoku, {
251
251
  method: "POST",
252
252
  body: JSON.stringify({ planId })
253
253
  }),
@@ -402,7 +402,7 @@ const useUrlUtils = () => {
402
402
 
403
403
  //#endregion
404
404
  //#region src/pages/CheckoutPage.tsx
405
- const CheckoutPage = ({ environmentName }) => {
405
+ const CheckoutPage = () => {
406
406
  const { planId } = useParams();
407
407
  const zudoku = useZudoku();
408
408
  const auth = useAuth();
@@ -411,7 +411,7 @@ const CheckoutPage = ({ environmentName }) => {
411
411
  queryKey: ["plan", planId],
412
412
  queryFn: async () => {
413
413
  if (!auth.profile?.email) throw new Error("No email found for user. Make sure your Authentication Provider exposes the email address.");
414
- const request = await zudoku.signRequest(new Request(`https://api.zuploedge.com/v3/zudoku-metering/${environmentName}/stripe/checkout`, {
414
+ const request = await zudoku.signRequest(new Request(`https://api.zuploedge.com/v3/zudoku-metering/${zudoku.env.ZUPLO_PUBLIC_DEPLOYMENT_NAME}/stripe/checkout`, {
415
415
  method: "POST",
416
416
  headers: { "Content-Type": "application/json" },
417
417
  body: JSON.stringify({
@@ -552,13 +552,13 @@ const PricingCard = ({ plan, isPopular = false, disabled = false }) => {
552
552
 
553
553
  //#endregion
554
554
  //#region src/pages/PricingPage.tsx
555
- const PricingPage = ({ environmentName, subtitle = "See our pricing options and choose the one that best suits your needs.", title = "Pricing" }) => {
556
- const { data: pricingTable } = useSuspenseQuery({ queryKey: [`/v3/zudoku-metering/${environmentName}/pricing-page`] });
555
+ const PricingPage = ({ subtitle = "See our pricing options and choose the one that best suits your needs.", title = "Pricing" }) => {
557
556
  const zudoku = useZudoku();
557
+ const { data: pricingTable } = useSuspenseQuery({ queryKey: [`/v3/zudoku-metering/${zudoku.env.ZUPLO_PUBLIC_DEPLOYMENT_NAME}/pricing-page`] });
558
558
  const auth = useAuth();
559
559
  const { data: subscriptions = { items: [] } } = useQuery({
560
560
  meta: { context: zudoku },
561
- queryKey: [`/v3/zudoku-metering/${environmentName}/subscriptions`],
561
+ queryKey: [`/v3/zudoku-metering/${zudoku.env.ZUPLO_PUBLIC_DEPLOYMENT_NAME}/subscriptions`],
562
562
  enabled: auth.isAuthenticated
563
563
  });
564
564
  const getGridCols = (count) => {
@@ -965,8 +965,9 @@ const Usage = ({ subscriptionId, environmentName, currentItems }) => {
965
965
 
966
966
  //#endregion
967
967
  //#region src/pages/SubscriptionsPage.tsx
968
- const SubscriptionsPage = ({ environmentName }) => {
969
- const { data } = useSubscriptions(environmentName);
968
+ const SubscriptionsPage = () => {
969
+ const deploymentName = useZudoku().env.ZUPLO_PUBLIC_DEPLOYMENT_NAME ?? "";
970
+ const { data } = useSubscriptions(deploymentName);
970
971
  const { subscriptionId } = useParams();
971
972
  const subscriptions = data?.items ?? [];
972
973
  const activeSubscription = useMemo(() => {
@@ -993,11 +994,11 @@ const SubscriptionsPage = ({ environmentName }) => {
993
994
  }),
994
995
  activeSubscription && /* @__PURE__ */ jsx(Usage, {
995
996
  subscriptionId: activeSubscription?.id,
996
- environmentName,
997
+ environmentName: deploymentName,
997
998
  currentItems: activePhase?.items
998
999
  }),
999
1000
  activeSubscription?.consumer?.apiKeys && /* @__PURE__ */ jsx(ApiKeysList, {
1000
- deploymentName: environmentName,
1001
+ deploymentName,
1001
1002
  consumerId: activeSubscription.consumer.id,
1002
1003
  apiKeys: activeSubscription.consumer.apiKeys
1003
1004
  }),
@@ -1018,23 +1019,27 @@ var SubscriptionsPage_default = SubscriptionsPage;
1018
1019
  //#region src/ZuploMonetizationPlugin.tsx
1019
1020
  const PRICING_PATH = "/pricing";
1020
1021
  const zuploMonetizationPlugin = createPlugin((options) => ({
1021
- transformConfig: (config) => {
1022
- return { slots: { "head-navigation-start": () => {
1023
- return /* @__PURE__ */ jsx(Link, {
1024
- to: PRICING_PATH,
1025
- children: "Pricing"
1026
- });
1027
- } } };
1022
+ transformConfig: ({ merge }) => merge({ slots: { "head-navigation-start": () => /* @__PURE__ */ jsx(Button, {
1023
+ asChild: true,
1024
+ variant: "ghost",
1025
+ children: /* @__PURE__ */ jsx(Link, {
1026
+ to: PRICING_PATH,
1027
+ children: "Pricing"
1028
+ })
1029
+ }) } }),
1030
+ initialize: (context) => {
1031
+ if (!context.env.ZUPLO_PUBLIC_DEPLOYMENT_NAME || options?.environmentName) throw new Error("ZUPLO_PUBLIC_DEPLOYMENT_NAME is not set");
1028
1032
  },
1029
1033
  getIdentities: async (context) => {
1030
1034
  return (await queryClient.fetchQuery({
1031
- queryKey: [`/v3/zudoku-metering/${options.environmentName}/subscriptions`],
1035
+ queryKey: [`/v3/zudoku-metering/${context.env.ZUPLO_PUBLIC_DEPLOYMENT_NAME}/subscriptions`],
1032
1036
  meta: { context }
1033
1037
  })).items.flatMap((item) => item.consumer.apiKeys.map((apiKey) => ({
1034
1038
  label: item.name,
1035
1039
  id: apiKey.id,
1036
1040
  authorizeRequest: async (request) => {
1037
- return new Request(request, { headers: { Authorization: `Bearer ${apiKey.key}` } });
1041
+ request.headers.set("Authorization", `Bearer ${apiKey.key}`);
1042
+ return request;
1038
1043
  },
1039
1044
  authorizationFields: { headers: ["Authorization"] }
1040
1045
  })));
@@ -1044,37 +1049,38 @@ const zuploMonetizationPlugin = createPlugin((options) => ({
1044
1049
  path: "/subscriptions",
1045
1050
  icon: StarsIcon
1046
1051
  }],
1047
- getRoutes: () => [{
1048
- Component: ZuploMonetizationWrapper_default,
1049
- handle: { layout: "none" },
1050
- children: [{
1051
- path: "/checkout/:planId?",
1052
- element: /* @__PURE__ */ jsx(CheckoutPage_default, { environmentName: options.environmentName })
1052
+ getRoutes: () => {
1053
+ return [{
1054
+ Component: ZuploMonetizationWrapper_default,
1055
+ handle: { layout: "none" },
1056
+ children: [{
1057
+ path: "/checkout/:planId?",
1058
+ element: /* @__PURE__ */ jsx(CheckoutPage_default, {})
1059
+ }, {
1060
+ path: "/checkout-confirm",
1061
+ element: /* @__PURE__ */ jsx(CheckoutConfimPage_default, {})
1062
+ }]
1053
1063
  }, {
1054
- path: "/checkout-confirm",
1055
- element: /* @__PURE__ */ jsx(CheckoutConfimPage_default, { environmentName: options.environmentName })
1056
- }]
1057
- }, {
1058
- Component: ZuploMonetizationWrapper_default,
1059
- children: [
1060
- {
1061
- path: "/pricing",
1062
- element: /* @__PURE__ */ jsx(PricingPage_default, {
1063
- environmentName: options.environmentName,
1064
- subtitle: options.pricing?.subtitle,
1065
- title: options.pricing?.title
1066
- })
1067
- },
1068
- {
1069
- path: "/checkout-failed",
1070
- element: /* @__PURE__ */ jsx(CheckoutFailedPage_default, {})
1071
- },
1072
- {
1073
- path: "/subscriptions/:subscriptionId?",
1074
- element: /* @__PURE__ */ jsx(SubscriptionsPage_default, { environmentName: options.environmentName })
1075
- }
1076
- ]
1077
- }],
1064
+ Component: ZuploMonetizationWrapper_default,
1065
+ children: [
1066
+ {
1067
+ path: "/pricing",
1068
+ element: /* @__PURE__ */ jsx(PricingPage_default, {
1069
+ subtitle: options?.pricing?.subtitle,
1070
+ title: options?.pricing?.title
1071
+ })
1072
+ },
1073
+ {
1074
+ path: "/checkout-failed",
1075
+ element: /* @__PURE__ */ jsx(CheckoutFailedPage_default, {})
1076
+ },
1077
+ {
1078
+ path: "/subscriptions/:subscriptionId?",
1079
+ element: /* @__PURE__ */ jsx(SubscriptionsPage_default, {})
1080
+ }
1081
+ ]
1082
+ }];
1083
+ },
1078
1084
  getProtectedRoutes: () => {
1079
1085
  return [
1080
1086
  "/checkout/*",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zuplo/zudoku-plugin-monetization",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "type": "module",
5
5
  "main": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.mts",