@scalemule/nextjs 0.1.30 → 0.1.31

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/README.md CHANGED
@@ -95,6 +95,44 @@ For zero-config auth (reads from env vars):
95
95
  export { GET, POST, DELETE, PATCH } from '@scalemule/nextjs/server/auth'
96
96
  ```
97
97
 
98
+ ## Proxy Mode (keep API keys server-side)
99
+
100
+ If you don't want a real ScaleMule API key in browser bundles, run the SDK in **proxy mode**: pass the literal sentinel string `'proxy-mode'` as `apiKey` and route all server-bound traffic through Next.js routes that hold the real `SCALEMULE_API_KEY` server-side. **Both** the auth route and the analytics route are required — without the analytics route, every `trackEvent` / `trackPageView` silently 401s against the gateway and the dashboard reads empty.
101
+
102
+ ```ts
103
+ // app/api/auth/[...scalemule]/route.ts
104
+ export { GET, POST, DELETE, PATCH } from '@scalemule/nextjs/server/auth'
105
+ ```
106
+
107
+ ```ts
108
+ // app/api/analytics/event/route.ts
109
+ import { NextRequest } from 'next/server'
110
+ import { createAnalyticsRoutes } from '@scalemule/nextjs/server'
111
+
112
+ const { POST: handler } = createAnalyticsRoutes({ simpleProxy: true })
113
+
114
+ export const dynamic = 'force-dynamic'
115
+ export function POST(req: NextRequest, ctx: { params: Promise<Record<string, string | string[]>> }) {
116
+ return handler(req, ctx)
117
+ }
118
+ ```
119
+
120
+ ```tsx
121
+ // app/layout.tsx
122
+ <ScaleMuleProvider
123
+ apiKey="proxy-mode"
124
+ applicationId={process.env.SCALEMULE_APP_ID}
125
+ environment="prod"
126
+ gatewayUrl="https://api.scalemule.com"
127
+ authProxyUrl="/api/auth"
128
+ analyticsProxyUrl="/api/analytics/event"
129
+ >
130
+ {children}
131
+ </ScaleMuleProvider>
132
+ ```
133
+
134
+ If `apiKey="proxy-mode"` is set without `analyticsProxyUrl` (and no `publishableKey`), `useAnalytics` emits a one-time dev console warning — that exact misconfig caused a silent-empty dashboard in production for one customer (Gistyo, app ID `019dda2a-c7d9-75a1-9258-12e4c48f1499`).
135
+
98
136
  ## Client-Side Provider
99
137
 
100
138
  ```tsx
package/dist/index.d.mts CHANGED
@@ -245,6 +245,14 @@ interface ScaleMuleContextValue {
245
245
  authProxyUrl?: string;
246
246
  /** Publishable key for browser-safe operations (analytics) */
247
247
  publishableKey?: string;
248
+ /**
249
+ * The configured `apiKey` value. Exposed so hooks like `useAnalytics`
250
+ * can detect the proxy-mode sentinel (`'proxy-mode'`) and warn when
251
+ * the corresponding proxy route is missing — silently 401'ing on the
252
+ * fallback is the worst possible failure mode for an analytics path.
253
+ * Treat as read-only diagnostic; consumers should not pass it onward.
254
+ */
255
+ apiKey?: string;
248
256
  /** Gateway URL for direct API calls */
249
257
  gatewayUrl?: string;
250
258
  /** Configured environment ('dev' or 'prod') */
package/dist/index.d.ts CHANGED
@@ -245,6 +245,14 @@ interface ScaleMuleContextValue {
245
245
  authProxyUrl?: string;
246
246
  /** Publishable key for browser-safe operations (analytics) */
247
247
  publishableKey?: string;
248
+ /**
249
+ * The configured `apiKey` value. Exposed so hooks like `useAnalytics`
250
+ * can detect the proxy-mode sentinel (`'proxy-mode'`) and warn when
251
+ * the corresponding proxy route is missing — silently 401'ing on the
252
+ * fallback is the worst possible failure mode for an analytics path.
253
+ * Treat as read-only diagnostic; consumers should not pass it onward.
254
+ */
255
+ apiKey?: string;
248
256
  /** Gateway URL for direct API calls */
249
257
  gatewayUrl?: string;
250
258
  /** Configured environment ('dev' or 'prod') */
package/dist/index.js CHANGED
@@ -8924,13 +8924,14 @@ function ScaleMuleProvider({
8924
8924
  analyticsProxyUrl,
8925
8925
  authProxyUrl,
8926
8926
  publishableKey,
8927
+ apiKey,
8927
8928
  gatewayUrl: resolvedGatewayUrl,
8928
8929
  environment: environment || void 0,
8929
8930
  enableAccountSwitcher,
8930
8931
  accountSwitcherPrivacy,
8931
8932
  bootstrapFlags
8932
8933
  }),
8933
- [client, money$1, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
8934
+ [client, money$1, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
8934
8935
  );
8935
8936
  return /* @__PURE__ */ jsxRuntime.jsx(ScaleMuleContext.Provider, { value, children });
8936
8937
  }
@@ -11645,7 +11646,18 @@ function useAnalytics(options = {}) {
11645
11646
  eventDedupMs = DEFAULT_EVENT_DEDUP_MS
11646
11647
  } = options;
11647
11648
  const shouldAutoCaptureUtmParams = autoCaptureUtmParams ?? autoCapturUtmParams ?? true;
11648
- const { client, user, analyticsProxyUrl, publishableKey, gatewayUrl } = useScaleMule();
11649
+ const { client, user, analyticsProxyUrl, publishableKey, apiKey, gatewayUrl } = useScaleMule();
11650
+ const proxyModeMisconfigWarnedRef = React.useRef(false);
11651
+ React.useEffect(() => {
11652
+ if (process.env.NODE_ENV === "production") return;
11653
+ if (proxyModeMisconfigWarnedRef.current) return;
11654
+ if (apiKey === "proxy-mode" && !analyticsProxyUrl && !publishableKey) {
11655
+ proxyModeMisconfigWarnedRef.current = true;
11656
+ console.warn(
11657
+ '[ScaleMule] useAnalytics: apiKey="proxy-mode" but neither `analyticsProxyUrl` nor `publishableKey` is configured. Every trackEvent / trackPageView will silently 401 against the gateway. Add an analytics proxy route (createAnalyticsRoutes({ simpleProxy: true })) and pass `analyticsProxyUrl` to <ScaleMuleProvider>. See @scalemule/nextjs README \u2192 Proxy Mode.'
11658
+ );
11659
+ }
11660
+ }, [apiKey, analyticsProxyUrl, publishableKey]);
11649
11661
  const [loading, setLoading] = React.useState(false);
11650
11662
  const [error, setError] = React.useState(null);
11651
11663
  const [utmParams, setUtmParams] = React.useState(null);
package/dist/index.mjs CHANGED
@@ -8904,13 +8904,14 @@ function ScaleMuleProvider({
8904
8904
  analyticsProxyUrl,
8905
8905
  authProxyUrl,
8906
8906
  publishableKey,
8907
+ apiKey,
8907
8908
  gatewayUrl: resolvedGatewayUrl,
8908
8909
  environment: environment || void 0,
8909
8910
  enableAccountSwitcher,
8910
8911
  accountSwitcherPrivacy,
8911
8912
  bootstrapFlags
8912
8913
  }),
8913
- [client, money, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
8914
+ [client, money, baseClient, user, handleSetUser, initializing, error, analyticsProxyUrl, authProxyUrl, publishableKey, apiKey, resolvedGatewayUrl, environment, enableAccountSwitcher, accountSwitcherPrivacy, bootstrapFlags, effectiveMediaPolicy]
8914
8915
  );
8915
8916
  return /* @__PURE__ */ jsx(ScaleMuleContext.Provider, { value, children });
8916
8917
  }
@@ -11625,7 +11626,18 @@ function useAnalytics(options = {}) {
11625
11626
  eventDedupMs = DEFAULT_EVENT_DEDUP_MS
11626
11627
  } = options;
11627
11628
  const shouldAutoCaptureUtmParams = autoCaptureUtmParams ?? autoCapturUtmParams ?? true;
11628
- const { client, user, analyticsProxyUrl, publishableKey, gatewayUrl } = useScaleMule();
11629
+ const { client, user, analyticsProxyUrl, publishableKey, apiKey, gatewayUrl } = useScaleMule();
11630
+ const proxyModeMisconfigWarnedRef = useRef(false);
11631
+ useEffect(() => {
11632
+ if (process.env.NODE_ENV === "production") return;
11633
+ if (proxyModeMisconfigWarnedRef.current) return;
11634
+ if (apiKey === "proxy-mode" && !analyticsProxyUrl && !publishableKey) {
11635
+ proxyModeMisconfigWarnedRef.current = true;
11636
+ console.warn(
11637
+ '[ScaleMule] useAnalytics: apiKey="proxy-mode" but neither `analyticsProxyUrl` nor `publishableKey` is configured. Every trackEvent / trackPageView will silently 401 against the gateway. Add an analytics proxy route (createAnalyticsRoutes({ simpleProxy: true })) and pass `analyticsProxyUrl` to <ScaleMuleProvider>. See @scalemule/nextjs README \u2192 Proxy Mode.'
11638
+ );
11639
+ }
11640
+ }, [apiKey, analyticsProxyUrl, publishableKey]);
11629
11641
  const [loading, setLoading] = useState(false);
11630
11642
  const [error, setError] = useState(null);
11631
11643
  const [utmParams, setUtmParams] = useState(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/nextjs",
3
- "version": "0.1.30",
3
+ "version": "0.1.31",
4
4
  "description": "ScaleMule SDK for Next.js applications - authentication, storage, and user management",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",