@neowhale/storefront 0.1.0
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/chunk-PR4PUHVN.js +273 -0
- package/dist/chunk-PR4PUHVN.js.map +1 -0
- package/dist/chunk-XMLH3TLA.cjs +275 -0
- package/dist/chunk-XMLH3TLA.cjs.map +1 -0
- package/dist/index.cjs +12 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +269 -0
- package/dist/index.d.ts +269 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/next/index.cjs +123 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +117 -0
- package/dist/next/index.d.ts +117 -0
- package/dist/next/index.js +115 -0
- package/dist/next/index.js.map +1 -0
- package/dist/react/index.cjs +673 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +243 -0
- package/dist/react/index.d.ts +243 -0
- package/dist/react/index.js +657 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +70 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/next/headers.ts","../../src/next/rewrite.ts","../../src/next/server.ts","../../src/next/image-loader.ts","../../src/next/middleware.ts"],"names":["WhaleClient","NextResponse"],"mappings":";;;;;;AAIO,IAAM,eAAA,GAAkB;AAAA,EAC7B;AAAA,IACE,GAAA,EAAK,wBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,2BAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,wBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,iBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,kBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA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EAAE,cAAA,EAAgB,SAAA,EAAW,UAAA,GAAa,iBAAgB,GAAI,OAAA;AAEpE,EAAA,OAAO,SAAS,WAAW,OAAA,EAAsB;AAC/C,IAAA,MAAM,EAAE,QAAA,EAAS,GAAI,OAAA,CAAQ,OAAA;AAG7B,IAAA,MAAM,cAAc,cAAA,CAAe,IAAA;AAAA,MACjC,CAAC,MAAM,QAAA,KAAa,CAAA,IAAK,SAAS,UAAA,CAAW,CAAA,EAAG,CAAC,CAAA,CAAA,CAAG;AAAA,KACtD;AAEA,IAAA,IAAI,CAAC,WAAA,EAAa;AAChB,MAAA,OAAOC,oBAAa,IAAA,EAAK;AAAA,IAC3B;AAGA,IAAA,MAAM,KAAA,GAAQ,OAAA,CAAQ,OAAA,CAAQ,GAAA,CAAI,UAAU,CAAA,EAAG,KAAA;AAE/C,IAAA,IAAI,CAAC,KAAA,EAAO;AACV,MAAA,MAAM,GAAA,GAAM,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAM;AAClC,MAAA,GAAA,CAAI,QAAA,GAAW,SAAA;AACf,MAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,UAAA,EAAY,QAAQ,CAAA;AACzC,MAAA,OAAOA,mBAAA,CAAa,SAAS,GAAG,CAAA;AAAA,IAClC;AAEA,IAAA,OAAOA,oBAAa,IAAA,EAAK;AAAA,EAC3B,CAAA;AACF","file":"index.cjs","sourcesContent":["/**\n * Security headers for Next.js storefronts.\n */\n\nexport const securityHeaders = [\n {\n key: 'X-DNS-Prefetch-Control',\n value: 'on',\n },\n {\n key: 'Strict-Transport-Security',\n value: 'max-age=63072000; includeSubDomains; preload',\n },\n {\n key: 'X-Content-Type-Options',\n value: 'nosniff',\n },\n {\n key: 'X-Frame-Options',\n value: 'SAMEORIGIN',\n },\n {\n key: 'X-XSS-Protection',\n value: '1; mode=block',\n },\n {\n key: 'Referrer-Policy',\n value: 'strict-origin-when-cross-origin',\n },\n {\n key: 'Permissions-Policy',\n value: 'camera=(), microphone=(), geolocation=(self), interest-cohort=()',\n },\n]\n\n/**\n * Returns a Next.js `headers()` config with security headers applied to all routes.\n * Use in next.config.ts:\n *\n * ```ts\n * import { withSecurityHeaders } from '@neowhale/storefront/next'\n * export default { headers: withSecurityHeaders() }\n * ```\n */\nexport function withSecurityHeaders(\n extra?: { key: string; value: string }[]\n): () => Promise<{ source: string; headers: { key: string; value: string }[] }[]> {\n const allHeaders = extra ? [...securityHeaders, ...extra] : securityHeaders\n return async () => [\n {\n source: '/:path*',\n headers: allHeaders,\n },\n ]\n}\n","/**\n * Gateway rewrite rule for Next.js.\n * Proxies client-side /api/gw/* requests to whale-gateway to avoid CORS.\n *\n * Usage in next.config.ts:\n * ```ts\n * import { whaleGatewayRewrite } from '@neowhale/storefront/next'\n * export default {\n * async rewrites() {\n * return [whaleGatewayRewrite()]\n * }\n * }\n * ```\n */\nexport function whaleGatewayRewrite(\n gatewayUrl = 'https://whale-gateway.fly.dev',\n proxyPath = '/api/gw'\n): { source: string; destination: string } {\n return {\n source: `${proxyPath}/:path*`,\n destination: `${gatewayUrl}/:path*`,\n }\n}\n","import { WhaleClient } from '../client.js'\nimport type { Product, WhaleStorefrontConfig } from '../types.js'\n\n/**\n * Creates a server-side WhaleClient.\n * Reads from env vars by default — override with explicit config.\n */\nexport function createServerClient(config?: Partial<WhaleStorefrontConfig>): WhaleClient {\n return new WhaleClient({\n storeId: config?.storeId || process.env.NEXT_PUBLIC_STORE_ID || '',\n apiKey: config?.apiKey || process.env.NEXT_PUBLIC_API_KEY || '',\n gatewayUrl: config?.gatewayUrl || process.env.NEXT_PUBLIC_API_URL || 'https://whale-gateway.fly.dev',\n proxyPath: config?.proxyPath,\n })\n}\n\n/**\n * Server-side: fetch all published products with ISR caching.\n * Drop-in replacement for Flora's `getAllProducts()`.\n */\nexport async function getAllProducts(options?: {\n /** Revalidate interval in seconds. Defaults to 60. */\n revalidate?: number\n /** Filter function to exclude products (e.g. hidden categories, out of stock) */\n filter?: (product: Product) => boolean\n /** Override client config */\n client?: WhaleClient\n}): Promise<Product[]> {\n const client = options?.client ?? createServerClient()\n return client.getAllProducts({\n status: 'published',\n revalidate: options?.revalidate ?? 60,\n filter: options?.filter,\n })\n}\n","import { WhaleClient } from '../client.js'\n\nconst ALLOWED_WIDTHS = [64, 96, 128, 256, 384, 640, 828, 1080, 1280, 1920]\n\nfunction snapWidth(w: number): number {\n for (const aw of ALLOWED_WIDTHS) {\n if (aw >= w) return aw\n }\n return ALLOWED_WIDTHS[ALLOWED_WIDTHS.length - 1]\n}\n\ninterface ImageLoaderParams {\n src: string\n width: number\n quality?: number\n}\n\n/**\n * Creates a Next.js custom image loader that proxies Supabase images through gateway.\n *\n * Usage in a loader file (e.g. src/lib/image-loader.ts):\n * ```ts\n * import { createImageLoader } from '@neowhale/storefront/next'\n * export default createImageLoader({\n * storeId: process.env.NEXT_PUBLIC_STORE_ID!,\n * gatewayUrl: 'https://whale-gateway.fly.dev',\n * supabaseHost: 'your-project.supabase.co',\n * signingSecret: process.env.NEXT_PUBLIC_MEDIA_SIGNING_SECRET!,\n * })\n * ```\n */\nexport function createImageLoader(config: {\n storeId: string\n gatewayUrl: string\n supabaseHost: string\n signingSecret: string\n}): (params: ImageLoaderParams) => string {\n return ({ src, width, quality }: ImageLoaderParams): string => {\n if (!src.includes(config.supabaseHost)) {\n return src\n }\n\n const w = String(snapWidth(width))\n const q = String(quality || 80)\n const f = 'webp'\n const encoded = WhaleClient.encodeBase64Url(src)\n const s = WhaleClient.signMedia(config.signingSecret, encoded, w, q, f)\n\n return `${config.gatewayUrl}/v1/stores/${config.storeId}/media?url=${encoded}&w=${w}&q=${q}&f=${f}&s=${s}`\n }\n}\n","import type { NextRequest } from 'next/server'\nimport { NextResponse } from 'next/server'\n\n/**\n * Creates a Next.js middleware that protects routes requiring authentication.\n *\n * Checks for a session token cookie or localStorage indicator.\n * Since middleware runs on the edge and can't access localStorage,\n * this checks for a cookie-based token instead.\n *\n * Usage in middleware.ts:\n * ```ts\n * import { createAuthMiddleware } from '@neowhale/storefront/next'\n * export const middleware = createAuthMiddleware({\n * protectedPaths: ['/account'],\n * loginPath: '/account',\n * })\n * export const config = { matcher: ['/account/:path*'] }\n * ```\n */\nexport function createAuthMiddleware(options: {\n protectedPaths: string[]\n loginPath: string\n cookieName?: string\n}) {\n const { protectedPaths, loginPath, cookieName = 'whale-session' } = options\n\n return function middleware(request: NextRequest) {\n const { pathname } = request.nextUrl\n\n // Check if this is a protected path\n const isProtected = protectedPaths.some(\n (p) => pathname === p || pathname.startsWith(`${p}/`)\n )\n\n if (!isProtected) {\n return NextResponse.next()\n }\n\n // Check for session cookie\n const token = request.cookies.get(cookieName)?.value\n\n if (!token) {\n const url = request.nextUrl.clone()\n url.pathname = loginPath\n url.searchParams.set('redirect', pathname)\n return NextResponse.redirect(url)\n }\n\n return NextResponse.next()\n }\n}\n"]}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { WhaleStorefrontConfig, WhaleClient, Product } from '../index.cjs';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Security headers for Next.js storefronts.
|
|
6
|
+
*/
|
|
7
|
+
declare const securityHeaders: {
|
|
8
|
+
key: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}[];
|
|
11
|
+
/**
|
|
12
|
+
* Returns a Next.js `headers()` config with security headers applied to all routes.
|
|
13
|
+
* Use in next.config.ts:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { withSecurityHeaders } from '@neowhale/storefront/next'
|
|
17
|
+
* export default { headers: withSecurityHeaders() }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare function withSecurityHeaders(extra?: {
|
|
21
|
+
key: string;
|
|
22
|
+
value: string;
|
|
23
|
+
}[]): () => Promise<{
|
|
24
|
+
source: string;
|
|
25
|
+
headers: {
|
|
26
|
+
key: string;
|
|
27
|
+
value: string;
|
|
28
|
+
}[];
|
|
29
|
+
}[]>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Gateway rewrite rule for Next.js.
|
|
33
|
+
* Proxies client-side /api/gw/* requests to whale-gateway to avoid CORS.
|
|
34
|
+
*
|
|
35
|
+
* Usage in next.config.ts:
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { whaleGatewayRewrite } from '@neowhale/storefront/next'
|
|
38
|
+
* export default {
|
|
39
|
+
* async rewrites() {
|
|
40
|
+
* return [whaleGatewayRewrite()]
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function whaleGatewayRewrite(gatewayUrl?: string, proxyPath?: string): {
|
|
46
|
+
source: string;
|
|
47
|
+
destination: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates a server-side WhaleClient.
|
|
52
|
+
* Reads from env vars by default — override with explicit config.
|
|
53
|
+
*/
|
|
54
|
+
declare function createServerClient(config?: Partial<WhaleStorefrontConfig>): WhaleClient;
|
|
55
|
+
/**
|
|
56
|
+
* Server-side: fetch all published products with ISR caching.
|
|
57
|
+
* Drop-in replacement for Flora's `getAllProducts()`.
|
|
58
|
+
*/
|
|
59
|
+
declare function getAllProducts(options?: {
|
|
60
|
+
/** Revalidate interval in seconds. Defaults to 60. */
|
|
61
|
+
revalidate?: number;
|
|
62
|
+
/** Filter function to exclude products (e.g. hidden categories, out of stock) */
|
|
63
|
+
filter?: (product: Product) => boolean;
|
|
64
|
+
/** Override client config */
|
|
65
|
+
client?: WhaleClient;
|
|
66
|
+
}): Promise<Product[]>;
|
|
67
|
+
|
|
68
|
+
interface ImageLoaderParams {
|
|
69
|
+
src: string;
|
|
70
|
+
width: number;
|
|
71
|
+
quality?: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Creates a Next.js custom image loader that proxies Supabase images through gateway.
|
|
75
|
+
*
|
|
76
|
+
* Usage in a loader file (e.g. src/lib/image-loader.ts):
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { createImageLoader } from '@neowhale/storefront/next'
|
|
79
|
+
* export default createImageLoader({
|
|
80
|
+
* storeId: process.env.NEXT_PUBLIC_STORE_ID!,
|
|
81
|
+
* gatewayUrl: 'https://whale-gateway.fly.dev',
|
|
82
|
+
* supabaseHost: 'your-project.supabase.co',
|
|
83
|
+
* signingSecret: process.env.NEXT_PUBLIC_MEDIA_SIGNING_SECRET!,
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function createImageLoader(config: {
|
|
88
|
+
storeId: string;
|
|
89
|
+
gatewayUrl: string;
|
|
90
|
+
supabaseHost: string;
|
|
91
|
+
signingSecret: string;
|
|
92
|
+
}): (params: ImageLoaderParams) => string;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a Next.js middleware that protects routes requiring authentication.
|
|
96
|
+
*
|
|
97
|
+
* Checks for a session token cookie or localStorage indicator.
|
|
98
|
+
* Since middleware runs on the edge and can't access localStorage,
|
|
99
|
+
* this checks for a cookie-based token instead.
|
|
100
|
+
*
|
|
101
|
+
* Usage in middleware.ts:
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { createAuthMiddleware } from '@neowhale/storefront/next'
|
|
104
|
+
* export const middleware = createAuthMiddleware({
|
|
105
|
+
* protectedPaths: ['/account'],
|
|
106
|
+
* loginPath: '/account',
|
|
107
|
+
* })
|
|
108
|
+
* export const config = { matcher: ['/account/:path*'] }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare function createAuthMiddleware(options: {
|
|
112
|
+
protectedPaths: string[];
|
|
113
|
+
loginPath: string;
|
|
114
|
+
cookieName?: string;
|
|
115
|
+
}): (request: NextRequest) => NextResponse<unknown>;
|
|
116
|
+
|
|
117
|
+
export { createAuthMiddleware, createImageLoader, createServerClient, getAllProducts, securityHeaders, whaleGatewayRewrite, withSecurityHeaders };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { WhaleStorefrontConfig, WhaleClient, Product } from '../index.js';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Security headers for Next.js storefronts.
|
|
6
|
+
*/
|
|
7
|
+
declare const securityHeaders: {
|
|
8
|
+
key: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}[];
|
|
11
|
+
/**
|
|
12
|
+
* Returns a Next.js `headers()` config with security headers applied to all routes.
|
|
13
|
+
* Use in next.config.ts:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { withSecurityHeaders } from '@neowhale/storefront/next'
|
|
17
|
+
* export default { headers: withSecurityHeaders() }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
declare function withSecurityHeaders(extra?: {
|
|
21
|
+
key: string;
|
|
22
|
+
value: string;
|
|
23
|
+
}[]): () => Promise<{
|
|
24
|
+
source: string;
|
|
25
|
+
headers: {
|
|
26
|
+
key: string;
|
|
27
|
+
value: string;
|
|
28
|
+
}[];
|
|
29
|
+
}[]>;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Gateway rewrite rule for Next.js.
|
|
33
|
+
* Proxies client-side /api/gw/* requests to whale-gateway to avoid CORS.
|
|
34
|
+
*
|
|
35
|
+
* Usage in next.config.ts:
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { whaleGatewayRewrite } from '@neowhale/storefront/next'
|
|
38
|
+
* export default {
|
|
39
|
+
* async rewrites() {
|
|
40
|
+
* return [whaleGatewayRewrite()]
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function whaleGatewayRewrite(gatewayUrl?: string, proxyPath?: string): {
|
|
46
|
+
source: string;
|
|
47
|
+
destination: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates a server-side WhaleClient.
|
|
52
|
+
* Reads from env vars by default — override with explicit config.
|
|
53
|
+
*/
|
|
54
|
+
declare function createServerClient(config?: Partial<WhaleStorefrontConfig>): WhaleClient;
|
|
55
|
+
/**
|
|
56
|
+
* Server-side: fetch all published products with ISR caching.
|
|
57
|
+
* Drop-in replacement for Flora's `getAllProducts()`.
|
|
58
|
+
*/
|
|
59
|
+
declare function getAllProducts(options?: {
|
|
60
|
+
/** Revalidate interval in seconds. Defaults to 60. */
|
|
61
|
+
revalidate?: number;
|
|
62
|
+
/** Filter function to exclude products (e.g. hidden categories, out of stock) */
|
|
63
|
+
filter?: (product: Product) => boolean;
|
|
64
|
+
/** Override client config */
|
|
65
|
+
client?: WhaleClient;
|
|
66
|
+
}): Promise<Product[]>;
|
|
67
|
+
|
|
68
|
+
interface ImageLoaderParams {
|
|
69
|
+
src: string;
|
|
70
|
+
width: number;
|
|
71
|
+
quality?: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Creates a Next.js custom image loader that proxies Supabase images through gateway.
|
|
75
|
+
*
|
|
76
|
+
* Usage in a loader file (e.g. src/lib/image-loader.ts):
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { createImageLoader } from '@neowhale/storefront/next'
|
|
79
|
+
* export default createImageLoader({
|
|
80
|
+
* storeId: process.env.NEXT_PUBLIC_STORE_ID!,
|
|
81
|
+
* gatewayUrl: 'https://whale-gateway.fly.dev',
|
|
82
|
+
* supabaseHost: 'your-project.supabase.co',
|
|
83
|
+
* signingSecret: process.env.NEXT_PUBLIC_MEDIA_SIGNING_SECRET!,
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare function createImageLoader(config: {
|
|
88
|
+
storeId: string;
|
|
89
|
+
gatewayUrl: string;
|
|
90
|
+
supabaseHost: string;
|
|
91
|
+
signingSecret: string;
|
|
92
|
+
}): (params: ImageLoaderParams) => string;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a Next.js middleware that protects routes requiring authentication.
|
|
96
|
+
*
|
|
97
|
+
* Checks for a session token cookie or localStorage indicator.
|
|
98
|
+
* Since middleware runs on the edge and can't access localStorage,
|
|
99
|
+
* this checks for a cookie-based token instead.
|
|
100
|
+
*
|
|
101
|
+
* Usage in middleware.ts:
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { createAuthMiddleware } from '@neowhale/storefront/next'
|
|
104
|
+
* export const middleware = createAuthMiddleware({
|
|
105
|
+
* protectedPaths: ['/account'],
|
|
106
|
+
* loginPath: '/account',
|
|
107
|
+
* })
|
|
108
|
+
* export const config = { matcher: ['/account/:path*'] }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare function createAuthMiddleware(options: {
|
|
112
|
+
protectedPaths: string[];
|
|
113
|
+
loginPath: string;
|
|
114
|
+
cookieName?: string;
|
|
115
|
+
}): (request: NextRequest) => NextResponse<unknown>;
|
|
116
|
+
|
|
117
|
+
export { createAuthMiddleware, createImageLoader, createServerClient, getAllProducts, securityHeaders, whaleGatewayRewrite, withSecurityHeaders };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { WhaleClient } from '../chunk-PR4PUHVN.js';
|
|
2
|
+
import { NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
// src/next/headers.ts
|
|
5
|
+
var securityHeaders = [
|
|
6
|
+
{
|
|
7
|
+
key: "X-DNS-Prefetch-Control",
|
|
8
|
+
value: "on"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
key: "Strict-Transport-Security",
|
|
12
|
+
value: "max-age=63072000; includeSubDomains; preload"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
key: "X-Content-Type-Options",
|
|
16
|
+
value: "nosniff"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
key: "X-Frame-Options",
|
|
20
|
+
value: "SAMEORIGIN"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
key: "X-XSS-Protection",
|
|
24
|
+
value: "1; mode=block"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
key: "Referrer-Policy",
|
|
28
|
+
value: "strict-origin-when-cross-origin"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
key: "Permissions-Policy",
|
|
32
|
+
value: "camera=(), microphone=(), geolocation=(self), interest-cohort=()"
|
|
33
|
+
}
|
|
34
|
+
];
|
|
35
|
+
function withSecurityHeaders(extra) {
|
|
36
|
+
const allHeaders = extra ? [...securityHeaders, ...extra] : securityHeaders;
|
|
37
|
+
return async () => [
|
|
38
|
+
{
|
|
39
|
+
source: "/:path*",
|
|
40
|
+
headers: allHeaders
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/next/rewrite.ts
|
|
46
|
+
function whaleGatewayRewrite(gatewayUrl = "https://whale-gateway.fly.dev", proxyPath = "/api/gw") {
|
|
47
|
+
return {
|
|
48
|
+
source: `${proxyPath}/:path*`,
|
|
49
|
+
destination: `${gatewayUrl}/:path*`
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/next/server.ts
|
|
54
|
+
function createServerClient(config) {
|
|
55
|
+
return new WhaleClient({
|
|
56
|
+
storeId: config?.storeId || process.env.NEXT_PUBLIC_STORE_ID || "",
|
|
57
|
+
apiKey: config?.apiKey || process.env.NEXT_PUBLIC_API_KEY || "",
|
|
58
|
+
gatewayUrl: config?.gatewayUrl || process.env.NEXT_PUBLIC_API_URL || "https://whale-gateway.fly.dev",
|
|
59
|
+
proxyPath: config?.proxyPath
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async function getAllProducts(options) {
|
|
63
|
+
const client = options?.client ?? createServerClient();
|
|
64
|
+
return client.getAllProducts({
|
|
65
|
+
status: "published",
|
|
66
|
+
revalidate: options?.revalidate ?? 60,
|
|
67
|
+
filter: options?.filter
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/next/image-loader.ts
|
|
72
|
+
var ALLOWED_WIDTHS = [64, 96, 128, 256, 384, 640, 828, 1080, 1280, 1920];
|
|
73
|
+
function snapWidth(w) {
|
|
74
|
+
for (const aw of ALLOWED_WIDTHS) {
|
|
75
|
+
if (aw >= w) return aw;
|
|
76
|
+
}
|
|
77
|
+
return ALLOWED_WIDTHS[ALLOWED_WIDTHS.length - 1];
|
|
78
|
+
}
|
|
79
|
+
function createImageLoader(config) {
|
|
80
|
+
return ({ src, width, quality }) => {
|
|
81
|
+
if (!src.includes(config.supabaseHost)) {
|
|
82
|
+
return src;
|
|
83
|
+
}
|
|
84
|
+
const w = String(snapWidth(width));
|
|
85
|
+
const q = String(quality || 80);
|
|
86
|
+
const f = "webp";
|
|
87
|
+
const encoded = WhaleClient.encodeBase64Url(src);
|
|
88
|
+
const s = WhaleClient.signMedia(config.signingSecret, encoded, w, q, f);
|
|
89
|
+
return `${config.gatewayUrl}/v1/stores/${config.storeId}/media?url=${encoded}&w=${w}&q=${q}&f=${f}&s=${s}`;
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function createAuthMiddleware(options) {
|
|
93
|
+
const { protectedPaths, loginPath, cookieName = "whale-session" } = options;
|
|
94
|
+
return function middleware(request) {
|
|
95
|
+
const { pathname } = request.nextUrl;
|
|
96
|
+
const isProtected = protectedPaths.some(
|
|
97
|
+
(p) => pathname === p || pathname.startsWith(`${p}/`)
|
|
98
|
+
);
|
|
99
|
+
if (!isProtected) {
|
|
100
|
+
return NextResponse.next();
|
|
101
|
+
}
|
|
102
|
+
const token = request.cookies.get(cookieName)?.value;
|
|
103
|
+
if (!token) {
|
|
104
|
+
const url = request.nextUrl.clone();
|
|
105
|
+
url.pathname = loginPath;
|
|
106
|
+
url.searchParams.set("redirect", pathname);
|
|
107
|
+
return NextResponse.redirect(url);
|
|
108
|
+
}
|
|
109
|
+
return NextResponse.next();
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { createAuthMiddleware, createImageLoader, createServerClient, getAllProducts, securityHeaders, whaleGatewayRewrite, withSecurityHeaders };
|
|
114
|
+
//# sourceMappingURL=index.js.map
|
|
115
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/next/headers.ts","../../src/next/rewrite.ts","../../src/next/server.ts","../../src/next/image-loader.ts","../../src/next/middleware.ts"],"names":[],"mappings":";;;;AAIO,IAAM,eAAA,GAAkB;AAAA,EAC7B;AAAA,IACE,GAAA,EAAK,wBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,2BAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,wBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,iBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,kBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,iBAAA;AAAA,IACL,KAAA,EAAO;AAAA,GACT;AAAA,EACA;AAAA,IACE,GAAA,EAAK,oBAAA;AAAA,IACL,KAAA,EAAO;AAAA;AAEX;AAWO,SAAS,oBACd,KAAA,EACgF;AAChF,EAAA,MAAM,aAAa,KAAA,GAAQ,CAAC,GAAG,eAAA,EAAiB,GAAG,KAAK,CAAA,GAAI,eAAA;AAC5D,EAAA,OAAO,YAAY;AAAA,IACjB;AAAA,MACE,MAAA,EAAQ,SAAA;AAAA,MACR,OAAA,EAAS;AAAA;AACX,GACF;AACF;;;ACxCO,SAAS,mBAAA,CACd,UAAA,GAAa,+BAAA,EACb,SAAA,GAAY,SAAA,EAC6B;AACzC,EAAA,OAAO;AAAA,IACL,MAAA,EAAQ,GAAG,SAAS,CAAA,OAAA,CAAA;AAAA,IACpB,WAAA,EAAa,GAAG,UAAU,C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GAAA,GAAM,OAAA,CAAQ,OAAA,CAAQ,KAAA,EAAM;AAClC,MAAA,GAAA,CAAI,QAAA,GAAW,SAAA;AACf,MAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,UAAA,EAAY,QAAQ,CAAA;AACzC,MAAA,OAAO,YAAA,CAAa,SAAS,GAAG,CAAA;AAAA,IAClC;AAEA,IAAA,OAAO,aAAa,IAAA,EAAK;AAAA,EAC3B,CAAA;AACF","file":"index.js","sourcesContent":["/**\n * Security headers for Next.js storefronts.\n */\n\nexport const securityHeaders = [\n {\n key: 'X-DNS-Prefetch-Control',\n value: 'on',\n },\n {\n key: 'Strict-Transport-Security',\n value: 'max-age=63072000; includeSubDomains; preload',\n },\n {\n key: 'X-Content-Type-Options',\n value: 'nosniff',\n },\n {\n key: 'X-Frame-Options',\n value: 'SAMEORIGIN',\n },\n {\n key: 'X-XSS-Protection',\n value: '1; mode=block',\n },\n {\n key: 'Referrer-Policy',\n value: 'strict-origin-when-cross-origin',\n },\n {\n key: 'Permissions-Policy',\n value: 'camera=(), microphone=(), geolocation=(self), interest-cohort=()',\n },\n]\n\n/**\n * Returns a Next.js `headers()` config with security headers applied to all routes.\n * Use in next.config.ts:\n *\n * ```ts\n * import { withSecurityHeaders } from '@neowhale/storefront/next'\n * export default { headers: withSecurityHeaders() }\n * ```\n */\nexport function withSecurityHeaders(\n extra?: { key: string; value: string }[]\n): () => Promise<{ source: string; headers: { key: string; value: string }[] }[]> {\n const allHeaders = extra ? [...securityHeaders, ...extra] : securityHeaders\n return async () => [\n {\n source: '/:path*',\n headers: allHeaders,\n },\n ]\n}\n","/**\n * Gateway rewrite rule for Next.js.\n * Proxies client-side /api/gw/* requests to whale-gateway to avoid CORS.\n *\n * Usage in next.config.ts:\n * ```ts\n * import { whaleGatewayRewrite } from '@neowhale/storefront/next'\n * export default {\n * async rewrites() {\n * return [whaleGatewayRewrite()]\n * }\n * }\n * ```\n */\nexport function whaleGatewayRewrite(\n gatewayUrl = 'https://whale-gateway.fly.dev',\n proxyPath = '/api/gw'\n): { source: string; destination: string } {\n return {\n source: `${proxyPath}/:path*`,\n destination: `${gatewayUrl}/:path*`,\n }\n}\n","import { WhaleClient } from '../client.js'\nimport type { Product, WhaleStorefrontConfig } from '../types.js'\n\n/**\n * Creates a server-side WhaleClient.\n * Reads from env vars by default — override with explicit config.\n */\nexport function createServerClient(config?: Partial<WhaleStorefrontConfig>): WhaleClient {\n return new WhaleClient({\n storeId: config?.storeId || process.env.NEXT_PUBLIC_STORE_ID || '',\n apiKey: config?.apiKey || process.env.NEXT_PUBLIC_API_KEY || '',\n gatewayUrl: config?.gatewayUrl || process.env.NEXT_PUBLIC_API_URL || 'https://whale-gateway.fly.dev',\n proxyPath: config?.proxyPath,\n })\n}\n\n/**\n * Server-side: fetch all published products with ISR caching.\n * Drop-in replacement for Flora's `getAllProducts()`.\n */\nexport async function getAllProducts(options?: {\n /** Revalidate interval in seconds. Defaults to 60. */\n revalidate?: number\n /** Filter function to exclude products (e.g. hidden categories, out of stock) */\n filter?: (product: Product) => boolean\n /** Override client config */\n client?: WhaleClient\n}): Promise<Product[]> {\n const client = options?.client ?? createServerClient()\n return client.getAllProducts({\n status: 'published',\n revalidate: options?.revalidate ?? 60,\n filter: options?.filter,\n })\n}\n","import { WhaleClient } from '../client.js'\n\nconst ALLOWED_WIDTHS = [64, 96, 128, 256, 384, 640, 828, 1080, 1280, 1920]\n\nfunction snapWidth(w: number): number {\n for (const aw of ALLOWED_WIDTHS) {\n if (aw >= w) return aw\n }\n return ALLOWED_WIDTHS[ALLOWED_WIDTHS.length - 1]\n}\n\ninterface ImageLoaderParams {\n src: string\n width: number\n quality?: number\n}\n\n/**\n * Creates a Next.js custom image loader that proxies Supabase images through gateway.\n *\n * Usage in a loader file (e.g. src/lib/image-loader.ts):\n * ```ts\n * import { createImageLoader } from '@neowhale/storefront/next'\n * export default createImageLoader({\n * storeId: process.env.NEXT_PUBLIC_STORE_ID!,\n * gatewayUrl: 'https://whale-gateway.fly.dev',\n * supabaseHost: 'your-project.supabase.co',\n * signingSecret: process.env.NEXT_PUBLIC_MEDIA_SIGNING_SECRET!,\n * })\n * ```\n */\nexport function createImageLoader(config: {\n storeId: string\n gatewayUrl: string\n supabaseHost: string\n signingSecret: string\n}): (params: ImageLoaderParams) => string {\n return ({ src, width, quality }: ImageLoaderParams): string => {\n if (!src.includes(config.supabaseHost)) {\n return src\n }\n\n const w = String(snapWidth(width))\n const q = String(quality || 80)\n const f = 'webp'\n const encoded = WhaleClient.encodeBase64Url(src)\n const s = WhaleClient.signMedia(config.signingSecret, encoded, w, q, f)\n\n return `${config.gatewayUrl}/v1/stores/${config.storeId}/media?url=${encoded}&w=${w}&q=${q}&f=${f}&s=${s}`\n }\n}\n","import type { NextRequest } from 'next/server'\nimport { NextResponse } from 'next/server'\n\n/**\n * Creates a Next.js middleware that protects routes requiring authentication.\n *\n * Checks for a session token cookie or localStorage indicator.\n * Since middleware runs on the edge and can't access localStorage,\n * this checks for a cookie-based token instead.\n *\n * Usage in middleware.ts:\n * ```ts\n * import { createAuthMiddleware } from '@neowhale/storefront/next'\n * export const middleware = createAuthMiddleware({\n * protectedPaths: ['/account'],\n * loginPath: '/account',\n * })\n * export const config = { matcher: ['/account/:path*'] }\n * ```\n */\nexport function createAuthMiddleware(options: {\n protectedPaths: string[]\n loginPath: string\n cookieName?: string\n}) {\n const { protectedPaths, loginPath, cookieName = 'whale-session' } = options\n\n return function middleware(request: NextRequest) {\n const { pathname } = request.nextUrl\n\n // Check if this is a protected path\n const isProtected = protectedPaths.some(\n (p) => pathname === p || pathname.startsWith(`${p}/`)\n )\n\n if (!isProtected) {\n return NextResponse.next()\n }\n\n // Check for session cookie\n const token = request.cookies.get(cookieName)?.value\n\n if (!token) {\n const url = request.nextUrl.clone()\n url.pathname = loginPath\n url.searchParams.set('redirect', pathname)\n return NextResponse.redirect(url)\n }\n\n return NextResponse.next()\n }\n}\n"]}
|