@simpleapps-com/augur-server 0.1.9 → 0.2.1
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/auth.js +0 -2
- package/dist/auth.js.map +1 -1
- package/dist/index.d.ts +1 -50
- package/dist/index.js +4 -166
- package/dist/index.js.map +1 -1
- package/dist/query.js +0 -1
- package/package.json +2 -2
- package/dist/chunk-DGUM43GV.js +0 -11
- package/dist/chunk-DGUM43GV.js.map +0 -1
package/dist/auth.js
CHANGED
package/dist/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/auth.ts"],"sourcesContent":["// @simpleapps-com/augur-server/auth\n// NextAuth 5 configuration factory for Augur ecommerce sites.\n\nimport type { NextAuthConfig } from \"next-auth\";\nimport Credentials from \"next-auth/providers/credentials\";\n\n/** Base user fields shared across all Augur sites. */\nexport interface AugurUser {\n id: string;\n username: string;\n isVerified: boolean;\n name?: string;\n email?: string;\n customerId?: string | number;\n contactId?: string | number;\n cartHdrUid?: number;\n token?: string;\n}\n\n/** Session shape returned by all Augur sites. */\nexport interface AugurSession {\n user: AugurUser;\n token?: string;\n expires: string;\n}\n\n/** JWT token shape used by Augur auth. */\nexport interface AugurJWT {\n id: string;\n username: string;\n isVerified: boolean;\n token?: string;\n}\n\n/** Site-specific callbacks the consumer can provide. */\nexport interface AugurAuthCallbacks {\n /** Fetch user profile from the Augur API given the user's Joomla ID. */\n getUserProfile: (userId: string) => Promise<AugurUser | null>;\n /** Look up or create a cart header for the authenticated user. */\n cartHdrLookup?: (\n userId: string | number,\n token: string,\n contactId: string | number,\n customerId: string | number,\n ) => Promise<{ cartHdrUid?: number } | null>;\n}\n\n/**\n * Minimal interface for the Augur SDK client used by auth internals.\n * Any `AugurAPI` instance from `@simpleapps-com/augur-api` satisfies this.\n */\nexport interface AugurAuthClient {\n joomla: {\n users: {\n doc: {\n get(\n userId: number,\n params?: { edgeCache?: number | string },\n ): Promise<{\n data: {\n id: number;\n name: string;\n username: string;\n email: string;\n customerId?: string;\n contactId?: string;\n };\n }>;\n };\n };\n };\n commerce: {\n cartHdr: {\n lookup: {\n get(params: {\n userId: number;\n customerId: number;\n contactId: number;\n cartToken?: string;\n }): Promise<{\n data: { cartHdrUid?: number };\n }>;\n };\n };\n };\n}\n\nexport interface CreateAuthConfigOptions {\n /** Augur SDK client. When provided, auth SDK calls are handled internally. */\n augurClient?: AugurAuthClient;\n /** Site-specific callbacks. Override internal SDK calls when custom behavior is needed. */\n callbacks?: Partial<AugurAuthCallbacks>;\n /** NextAuth secret (defaults to NEXT_PUBLIC_AUTH_SECRET env var). */\n secret?: string;\n /** Session max age in seconds (defaults to 4 hours). */\n maxAge?: number;\n /** Default customer ID when profile doesn't provide one. */\n defaultCustomerId?: string | number;\n /** Default contact ID for cart header lookups. */\n defaultContactId?: string | number;\n /** Enable NextAuth debug logging (defaults to NODE_ENV === \"development\"). */\n debug?: boolean;\n}\n\n/** Build internal callbacks that call the Augur SDK directly. */\nfunction buildInternalCallbacks(\n augurClient: AugurAuthClient,\n): AugurAuthCallbacks {\n return {\n async getUserProfile(userId: string): Promise<AugurUser | null> {\n if (!userId) return null;\n const uid = parseInt(userId, 10);\n if (isNaN(uid)) return null;\n const result = await augurClient.joomla.users.doc.get(uid, {\n edgeCache: 1,\n });\n const data = result.data;\n if (!data) return null;\n return {\n id: String(data.id),\n name: data.name,\n username: data.username,\n email: data.email,\n isVerified: true,\n customerId: data.customerId,\n contactId: data.contactId,\n };\n },\n async cartHdrLookup(\n userId: string | number,\n _token: string,\n contactId: string | number,\n customerId: string | number,\n ): Promise<{ cartHdrUid?: number } | null> {\n const result = await augurClient.commerce.cartHdr.lookup.get({\n userId: Number(userId) || 0,\n customerId: Number(customerId) || 0,\n contactId: Number(contactId) || 0,\n });\n return result.data ?? null;\n },\n };\n}\n\n/** Resolve final callbacks: callback overrides take priority over internal SDK calls. */\nfunction resolveCallbacks(\n options: CreateAuthConfigOptions,\n): AugurAuthCallbacks {\n const { augurClient, callbacks } = options;\n\n if (augurClient) {\n const internal = buildInternalCallbacks(augurClient);\n return {\n getUserProfile: callbacks?.getUserProfile ?? internal.getUserProfile,\n cartHdrLookup: callbacks?.cartHdrLookup ?? internal.cartHdrLookup,\n };\n }\n\n if (!callbacks?.getUserProfile) {\n throw new Error(\n \"createAuthConfig requires either `augurClient` or `callbacks.getUserProfile`\",\n );\n }\n\n return {\n getUserProfile: callbacks.getUserProfile,\n cartHdrLookup: callbacks.cartHdrLookup,\n };\n}\n\n/**\n * Create a NextAuth 5 configuration for an Augur ecommerce site.\n *\n * Accepts either an `augurClient` (recommended) for zero-boilerplate auth,\n * or explicit `callbacks` for custom behavior. Callback overrides take\n * priority when both are provided.\n *\n * @example\n * ```ts\n * // Recommended: pass the SDK client directly\n * import NextAuth from \"next-auth\";\n * import { createAuthConfig } from \"@simpleapps-com/augur-server/auth\";\n * import { augurClient } from \"@/lib/augur-client\";\n *\n * export const { handlers, signIn, signOut, auth } = NextAuth(\n * createAuthConfig({\n * augurClient,\n * defaultCustomerId: process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_ID,\n * defaultContactId: process.env.NEXT_PUBLIC_DEFAULT_CONTACT_ID,\n * }),\n * );\n * ```\n *\n * @example\n * ```ts\n * // Custom callbacks (backward-compatible)\n * import NextAuth from \"next-auth\";\n * import { createAuthConfig } from \"@simpleapps-com/augur-server/auth\";\n *\n * export const { handlers, signIn, signOut, auth } = NextAuth(\n * createAuthConfig({\n * callbacks: { getUserProfile, cartHdrLookup },\n * defaultCustomerId: process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_ID,\n * }),\n * );\n * ```\n */\nasync function lookupCartHdr(\n siteCallbacks: AugurAuthCallbacks,\n profile: AugurUser,\n defaultCustomerId?: string | number,\n defaultContactId?: string | number,\n): Promise<number | undefined> {\n if (!siteCallbacks.cartHdrLookup || !profile.id) return undefined;\n const cartHdr = await siteCallbacks.cartHdrLookup(\n profile.id,\n \"\",\n profile.contactId ?? defaultContactId ?? \"\",\n profile.customerId ?? defaultCustomerId ?? \"\",\n );\n return cartHdr?.cartHdrUid;\n}\n\nexport function createAuthConfig(\n options: CreateAuthConfigOptions,\n): NextAuthConfig {\n const {\n secret = process.env.NEXT_PUBLIC_AUTH_SECRET,\n maxAge = 4 * 60 * 60,\n defaultCustomerId,\n defaultContactId,\n debug = process.env.NODE_ENV === \"development\",\n } = options;\n\n const siteCallbacks = resolveCallbacks(options);\n\n return {\n providers: [\n Credentials({\n name: \"Credentials\",\n credentials: {\n username: { label: \"Username\", type: \"text\" },\n password: { label: \"Password\", type: \"password\" },\n id: { label: \"ID\", type: \"text\" },\n isVerified: { label: \"Is Verified\", type: \"text\" },\n token: { label: \"Token\", type: \"text\" },\n },\n async authorize(credentials) {\n if (!credentials) return null;\n\n const { id, isVerified, username, token } = credentials as {\n id: string;\n isVerified: string;\n username: string;\n token: string;\n };\n\n return {\n id,\n isVerified: isVerified === \"true\",\n username,\n token,\n };\n },\n }),\n ],\n callbacks: {\n async signIn() {\n return true;\n },\n async redirect({ baseUrl }) {\n return baseUrl;\n },\n async session({ session, token }) {\n const baseUser = {\n id: (token.id as string) || \"\",\n username: (token.username as string) || \"\",\n isVerified: (token.isVerified as boolean) || false,\n };\n\n const buildSession = (userOverrides = {}) => ({\n ...session,\n user: { ...session.user, ...baseUser, ...userOverrides },\n token: token.token as string | undefined,\n });\n\n try {\n const userProfile = await siteCallbacks.getUserProfile(baseUser.id);\n if (!userProfile) return buildSession();\n\n const cartHdrUid = await lookupCartHdr(\n siteCallbacks,\n userProfile,\n defaultCustomerId,\n defaultContactId,\n );\n\n return buildSession({\n name: userProfile.name ?? session.user?.name,\n email: userProfile.email ?? session.user?.email,\n customerId: userProfile.customerId ?? defaultCustomerId,\n contactId: userProfile.contactId,\n cartHdrUid,\n });\n } catch {\n return buildSession();\n }\n },\n async jwt({ token, user }) {\n if (user) {\n const typedUser = user as {\n id: string;\n isVerified: boolean;\n username: string;\n token: string;\n };\n\n return {\n ...token,\n id: typedUser.id,\n isVerified: typedUser.isVerified,\n username: typedUser.username,\n token: typedUser.token,\n };\n }\n return token;\n },\n },\n secret,\n debug,\n session: {\n strategy: \"jwt\",\n maxAge,\n },\n trustHost: true,\n };\n}\n"],"mappings":";;;AAIA,OAAO,iBAAiB;AAqGxB,SAAS,uBACP,aACoB;AACpB,SAAO;AAAA,IACL,MAAM,eAAe,QAA2C;AAC9D,UAAI,CAAC,OAAQ,QAAO;AACpB,YAAM,MAAM,SAAS,QAAQ,EAAE;AAC/B,UAAI,MAAM,GAAG,EAAG,QAAO;AACvB,YAAM,SAAS,MAAM,YAAY,OAAO,MAAM,IAAI,IAAI,KAAK;AAAA,QACzD,WAAW;AAAA,MACb,CAAC;AACD,YAAM,OAAO,OAAO;AACpB,UAAI,CAAC,KAAM,QAAO;AAClB,aAAO;AAAA,QACL,IAAI,OAAO,KAAK,EAAE;AAAA,QAClB,MAAM,KAAK;AAAA,QACX,UAAU,KAAK;AAAA,QACf,OAAO,KAAK;AAAA,QACZ,YAAY;AAAA,QACZ,YAAY,KAAK;AAAA,QACjB,WAAW,KAAK;AAAA,MAClB;AAAA,IACF;AAAA,IACA,MAAM,cACJ,QACA,QACA,WACA,YACyC;AACzC,YAAM,SAAS,MAAM,YAAY,SAAS,QAAQ,OAAO,IAAI;AAAA,QAC3D,QAAQ,OAAO,MAAM,KAAK;AAAA,QAC1B,YAAY,OAAO,UAAU,KAAK;AAAA,QAClC,WAAW,OAAO,SAAS,KAAK;AAAA,MAClC,CAAC;AACD,aAAO,OAAO,QAAQ;AAAA,IACxB;AAAA,EACF;AACF;AAGA,SAAS,iBACP,SACoB;AACpB,QAAM,EAAE,aAAa,UAAU,IAAI;AAEnC,MAAI,aAAa;AACf,UAAM,WAAW,uBAAuB,WAAW;AACnD,WAAO;AAAA,MACL,gBAAgB,WAAW,kBAAkB,SAAS;AAAA,MACtD,eAAe,WAAW,iBAAiB,SAAS;AAAA,IACtD;AAAA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AAA,EACb;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/auth.ts"],"sourcesContent":["// @simpleapps-com/augur-server/auth\n// NextAuth 5 configuration factory for Augur ecommerce sites.\n\nimport type { NextAuthConfig } from \"next-auth\";\nimport Credentials from \"next-auth/providers/credentials\";\n\n/** Base user fields shared across all Augur sites. */\nexport interface AugurUser {\n id: string;\n username: string;\n isVerified: boolean;\n name?: string;\n email?: string;\n customerId?: string | number;\n contactId?: string | number;\n cartHdrUid?: number;\n token?: string;\n}\n\n/** Session shape returned by all Augur sites. */\nexport interface AugurSession {\n user: AugurUser;\n token?: string;\n expires: string;\n}\n\n/** JWT token shape used by Augur auth. */\nexport interface AugurJWT {\n id: string;\n username: string;\n isVerified: boolean;\n token?: string;\n}\n\n/** Site-specific callbacks the consumer can provide. */\nexport interface AugurAuthCallbacks {\n /** Fetch user profile from the Augur API given the user's Joomla ID. */\n getUserProfile: (userId: string) => Promise<AugurUser | null>;\n /** Look up or create a cart header for the authenticated user. */\n cartHdrLookup?: (\n userId: string | number,\n token: string,\n contactId: string | number,\n customerId: string | number,\n ) => Promise<{ cartHdrUid?: number } | null>;\n}\n\n/**\n * Minimal interface for the Augur SDK client used by auth internals.\n * Any `AugurAPI` instance from `@simpleapps-com/augur-api` satisfies this.\n */\nexport interface AugurAuthClient {\n joomla: {\n users: {\n doc: {\n get(\n userId: number,\n params?: { edgeCache?: number | string },\n ): Promise<{\n data: {\n id: number;\n name: string;\n username: string;\n email: string;\n customerId?: string;\n contactId?: string;\n };\n }>;\n };\n };\n };\n commerce: {\n cartHdr: {\n lookup: {\n get(params: {\n userId: number;\n customerId: number;\n contactId: number;\n cartToken?: string;\n }): Promise<{\n data: { cartHdrUid?: number };\n }>;\n };\n };\n };\n}\n\nexport interface CreateAuthConfigOptions {\n /** Augur SDK client. When provided, auth SDK calls are handled internally. */\n augurClient?: AugurAuthClient;\n /** Site-specific callbacks. Override internal SDK calls when custom behavior is needed. */\n callbacks?: Partial<AugurAuthCallbacks>;\n /** NextAuth secret (defaults to NEXT_PUBLIC_AUTH_SECRET env var). */\n secret?: string;\n /** Session max age in seconds (defaults to 4 hours). */\n maxAge?: number;\n /** Default customer ID when profile doesn't provide one. */\n defaultCustomerId?: string | number;\n /** Default contact ID for cart header lookups. */\n defaultContactId?: string | number;\n /** Enable NextAuth debug logging (defaults to NODE_ENV === \"development\"). */\n debug?: boolean;\n}\n\n/** Build internal callbacks that call the Augur SDK directly. */\nfunction buildInternalCallbacks(\n augurClient: AugurAuthClient,\n): AugurAuthCallbacks {\n return {\n async getUserProfile(userId: string): Promise<AugurUser | null> {\n if (!userId) return null;\n const uid = parseInt(userId, 10);\n if (isNaN(uid)) return null;\n const result = await augurClient.joomla.users.doc.get(uid, {\n edgeCache: 1,\n });\n const data = result.data;\n if (!data) return null;\n return {\n id: String(data.id),\n name: data.name,\n username: data.username,\n email: data.email,\n isVerified: true,\n customerId: data.customerId,\n contactId: data.contactId,\n };\n },\n async cartHdrLookup(\n userId: string | number,\n _token: string,\n contactId: string | number,\n customerId: string | number,\n ): Promise<{ cartHdrUid?: number } | null> {\n const result = await augurClient.commerce.cartHdr.lookup.get({\n userId: Number(userId) || 0,\n customerId: Number(customerId) || 0,\n contactId: Number(contactId) || 0,\n });\n return result.data ?? null;\n },\n };\n}\n\n/** Resolve final callbacks: callback overrides take priority over internal SDK calls. */\nfunction resolveCallbacks(\n options: CreateAuthConfigOptions,\n): AugurAuthCallbacks {\n const { augurClient, callbacks } = options;\n\n if (augurClient) {\n const internal = buildInternalCallbacks(augurClient);\n return {\n getUserProfile: callbacks?.getUserProfile ?? internal.getUserProfile,\n cartHdrLookup: callbacks?.cartHdrLookup ?? internal.cartHdrLookup,\n };\n }\n\n if (!callbacks?.getUserProfile) {\n throw new Error(\n \"createAuthConfig requires either `augurClient` or `callbacks.getUserProfile`\",\n );\n }\n\n return {\n getUserProfile: callbacks.getUserProfile,\n cartHdrLookup: callbacks.cartHdrLookup,\n };\n}\n\n/**\n * Create a NextAuth 5 configuration for an Augur ecommerce site.\n *\n * Accepts either an `augurClient` (recommended) for zero-boilerplate auth,\n * or explicit `callbacks` for custom behavior. Callback overrides take\n * priority when both are provided.\n *\n * @example\n * ```ts\n * // Recommended: pass the SDK client directly\n * import NextAuth from \"next-auth\";\n * import { createAuthConfig } from \"@simpleapps-com/augur-server/auth\";\n * import { augurClient } from \"@/lib/augur-client\";\n *\n * export const { handlers, signIn, signOut, auth } = NextAuth(\n * createAuthConfig({\n * augurClient,\n * defaultCustomerId: process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_ID,\n * defaultContactId: process.env.NEXT_PUBLIC_DEFAULT_CONTACT_ID,\n * }),\n * );\n * ```\n *\n * @example\n * ```ts\n * // Custom callbacks (backward-compatible)\n * import NextAuth from \"next-auth\";\n * import { createAuthConfig } from \"@simpleapps-com/augur-server/auth\";\n *\n * export const { handlers, signIn, signOut, auth } = NextAuth(\n * createAuthConfig({\n * callbacks: { getUserProfile, cartHdrLookup },\n * defaultCustomerId: process.env.NEXT_PUBLIC_DEFAULT_CUSTOMER_ID,\n * }),\n * );\n * ```\n */\nasync function lookupCartHdr(\n siteCallbacks: AugurAuthCallbacks,\n profile: AugurUser,\n defaultCustomerId?: string | number,\n defaultContactId?: string | number,\n): Promise<number | undefined> {\n if (!siteCallbacks.cartHdrLookup || !profile.id) return undefined;\n const cartHdr = await siteCallbacks.cartHdrLookup(\n profile.id,\n \"\",\n profile.contactId ?? defaultContactId ?? \"\",\n profile.customerId ?? defaultCustomerId ?? \"\",\n );\n return cartHdr?.cartHdrUid;\n}\n\nexport function createAuthConfig(\n options: CreateAuthConfigOptions,\n): NextAuthConfig {\n const {\n secret = process.env.NEXT_PUBLIC_AUTH_SECRET,\n maxAge = 4 * 60 * 60,\n defaultCustomerId,\n defaultContactId,\n debug = process.env.NODE_ENV === \"development\",\n } = options;\n\n const siteCallbacks = resolveCallbacks(options);\n\n return {\n providers: [\n Credentials({\n name: \"Credentials\",\n credentials: {\n username: { label: \"Username\", type: \"text\" },\n password: { label: \"Password\", type: \"password\" },\n id: { label: \"ID\", type: \"text\" },\n isVerified: { label: \"Is Verified\", type: \"text\" },\n token: { label: \"Token\", type: \"text\" },\n },\n async authorize(credentials) {\n if (!credentials) return null;\n\n const { id, isVerified, username, token } = credentials as {\n id: string;\n isVerified: string;\n username: string;\n token: string;\n };\n\n return {\n id,\n isVerified: isVerified === \"true\",\n username,\n token,\n };\n },\n }),\n ],\n callbacks: {\n async signIn() {\n return true;\n },\n async redirect({ baseUrl }) {\n return baseUrl;\n },\n async session({ session, token }) {\n const baseUser = {\n id: (token.id as string) || \"\",\n username: (token.username as string) || \"\",\n isVerified: (token.isVerified as boolean) || false,\n };\n\n const buildSession = (userOverrides = {}) => ({\n ...session,\n user: { ...session.user, ...baseUser, ...userOverrides },\n token: token.token as string | undefined,\n });\n\n try {\n const userProfile = await siteCallbacks.getUserProfile(baseUser.id);\n if (!userProfile) return buildSession();\n\n const cartHdrUid = await lookupCartHdr(\n siteCallbacks,\n userProfile,\n defaultCustomerId,\n defaultContactId,\n );\n\n return buildSession({\n name: userProfile.name ?? session.user?.name,\n email: userProfile.email ?? session.user?.email,\n customerId: userProfile.customerId ?? defaultCustomerId,\n contactId: userProfile.contactId,\n cartHdrUid,\n });\n } catch {\n return buildSession();\n }\n },\n async jwt({ token, user }) {\n if (user) {\n const typedUser = user as {\n id: string;\n isVerified: boolean;\n username: string;\n token: string;\n };\n\n return {\n ...token,\n id: typedUser.id,\n isVerified: typedUser.isVerified,\n username: typedUser.username,\n token: typedUser.token,\n };\n }\n return token;\n },\n },\n secret,\n debug,\n session: {\n strategy: \"jwt\",\n maxAge,\n },\n trustHost: true,\n };\n}\n"],"mappings":";AAIA,OAAO,iBAAiB;AAqGxB,SAAS,uBACP,aACoB;AACpB,SAAO;AAAA,IACL,MAAM,eAAe,QAA2C;AAC9D,UAAI,CAAC,OAAQ,QAAO;AACpB,YAAM,MAAM,SAAS,QAAQ,EAAE;AAC/B,UA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H,MAAM,EAAE,GAAG,QAAQ,MAAM,GAAG,UAAU,GAAG,cAAc;AAAA,UACvD,OAAO,MAAM;AAAA,QACf;AAEA,YAAI;AACF,gBAAM,cAAc,MAAM,cAAc,eAAe,SAAS,EAAE;AAClE,cAAI,CAAC,YAAa,QAAO,aAAa;AAEtC,gBAAM,aAAa,MAAM;AAAA,YACvB;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAEA,iBAAO,aAAa;AAAA,YAClB,MAAM,YAAY,QAAQ,QAAQ,MAAM;AAAA,YACxC,OAAO,YAAY,SAAS,QAAQ,MAAM;AAAA,YAC1C,YAAY,YAAY,cAAc;AAAA,YACtC,WAAW,YAAY;AAAA,YACvB;AAAA,UACF,CAAC;AAAA,QACH,QAAQ;AACN,iBAAO,aAAa;AAAA,QACtB;AAAA,MACF;AAAA,MACA,MAAM,IAAI,EAAE,OAAO,KAAK,GAAG;AACzB,YAAI,MAAM;AACR,gBAAM,YAAY;AAOlB,iBAAO;AAAA,YACL,GAAG;AAAA,YACH,IAAI,UAAU;AAAA,YACd,YAAY,UAAU;AAAA,YACtB,UAAU,UAAU;AAAA,YACpB,OAAO,UAAU;AAAA,UACnB;AAAA,QACF;AACA,eAAO;AAAA,MACT;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS;AAAA,MACP,UAAU;AAAA,MACV;AAAA,IACF;AAAA,IACA,WAAW;AAAA,EACb;AACF;","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,55 +11,6 @@ declare function cacheSet(key: string, value: string, ttlSeconds: number): Promi
|
|
|
11
11
|
declare function getCircuitState(): "closed" | "open";
|
|
12
12
|
declare function isRedisConnected(): boolean;
|
|
13
13
|
|
|
14
|
-
/**
|
|
15
|
-
* Wraps an SDK call with Redis caching.
|
|
16
|
-
*
|
|
17
|
-
* @param prefix - Redis key prefix (e.g. "mysite:")
|
|
18
|
-
* @param methodPath - Dot-separated SDK method path (used as cache key)
|
|
19
|
-
* @param method - The SDK method to call
|
|
20
|
-
* @param args - Arguments to pass to the SDK method. If any argument
|
|
21
|
-
* contains an `edgeCache` property, the result is cached in Redis for
|
|
22
|
-
* that duration. Without `edgeCache`, the call bypasses cache.
|
|
23
|
-
*
|
|
24
|
-
* Accepted `edgeCache` values (matching augur-api CacheParams):
|
|
25
|
-
* - Numbers: 1–8 (hours)
|
|
26
|
-
* - Sub-hour strings: '30s', '1m', '5m'
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* const result = await cachedSdkCall(
|
|
31
|
-
* "mysite:",
|
|
32
|
-
* "items.categories.get",
|
|
33
|
-
* augurServices.items.categories.get,
|
|
34
|
-
* itemCategoryUid,
|
|
35
|
-
* { edgeCache: 1 },
|
|
36
|
-
* );
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
declare function cachedSdkCall<TArgs extends unknown[], TResult>(prefix: string, methodPath: string, method: (...args: TArgs) => Promise<TResult>, ...args: TArgs): Promise<TResult>;
|
|
40
|
-
declare function cachedSdkCall(prefix: string, methodPath: string, method: (...args: any[]) => Promise<any>, ...args: any[]): Promise<any>;
|
|
41
|
-
/** Returns aggregated cache stats for monitoring endpoints. */
|
|
42
|
-
declare function getCacheStats(): {
|
|
43
|
-
stats: {
|
|
44
|
-
hits: number;
|
|
45
|
-
misses: number;
|
|
46
|
-
errors: number;
|
|
47
|
-
hitRate: string;
|
|
48
|
-
};
|
|
49
|
-
topKeys: {
|
|
50
|
-
key: string;
|
|
51
|
-
hits: number;
|
|
52
|
-
misses: number;
|
|
53
|
-
avgMs: number;
|
|
54
|
-
}[];
|
|
55
|
-
recentOps: {
|
|
56
|
-
op: "HIT" | "MISS" | "SKIP" | "ERROR";
|
|
57
|
-
key: string;
|
|
58
|
-
ms: number;
|
|
59
|
-
ago: string;
|
|
60
|
-
}[];
|
|
61
|
-
};
|
|
62
|
-
|
|
63
14
|
/**
|
|
64
15
|
* Calls an Augur SDK method, forwarding all arguments with full type safety.
|
|
65
16
|
*
|
|
@@ -96,4 +47,4 @@ declare function createServerQueryClient(): QueryClient;
|
|
|
96
47
|
*/
|
|
97
48
|
declare const getServerQueryClient: () => QueryClient;
|
|
98
49
|
|
|
99
|
-
export { cacheGet, cacheSet,
|
|
50
|
+
export { cacheGet, cacheSet, createServerQueryClient, env, getCircuitState, getServerQueryClient, isDev, isProduction, isRedisConnected, isStaging, sdkCall };
|
package/dist/index.js
CHANGED
|
@@ -2,9 +2,6 @@ import {
|
|
|
2
2
|
createQueryOptions,
|
|
3
3
|
createSuspenseQueryOptions
|
|
4
4
|
} from "./chunk-I26RJDRF.js";
|
|
5
|
-
import {
|
|
6
|
-
__require
|
|
7
|
-
} from "./chunk-DGUM43GV.js";
|
|
8
5
|
|
|
9
6
|
// src/environment.ts
|
|
10
7
|
function getEnvironment() {
|
|
@@ -73,7 +70,7 @@ function getRedisUrl() {
|
|
|
73
70
|
if (isStaging) return process.env.REDIS_URL_STAGING;
|
|
74
71
|
return process.env.REDIS_URL_PROD;
|
|
75
72
|
}
|
|
76
|
-
function getClient() {
|
|
73
|
+
async function getClient() {
|
|
77
74
|
if (state.client) return state.client;
|
|
78
75
|
const url = getRedisUrl();
|
|
79
76
|
if (!url) {
|
|
@@ -81,7 +78,7 @@ function getClient() {
|
|
|
81
78
|
return null;
|
|
82
79
|
}
|
|
83
80
|
try {
|
|
84
|
-
const IORedis =
|
|
81
|
+
const { default: IORedis } = await import("ioredis");
|
|
85
82
|
state.client = new IORedis(url, {
|
|
86
83
|
maxRetriesPerRequest: 1,
|
|
87
84
|
connectTimeout: 3e3,
|
|
@@ -103,7 +100,7 @@ function getClient() {
|
|
|
103
100
|
}
|
|
104
101
|
async function cacheGet(key) {
|
|
105
102
|
if (isCircuitOpen()) return null;
|
|
106
|
-
const client = getClient();
|
|
103
|
+
const client = await getClient();
|
|
107
104
|
if (!client) return null;
|
|
108
105
|
try {
|
|
109
106
|
const value = await client.get(key);
|
|
@@ -116,7 +113,7 @@ async function cacheGet(key) {
|
|
|
116
113
|
}
|
|
117
114
|
async function cacheSet(key, value, ttlSeconds) {
|
|
118
115
|
if (isCircuitOpen()) return;
|
|
119
|
-
const client = getClient();
|
|
116
|
+
const client = await getClient();
|
|
120
117
|
if (!client) return;
|
|
121
118
|
try {
|
|
122
119
|
await client.setex(key, ttlSeconds, value);
|
|
@@ -132,163 +129,6 @@ function isRedisConnected() {
|
|
|
132
129
|
return state.client?.status === "ready";
|
|
133
130
|
}
|
|
134
131
|
|
|
135
|
-
// src/cache/sdk-cache.ts
|
|
136
|
-
import crypto from "crypto";
|
|
137
|
-
var CACHE_ENABLED = process.env.REDIS_CACHE_ENABLED !== "false";
|
|
138
|
-
var debugEnabled2 = isDev || isStaging;
|
|
139
|
-
var g2 = globalThis;
|
|
140
|
-
if (!g2.__sdkCacheState) {
|
|
141
|
-
g2.__sdkCacheState = { statsMap: /* @__PURE__ */ new Map(), recentOps: [] };
|
|
142
|
-
}
|
|
143
|
-
var { statsMap, recentOps } = g2.__sdkCacheState;
|
|
144
|
-
var MAX_RECENT = 50;
|
|
145
|
-
function pushRecent(op) {
|
|
146
|
-
recentOps.push(op);
|
|
147
|
-
if (recentOps.length > MAX_RECENT) recentOps.shift();
|
|
148
|
-
}
|
|
149
|
-
function getOrCreateStats(method) {
|
|
150
|
-
let s = statsMap.get(method);
|
|
151
|
-
if (!s) {
|
|
152
|
-
s = { hits: 0, misses: 0, errors: 0, totalMs: 0 };
|
|
153
|
-
statsMap.set(method, s);
|
|
154
|
-
}
|
|
155
|
-
return s;
|
|
156
|
-
}
|
|
157
|
-
function hashArgs(args) {
|
|
158
|
-
const normalized = JSON.stringify(args, (_, v) => {
|
|
159
|
-
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
160
|
-
return Object.keys(v).sort().reduce(
|
|
161
|
-
(acc, k) => {
|
|
162
|
-
acc[k] = v[k];
|
|
163
|
-
return acc;
|
|
164
|
-
},
|
|
165
|
-
{}
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
return v;
|
|
169
|
-
});
|
|
170
|
-
return crypto.createHash("sha256").update(normalized).digest("hex").slice(0, 8);
|
|
171
|
-
}
|
|
172
|
-
function extractTtlSeconds(args) {
|
|
173
|
-
for (let i = args.length - 1; i >= 0; i--) {
|
|
174
|
-
const arg = args[i];
|
|
175
|
-
if (arg && typeof arg === "object" && "edgeCache" in arg) {
|
|
176
|
-
const val = arg.edgeCache;
|
|
177
|
-
if (typeof val === "number") return val * 3600;
|
|
178
|
-
if (typeof val === "string") {
|
|
179
|
-
if (val.endsWith("s")) return parseInt(val, 10);
|
|
180
|
-
if (val.endsWith("m")) return parseInt(val, 10) * 60;
|
|
181
|
-
const n = Number(val);
|
|
182
|
-
return isNaN(n) ? void 0 : n * 3600;
|
|
183
|
-
}
|
|
184
|
-
return void 0;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return void 0;
|
|
188
|
-
}
|
|
189
|
-
function formatTtl(seconds) {
|
|
190
|
-
if (seconds < 60) return `${seconds}s`;
|
|
191
|
-
if (seconds < 3600) return `${Math.round(seconds / 60)}m`;
|
|
192
|
-
return `${Math.round(seconds / 3600)}h`;
|
|
193
|
-
}
|
|
194
|
-
async function cachedSdkCall(prefix, methodPath, method, ...args) {
|
|
195
|
-
const ttlSeconds = extractTtlSeconds(args);
|
|
196
|
-
if (!CACHE_ENABLED || ttlSeconds === void 0) {
|
|
197
|
-
if (debugEnabled2) {
|
|
198
|
-
console.log(`[SDK Cache] SKIP ${methodPath} (no edgeCache, REALTIME)`);
|
|
199
|
-
}
|
|
200
|
-
pushRecent({ op: "SKIP", key: methodPath, ms: 0, ts: Date.now() });
|
|
201
|
-
return method(...args);
|
|
202
|
-
}
|
|
203
|
-
const argsHash = hashArgs(args);
|
|
204
|
-
const cacheKey = `${prefix}sdk:${methodPath}:${argsHash}`;
|
|
205
|
-
const start = Date.now();
|
|
206
|
-
const stats = getOrCreateStats(methodPath);
|
|
207
|
-
try {
|
|
208
|
-
const cached = await cacheGet(cacheKey);
|
|
209
|
-
if (cached !== null) {
|
|
210
|
-
const parsed = JSON.parse(cached);
|
|
211
|
-
const ms2 = Date.now() - start;
|
|
212
|
-
stats.hits++;
|
|
213
|
-
stats.totalMs += ms2;
|
|
214
|
-
if (debugEnabled2) {
|
|
215
|
-
console.log(`[SDK Cache] HIT ${methodPath}:${argsHash} (${ms2}ms)`);
|
|
216
|
-
}
|
|
217
|
-
pushRecent({
|
|
218
|
-
op: "HIT",
|
|
219
|
-
key: `${methodPath}:${argsHash}`,
|
|
220
|
-
ms: ms2,
|
|
221
|
-
ts: Date.now()
|
|
222
|
-
});
|
|
223
|
-
return parsed;
|
|
224
|
-
}
|
|
225
|
-
} catch {
|
|
226
|
-
stats.errors++;
|
|
227
|
-
}
|
|
228
|
-
const result = await method(...args);
|
|
229
|
-
const ms = Date.now() - start;
|
|
230
|
-
stats.misses++;
|
|
231
|
-
stats.totalMs += ms;
|
|
232
|
-
if (debugEnabled2) {
|
|
233
|
-
console.log(
|
|
234
|
-
`[SDK Cache] MISS ${methodPath}:${argsHash} (${ms}ms -> cached, TTL ${formatTtl(ttlSeconds)})`
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
|
-
pushRecent({
|
|
238
|
-
op: "MISS",
|
|
239
|
-
key: `${methodPath}:${argsHash}`,
|
|
240
|
-
ms,
|
|
241
|
-
ts: Date.now()
|
|
242
|
-
});
|
|
243
|
-
try {
|
|
244
|
-
const serialized = JSON.stringify(result);
|
|
245
|
-
cacheSet(cacheKey, serialized, ttlSeconds).catch(() => {
|
|
246
|
-
});
|
|
247
|
-
} catch {
|
|
248
|
-
}
|
|
249
|
-
return result;
|
|
250
|
-
}
|
|
251
|
-
function formatAgo(ms) {
|
|
252
|
-
if (ms < 1e3) return "<1s";
|
|
253
|
-
const s = Math.floor(ms / 1e3);
|
|
254
|
-
if (s < 60) return `${s}s ago`;
|
|
255
|
-
const m = Math.floor(s / 60);
|
|
256
|
-
return `${m}m ago`;
|
|
257
|
-
}
|
|
258
|
-
function getCacheStats() {
|
|
259
|
-
let totalHits = 0;
|
|
260
|
-
let totalMisses = 0;
|
|
261
|
-
let totalErrors = 0;
|
|
262
|
-
const topKeys = [];
|
|
263
|
-
statsMap.forEach((s, key) => {
|
|
264
|
-
totalHits += s.hits;
|
|
265
|
-
totalMisses += s.misses;
|
|
266
|
-
totalErrors += s.errors;
|
|
267
|
-
const total2 = s.hits + s.misses;
|
|
268
|
-
topKeys.push({
|
|
269
|
-
key,
|
|
270
|
-
hits: s.hits,
|
|
271
|
-
misses: s.misses,
|
|
272
|
-
avgMs: total2 > 0 ? Math.round(s.totalMs / total2) : 0
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
topKeys.sort((a, b) => b.hits - a.hits);
|
|
276
|
-
const total = totalHits + totalMisses;
|
|
277
|
-
const hitRate = total > 0 ? (totalHits / total * 100).toFixed(1) + "%" : "0%";
|
|
278
|
-
const now = Date.now();
|
|
279
|
-
const recent = recentOps.slice(-20).reverse().map((op) => ({
|
|
280
|
-
op: op.op,
|
|
281
|
-
key: op.key,
|
|
282
|
-
ms: op.ms,
|
|
283
|
-
ago: formatAgo(now - op.ts)
|
|
284
|
-
}));
|
|
285
|
-
return {
|
|
286
|
-
stats: { hits: totalHits, misses: totalMisses, errors: totalErrors, hitRate },
|
|
287
|
-
topKeys: topKeys.slice(0, 15),
|
|
288
|
-
recentOps: recent
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
|
|
292
132
|
// src/sdk-call.ts
|
|
293
133
|
async function sdkCall(method, ...args) {
|
|
294
134
|
return method(...args);
|
|
@@ -318,12 +158,10 @@ var getServerQueryClient = cache(
|
|
|
318
158
|
export {
|
|
319
159
|
cacheGet,
|
|
320
160
|
cacheSet,
|
|
321
|
-
cachedSdkCall,
|
|
322
161
|
createQueryOptions,
|
|
323
162
|
createServerQueryClient,
|
|
324
163
|
createSuspenseQueryOptions,
|
|
325
164
|
env,
|
|
326
|
-
getCacheStats,
|
|
327
165
|
getCircuitState,
|
|
328
166
|
getServerQueryClient,
|
|
329
167
|
isDev,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/environment.ts","../src/cache/redis-client.ts","../src/cache/sdk-cache.ts","../src/sdk-call.ts","../src/server-query-client.ts"],"sourcesContent":["/**\n * Environment detection that works across server, client, and containers.\n *\n * Priority:\n * 1. DEPLOYMENT_ENV (\"dev\" → staging, \"live\" → production)\n * 2. Server-side NODE_ENV fallback\n * 3. Client-side hostname detection\n */\nfunction getEnvironment(): \"development\" | \"staging\" | \"production\" {\n const deploymentEnv = process.env.DEPLOYMENT_ENV;\n if (deploymentEnv === \"dev\") return \"staging\";\n if (deploymentEnv === \"live\") return \"production\";\n\n if (typeof window === \"undefined\") {\n if (process.env.NODE_ENV === \"development\") return \"development\";\n return \"production\";\n }\n\n const host = window.location.hostname;\n if (host.includes(\"localhost\") || host.includes(\"127.0.0.1\"))\n return \"development\";\n if (host.includes(\"agr-hosting.dev\")) return \"staging\";\n return \"production\";\n}\n\nexport const env = getEnvironment();\nexport const isDev = env === \"development\";\nexport const isStaging = env === \"staging\";\nexport const isProduction = env === \"production\";\n","import type Redis from \"ioredis\";\nimport { isDev, isStaging } from \"../environment\";\n\nconst CIRCUIT_BREAKER_THRESHOLD = 5;\nconst CIRCUIT_BREAKER_RESET_MS = 60_000;\n\ninterface RedisGlobalState {\n client: Redis | null;\n consecutiveFailures: number;\n circuitOpenUntil: number;\n}\n\nconst g = globalThis as unknown as { __redisState?: RedisGlobalState };\nif (!g.__redisState) {\n g.__redisState = {\n client: null,\n consecutiveFailures: 0,\n circuitOpenUntil: 0,\n };\n}\nconst state = g.__redisState;\n\nconst debugEnabled = isDev || isStaging;\n\nfunction log(...args: unknown[]) {\n if (debugEnabled) {\n console.log(\"[Redis]\", ...args);\n }\n}\n\nfunction isCircuitOpen(): boolean {\n if (state.circuitOpenUntil === 0) return false;\n if (Date.now() >= state.circuitOpenUntil) {\n state.circuitOpenUntil = 0;\n state.consecutiveFailures = 0;\n log(\"Circuit breaker reset -- retrying Redis\");\n return false;\n }\n return true;\n}\n\nfunction recordFailure() {\n state.consecutiveFailures++;\n if (state.consecutiveFailures >= CIRCUIT_BREAKER_THRESHOLD) {\n state.circuitOpenUntil = Date.now() + CIRCUIT_BREAKER_RESET_MS;\n log(\n `Circuit breaker OPEN -- skipping Redis for ${CIRCUIT_BREAKER_RESET_MS / 1000}s`,\n );\n }\n}\n\nfunction recordSuccess() {\n state.consecutiveFailures = 0;\n}\n\nfunction getRedisUrl(): string | undefined {\n const containerRedis = process.env.REDIS_SERVERS;\n if (containerRedis) return `redis://${containerRedis}`;\n\n if (isDev) return process.env.REDIS_URL_DEV;\n if (isStaging) return process.env.REDIS_URL_STAGING;\n return process.env.REDIS_URL_PROD;\n}\n\nfunction getClient(): Redis | null {\n if (state.client) return state.client;\n\n const url = getRedisUrl();\n if (!url) {\n log(\"No REDIS_URL -- cache disabled\");\n return null;\n }\n\n try {\n // Dynamic import at module level isn't possible, so we require ioredis\n // at runtime. It's an optional peer dep -- if not installed, cache is\n // silently disabled.\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n const IORedis = require(\"ioredis\") as typeof import(\"ioredis\").default;\n\n state.client = new IORedis(url, {\n maxRetriesPerRequest: 1,\n connectTimeout: 3000,\n lazyConnect: true,\n enableOfflineQueue: false,\n });\n\n state.client.on(\"error\", (err: Error) => {\n log(\"Connection error:\", err.message);\n recordFailure();\n });\n\n state.client.on(\"connect\", () => log(\"Connected\"));\n\n state.client.connect().catch(() => {\n /* handled by error event */\n });\n\n return state.client;\n } catch {\n log(\"Failed to create client (ioredis not installed?)\");\n return null;\n }\n}\n\nexport async function cacheGet(key: string): Promise<string | null> {\n if (isCircuitOpen()) return null;\n\n const client = getClient();\n if (!client) return null;\n\n try {\n const value = await client.get(key);\n recordSuccess();\n return value;\n } catch {\n recordFailure();\n return null;\n }\n}\n\nexport async function cacheSet(\n key: string,\n value: string,\n ttlSeconds: number,\n): Promise<void> {\n if (isCircuitOpen()) return;\n\n const client = getClient();\n if (!client) return;\n\n try {\n await client.setex(key, ttlSeconds, value);\n recordSuccess();\n } catch {\n recordFailure();\n }\n}\n\nexport function getCircuitState(): \"closed\" | \"open\" {\n return isCircuitOpen() ? \"open\" : \"closed\";\n}\n\nexport function isRedisConnected(): boolean {\n return state.client?.status === \"ready\";\n}\n","import crypto from \"crypto\";\nimport { cacheGet, cacheSet } from \"./redis-client\";\nimport { isDev, isStaging } from \"../environment\";\n\nconst CACHE_ENABLED = process.env.REDIS_CACHE_ENABLED !== \"false\";\nconst debugEnabled = isDev || isStaging;\n\ninterface MethodStats {\n hits: number;\n misses: number;\n errors: number;\n totalMs: number;\n}\n\ninterface RecentOp {\n op: \"HIT\" | \"MISS\" | \"SKIP\" | \"ERROR\";\n key: string;\n ms: number;\n ts: number;\n}\n\ninterface CacheGlobalState {\n statsMap: Map<string, MethodStats>;\n recentOps: RecentOp[];\n}\n\nconst g = globalThis as unknown as { __sdkCacheState?: CacheGlobalState };\nif (!g.__sdkCacheState) {\n g.__sdkCacheState = { statsMap: new Map(), recentOps: [] };\n}\nconst { statsMap, recentOps } = g.__sdkCacheState;\n\nconst MAX_RECENT = 50;\n\nfunction pushRecent(op: RecentOp) {\n recentOps.push(op);\n if (recentOps.length > MAX_RECENT) recentOps.shift();\n}\n\nfunction getOrCreateStats(method: string): MethodStats {\n let s = statsMap.get(method);\n if (!s) {\n s = { hits: 0, misses: 0, errors: 0, totalMs: 0 };\n statsMap.set(method, s);\n }\n return s;\n}\n\nfunction hashArgs(args: unknown[]): string {\n const normalized = JSON.stringify(args, (_, v) => {\n if (v && typeof v === \"object\" && !Array.isArray(v)) {\n return Object.keys(v)\n .sort()\n .reduce(\n (acc, k) => {\n acc[k] = v[k];\n return acc;\n },\n {} as Record<string, unknown>,\n );\n }\n return v;\n });\n return crypto\n .createHash(\"sha256\")\n .update(normalized)\n .digest(\"hex\")\n .slice(0, 8);\n}\n\n/**\n * Extracts the `edgeCache` value from the arguments and converts it to\n * a TTL in seconds. Supports both the legacy numeric-hours format and the\n * augur-api >= 0.9.6 sub-hour string formats ('30s', '1m', '5m').\n *\n * - Number values (1, 2, 3, 4, 5, 8): treated as hours\n * - Numeric strings ('1', '2', etc.): treated as hours\n * - Duration strings ('30s', '1m', '5m'): parsed as seconds/minutes\n */\nfunction extractTtlSeconds(args: unknown[]): number | undefined {\n for (let i = args.length - 1; i >= 0; i--) {\n const arg = args[i];\n if (arg && typeof arg === \"object\" && \"edgeCache\" in arg) {\n const val = (arg as { edgeCache: unknown }).edgeCache;\n if (typeof val === \"number\") return val * 3600;\n if (typeof val === \"string\") {\n if (val.endsWith(\"s\")) return parseInt(val, 10);\n if (val.endsWith(\"m\")) return parseInt(val, 10) * 60;\n const n = Number(val);\n return isNaN(n) ? undefined : n * 3600;\n }\n return undefined;\n }\n }\n return undefined;\n}\n\nfunction formatTtl(seconds: number): string {\n if (seconds < 60) return `${seconds}s`;\n if (seconds < 3600) return `${Math.round(seconds / 60)}m`;\n return `${Math.round(seconds / 3600)}h`;\n}\n\n/**\n * Wraps an SDK call with Redis caching.\n *\n * @param prefix - Redis key prefix (e.g. \"mysite:\")\n * @param methodPath - Dot-separated SDK method path (used as cache key)\n * @param method - The SDK method to call\n * @param args - Arguments to pass to the SDK method. If any argument\n * contains an `edgeCache` property, the result is cached in Redis for\n * that duration. Without `edgeCache`, the call bypasses cache.\n *\n * Accepted `edgeCache` values (matching augur-api CacheParams):\n * - Numbers: 1–8 (hours)\n * - Sub-hour strings: '30s', '1m', '5m'\n *\n * @example\n * ```ts\n * const result = await cachedSdkCall(\n * \"mysite:\",\n * \"items.categories.get\",\n * augurServices.items.categories.get,\n * itemCategoryUid,\n * { edgeCache: 1 },\n * );\n * ```\n */\n// Strict overload (preferred when types match)\nexport async function cachedSdkCall<TArgs extends unknown[], TResult>(\n prefix: string,\n methodPath: string,\n method: (...args: TArgs) => Promise<TResult>,\n ...args: TArgs\n): Promise<TResult>;\n/* eslint-disable @typescript-eslint/no-explicit-any */\n// Relaxed overload for SDK methods whose types don't include edgeCache params\nexport async function cachedSdkCall(\n prefix: string,\n methodPath: string,\n method: (...args: any[]) => Promise<any>,\n ...args: any[]\n): Promise<any>;\n/* eslint-enable @typescript-eslint/no-explicit-any */\n// Implementation\nexport async function cachedSdkCall<TArgs extends unknown[], TResult>(\n prefix: string,\n methodPath: string,\n method: (...args: TArgs) => Promise<TResult>,\n ...args: TArgs\n): Promise<TResult> {\n const ttlSeconds = extractTtlSeconds(args);\n\n if (!CACHE_ENABLED || ttlSeconds === undefined) {\n if (debugEnabled) {\n console.log(`[SDK Cache] SKIP ${methodPath} (no edgeCache, REALTIME)`);\n }\n pushRecent({ op: \"SKIP\", key: methodPath, ms: 0, ts: Date.now() });\n return method(...args);\n }\n\n const argsHash = hashArgs(args);\n const cacheKey = `${prefix}sdk:${methodPath}:${argsHash}`;\n const start = Date.now();\n const stats = getOrCreateStats(methodPath);\n\n try {\n const cached = await cacheGet(cacheKey);\n if (cached !== null) {\n const parsed = JSON.parse(cached);\n const ms = Date.now() - start;\n stats.hits++;\n stats.totalMs += ms;\n if (debugEnabled) {\n console.log(`[SDK Cache] HIT ${methodPath}:${argsHash} (${ms}ms)`);\n }\n pushRecent({\n op: \"HIT\",\n key: `${methodPath}:${argsHash}`,\n ms,\n ts: Date.now(),\n });\n return parsed as TResult;\n }\n } catch {\n stats.errors++;\n }\n\n const result = await method(...args);\n const ms = Date.now() - start;\n stats.misses++;\n stats.totalMs += ms;\n\n if (debugEnabled) {\n console.log(\n `[SDK Cache] MISS ${methodPath}:${argsHash} (${ms}ms -> cached, TTL ${formatTtl(ttlSeconds)})`,\n );\n }\n pushRecent({\n op: \"MISS\",\n key: `${methodPath}:${argsHash}`,\n ms,\n ts: Date.now(),\n });\n\n try {\n const serialized = JSON.stringify(result);\n cacheSet(cacheKey, serialized, ttlSeconds).catch(() => {\n /* swallow */\n });\n } catch {\n // Non-serializable result -- skip caching\n }\n\n return result;\n}\n\nfunction formatAgo(ms: number): string {\n if (ms < 1000) return \"<1s\";\n const s = Math.floor(ms / 1000);\n if (s < 60) return `${s}s ago`;\n const m = Math.floor(s / 60);\n return `${m}m ago`;\n}\n\n/** Returns aggregated cache stats for monitoring endpoints. */\nexport function getCacheStats() {\n let totalHits = 0;\n let totalMisses = 0;\n let totalErrors = 0;\n\n const topKeys: {\n key: string;\n hits: number;\n misses: number;\n avgMs: number;\n }[] = [];\n\n statsMap.forEach((s, key) => {\n totalHits += s.hits;\n totalMisses += s.misses;\n totalErrors += s.errors;\n const total = s.hits + s.misses;\n topKeys.push({\n key,\n hits: s.hits,\n misses: s.misses,\n avgMs: total > 0 ? Math.round(s.totalMs / total) : 0,\n });\n });\n\n topKeys.sort((a, b) => b.hits - a.hits);\n\n const total = totalHits + totalMisses;\n const hitRate =\n total > 0 ? ((totalHits / total) * 100).toFixed(1) + \"%\" : \"0%\";\n\n const now = Date.now();\n const recent = recentOps\n .slice(-20)\n .reverse()\n .map((op) => ({\n op: op.op,\n key: op.key,\n ms: op.ms,\n ago: formatAgo(now - op.ts),\n }));\n\n return {\n stats: { hits: totalHits, misses: totalMisses, errors: totalErrors, hitRate },\n topKeys: topKeys.slice(0, 15),\n recentOps: recent,\n };\n}\n","/**\n * Calls an Augur SDK method, forwarding all arguments with full type safety.\n *\n * With augur-api >= 0.9.6, SDK methods properly declare their `CacheParams`\n * option (including `edgeCache`), so this wrapper preserves both parameter\n * and return-type inference end-to-end.\n *\n * @example\n * ```ts\n * const result = await sdkCall(\n * augurServices.items.invMast.get,\n * invMastUid,\n * { edgeCache: 4 },\n * );\n * ```\n */\nexport async function sdkCall<TArgs extends unknown[], TResult>(\n method: (...args: TArgs) => Promise<TResult>,\n ...args: TArgs\n): Promise<TResult> {\n return method(...args);\n}\n","import { cache } from \"react\";\nimport { QueryClient } from \"@tanstack/react-query\";\nimport { CACHE_CONFIG } from \"@simpleapps-com/augur-utils\";\n\n/**\n * Creates a server-side query client optimised for prefetching.\n *\n * - No persistence (server-only)\n * - Longer cache times for prefetched data\n * - No retries (fail fast on server)\n * - No refetching (server renders are one-shot)\n */\nexport function createServerQueryClient(): QueryClient {\n return new QueryClient({\n defaultOptions: {\n queries: {\n staleTime: CACHE_CONFIG.STATIC.staleTime,\n gcTime: CACHE_CONFIG.STATIC.staleTime,\n retry: 0,\n refetchOnMount: false,\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n },\n },\n });\n}\n\n/**\n * Returns a per-request singleton QueryClient for React Server Components.\n *\n * React's `cache()` deduplicates calls within a single server request,\n * so each request gets its own QueryClient while avoiding the cross-request\n * state leakage that a module-level singleton would cause.\n */\nexport const getServerQueryClient = cache(\n (): QueryClient => createServerQueryClient(),\n);\n"],"mappings":";;;;;;;;;AAQA,SAAS,iBAA2D;AAClE,QAAM,gBAAgB,QAAQ,IAAI;AAClC,MAAI,kBAAkB,MAAO,QAAO;AACpC,MAAI,kBAAkB,OAAQ,QAAO;AAErC,MAAI,OAAO,WAAW,aAAa;AACjC,QAAI,QAAQ,IAAI,aAAa,cAAe,QAAO;AACnD,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,OAAO,SAAS;AAC7B,MAAI,KAAK,SAAS,WAAW,KAAK,KAAK,SAAS,WAAW;AACzD,WAAO;AACT,MAAI,KAAK,SAAS,iBAAiB,EAAG,QAAO;AAC7C,SAAO;AACT;AAEO,IAAM,MAAM,eAAe;AAC3B,IAAM,QAAQ,QAAQ;AACtB,IAAM,YAAY,QAAQ;AAC1B,IAAM,eAAe,QAAQ;;;ACzBpC,IAAM,4BAA4B;AAClC,IAAM,2BAA2B;AAQjC,IAAM,IAAI;AACV,IAAI,CAAC,EAAE,cAAc;AACnB,IAAE,eAAe;AAAA,IACf,QAAQ;AAAA,IACR,qBAAqB;AAAA,IACrB,kBAAkB;AAAA,EACpB;AACF;AACA,IAAM,QAAQ,EAAE;AAEhB,IAAM,eAAe,SAAS;AAE9B,S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EAAE;AAAA,EAC5B,EAAE;AAEJ,SAAO;AAAA,IACL,OAAO,EAAE,MAAM,WAAW,QAAQ,aAAa,QAAQ,aAAa,QAAQ;AAAA,IAC5E,SAAS,QAAQ,MAAM,GAAG,EAAE;AAAA,IAC5B,WAAW;AAAA,EACb;AACF;;;ACjQA,eAAsB,QACpB,WACG,MACe;AAClB,SAAO,OAAO,GAAG,IAAI;AACvB;;;ACrBA,SAAS,aAAa;AACtB,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAUtB,SAAS,0BAAuC;AACrD,SAAO,IAAI,YAAY;AAAA,IACrB,gBAAgB;AAAA,MACd,SAAS;AAAA,QACP,WAAW,aAAa,OAAO;AAAA,QAC/B,QAAQ,aAAa,OAAO;AAAA,QAC5B,OAAO;AAAA,QACP,gBAAgB;AAAA,QAChB,sBAAsB;AAAA,QACtB,oBAAoB;AAAA,MACtB;AAAA,IACF;AAAA,EACF,CAAC;AACH;AASO,IAAM,uBAAuB;AAAA,EAClC,MAAmB,wBAAwB;AAC7C;","names":["debugEnabled","g","ms","total"]}
|
|
1
|
+
{"version":3,"sources":["../src/environment.ts","../src/cache/redis-client.ts","../src/sdk-call.ts","../src/server-query-client.ts"],"sourcesContent":["/**\n * Environment detection that works across server, client, and containers.\n *\n * Priority:\n * 1. DEPLOYMENT_ENV (\"dev\" → staging, \"live\" → production)\n * 2. Server-side NODE_ENV fallback\n * 3. Client-side hostname detection\n */\nfunction getEnvironment(): \"development\" | \"staging\" | \"production\" {\n const deploymentEnv = process.env.DEPLOYMENT_ENV;\n if (deploymentEnv === \"dev\") return \"staging\";\n if (deploymentEnv === \"live\") return \"production\";\n\n if (typeof window === \"undefined\") {\n if (process.env.NODE_ENV === \"development\") return \"development\";\n return \"production\";\n }\n\n const host = window.location.hostname;\n if (host.includes(\"localhost\") || host.includes(\"127.0.0.1\"))\n return \"development\";\n if (host.includes(\"agr-hosting.dev\")) return \"staging\";\n return \"production\";\n}\n\nexport const env = getEnvironment();\nexport const isDev = env === \"development\";\nexport const isStaging = env === \"staging\";\nexport const isProduction = env === \"production\";\n","import type Redis from \"ioredis\";\nimport { isDev, isStaging } from \"../environment\";\n\nconst CIRCUIT_BREAKER_THRESHOLD = 5;\nconst CIRCUIT_BREAKER_RESET_MS = 60_000;\n\ninterface RedisGlobalState {\n client: Redis | null;\n consecutiveFailures: number;\n circuitOpenUntil: number;\n}\n\nconst g = globalThis as unknown as { __redisState?: RedisGlobalState };\nif (!g.__redisState) {\n g.__redisState = {\n client: null,\n consecutiveFailures: 0,\n circuitOpenUntil: 0,\n };\n}\nconst state = g.__redisState;\n\nconst debugEnabled = isDev || isStaging;\n\nfunction log(...args: unknown[]) {\n if (debugEnabled) {\n console.log(\"[Redis]\", ...args);\n }\n}\n\nfunction isCircuitOpen(): boolean {\n if (state.circuitOpenUntil === 0) return false;\n if (Date.now() >= state.circuitOpenUntil) {\n state.circuitOpenUntil = 0;\n state.consecutiveFailures = 0;\n log(\"Circuit breaker reset -- retrying Redis\");\n return false;\n }\n return true;\n}\n\nfunction recordFailure() {\n state.consecutiveFailures++;\n if (state.consecutiveFailures >= CIRCUIT_BREAKER_THRESHOLD) {\n state.circuitOpenUntil = Date.now() + CIRCUIT_BREAKER_RESET_MS;\n log(\n `Circuit breaker OPEN -- skipping Redis for ${CIRCUIT_BREAKER_RESET_MS / 1000}s`,\n );\n }\n}\n\nfunction recordSuccess() {\n state.consecutiveFailures = 0;\n}\n\nfunction getRedisUrl(): string | undefined {\n const containerRedis = process.env.REDIS_SERVERS;\n if (containerRedis) return `redis://${containerRedis}`;\n\n if (isDev) return process.env.REDIS_URL_DEV;\n if (isStaging) return process.env.REDIS_URL_STAGING;\n return process.env.REDIS_URL_PROD;\n}\n\nasync function getClient(): Promise<Redis | null> {\n if (state.client) return state.client;\n\n const url = getRedisUrl();\n if (!url) {\n log(\"No REDIS_URL -- cache disabled\");\n return null;\n }\n\n try {\n // Dynamic import so the built ESM output doesn't use esbuild's __require\n // shim, which breaks in pure ESM environments (Next.js 16 + Turbopack).\n // ioredis is CJS-only; Node wraps its module.exports as { default }.\n const { default: IORedis } = await import(\"ioredis\");\n\n state.client = new IORedis(url, {\n maxRetriesPerRequest: 1,\n connectTimeout: 3000,\n lazyConnect: true,\n enableOfflineQueue: false,\n });\n\n state.client.on(\"error\", (err: Error) => {\n log(\"Connection error:\", err.message);\n recordFailure();\n });\n\n state.client.on(\"connect\", () => log(\"Connected\"));\n\n state.client.connect().catch(() => {\n /* handled by error event */\n });\n\n return state.client;\n } catch {\n log(\"Failed to create client (ioredis not installed?)\");\n return null;\n }\n}\n\nexport async function cacheGet(key: string): Promise<string | null> {\n if (isCircuitOpen()) return null;\n\n const client = await getClient();\n if (!client) return null;\n\n try {\n const value = await client.get(key);\n recordSuccess();\n return value;\n } catch {\n recordFailure();\n return null;\n }\n}\n\nexport async function cacheSet(\n key: string,\n value: string,\n ttlSeconds: number,\n): Promise<void> {\n if (isCircuitOpen()) return;\n\n const client = await getClient();\n if (!client) return;\n\n try {\n await client.setex(key, ttlSeconds, value);\n recordSuccess();\n } catch {\n recordFailure();\n }\n}\n\nexport function getCircuitState(): \"closed\" | \"open\" {\n return isCircuitOpen() ? \"open\" : \"closed\";\n}\n\nexport function isRedisConnected(): boolean {\n return state.client?.status === \"ready\";\n}\n","/**\n * Calls an Augur SDK method, forwarding all arguments with full type safety.\n *\n * With augur-api >= 0.9.6, SDK methods properly declare their `CacheParams`\n * option (including `edgeCache`), so this wrapper preserves both parameter\n * and return-type inference end-to-end.\n *\n * @example\n * ```ts\n * const result = await sdkCall(\n * augurServices.items.invMast.get,\n * invMastUid,\n * { edgeCache: 4 },\n * );\n * ```\n */\nexport async function sdkCall<TArgs extends unknown[], TResult>(\n method: (...args: TArgs) => Promise<TResult>,\n ...args: TArgs\n): Promise<TResult> {\n return method(...args);\n}\n","import { cache } from \"react\";\nimport { QueryClient } from \"@tanstack/react-query\";\nimport { CACHE_CONFIG } from \"@simpleapps-com/augur-utils\";\n\n/**\n * Creates a server-side query client optimised for prefetching.\n *\n * - No persistence (server-only)\n * - Longer cache times for prefetched data\n * - No retries (fail fast on server)\n * - No refetching (server renders are one-shot)\n */\nexport function createServerQueryClient(): QueryClient {\n return new QueryClient({\n defaultOptions: {\n queries: {\n staleTime: CACHE_CONFIG.STATIC.staleTime,\n gcTime: CACHE_CONFIG.STATIC.staleTime,\n retry: 0,\n refetchOnMount: false,\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n },\n },\n });\n}\n\n/**\n * Returns a per-request singleton QueryClient for React Server Components.\n *\n * React's `cache()` deduplicates calls within a single server request,\n * so each request gets its own QueryClient while avoiding the cross-request\n * state leakage that a module-level singleton would cause.\n */\nexport const getServerQueryClient = cache(\n (): QueryClient => createServerQueryClient(),\n);\n"],"mappings":";;;;;;AAQA,SAAS,iBAA2D;AAClE,QAAM,gBAAgB,QAAQ,IAAI;AAClC,MAAI,kBAAkB,MAAO,QAAO;AACpC,MAAI,kBAAkB,OAAQ,QAAO;AAErC,MAAI,OAAO,WAAW,aAAa;AACjC,QAAI,QAAQ,IAAI,aAAa,cAAe,QAAO;AACnD,WAAO;AAAA,EACT;AAEA,QAAM,OAAO,OAAO,SAAS;AAC7B,MAAI,KAAK,SAAS,WAAW,KAAK,KAAK,SAAS,WAAW;AACzD,WAAO;AACT,MAAI,KAAK,SAAS,iBAAiB,EAAG,QAAO;AAC7C,SAAO;AACT;AAEO,IAAM,MAAM,eAAe;AAC3B,IAAM,QAAQ,QAAQ;AACtB,IAAM,YAAY,QAAQ;AAC1B,IAAM,eAAe,QAAQ;;;ACzBpC,IAAM,4BAA4B;AAClC,IAAM,2BAA2B;AAQjC,IAAM,IAAI;AACV,IAAI,CAAC,EAAE,cAAc;AACnB,IAAE,eAAe;AAAA,IACf,QAAQ;AAAA,IACR,qBAAqB;AAAA,IACrB,kBAAkB;AAAA,EACpB;AACF;AACA,IAAM,QAAQ,EAAE;AAEhB,IAAM,eAAe,SAAS;AAE9B,SAAS,OAAO,MAAiB;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1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simpleapps-com/augur-server",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Server-side utilities for Augur ecommerce sites (Redis caching, SDK helpers, auth)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"valibot": "^1.0.0",
|
|
36
|
-
"@simpleapps-com/augur-utils": "0.1
|
|
36
|
+
"@simpleapps-com/augur-utils": "0.2.1"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"@simpleapps-com/augur-api": "^0.9.6",
|
package/dist/chunk-DGUM43GV.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
__require
|
|
10
|
-
};
|
|
11
|
-
//# sourceMappingURL=chunk-DGUM43GV.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|