@htlkg/astro 0.0.13 → 0.0.14

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
@@ -103,8 +103,8 @@ await requireAdminAccess(Astro);
103
103
  │ └── Topbar.astro
104
104
  ├── htlkg/ # Astro integration
105
105
  │ ├── index.ts # Main integration
106
- ├── config.ts # Configuration types
107
- │ └── virtual-modules.ts
106
+ └── config.ts # Configuration types
107
+ ├── runtime-config.ts # Runtime configuration storage
108
108
  ├── layouts/ # Page layouts
109
109
  │ ├── AdminLayout.astro
110
110
  │ ├── BrandLayout.astro
@@ -0,0 +1,22 @@
1
+ // src/runtime-config.ts
2
+ var _routeGuardConfig = null;
3
+ var _loginPageConfig = null;
4
+ var _amplifyConfig = null;
5
+ function setHtlkgConfig(config) {
6
+ _routeGuardConfig = config.routeGuard;
7
+ _loginPageConfig = config.loginPage;
8
+ _amplifyConfig = config.amplify;
9
+ }
10
+ function getHtlkgConfig() {
11
+ return {
12
+ routeGuardConfig: _routeGuardConfig,
13
+ loginPageConfig: _loginPageConfig,
14
+ amplifyConfig: _amplifyConfig
15
+ };
16
+ }
17
+
18
+ export {
19
+ setHtlkgConfig,
20
+ getHtlkgConfig
21
+ };
22
+ //# sourceMappingURL=chunk-LP4XHGBI.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/runtime-config.ts"],"sourcesContent":["/**\n * Runtime Configuration for @htlkg/astro\n *\n * This module stores configuration set during Astro's config setup phase\n * and makes it available to middleware and pages at request time.\n *\n * This replaces the virtual:htlkg-config virtual module approach,\n * which required bundling to resolve virtual imports.\n *\n * @internal This module is internal to @htlkg/astro\n */\n\nimport type { RouteGuardConfig, LoginPageConfig } from './htlkg/config.js';\n\n// Module-level storage (persists across requests in the same Node.js process)\nlet _routeGuardConfig: RouteGuardConfig | null = null;\nlet _loginPageConfig: LoginPageConfig | null = null;\nlet _amplifyConfig: Record<string, unknown> | null = null;\n\n/**\n * Set the runtime configuration\n * Called by the htlkg integration during astro:config:setup\n */\nexport function setHtlkgConfig(config: {\n\trouteGuard: RouteGuardConfig;\n\tloginPage: LoginPageConfig | null;\n\tamplify: Record<string, unknown> | null;\n}): void {\n\t_routeGuardConfig = config.routeGuard;\n\t_loginPageConfig = config.loginPage;\n\t_amplifyConfig = config.amplify;\n}\n\n/**\n * Get the runtime configuration\n * Called by middleware and pages at request time\n */\nexport function getHtlkgConfig(): {\n\trouteGuardConfig: RouteGuardConfig | null;\n\tloginPageConfig: LoginPageConfig | null;\n\tamplifyConfig: Record<string, unknown> | null;\n} {\n\treturn {\n\t\trouteGuardConfig: _routeGuardConfig,\n\t\tloginPageConfig: _loginPageConfig,\n\t\tamplifyConfig: _amplifyConfig,\n\t};\n}\n"],"mappings":";AAeA,IAAI,oBAA6C;AACjD,IAAI,mBAA2C;AAC/C,IAAI,iBAAiD;AAM9C,SAAS,eAAe,QAItB;AACR,sBAAoB,OAAO;AAC3B,qBAAmB,OAAO;AAC1B,mBAAiB,OAAO;AACzB;AAMO,SAAS,iBAId;AACD,SAAO;AAAA,IACN,kBAAkB;AAAA,IAClB,iBAAiB;AAAA,IACjB,eAAe;AAAA,EAChB;AACD;","names":[]}
@@ -1,58 +1,10 @@
1
+ import {
2
+ setHtlkgConfig
3
+ } from "./chunk-LP4XHGBI.js";
4
+
1
5
  // src/htlkg/index.ts
2
6
  import tailwind from "@astrojs/tailwind";
3
7
  import vue from "@astrojs/vue";
4
-
5
- // src/htlkg/virtual-modules.ts
6
- function serializePattern(pattern) {
7
- if (pattern instanceof RegExp) {
8
- return `new RegExp(${JSON.stringify(pattern.source)}, ${JSON.stringify(pattern.flags)})`;
9
- }
10
- return JSON.stringify(pattern);
11
- }
12
- function serializePatterns(patterns) {
13
- if (!patterns || patterns.length === 0) return "[]";
14
- return `[${patterns.map(serializePattern).join(", ")}]`;
15
- }
16
- function createVirtualModulePlugin(authConfig, loginPageConfig, amplifyConfig) {
17
- const virtualModuleId = "virtual:htlkg-config";
18
- const resolvedVirtualModuleId = "\0" + virtualModuleId;
19
- return {
20
- name: "htlkg-config",
21
- enforce: "pre",
22
- resolveId(id) {
23
- if (id === virtualModuleId) {
24
- return resolvedVirtualModuleId;
25
- }
26
- },
27
- load(id) {
28
- if (id === resolvedVirtualModuleId) {
29
- const serializedAuthConfig = `{
30
- publicRoutes: ${serializePatterns(authConfig.publicRoutes || [])},
31
- authenticatedRoutes: ${serializePatterns(authConfig.authenticatedRoutes || [])},
32
- adminRoutes: ${serializePatterns(authConfig.adminRoutes || [])},
33
- brandRoutes: ${JSON.stringify(authConfig.brandRoutes || [])},
34
- loginUrl: ${JSON.stringify(authConfig.loginUrl || "/login")}
35
- }`;
36
- const serializedAmplifyConfig = amplifyConfig ? JSON.stringify(amplifyConfig) : "null";
37
- const serializedLoginPageConfig = loginPageConfig !== false && loginPageConfig !== null ? JSON.stringify(loginPageConfig) : "null";
38
- return `export const routeGuardConfig = ${serializedAuthConfig};
39
- export const loginPageConfig = ${serializedLoginPageConfig};
40
- export const amplifyConfig = ${serializedAmplifyConfig};`;
41
- }
42
- }
43
- };
44
- }
45
- var virtualModuleTypes = `
46
- declare module 'virtual:htlkg-config' {
47
- import type { RouteGuardConfig, LoginPageConfig } from '@htlkg/astro/htlkg/config';
48
-
49
- export const routeGuardConfig: RouteGuardConfig;
50
- export const loginPageConfig: LoginPageConfig | null;
51
- export const amplifyConfig: Record<string, unknown> | null;
52
- }
53
- `;
54
-
55
- // src/htlkg/index.ts
56
8
  var DEFAULT_ENV_VARS = [
57
9
  "PUBLIC_COGNITO_USER_POOL_ID",
58
10
  "PUBLIC_COGNITO_USER_POOL_CLIENT_ID"
@@ -95,18 +47,20 @@ function htlkg(options = {}) {
95
47
  logger.warn(`Missing env vars: ${missing.join(", ")}`);
96
48
  }
97
49
  }
98
- const virtualModulePlugin = createVirtualModulePlugin(auth, loginPage, amplify || null);
99
- const vitePlugins = [virtualModulePlugin];
50
+ setHtlkgConfig({
51
+ routeGuard: auth,
52
+ loginPage: loginPage || null,
53
+ amplify: amplify || null
54
+ });
100
55
  const isDev = process.env.NODE_ENV !== "production";
101
56
  if (isDev) {
102
57
  try {
103
58
  const vueDevTools = (await import("vite-plugin-vue-devtools")).default;
104
- vitePlugins.push(vueDevTools());
59
+ updateConfig({ vite: { plugins: [vueDevTools()] } });
105
60
  logger.info("Vue DevTools enabled");
106
61
  } catch {
107
62
  }
108
63
  }
109
- updateConfig({ vite: { plugins: vitePlugins } });
110
64
  if (useFullVueSetup) {
111
65
  addMiddleware({ entrypoint: "@htlkg/astro/middleware", order: "pre" });
112
66
  logger.info("Authentication middleware injected");
@@ -141,8 +95,6 @@ declare global {
141
95
  }
142
96
  }
143
97
 
144
- ${virtualModuleTypes}
145
-
146
98
  export {};
147
99
  `
148
100
  });
@@ -155,4 +107,4 @@ export {};
155
107
  export {
156
108
  htlkg
157
109
  };
158
- //# sourceMappingURL=chunk-IA7L6IR5.js.map
110
+ //# sourceMappingURL=chunk-TJ5M2EBN.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/htlkg/index.ts"],"sourcesContent":["/**\n * htlkg Astro Integration\n *\n * Supports static, hybrid, and full SSR output modes.\n */\n\nimport tailwind from '@astrojs/tailwind';\nimport vue from '@astrojs/vue';\nimport type { AstroIntegration } from 'astro';\nimport type { HtlkgIntegrationOptions } from './config.js';\nimport { setHtlkgConfig } from '../runtime-config.js';\n\nconst DEFAULT_ENV_VARS = [\n\t'PUBLIC_COGNITO_USER_POOL_ID',\n\t'PUBLIC_COGNITO_USER_POOL_CLIENT_ID'\n];\n\nexport function htlkg(\n\toptions: HtlkgIntegrationOptions = {},\n): AstroIntegration | AstroIntegration[] {\n\tconst {\n\t\tauth = {},\n\t\tloginPage = { path: '/login', title: 'Sign In', redirectUrl: '/admin' },\n\t\tvalidateEnv = true,\n\t\trequiredEnvVars = DEFAULT_ENV_VARS,\n\t\ttailwind: tailwindOptions,\n\t\tamplify,\n\t\tvueAppSetup = 'auto',\n\t} = options;\n\n\tconst integrations: AstroIntegration[] = [];\n\n\t// Add Tailwind integration (enabled by default)\n\tif (tailwindOptions !== false) {\n\t\tconst tailwindConfig =\n\t\t\ttypeof tailwindOptions === 'object' ? tailwindOptions : undefined;\n\t\tintegrations.push(\n\t\t\ttailwind(tailwindConfig as Parameters<typeof tailwind>[0]),\n\t\t);\n\t}\n\n\t// Determine Vue setup mode:\n\t// - 'full': Use Amplify app entrypoint (requires SSR)\n\t// - 'basic': Basic Vue without app entrypoint (works with static)\n\t// - 'auto': Default to 'basic' for compatibility with static builds\n\tconst useFullVueSetup = vueAppSetup === 'full';\n\n\t// Add Vue integration\n\tif (useFullVueSetup) {\n\t\tintegrations.push(vue({ appEntrypoint: '@htlkg/astro/vue-app-setup' }));\n\t} else {\n\t\tintegrations.push(vue());\n\t}\n\n\t// Add the main htlkg integration\n\tintegrations.push({\n\t\tname: '@htlkg/astro',\n\t\thooks: {\n\t\t\t'astro:config:setup': async ({ updateConfig, addMiddleware, injectRoute, logger }) => {\n\t\t\t\ttry {\n\t\t\t\t\tlogger.info(useFullVueSetup \n\t\t\t\t\t\t? 'Vue configured with Amplify app setup' \n\t\t\t\t\t\t: 'Vue configured (basic mode)');\n\n\t\t\t\t\tif (amplify) {\n\t\t\t\t\t\tlogger.info('Amplify configuration provided');\n\t\t\t\t\t}\n\n\t\t\t\t\t// Validate env vars (only for full setup)\n\t\t\t\t\tif (validateEnv && !amplify && useFullVueSetup) {\n\t\t\t\t\t\tconst missing = requiredEnvVars.filter(v => !process.env[v]);\n\t\t\t\t\t\tif (missing.length > 0) {\n\t\t\t\t\t\t\tlogger.warn(`Missing env vars: ${missing.join(', ')}`);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Set runtime configuration (replaces virtual module approach)\n\t\t\t\t\tsetHtlkgConfig({\n\t\t\t\t\t\trouteGuard: auth,\n\t\t\t\t\t\tloginPage: loginPage || null,\n\t\t\t\t\t\tamplify: amplify || null,\n\t\t\t\t\t});\n\n\t\t\t\t\t// Only load Vue DevTools during development (dynamic import to avoid bundling in production)\n\t\t\t\t\tconst isDev = process.env.NODE_ENV !== 'production';\n\t\t\t\t\tif (isDev) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst vueDevTools = (await import('vite-plugin-vue-devtools')).default;\n\t\t\t\t\t\t\tupdateConfig({ vite: { plugins: [vueDevTools()] } });\n\t\t\t\t\t\t\tlogger.info('Vue DevTools enabled');\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// DevTools not available, skip silently\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Inject middleware (only for full setup)\n\t\t\t\t\tif (useFullVueSetup) {\n\t\t\t\t\t\taddMiddleware({ entrypoint: '@htlkg/astro/middleware', order: 'pre' });\n\t\t\t\t\t\tlogger.info('Authentication middleware injected');\n\t\t\t\t\t}\n\n\t\t\t\t\t// Inject login page (only for full setup)\n\t\t\t\t\tif (loginPage !== false && useFullVueSetup) {\n\t\t\t\t\t\tconst loginPath = loginPage.path || '/login';\n\t\t\t\t\t\tinjectRoute({\n\t\t\t\t\t\t\tpattern: loginPath,\n\t\t\t\t\t\t\tentrypoint: '@htlkg/astro/auth/LoginPage.astro',\n\t\t\t\t\t\t\tprerender: false,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tlogger.info(`Login page injected at ${loginPath}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tlogger.info('htlkg integration configured');\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst msg = error instanceof Error ? error.message : 'Unknown error';\n\t\t\t\t\tlogger.error(`htlkg configuration failed: ${msg}`);\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t\t'astro:config:done': ({ injectTypes }) => {\n\t\t\t\tinjectTypes({\n\t\t\t\t\tfilename: 'htlkg.d.ts',\n\t\t\t\t\tcontent: `\nimport type { AuthUser } from '@htlkg/core/types';\n\ndeclare global {\n namespace App {\n interface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CHANGED
@@ -1,9 +1,10 @@
1
1
  import {
2
2
  htlkg
3
- } from "./chunk-IA7L6IR5.js";
3
+ } from "./chunk-TJ5M2EBN.js";
4
4
  import {
5
5
  isAuthenticatedUser
6
6
  } from "./chunk-UBF5F2RG.js";
7
+ import "./chunk-LP4XHGBI.js";
7
8
  import {
8
9
  applyClientFilters,
9
10
  buildGraphQLFilter,
@@ -17,6 +18,16 @@ import {
17
18
  processListData,
18
19
  sortItems
19
20
  } from "./chunk-2GML443T.js";
21
+ import {
22
+ getClientIP,
23
+ getQueryParams,
24
+ getRequestHeaders,
25
+ getServerData,
26
+ isMobileDevice,
27
+ isServerSide,
28
+ setCacheControl,
29
+ setResponseHeaders
30
+ } from "./chunk-WNMPTDCR.js";
20
31
  import {
21
32
  chunkArray,
22
33
  filterItems,
@@ -35,16 +46,6 @@ import {
35
46
  serializeForHydration,
36
47
  shouldHydrate
37
48
  } from "./chunk-64USRLVP.js";
38
- import {
39
- getClientIP,
40
- getQueryParams,
41
- getRequestHeaders,
42
- getServerData,
43
- isMobileDevice,
44
- isServerSide,
45
- setCacheControl,
46
- setResponseHeaders
47
- } from "./chunk-WNMPTDCR.js";
48
49
 
49
50
  // src/factories/createListPage.ts
50
51
  function generateBreadcrumbs(pageId, title) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/factories/createListPage.ts"],"sourcesContent":["/**\n * List Page Factory\n *\n * Factory function for creating standardized list pages with automatic\n * data fetching, filtering, sorting, and pagination.\n */\n\nimport type { AstroGlobal } from \"astro\";\nimport type { WritableAtom } from \"nanostores\";\n\nimport {\n\tparseListParams,\n\ttype ListParams,\n\ttype ListParamConfig,\n\ttype FilterFieldConfig,\n} from \"../utils/params\";\nimport {\n\tbuildGraphQLFilter,\n\tapplyClientFilters,\n\tsortItems,\n\tpaginateItems,\n} from \"../utils/filters\";\n\n/**\n * Breadcrumb item\n */\nexport interface BreadcrumbItem {\n\tlabel: string;\n\thref?: string;\n}\n\n/**\n * Layout props returned by the factory\n */\nexport interface LayoutProps {\n\ttitle: string;\n\tdescription?: string;\n\tcurrentPage: string;\n\tbreadcrumbs: BreadcrumbItem[];\n}\n\n/**\n * Table initial state\n */\nexport interface TableInitialState {\n\tcurrentPage: number;\n\tpageSize: number;\n\tsortKey: string;\n\tsortOrder: \"asc\" | \"desc\";\n\ttotalItems: number;\n\ttotalPages: number;\n\tfilters: Record<string, any>;\n\tsearch?: string;\n}\n\n/**\n * Related store configuration\n */\nexport interface RelatedStoreConfig<T = any> {\n\tstore: WritableAtom<T[]>;\n\tfetch: () => Promise<T[]>;\n}\n\n/**\n * Configuration for createListPage\n */\nexport interface ListPageConfig<T> {\n\t// Page metadata\n\t/** Page title */\n\ttitle: string;\n\t/** Page description */\n\tdescription?: string;\n\t/** Page identifier (for sidebar active state) */\n\tpageId: string;\n\t/** Custom breadcrumbs (auto-generated if not provided) */\n\tbreadcrumbs?: BreadcrumbItem[];\n\n\t// Data fetching\n\t/** Fetch function that retrieves data */\n\tfetchFn: (params: {\n\t\tfilter: any;\n\t\tlimit?: number;\n\t\tnextToken?: string;\n\t}) => Promise<{ data: T[]; nextToken?: string | null }>;\n\n\t// Data handling\n\t/** Transform function to apply to fetched data */\n\ttransform?: (item: any) => T;\n\t/** Main store to set with data */\n\tstore: WritableAtom<T[]>;\n\t/** Related stores to populate */\n\trelatedStores?: Record<string, RelatedStoreConfig>;\n\n\t// Filtering/sorting config\n\t/** Searchable field keys */\n\tsearchableFields?: string[];\n\t/** Filterable field configurations */\n\tfilterableFields?: FilterFieldConfig[];\n\t/** Sortable field keys (all by default) */\n\tsortableFields?: string[];\n\t/** Default sort configuration */\n\tdefaultSort?: { key: string; order: \"asc\" | \"desc\" };\n\t/** Default page size */\n\tdefaultPageSize?: number;\n\t/** Maximum items to fetch from API */\n\tfetchLimit?: number;\n}\n\n/**\n * Result from createListPage\n */\nexport interface ListPageResult<T> {\n\t/** Props to spread to Layout component */\n\tlayoutProps: LayoutProps;\n\t/** Initial state for table component */\n\tinitialState: TableInitialState;\n\t/** Processed items (paginated) */\n\titems: T[];\n\t/** All items (pre-pagination, for client-side operations) */\n\tallItems: T[];\n\t/** Parsed URL parameters */\n\tparams: ListParams;\n}\n\n/**\n * Default breadcrumb generator\n */\nfunction generateBreadcrumbs(pageId: string, title: string): BreadcrumbItem[] {\n\treturn [\n\t\t{ label: \"Admin\", href: \"/admin\" },\n\t\t{ label: title },\n\t];\n}\n\n/**\n * Create a list page with automatic data handling\n *\n * This factory handles:\n * - URL parameter parsing (page, pageSize, sortKey, sortOrder, filters)\n * - Data fetching with GraphQL filter building\n * - Data transformation\n * - Client-side filtering (for computed fields)\n * - Sorting\n * - Pagination\n * - Store population\n *\n * @example\n * ```astro\n * ---\n * import { createListPage } from '@htlkg/astro/factories';\n * import { $accounts } from '@/stores/accounts';\n *\n * const { layoutProps, initialState, items } = await createListPage(Astro, {\n * title: 'Accounts',\n * pageId: 'accounts',\n * store: $accounts,\n *\n * fetchFn: async ({ filter }) => {\n * const client = getServerClient(Astro);\n * return await client.models.Account.list({ filter });\n * },\n *\n * transform: (account) => ({\n * id: account.id,\n * name: account.name,\n * brandCount: account.brands?.length ?? 0,\n * }),\n *\n * filterableFields: [\n * { key: 'name', type: 'text', graphql: true },\n * { key: 'status', type: 'select', graphql: true, options: ['active', 'inactive'] },\n * ],\n *\n * defaultSort: { key: 'name', order: 'asc' },\n * });\n * ---\n *\n * <Layout {...layoutProps}>\n * <AccountsTable client:load initialState={initialState} />\n * </Layout>\n * ```\n */\nexport async function createListPage<T extends Record<string, any>>(\n\tastro: AstroGlobal,\n\tconfig: ListPageConfig<T>\n): Promise<ListPageResult<T>> {\n\tconst {\n\t\ttitle,\n\t\tdescription,\n\t\tpageId,\n\t\tbreadcrumbs,\n\t\tfetchFn,\n\t\ttransform,\n\t\tstore,\n\t\trelatedStores,\n\t\tsearchableFields,\n\t\tfilterableFields = [],\n\t\tdefaultSort,\n\t\tdefaultPageSize = 25,\n\t\tfetchLimit = 1000,\n\t} = config;\n\n\t// 1. Parse URL parameters\n\tconst paramConfig: ListParamConfig = {\n\t\tdefaultPageSize,\n\t\tdefaultSort,\n\t\tsearchableFields,\n\t\tfilterableFields,\n\t};\n\tconst params = parseListParams(astro.url, paramConfig);\n\n\t// 2. Build GraphQL filter from URL params\n\tconst graphqlFilter = buildGraphQLFilter(params.filters, filterableFields, {\n\t\tsearch: params.search,\n\t\tsearchFields: searchableFields,\n\t});\n\n\t// 3. Fetch data with pagination\n\tlet allItems: T[] = [];\n\tlet nextToken: string | null | undefined = undefined;\n\n\tdo {\n\t\tconst result = await fetchFn({\n\t\t\tfilter: graphqlFilter,\n\t\t\tlimit: fetchLimit,\n\t\t\tnextToken: nextToken ?? undefined,\n\t\t});\n\n\t\tconst data = result.data ?? [];\n\t\tallItems = allItems.concat(data);\n\t\tnextToken = result.nextToken;\n\t} while (nextToken);\n\n\t// 4. Transform data if needed\n\tif (transform) {\n\t\tallItems = allItems.map(transform);\n\t}\n\n\t// 5. Apply client-side filtering (for computed fields not in GraphQL)\n\tallItems = applyClientFilters(allItems, params.filters, filterableFields, {\n\t\tsearch: params.search,\n\t\tsearchFields: searchableFields,\n\t});\n\n\t// 6. Sort\n\tallItems = sortItems(allItems, params.sortKey, params.sortOrder);\n\n\t// 7. Paginate\n\tconst { paginatedItems, totalItems, totalPages, currentPage, pageSize } = paginateItems(\n\t\tallItems,\n\t\tparams.page,\n\t\tparams.pageSize\n\t);\n\n\t// 8. Set main store\n\tstore.set(paginatedItems);\n\n\t// 9. Fetch and set related stores\n\tif (relatedStores) {\n\t\tawait Promise.all(\n\t\t\tObject.entries(relatedStores).map(async ([, config]) => {\n\t\t\t\tconst data = await config.fetch();\n\t\t\t\tconfig.store.set(data);\n\t\t\t})\n\t\t);\n\t}\n\n\t// 10. Build result\n\treturn {\n\t\tlayoutProps: {\n\t\t\ttitle,\n\t\t\tdescription,\n\t\t\tcurrentPage: pageId,\n\t\t\tbreadcrumbs: breadcrumbs ?? generateBreadcrumbs(pageId, title),\n\t\t},\n\t\tinitialState: {\n\t\t\tcurrentPage,\n\t\t\tpageSize,\n\t\t\tsortKey: params.sortKey,\n\t\t\tsortOrder: params.sortOrder,\n\t\t\ttotalItems,\n\t\t\ttotalPages,\n\t\t\tfilters: params.filters,\n\t\t\tsearch: params.search,\n\t\t},\n\t\titems: paginatedItems,\n\t\tallItems,\n\t\tparams,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+HA,SAAS,oBAAoB,QAAgB,OAAiC;AAC7E,SAAO;AAAA,IACN,EAAE,OAAO,SAAS,MAAM,SAAS;AAAA,IACjC,EAAE,OAAO,MAAM;AAAA,EAChB;AACD;AAkDA,eAAsB,eACrB,OACA,QAC6B;AAC7B,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,mBAAmB,CAAC;AAAA,IACpB;AAAA,IACA,kBAAkB;AAAA,IAClB,aAAa;AAAA,EACd,IAAI;AAGJ,QAAM,cAA+B;AAAA,IACpC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACA,QAAM,SAAS,gBAAgB,MAAM,KAAK,WAAW;AAGrD,QAAM,gBAAgB,mBAAmB,OAAO,SAAS,kBAAkB;AAAA,IAC1E,QAAQ,OAAO;AAAA,IACf,cAAc;AAAA,EACf,CAAC;AAGD,MAAI,WAAgB,CAAC;AACrB,MAAI,YAAuC;AAE3C,KAAG;AACF,UAAM,SAAS,MAAM,QAAQ;AAAA,MAC5B,QAAQ;AAAA,MACR,OAAO;AAAA,MACP,WAAW,aAAa;AAAA,IACzB,CAAC;AAED,UAAM,OAAO,OAAO,QAAQ,CAAC;AAC7B,eAAW,SAAS,OAAO,IAAI;AAC/B,gBAAY,OAAO;AAAA,EACpB,SAAS;AAGT,MAAI,WAAW;AACd,eAAW,SAAS,IAAI,SAAS;AAAA,EAClC;AAGA,aAAW,mBAAmB,UAAU,OAAO,SAAS,kBAAkB;AAAA,IACzE,QAAQ,OAAO;AAAA,IACf,cAAc;AAAA,EACf,CAAC;AAGD,aAAW,UAAU,UAAU,OAAO,SAAS,OAAO,SAAS;AAG/D,QAAM,EAAE,gBAAgB,YAAY,YAAY,aAAa,SAAS,IAAI;AAAA,IACzE;AAAA,IACA,OAAO;AAAA,IACP,OAAO;AAAA,EACR;AAGA,QAAM,IAAI,cAAc;AAGxB,MAAI,eAAe;AAClB,UAAM,QAAQ;AAAA,MACb,OAAO,QAAQ,aAAa,EAAE,IAAI,OAAO,CAAC,EAAEA,OAAM,MAAM;AACvD,cAAM,OAAO,MAAMA,QAAO,MAAM;AAChC,QAAAA,QAAO,MAAM,IAAI,IAAI;AAAA,MACtB,CAAC;AAAA,IACF;AAAA,EACD;AAGA,SAAO;AAAA,IACN,aAAa;AAAA,MACZ;AAAA,MACA;AAAA,MACA,aAAa;AAAA,MACb,aAAa,eAAe,oBAAoB,QAAQ,KAAK;AAAA,IAC9D;AAAA,IACA,cAAc;AAAA,MACb;AAAA,MACA;AAAA,MACA,SAAS,OAAO;AAAA,MAChB,WAAW,OAAO;AAAA,MAClB;AAAA,MACA;AAAA,MACA,SAAS,OAAO;AAAA,MAChB,QAAQ,OAAO;AAAA,IAChB;AAAA,IACA,OAAO;AAAA,IACP;AAAA,IACA;AAAA,EACD;AACD;","names":["config"]}
1
+ {"version":3,"sources":["../src/factories/createListPage.ts"],"sourcesContent":["/**\n * List Page Factory\n *\n * Factory function for creating standardized list pages with automatic\n * data fetching, filtering, sorting, and pagination.\n */\n\nimport type { AstroGlobal } from \"astro\";\nimport type { WritableAtom } from \"nanostores\";\n\nimport {\n\tparseListParams,\n\ttype ListParams,\n\ttype ListParamConfig,\n\ttype FilterFieldConfig,\n} from \"../utils/params\";\nimport {\n\tbuildGraphQLFilter,\n\tapplyClientFilters,\n\tsortItems,\n\tpaginateItems,\n} from \"../utils/filters\";\n\n/**\n * Breadcrumb item\n */\nexport interface BreadcrumbItem {\n\tlabel: string;\n\thref?: string;\n}\n\n/**\n * Layout props returned by the factory\n */\nexport interface LayoutProps {\n\ttitle: string;\n\tdescription?: string;\n\tcurrentPage: string;\n\tbreadcrumbs: BreadcrumbItem[];\n}\n\n/**\n * Table initial state\n */\nexport interface TableInitialState {\n\tcurrentPage: number;\n\tpageSize: number;\n\tsortKey: string;\n\tsortOrder: \"asc\" | \"desc\";\n\ttotalItems: number;\n\ttotalPages: number;\n\tfilters: Record<string, any>;\n\tsearch?: string;\n}\n\n/**\n * Related store configuration\n */\nexport interface RelatedStoreConfig<T = any> {\n\tstore: WritableAtom<T[]>;\n\tfetch: () => Promise<T[]>;\n}\n\n/**\n * Configuration for createListPage\n */\nexport interface ListPageConfig<T> {\n\t// Page metadata\n\t/** Page title */\n\ttitle: string;\n\t/** Page description */\n\tdescription?: string;\n\t/** Page identifier (for sidebar active state) */\n\tpageId: string;\n\t/** Custom breadcrumbs (auto-generated if not provided) */\n\tbreadcrumbs?: BreadcrumbItem[];\n\n\t// Data fetching\n\t/** Fetch function that retrieves data */\n\tfetchFn: (params: {\n\t\tfilter: any;\n\t\tlimit?: number;\n\t\tnextToken?: string;\n\t}) => Promise<{ data: T[]; nextToken?: string | null }>;\n\n\t// Data handling\n\t/** Transform function to apply to fetched data */\n\ttransform?: (item: any) => T;\n\t/** Main store to set with data */\n\tstore: WritableAtom<T[]>;\n\t/** Related stores to populate */\n\trelatedStores?: Record<string, RelatedStoreConfig>;\n\n\t// Filtering/sorting config\n\t/** Searchable field keys */\n\tsearchableFields?: string[];\n\t/** Filterable field configurations */\n\tfilterableFields?: FilterFieldConfig[];\n\t/** Sortable field keys (all by default) */\n\tsortableFields?: string[];\n\t/** Default sort configuration */\n\tdefaultSort?: { key: string; order: \"asc\" | \"desc\" };\n\t/** Default page size */\n\tdefaultPageSize?: number;\n\t/** Maximum items to fetch from API */\n\tfetchLimit?: number;\n}\n\n/**\n * Result from createListPage\n */\nexport interface ListPageResult<T> {\n\t/** Props to spread to Layout component */\n\tlayoutProps: LayoutProps;\n\t/** Initial state for table component */\n\tinitialState: TableInitialState;\n\t/** Processed items (paginated) */\n\titems: T[];\n\t/** All items (pre-pagination, for client-side operations) */\n\tallItems: T[];\n\t/** Parsed URL parameters */\n\tparams: ListParams;\n}\n\n/**\n * Default breadcrumb generator\n */\nfunction generateBreadcrumbs(pageId: string, title: string): BreadcrumbItem[] {\n\treturn [\n\t\t{ label: \"Admin\", href: \"/admin\" },\n\t\t{ label: title },\n\t];\n}\n\n/**\n * Create a list page with automatic data handling\n *\n * This factory handles:\n * - URL parameter parsing (page, pageSize, sortKey, sortOrder, filters)\n * - Data fetching with GraphQL filter building\n * - Data transformation\n * - Client-side filtering (for computed fields)\n * - Sorting\n * - Pagination\n * - Store population\n *\n * @example\n * ```astro\n * ---\n * import { createListPage } from '@htlkg/astro/factories';\n * import { $accounts } from '@/stores/accounts';\n *\n * const { layoutProps, initialState, items } = await createListPage(Astro, {\n * title: 'Accounts',\n * pageId: 'accounts',\n * store: $accounts,\n *\n * fetchFn: async ({ filter }) => {\n * const client = getServerClient(Astro);\n * return await client.models.Account.list({ filter });\n * },\n *\n * transform: (account) => ({\n * id: account.id,\n * name: account.name,\n * brandCount: account.brands?.length ?? 0,\n * }),\n *\n * filterableFields: [\n * { key: 'name', type: 'text', graphql: true },\n * { key: 'status', type: 'select', graphql: true, options: ['active', 'inactive'] },\n * ],\n *\n * defaultSort: { key: 'name', order: 'asc' },\n * });\n * ---\n *\n * <Layout {...layoutProps}>\n * <AccountsTable client:load initialState={initialState} />\n * </Layout>\n * ```\n */\nexport async function createListPage<T extends Record<string, any>>(\n\tastro: AstroGlobal,\n\tconfig: ListPageConfig<T>\n): Promise<ListPageResult<T>> {\n\tconst {\n\t\ttitle,\n\t\tdescription,\n\t\tpageId,\n\t\tbreadcrumbs,\n\t\tfetchFn,\n\t\ttransform,\n\t\tstore,\n\t\trelatedStores,\n\t\tsearchableFields,\n\t\tfilterableFields = [],\n\t\tdefaultSort,\n\t\tdefaultPageSize = 25,\n\t\tfetchLimit = 1000,\n\t} = config;\n\n\t// 1. Parse URL parameters\n\tconst paramConfig: ListParamConfig = {\n\t\tdefaultPageSize,\n\t\tdefaultSort,\n\t\tsearchableFields,\n\t\tfilterableFields,\n\t};\n\tconst params = parseListParams(astro.url, paramConfig);\n\n\t// 2. Build GraphQL filter from URL params\n\tconst graphqlFilter = buildGraphQLFilter(params.filters, filterableFields, {\n\t\tsearch: params.search,\n\t\tsearchFields: searchableFields,\n\t});\n\n\t// 3. Fetch data with pagination\n\tlet allItems: T[] = [];\n\tlet nextToken: string | null | undefined = undefined;\n\n\tdo {\n\t\tconst result = await fetchFn({\n\t\t\tfilter: graphqlFilter,\n\t\t\tlimit: fetchLimit,\n\t\t\tnextToken: nextToken ?? undefined,\n\t\t});\n\n\t\tconst data = result.data ?? [];\n\t\tallItems = allItems.concat(data);\n\t\tnextToken = result.nextToken;\n\t} while (nextToken);\n\n\t// 4. Transform data if needed\n\tif (transform) {\n\t\tallItems = allItems.map(transform);\n\t}\n\n\t// 5. Apply client-side filtering (for computed fields not in GraphQL)\n\tallItems = applyClientFilters(allItems, params.filters, filterableFields, {\n\t\tsearch: params.search,\n\t\tsearchFields: searchableFields,\n\t});\n\n\t// 6. Sort\n\tallItems = sortItems(allItems, params.sortKey, params.sortOrder);\n\n\t// 7. Paginate\n\tconst { paginatedItems, totalItems, totalPages, currentPage, pageSize } = paginateItems(\n\t\tallItems,\n\t\tparams.page,\n\t\tparams.pageSize\n\t);\n\n\t// 8. Set main store\n\tstore.set(paginatedItems);\n\n\t// 9. Fetch and set related stores\n\tif (relatedStores) {\n\t\tawait Promise.all(\n\t\t\tObject.entries(relatedStores).map(async ([, config]) => {\n\t\t\t\tconst data = await config.fetch();\n\t\t\t\tconfig.store.set(data);\n\t\t\t})\n\t\t);\n\t}\n\n\t// 10. Build result\n\treturn {\n\t\tlayoutProps: {\n\t\t\ttitle,\n\t\t\tdescription,\n\t\t\tcurrentPage: pageId,\n\t\t\tbreadcrumbs: breadcrumbs ?? generateBreadcrumbs(pageId, title),\n\t\t},\n\t\tinitialState: {\n\t\t\tcurrentPage,\n\t\t\tpageSize,\n\t\t\tsortKey: params.sortKey,\n\t\t\tsortOrder: params.sortOrder,\n\t\t\ttotalItems,\n\t\t\ttotalPages,\n\t\t\tfilters: params.filters,\n\t\t\tsearch: params.search,\n\t\t},\n\t\titems: paginatedItems,\n\t\tallItems,\n\t\tparams,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+HA,SAAS,oBAAoB,QAAgB,OAAiC;AAC7E,SAAO;AAAA,IACN,EAAE,OAAO,SAAS,MAAM,SAAS;AAAA,IACjC,EAAE,OAAO,MAAM;AAAA,EAChB;AACD;AAkDA,eAAsB,eACrB,OACA,QAC6B;AAC7B,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,mBAAmB,CAAC;AAAA,IACpB;AAAA,IACA,kBAAkB;AAAA,IAClB,aAAa;AAAA,EACd,IAAI;AAGJ,QAAM,cAA+B;AAAA,IACpC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACA,QAAM,SAAS,gBAAgB,MAAM,KAAK,WAAW;AAGrD,QAAM,gBAAgB,mBAAmB,OAAO,SAAS,kBAAkB;AAAA,IAC1E,QAAQ,OAAO;AAAA,IACf,cAAc;AAAA,EACf,CAAC;AAGD,MAAI,WAAgB,CAAC;AACrB,MAAI,YAAuC;AAE3C,KAAG;AACF,UAAM,SAAS,MAAM,QAAQ;AAAA,MAC5B,QAAQ;AAAA,MACR,OAAO;AAAA,MACP,WAAW,aAAa;AAAA,IACzB,CAAC;AAED,UAAM,OAAO,OAAO,QAAQ,CAAC;AAC7B,eAAW,SAAS,OAAO,IAAI;AAC/B,gBAAY,OAAO;AAAA,EACpB,SAAS;AAGT,MAAI,WAAW;AACd,eAAW,SAAS,IAAI,SAAS;AAAA,EAClC;AAGA,aAAW,mBAAmB,UAAU,OAAO,SAAS,kBAAkB;AAAA,IACzE,QAAQ,OAAO;AAAA,IACf,cAAc;AAAA,EACf,CAAC;AAGD,aAAW,UAAU,UAAU,OAAO,SAAS,OAAO,SAAS;AAG/D,QAAM,EAAE,gBAAgB,YAAY,YAAY,aAAa,SAAS,IAAI;AAAA,IACzE;AAAA,IACA,OAAO;AAAA,IACP,OAAO;AAAA,EACR;AAGA,QAAM,IAAI,cAAc;AAGxB,MAAI,eAAe;AAClB,UAAM,QAAQ;AAAA,MACb,OAAO,QAAQ,aAAa,EAAE,IAAI,OAAO,CAAC,EAAEA,OAAM,MAAM;AACvD,cAAM,OAAO,MAAMA,QAAO,MAAM;AAChC,QAAAA,QAAO,MAAM,IAAI,IAAI;AAAA,MACtB,CAAC;AAAA,IACF;AAAA,EACD;AAGA,SAAO;AAAA,IACN,aAAa;AAAA,MACZ;AAAA,MACA;AAAA,MACA,aAAa;AAAA,MACb,aAAa,eAAe,oBAAoB,QAAQ,KAAK;AAAA,IAC9D;AAAA,IACA,cAAc;AAAA,MACb;AAAA,MACA;AAAA,MACA,SAAS,OAAO;AAAA,MAChB,WAAW,OAAO;AAAA,MAClB;AAAA,MACA;AAAA,MACA,SAAS,OAAO;AAAA,MAChB,QAAQ,OAAO;AAAA,IAChB;AAAA,IACA,OAAO;AAAA,IACP;AAAA,IACA;AAAA,EACD;AACD;","names":["config"]}
@@ -1,3 +1,7 @@
1
+ import {
2
+ getHtlkgConfig
3
+ } from "../chunk-LP4XHGBI.js";
4
+
1
5
  // src/middleware/index.ts
2
6
  import { sequence } from "astro:middleware";
3
7
 
@@ -5,11 +9,11 @@ import { sequence } from "astro:middleware";
5
9
  import { Amplify } from "aws-amplify";
6
10
  import { getUser } from "@htlkg/core/auth";
7
11
  import { globalSettings } from "@htlkg/core/amplify-astro-adapter";
8
- import { amplifyConfig } from "virtual:htlkg-config";
9
12
  var amplifyConfigured = false;
10
13
  function ensureAmplifyConfigured() {
11
14
  if (amplifyConfigured) return;
12
15
  try {
16
+ const { amplifyConfig } = getHtlkgConfig();
13
17
  if (!amplifyConfig) {
14
18
  console.warn("[htlkg Auth] No Amplify configuration provided");
15
19
  return;
@@ -44,8 +48,6 @@ var authMiddleware = async (context, next) => {
44
48
  };
45
49
 
46
50
  // src/middleware/route-guards.ts
47
- import { routeGuardConfig } from "virtual:htlkg-config";
48
- var config = routeGuardConfig;
49
51
  function matchesPattern(pathname, patterns) {
50
52
  return patterns.some((pattern) => {
51
53
  try {
@@ -68,13 +70,14 @@ function matchesPattern(pathname, patterns) {
68
70
  var routeGuard = async (context, next) => {
69
71
  const { locals, url, redirect } = context;
70
72
  const pathname = url.pathname;
73
+ const { routeGuardConfig } = getHtlkgConfig();
71
74
  const {
72
75
  publicRoutes = [],
73
76
  authenticatedRoutes = [],
74
77
  adminRoutes = [],
75
78
  brandRoutes = [],
76
79
  loginUrl = "/login"
77
- } = config;
80
+ } = routeGuardConfig || {};
78
81
  try {
79
82
  if (matchesPattern(pathname, publicRoutes)) {
80
83
  return next();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/middleware/index.ts","../../src/middleware/auth.ts","../../src/middleware/route-guards.ts"],"sourcesContent":["/**\n * Middleware for @htlkg/astro\n * \n * This module exports:\n * - authMiddleware: Retrieves authenticated user and injects into locals\n * - routeGuard: Enforces access control based on route configuration\n * - onRequest: Composed middleware chain (auth + route guard)\n * - Helper functions: requireAuth, requireAdminAccess, requireBrandAccess\n */\n\nimport { sequence } from 'astro:middleware';\nimport { authMiddleware } from './auth.js';\nimport { routeGuard } from './route-guards.js';\n\n// Export individual middleware\nexport { authMiddleware } from './auth.js';\nexport {\n\trouteGuard,\n\trequireAuth,\n\trequireAdminAccess,\n\trequireBrandAccess,\n} from './route-guards.js';\n\n/**\n * Export composed middleware chain\n * Auth middleware runs first to populate locals.user\n * Route guard middleware runs second to enforce access control\n * \n * This is the default export that gets injected by the htlkg integration\n */\nexport const onRequest = sequence(authMiddleware, routeGuard);\n","/**\n * Authentication middleware for htlkg integration\n * \n * This middleware:\n * - Configures AWS Amplify for server-side auth\n * - Retrieves the authenticated user from AWS Amplify\n * - Injects the user into Astro.locals for use in pages and API routes\n * - Handles authentication errors gracefully\n */\n\nimport type { MiddlewareHandler } from 'astro';\nimport { Amplify } from 'aws-amplify';\nimport { getUser } from '@htlkg/core/auth';\nimport { globalSettings } from '@htlkg/core/amplify-astro-adapter';\nimport { amplifyConfig } from 'virtual:htlkg-config';\n\n// Track if Amplify has been configured\nlet amplifyConfigured = false;\n\n/**\n * Configure Amplify on first request\n */\nfunction ensureAmplifyConfigured(): void {\n\tif (amplifyConfigured) return;\n\n\ttry {\n\t\tif (!amplifyConfig) {\n\t\t\tconsole.warn('[htlkg Auth] No Amplify configuration provided');\n\t\t\treturn;\n\t\t}\n\n\t\t// Configure Amplify with SSR support\n\t\tAmplify.configure(amplifyConfig as any, { ssr: true });\n\t\t\n\t\t// Enable server-side auth in the adapter\n\t\tglobalSettings.enableServerSideAuth();\n\t\t\n\t\t// Check if we're on HTTPS (for secure cookies)\n\t\tconst isSSL = typeof window !== 'undefined' \n\t\t\t? window.location.protocol === 'https:' \n\t\t\t: process.env.NODE_ENV === 'production';\n\t\tglobalSettings.setIsSSLOrigin(isSSL);\n\t\t\n\t\tamplifyConfigured = true;\n\t\tconsole.info('[htlkg Auth] Amplify configured for server-side auth');\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : 'Unknown error';\n\t\tconsole.error(`[htlkg Auth] Failed to configure Amplify: ${errorMsg}`);\n\t}\n}\n\n/**\n * Auth middleware - retrieves authenticated user and injects into locals\n * \n * This middleware runs on every request and attempts to get the current\n * authenticated user from AWS Amplify. If successful, the user is injected\n * into context.locals.user. If authentication fails, locals.user is set to null.\n * \n * @example\n * // In an Astro page\n * const { user } = Astro.locals;\n * if (user) {\n * console.log('Authenticated user:', user.email);\n * }\n */\nexport const authMiddleware: MiddlewareHandler = async (context, next) => {\n\tconst { locals } = context;\n\n\t// Ensure Amplify is configured before attempting auth\n\tensureAmplifyConfigured();\n\n\ttry {\n\t\tconst user = await getUser(context);\n\t\tlocals.user = user;\n\t} catch (error) {\n\t\t// Set user to null on any authentication error\n\t\tlocals.user = null;\n\n\t\t// Log error without exposing sensitive information\n\t\tif (error instanceof Error) {\n\t\t\t// Filter out sensitive data from error messages\n\t\t\tconst safeMessage = error.message\n\t\t\t\t.replace(/token[=:]\\s*[^\\s,}]+/gi, 'token=***')\n\t\t\t\t.replace(/key[=:]\\s*[^\\s,}]+/gi, 'key=***')\n\t\t\t\t.replace(/secret[=:]\\s*[^\\s,}]+/gi, 'secret=***')\n\t\t\t\t.replace(/password[=:]\\s*[^\\s,}]+/gi, 'password=***');\n\n\t\t\tconsole.error('[htlkg Auth] Authentication failed:', safeMessage);\n\t\t} else {\n\t\t\tconsole.error('[htlkg Auth] Authentication failed: Unknown error');\n\t\t}\n\t}\n\n\treturn next();\n};\n","/**\n * Route guard middleware for htlkg integration\n * \n * This middleware enforces access control based on route configuration:\n * - Public routes: accessible to everyone\n * - Authenticated routes: require any logged-in user\n * - Admin routes: require admin privileges\n * - Brand routes: require brand-specific access or admin privileges\n */\n\nimport type { MiddlewareHandler } from 'astro';\nimport type { RoutePattern, RouteGuardConfig } from '../htlkg/config.js';\n\n// Import configuration from virtual module\nimport { routeGuardConfig } from 'virtual:htlkg-config';\n\n// Type assertion for the imported config\nconst config = routeGuardConfig as RouteGuardConfig;\n\n/**\n * Helper: Check if pathname matches any of the provided patterns\n */\nfunction matchesPattern(pathname: string, patterns: RoutePattern[]): boolean {\n\treturn patterns.some((pattern) => {\n\t\ttry {\n\t\t\tif (typeof pattern === 'string') {\n\t\t\t\t// Special case: \"/\" only matches exactly\n\t\t\t\tif (pattern === '/') {\n\t\t\t\t\treturn pathname === '/';\n\t\t\t\t}\n\t\t\t\t// String pattern: exact match or starts with (for sub-routes)\n\t\t\t\treturn pathname === pattern || pathname.startsWith(pattern + '/');\n\t\t\t}\n\t\t\t// RegExp pattern: test against pathname\n\t\t\treturn pattern.test(pathname);\n\t\t} catch (error) {\n\t\t\t// Log pattern matching errors but don't block the request\n\t\t\tconsole.error(\n\t\t\t\t'[htlkg] Error matching route pattern:',\n\t\t\t\terror instanceof Error ? error.message : 'Unknown error',\n\t\t\t);\n\t\t\treturn false;\n\t\t}\n\t});\n}\n\n/**\n * Route guard middleware - protects routes based on configuration\n * \n * This middleware runs after authMiddleware and enforces access control\n * based on the route configuration provided to the htlkg integration.\n * \n * @example\n * // In astro.config.mjs\n * htlkg({\n * auth: {\n * publicRoutes: ['/login', '/'],\n * adminRoutes: [/^\\/admin/],\n * brandRoutes: [\n * { pattern: /^\\/brands\\/(\\d+)/, brandIdParam: 1 }\n * ],\n * loginUrl: '/login'\n * }\n * })\n */\nexport const routeGuard: MiddlewareHandler = async (context, next) => {\n\tconst { locals, url, redirect } = context;\n\tconst pathname = url.pathname;\n\n\tconst {\n\t\tpublicRoutes = [],\n\t\tauthenticatedRoutes = [],\n\t\tadminRoutes = [],\n\t\tbrandRoutes = [],\n\t\tloginUrl = '/login',\n\t} = config;\n\n\ttry {\n\t\t// Public routes - no auth required\n\t\tif (matchesPattern(pathname, publicRoutes)) {\n\t\t\treturn next();\n\t\t}\n\n\t\tconst user = locals.user;\n\n\t\t// Admin routes - require admin role\n\t\tif (matchesPattern(pathname, adminRoutes)) {\n\t\t\tif (!user || !user.isAdmin) {\n\t\t\t\treturn redirect(`${loginUrl}?error=admin_required`);\n\t\t\t}\n\t\t\treturn next();\n\t\t}\n\n\t\t// Brand routes - require brand access or admin\n\t\tfor (const brandRoute of brandRoutes) {\n\t\t\ttry {\n\t\t\t\tconst match = pathname.match(brandRoute.pattern);\n\t\t\t\tif (match) {\n\t\t\t\t\tconst brandId = Number.parseInt(match[brandRoute.brandIdParam], 10);\n\n\t\t\t\t\tif (Number.isNaN(brandId)) {\n\t\t\t\t\t\tconsole.warn(`[htlkg] Invalid brandId extracted from ${pathname}`);\n\t\t\t\t\t\treturn redirect(`${loginUrl}?error=invalid_brand`);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!user || (!user.isAdmin && !user.brandIds.includes(brandId))) {\n\t\t\t\t\t\treturn redirect(`${loginUrl}?error=access_denied`);\n\t\t\t\t\t}\n\t\t\t\t\treturn next();\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[htlkg] Error processing brand route for ${pathname}:`,\n\t\t\t\t\terror instanceof Error ? error.message : 'Unknown error',\n\t\t\t\t);\n\t\t\t\t// Fail-safe: deny access on error\n\t\t\t\treturn redirect(`${loginUrl}?error=access_denied`);\n\t\t\t}\n\t\t}\n\n\t\t// Authenticated routes - require any logged-in user\n\t\tif (matchesPattern(pathname, authenticatedRoutes)) {\n\t\t\tif (!user) {\n\t\t\t\tconst returnUrl = encodeURIComponent(pathname + url.search);\n\t\t\t\treturn redirect(`${loginUrl}?redirect=${returnUrl}`);\n\t\t\t}\n\t\t\treturn next();\n\t\t}\n\n\t\t// Default: allow access\n\t\treturn next();\n\t} catch (error) {\n\t\t// Catch-all error handler for route guard\n\t\tconsole.error(\n\t\t\t'[htlkg] Unexpected error in route guard:',\n\t\t\terror instanceof Error ? error.message : 'Unknown error',\n\t\t);\n\t\t// Fail-safe: deny access and redirect to login\n\t\treturn redirect(`${loginUrl}?error=server_error`);\n\t}\n};\n\n/**\n * Helper functions for programmatic route protection in pages\n * These can be used in Astro pages for more fine-grained control\n */\n\n/**\n * Require authentication for a page\n * Redirects to login if user is not authenticated\n */\nexport async function requireAuth(context: any, loginUrl = '/login') {\n\tconst user = context.locals.user;\n\tif (!user) {\n\t\tconst currentUrl = context.url.pathname + context.url.search;\n\t\tconst encodedReturnUrl = encodeURIComponent(currentUrl);\n\t\treturn context.redirect(`${loginUrl}?redirect=${encodedReturnUrl}`);\n\t}\n\treturn user;\n}\n\n/**\n * Require admin access for a page\n * Redirects to login if user is not an admin\n */\nexport async function requireAdminAccess(context: any, loginUrl = '/login') {\n\tconst user = context.locals.user;\n\tif (!user) {\n\t\treturn context.redirect(`${loginUrl}?error=not_authenticated`);\n\t}\n\tif (!user.isAdmin) {\n\t\treturn context.redirect(`${loginUrl}?error=admin_required`);\n\t}\n\treturn user;\n}\n\n/**\n * Require brand access for a page\n * Redirects to login if user doesn't have access to the specified brand\n */\nexport async function requireBrandAccess(\n\tcontext: any,\n\tbrandId: number,\n\tloginUrl = '/login'\n) {\n\tconst user = context.locals.user;\n\tif (!user) {\n\t\treturn context.redirect(`${loginUrl}?error=not_authenticated`);\n\t}\n\tif (!user.isAdmin && !user.brandIds.includes(brandId)) {\n\t\treturn context.redirect(`${loginUrl}?error=access_denied`);\n\t}\n\treturn user;\n}\n"],"mappings":";AAUA,SAAS,gBAAgB;;;ACCzB,SAAS,eAAe;AACxB,SAAS,eAAe;AACxB,SAAS,sBAAsB;AAC/B,SAAS,qBAAqB;AAG9B,IAAI,oBAAoB;AAKxB,SAAS,0BAAgC;AACxC,MAAI,kBAAmB;AAEvB,MAAI;AACH,QAAI,CAAC,eAAe;AACnB,cAAQ,KAAK,gDAAgD;AAC7D;AAAA,IACD;AAGA,YAAQ,UAAU,eAAsB,EAAE,KAAK,KAAK,CAAC;AAGrD,mBAAe,qBAAqB;AAGpC,UAAM,QAAQ,OAAO,WAAW,cAC7B,OAAO,SAAS,aAAa,WAC7B,QAAQ,IAAI,aAAa;AAC5B,mBAAe,eAAe,KAAK;AAEnC,wBAAoB;AACpB,YAAQ,KAAK,sDAAsD;AAAA,EACpE,SAAS,OAAO;AACf,UAAM,WAAW,iBAAiB,QAAQ,MAAM,UAAU;AAC1D,YAAQ,MAAM,6CAA6C,QAAQ,EAAE;AAAA,EACtE;AACD;AAgBO,IAAM,iBAAoC,OAAO,SAAS,SAAS;AACzE,QAAM,EAAE,OAAO,I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AAM;AACV,UAAM,aAAa,QAAQ,IAAI,WAAW,QAAQ,IAAI;AACtD,UAAM,mBAAmB,mBAAmB,UAAU;AACtD,WAAO,QAAQ,SAAS,GAAG,QAAQ,aAAa,gBAAgB,EAAE;AAAA,EACnE;AACA,SAAO;AACR;AAMA,eAAsB,mBAAmB,SAAc,WAAW,UAAU;AAC3E,QAAM,OAAO,QAAQ,OAAO;AAC5B,MAAI,CAAC,MAAM;AACV,WAAO,QAAQ,SAAS,GAAG,QAAQ,0BAA0B;AAAA,EAC9D;AACA,MAAI,CAAC,KAAK,SAAS;AAClB,WAAO,QAAQ,SAAS,GAAG,QAAQ,uBAAuB;AAAA,EAC3D;AACA,SAAO;AACR;AAMA,eAAsB,mBACrB,SACA,SACA,WAAW,UACV;AACD,QAAM,OAAO,QAAQ,OAAO;AAC5B,MAAI,CAAC,MAAM;AACV,WAAO,QAAQ,SAAS,GAAG,QAAQ,0BAA0B;AAAA,EAC9D;AACA,MAAI,CAAC,KAAK,WAAW,CAAC,KAAK,SAAS,SAAS,OAAO,GAAG;AACtD,WAAO,QAAQ,SAAS,GAAG,QAAQ,sBAAsB;AAAA,EAC1D;AACA,SAAO;AACR;;;AFnKO,IAAM,YAAY,SAAS,gBAAgB,UAAU;","names":[]}
1
+ {"version":3,"sources":["../../src/middleware/index.ts","../../src/middleware/auth.ts","../../src/middleware/route-guards.ts"],"sourcesContent":["/**\n * Middleware for @htlkg/astro\n * \n * This module exports:\n * - authMiddleware: Retrieves authenticated user and injects into locals\n * - routeGuard: Enforces access control based on route configuration\n * - onRequest: Composed middleware chain (auth + route guard)\n * - Helper functions: requireAuth, requireAdminAccess, requireBrandAccess\n */\n\nimport { sequence } from 'astro:middleware';\nimport { authMiddleware } from './auth.js';\nimport { routeGuard } from './route-guards.js';\n\n// Export individual middleware\nexport { authMiddleware } from './auth.js';\nexport {\n\trouteGuard,\n\trequireAuth,\n\trequireAdminAccess,\n\trequireBrandAccess,\n} from './route-guards.js';\n\n/**\n * Export composed middleware chain\n * Auth middleware runs first to populate locals.user\n * Route guard middleware runs second to enforce access control\n * \n * This is the default export that gets injected by the htlkg integration\n */\nexport const onRequest = sequence(authMiddleware, routeGuard);\n","/**\n * Authentication middleware for htlkg integration\n * \n * This middleware:\n * - Configures AWS Amplify for server-side auth\n * - Retrieves the authenticated user from AWS Amplify\n * - Injects the user into Astro.locals for use in pages and API routes\n * - Handles authentication errors gracefully\n */\n\nimport type { MiddlewareHandler } from 'astro';\nimport { Amplify } from 'aws-amplify';\nimport { getUser } from '@htlkg/core/auth';\nimport { globalSettings } from '@htlkg/core/amplify-astro-adapter';\nimport { getHtlkgConfig } from '../runtime-config.js';\n\n// Track if Amplify has been configured\nlet amplifyConfigured = false;\n\n/**\n * Configure Amplify on first request\n */\nfunction ensureAmplifyConfigured(): void {\n\tif (amplifyConfigured) return;\n\n\ttry {\n\t\tconst { amplifyConfig } = getHtlkgConfig();\n\t\tif (!amplifyConfig) {\n\t\t\tconsole.warn('[htlkg Auth] No Amplify configuration provided');\n\t\t\treturn;\n\t\t}\n\n\t\t// Configure Amplify with SSR support\n\t\tAmplify.configure(amplifyConfig as any, { ssr: true });\n\t\t\n\t\t// Enable server-side auth in the adapter\n\t\tglobalSettings.enableServerSideAuth();\n\t\t\n\t\t// Check if we're on HTTPS (for secure cookies)\n\t\tconst isSSL = typeof window !== 'undefined' \n\t\t\t? window.location.protocol === 'https:' \n\t\t\t: process.env.NODE_ENV === 'production';\n\t\tglobalSettings.setIsSSLOrigin(isSSL);\n\t\t\n\t\tamplifyConfigured = true;\n\t\tconsole.info('[htlkg Auth] Amplify configured for server-side auth');\n\t} catch (error) {\n\t\tconst errorMsg = error instanceof Error ? error.message : 'Unknown error';\n\t\tconsole.error(`[htlkg Auth] Failed to configure Amplify: ${errorMsg}`);\n\t}\n}\n\n/**\n * Auth middleware - retrieves authenticated user and injects into locals\n * \n * This middleware runs on every request and attempts to get the current\n * authenticated user from AWS Amplify. If successful, the user is injected\n * into context.locals.user. If authentication fails, locals.user is set to null.\n * \n * @example\n * // In an Astro page\n * const { user } = Astro.locals;\n * if (user) {\n * console.log('Authenticated user:', user.email);\n * }\n */\nexport const authMiddleware: MiddlewareHandler = async (context, next) => {\n\tconst { locals } = context;\n\n\t// Ensure Amplify is configured before attempting auth\n\tensureAmplifyConfigured();\n\n\ttry {\n\t\tconst user = await getUser(context);\n\t\tlocals.user = user;\n\t} catch (error) {\n\t\t// Set user to null on any authentication error\n\t\tlocals.user = null;\n\n\t\t// Log error without exposing sensitive information\n\t\tif (error instanceof Error) {\n\t\t\t// Filter out sensitive data from error messages\n\t\t\tconst safeMessage = error.message\n\t\t\t\t.replace(/token[=:]\\s*[^\\s,}]+/gi, 'token=***')\n\t\t\t\t.replace(/key[=:]\\s*[^\\s,}]+/gi, 'key=***')\n\t\t\t\t.replace(/secret[=:]\\s*[^\\s,}]+/gi, 'secret=***')\n\t\t\t\t.replace(/password[=:]\\s*[^\\s,}]+/gi, 'password=***');\n\n\t\t\tconsole.error('[htlkg Auth] Authentication failed:', safeMessage);\n\t\t} else {\n\t\t\tconsole.error('[htlkg Auth] Authentication failed: Unknown error');\n\t\t}\n\t}\n\n\treturn next();\n};\n","/**\n * Route guard middleware for htlkg integration\n * \n * This middleware enforces access control based on route configuration:\n * - Public routes: accessible to everyone\n * - Authenticated routes: require any logged-in user\n * - Admin routes: require admin privileges\n * - Brand routes: require brand-specific access or admin privileges\n */\n\nimport type { MiddlewareHandler } from 'astro';\nimport type { RoutePattern, RouteGuardConfig } from '../htlkg/config.js';\nimport { getHtlkgConfig } from '../runtime-config.js';\n\n/**\n * Helper: Check if pathname matches any of the provided patterns\n */\nfunction matchesPattern(pathname: string, patterns: RoutePattern[]): boolean {\n\treturn patterns.some((pattern) => {\n\t\ttry {\n\t\t\tif (typeof pattern === 'string') {\n\t\t\t\t// Special case: \"/\" only matches exactly\n\t\t\t\tif (pattern === '/') {\n\t\t\t\t\treturn pathname === '/';\n\t\t\t\t}\n\t\t\t\t// String pattern: exact match or starts with (for sub-routes)\n\t\t\t\treturn pathname === pattern || pathname.startsWith(pattern + '/');\n\t\t\t}\n\t\t\t// RegExp pattern: test against pathname\n\t\t\treturn pattern.test(pathname);\n\t\t} catch (error) {\n\t\t\t// Log pattern matching errors but don't block the request\n\t\t\tconsole.error(\n\t\t\t\t'[htlkg] Error matching route pattern:',\n\t\t\t\terror instanceof Error ? error.message : 'Unknown error',\n\t\t\t);\n\t\t\treturn false;\n\t\t}\n\t});\n}\n\n/**\n * Route guard middleware - protects routes based on configuration\n * \n * This middleware runs after authMiddleware and enforces access control\n * based on the route configuration provided to the htlkg integration.\n * \n * @example\n * // In astro.config.mjs\n * htlkg({\n * auth: {\n * publicRoutes: ['/login', '/'],\n * adminRoutes: [/^\\/admin/],\n * brandRoutes: [\n * { pattern: /^\\/brands\\/(\\d+)/, brandIdParam: 1 }\n * ],\n * loginUrl: '/login'\n * }\n * })\n */\nexport const routeGuard: MiddlewareHandler = async (context, next) => {\n\tconst { locals, url, redirect } = context;\n\tconst pathname = url.pathname;\n\n\tconst { routeGuardConfig } = getHtlkgConfig();\n\tconst {\n\t\tpublicRoutes = [],\n\t\tauthenticatedRoutes = [],\n\t\tadminRoutes = [],\n\t\tbrandRoutes = [],\n\t\tloginUrl = '/login',\n\t} = routeGuardConfig || {};\n\n\ttry {\n\t\t// Public routes - no auth required\n\t\tif (matchesPattern(pathname, publicRoutes)) {\n\t\t\treturn next();\n\t\t}\n\n\t\tconst user = locals.user;\n\n\t\t// Admin routes - require admin role\n\t\tif (matchesPattern(pathname, adminRoutes)) {\n\t\t\tif (!user || !user.isAdmin) {\n\t\t\t\treturn redirect(`${loginUrl}?error=admin_required`);\n\t\t\t}\n\t\t\treturn next();\n\t\t}\n\n\t\t// Brand routes - require brand access or admin\n\t\tfor (const brandRoute of brandRoutes) {\n\t\t\ttry {\n\t\t\t\tconst match = pathname.match(brandRoute.pattern);\n\t\t\t\tif (match) {\n\t\t\t\t\tconst brandId = Number.parseInt(match[brandRoute.brandIdParam], 10);\n\n\t\t\t\t\tif (Number.isNaN(brandId)) {\n\t\t\t\t\t\tconsole.warn(`[htlkg] Invalid brandId extracted from ${pathname}`);\n\t\t\t\t\t\treturn redirect(`${loginUrl}?error=invalid_brand`);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!user || (!user.isAdmin && !user.brandIds.includes(brandId))) {\n\t\t\t\t\t\treturn redirect(`${loginUrl}?error=access_denied`);\n\t\t\t\t\t}\n\t\t\t\t\treturn next();\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[htlkg] Error processing brand route for ${pathname}:`,\n\t\t\t\t\terror instanceof Error ? error.message : 'Unknown error',\n\t\t\t\t);\n\t\t\t\t// Fail-safe: deny access on error\n\t\t\t\treturn redirect(`${loginUrl}?error=access_denied`);\n\t\t\t}\n\t\t}\n\n\t\t// Authenticated routes - require any logged-in user\n\t\tif (matchesPattern(pathname, authenticatedRoutes)) {\n\t\t\tif (!user) {\n\t\t\t\tconst returnUrl = encodeURIComponent(pathname + url.search);\n\t\t\t\treturn redirect(`${loginUrl}?redirect=${returnUrl}`);\n\t\t\t}\n\t\t\treturn next();\n\t\t}\n\n\t\t// Default: allow access\n\t\treturn next();\n\t} catch (error) {\n\t\t// Catch-all error handler for route guard\n\t\tconsole.error(\n\t\t\t'[htlkg] Unexpected error in route guard:',\n\t\t\terror instanceof Error ? error.message : 'Unknown error',\n\t\t);\n\t\t// Fail-safe: deny access and redirect to login\n\t\treturn redirect(`${loginUrl}?error=server_error`);\n\t}\n};\n\n/**\n * Helper functions for programmatic route protection in pages\n * These can be used in Astro pages for more fine-grained control\n */\n\n/**\n * Require authentication for a page\n * Redirects to login if user is not authenticated\n */\nexport async function requireAuth(context: any, loginUrl = '/login') {\n\tconst user = context.locals.user;\n\tif (!user) {\n\t\tconst currentUrl = context.url.pathname + context.url.search;\n\t\tconst encodedReturnUrl = encodeURIComponent(currentUrl);\n\t\treturn context.redirect(`${loginUrl}?redirect=${encodedReturnUrl}`);\n\t}\n\treturn user;\n}\n\n/**\n * Require admin access for a page\n * Redirects to login if user is not an admin\n */\nexport async function requireAdminAccess(context: any, loginUrl = '/login') {\n\tconst user = context.locals.user;\n\tif (!user) {\n\t\treturn context.redirect(`${loginUrl}?error=not_authenticated`);\n\t}\n\tif (!user.isAdmin) {\n\t\treturn context.redirect(`${loginUrl}?error=admin_required`);\n\t}\n\treturn user;\n}\n\n/**\n * Require brand access for a page\n * Redirects to login if user doesn't have access to the specified brand\n */\nexport async function requireBrandAccess(\n\tcontext: any,\n\tbrandId: number,\n\tloginUrl = '/login'\n) {\n\tconst user = context.locals.user;\n\tif (!user) {\n\t\treturn context.redirect(`${loginUrl}?error=not_authenticated`);\n\t}\n\tif (!user.isAdmin && !user.brandIds.includes(brandId)) {\n\t\treturn context.redirect(`${loginUrl}?error=access_denied`);\n\t}\n\treturn user;\n}\n"],"mappings":";;;;;AAUA,SAAS,gBAAgB;;;ACCzB,SAAS,eAAe;AACxB,SAAS,eAAe;AACxB,SAAS,sBAAsB;AAI/B,IAAI,oBAAoB;AAKxB,SAAS,0BAAgC;AACxC,MAAI,kBAAmB;AAEvB,MAAI;AACH,UAAM,EAAE,cAAc,IAAI,eAAe;AACzC,QAAI,CAAC,eAAe;AACnB,cAAQ,KAAK,gDAAgD;AAC7D;AAAA,IACD;AAGA,YAAQ,UAAU,eAAsB,EAAE,KAAK,KAAK,CAAC;AAGrD,mBAAe,qBAAqB;AAGpC,UAAM,QAAQ,OAAO,WAAW,cAC7B,OAAO,SAAS,aAAa,WAC7B,QAAQ,IAAI,aAAa;AAC5B,mBAAe,eAAe,KAAK;AAEnC,wBAAoB;AACpB,YAAQ,KAAK,sDAAsD;AAAA,EACpE,SAAS,OAAO;AACf,UAAM,WAAW,iBAAiB,QAAQ,MAAM,UAAU;AAC1D,YAAQ,MAAM,6CAA6C,QAAQ,EAAE;AAAA,EACtE;AACD;AAgBO,IAAM,iBAAoC,OAAO,SAAS,SA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AAM,OAAO,QAAQ,OAAO;AAC5B,MAAI,CAAC,MAAM;AACV,UAAM,aAAa,QAAQ,IAAI,WAAW,QAAQ,IAAI;AACtD,UAAM,mBAAmB,mBAAmB,UAAU;AACtD,WAAO,QAAQ,SAAS,GAAG,QAAQ,aAAa,gBAAgB,EAAE;AAAA,EACnE;AACA,SAAO;AACR;AAMA,eAAsB,mBAAmB,SAAc,WAAW,UAAU;AAC3E,QAAM,OAAO,QAAQ,OAAO;AAC5B,MAAI,CAAC,MAAM;AACV,WAAO,QAAQ,SAAS,GAAG,QAAQ,0BAA0B;AAAA,EAC9D;AACA,MAAI,CAAC,KAAK,SAAS;AAClB,WAAO,QAAQ,SAAS,GAAG,QAAQ,uBAAuB;AAAA,EAC3D;AACA,SAAO;AACR;AAMA,eAAsB,mBACrB,SACA,SACA,WAAW,UACV;AACD,QAAM,OAAO,QAAQ,OAAO;AAC5B,MAAI,CAAC,MAAM;AACV,WAAO,QAAQ,SAAS,GAAG,QAAQ,0BAA0B;AAAA,EAC9D;AACA,MAAI,CAAC,KAAK,WAAW,CAAC,KAAK,SAAS,SAAS,OAAO,GAAG;AACtD,WAAO,QAAQ,SAAS,GAAG,QAAQ,sBAAsB;AAAA,EAC1D;AACA,SAAO;AACR;;;AF/JO,IAAM,YAAY,SAAS,gBAAgB,UAAU;","names":[]}
@@ -0,0 +1,9 @@
1
+ import {
2
+ getHtlkgConfig,
3
+ setHtlkgConfig
4
+ } from "./chunk-LP4XHGBI.js";
5
+ export {
6
+ getHtlkgConfig,
7
+ setHtlkgConfig
8
+ };
9
+ //# sourceMappingURL=runtime-config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -10,6 +10,16 @@ import {
10
10
  parseListParams,
11
11
  processListData
12
12
  } from "../chunk-2GML443T.js";
13
+ import {
14
+ getClientIP,
15
+ getQueryParams,
16
+ getRequestHeaders,
17
+ getServerData,
18
+ isMobileDevice,
19
+ isServerSide,
20
+ setCacheControl,
21
+ setResponseHeaders
22
+ } from "../chunk-WNMPTDCR.js";
13
23
  import {
14
24
  chunkArray,
15
25
  filterItems,
@@ -28,16 +38,6 @@ import {
28
38
  serializeForHydration,
29
39
  shouldHydrate
30
40
  } from "../chunk-64USRLVP.js";
31
- import {
32
- getClientIP,
33
- getQueryParams,
34
- getRequestHeaders,
35
- getServerData,
36
- isMobileDevice,
37
- isServerSide,
38
- setCacheControl,
39
- setResponseHeaders
40
- } from "../chunk-WNMPTDCR.js";
41
41
  export {
42
42
  applyClientFilters,
43
43
  buildGraphQLFilter,
@@ -1,5 +1,8 @@
1
+ import {
2
+ getHtlkgConfig
3
+ } from "./chunk-LP4XHGBI.js";
4
+
1
5
  // src/vue-app-setup.ts
2
- import { amplifyConfig } from "virtual:htlkg-config";
3
6
  import { Amplify } from "aws-amplify";
4
7
  function setupVueApp(app) {
5
8
  if (import.meta.env.DEV && typeof window !== "undefined") {
@@ -12,6 +15,7 @@ function setupVueApp(app) {
12
15
  }
13
16
  }
14
17
  try {
18
+ const { amplifyConfig } = getHtlkgConfig();
15
19
  if (!amplifyConfig) {
16
20
  console.warn("[htlkg] No Amplify configuration provided");
17
21
  return;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/vue-app-setup.ts"],"sourcesContent":["/**\n * Vue App Setup for htlkg Integration\n * \n * This file is automatically loaded by the Vue integration when vueAppSetup is enabled.\n * It configures AWS Amplify for client-side authentication in all Vue components.\n * It also sets up nanostores devtools integration for Vue DevTools.\n */\n\n/// <reference types=\"vite/client\" />\n\nimport { amplifyConfig } from \"virtual:htlkg-config\";\nimport type { App } from \"vue\";\nimport type { ResourcesConfig } from \"aws-amplify\";\nimport { Amplify } from \"aws-amplify\";\n\n/**\n * Setup function called by Astro's Vue integration\n * Configures Amplify for client-side authentication and nanostores devtools\n */\nexport default function setupVueApp(app: App): void {\n\t// Setup nanostores devtools in development\n\t// The devtools plugin will automatically detect stores used in components\n\tif (import.meta.env.DEV && typeof window !== 'undefined') {\n\t\ttry {\n\t\t\t// Dynamically import devtools plugin (only in browser)\n\t\t\timport('@nanostores/vue/devtools').then(({ devtools }) => {\n\t\t\t\t// Install devtools plugin - it will detect stores automatically\n\t\t\t\tapp.use(devtools, {});\n\t\t\t}).catch(() => {\n\t\t\t\t// Silently ignore - devtools are optional\n\t\t\t});\n\t\t} catch {\n\t\t\t// Silently ignore - devtools are optional\n\t\t}\n\t}\n\t\n\t// Setup Amplify\n\ttry {\n\t\tif (!amplifyConfig) {\n\t\t\tconsole.warn(\"[htlkg] No Amplify configuration provided\");\n\t\t\treturn;\n\t\t}\n\n\t\t// Check if this is a full amplify_outputs.json config\n\t\tif (\n\t\t\t\"auth\" in amplifyConfig ||\n\t\t\t\"data\" in amplifyConfig ||\n\t\t\t\"storage\" in amplifyConfig\n\t\t) {\n\t\t\tAmplify.configure(amplifyConfig as ResourcesConfig, { ssr: true });\n\t\t} else {\n\t\t\t// Legacy individual config properties\n\t\t\tconst { userPoolId, userPoolClientId, region } = amplifyConfig as {\n\t\t\t\tuserPoolId?: string;\n\t\t\t\tuserPoolClientId?: string;\n\t\t\t\tregion?: string;\n\t\t\t};\n\n\t\t\tif (userPoolId && userPoolClientId) {\n\t\t\t\tconst config: ResourcesConfig = {\n\t\t\t\t\tAuth: {\n\t\t\t\t\t\tCognito: {\n\t\t\t\t\t\t\tuserPoolId,\n\t\t\t\t\t\t\tuserPoolClientId,\n\t\t\t\t\t\t\t...(region && { region }),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\n\t\t\t\tAmplify.configure(config, { ssr: true });\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[htlkg] Missing required Amplify configuration (userPoolId, userPoolClientId)\",\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tconsole.error(\"[htlkg] Failed to setup Vue app:\", error);\n\t\t// Don't throw - allow app to continue even if Amplify setup fails\n\t}\n}\n"],"mappings":";AAUA,SAAS,qBAAqB;AAG9B,SAAS,eAAe;AAMT,SAAR,YAA6B,KAAgB;AAGnD,MAAI,YAAY,IAAI,OAAO,OAAO,WAAW,aAAa;AACzD,QAAI;AAEH,aAAO,0BAA0B,EAAE,KAAK,CAAC,EAAE,SAAS,MAAM;AAEzD,YAAI,IAAI,UAAU,CAAC,CAAC;AAAA,MACrB,CAAC,EAAE,MAAM,MAAM;AAAA,MAEf,CAAC;AAAA,IACF,QAAQ;AAAA,IAER;AAAA,EACD;AAGA,MAAI;AACH,QAAI,CAAC,eAAe;AACnB,cAAQ,KAAK,2CAA2C;AACxD;AAAA,IACD;AAGA,QACC,UAAU,iBACV,UAAU,iBACV,aAAa,eACZ;AACD,cAAQ,UAAU,eAAkC,EAAE,KAAK,KAAK,CAAC;AAAA,IAClE,OAAO;AAEN,YAAM,EAAE,YAAY,kBAAkB,OAAO,IAAI;AAMjD,UAAI,cAAc,kBAAkB;AACnC,cAAM,SAA0B;AAAA,UAC/B,MAAM;AAAA,YACL,SAAS;AAAA,cACR;AAAA,cACA;AAAA,cACA,GAAI,UAAU,EAAE,OAAO;AAAA,YACxB;AAAA,UACD;AAAA,QACD;AAEA,gBAAQ,UAAU,QAAQ,EAAE,KAAK,KAAK,CAAC;AAAA,MACxC,OAAO;AACN,gBAAQ;AAAA,UACP;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD,SAAS,OAAO;AACf,YAAQ,MAAM,oCAAoC,KAAK;AAAA,EAExD;AACD;","names":[]}
1
+ {"version":3,"sources":["../src/vue-app-setup.ts"],"sourcesContent":["/**\n * Vue App Setup for htlkg Integration\n * \n * This file is automatically loaded by the Vue integration when vueAppSetup is enabled.\n * It configures AWS Amplify for client-side authentication in all Vue components.\n * It also sets up nanostores devtools integration for Vue DevTools.\n */\n\n/// <reference types=\"vite/client\" />\n\nimport type { App } from \"vue\";\nimport type { ResourcesConfig } from \"aws-amplify\";\nimport { Amplify } from \"aws-amplify\";\nimport { getHtlkgConfig } from \"./runtime-config.js\";\n\n/**\n * Setup function called by Astro's Vue integration\n * Configures Amplify for client-side authentication and nanostores devtools\n */\nexport default function setupVueApp(app: App): void {\n\t// Setup nanostores devtools in development\n\t// The devtools plugin will automatically detect stores used in components\n\tif (import.meta.env.DEV && typeof window !== 'undefined') {\n\t\ttry {\n\t\t\t// Dynamically import devtools plugin (only in browser)\n\t\t\timport('@nanostores/vue/devtools').then(({ devtools }) => {\n\t\t\t\t// Install devtools plugin - it will detect stores automatically\n\t\t\t\tapp.use(devtools, {});\n\t\t\t}).catch(() => {\n\t\t\t\t// Silently ignore - devtools are optional\n\t\t\t});\n\t\t} catch {\n\t\t\t// Silently ignore - devtools are optional\n\t\t}\n\t}\n\t\n\t// Setup Amplify\n\ttry {\n\t\tconst { amplifyConfig } = getHtlkgConfig();\n\t\tif (!amplifyConfig) {\n\t\t\tconsole.warn(\"[htlkg] No Amplify configuration provided\");\n\t\t\treturn;\n\t\t}\n\n\t\t// Check if this is a full amplify_outputs.json config\n\t\tif (\n\t\t\t\"auth\" in amplifyConfig ||\n\t\t\t\"data\" in amplifyConfig ||\n\t\t\t\"storage\" in amplifyConfig\n\t\t) {\n\t\t\tAmplify.configure(amplifyConfig as ResourcesConfig, { ssr: true });\n\t\t} else {\n\t\t\t// Legacy individual config properties\n\t\t\tconst { userPoolId, userPoolClientId, region } = amplifyConfig as {\n\t\t\t\tuserPoolId?: string;\n\t\t\t\tuserPoolClientId?: string;\n\t\t\t\tregion?: string;\n\t\t\t};\n\n\t\t\tif (userPoolId && userPoolClientId) {\n\t\t\t\tconst config: ResourcesConfig = {\n\t\t\t\t\tAuth: {\n\t\t\t\t\t\tCognito: {\n\t\t\t\t\t\t\tuserPoolId,\n\t\t\t\t\t\t\tuserPoolClientId,\n\t\t\t\t\t\t\t...(region && { region }),\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t};\n\n\t\t\t\tAmplify.configure(config, { ssr: true });\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[htlkg] Missing required Amplify configuration (userPoolId, userPoolClientId)\",\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tconsole.error(\"[htlkg] Failed to setup Vue app:\", error);\n\t\t// Don't throw - allow app to continue even if Amplify setup fails\n\t}\n}\n"],"mappings":";;;;;AAYA,SAAS,eAAe;AAOT,SAAR,YAA6B,KAAgB;AAGnD,MAAI,YAAY,IAAI,OAAO,OAAO,WAAW,aAAa;AACzD,QAAI;AAEH,aAAO,0BAA0B,EAAE,KAAK,CAAC,EAAE,SAAS,MAAM;AAEzD,YAAI,IAAI,UAAU,CAAC,CAAC;AAAA,MACrB,CAAC,EAAE,MAAM,MAAM;AAAA,MAEf,CAAC;AAAA,IACF,QAAQ;AAAA,IAER;AAAA,EACD;AAGA,MAAI;AACH,UAAM,EAAE,cAAc,IAAI,eAAe;AACzC,QAAI,CAAC,eAAe;AACnB,cAAQ,KAAK,2CAA2C;AACxD;AAAA,IACD;AAGA,QACC,UAAU,iBACV,UAAU,iBACV,aAAa,eACZ;AACD,cAAQ,UAAU,eAAkC,EAAE,KAAK,KAAK,CAAC;AAAA,IAClE,OAAO;AAEN,YAAM,EAAE,YAAY,kBAAkB,OAAO,IAAI;AAMjD,UAAI,cAAc,kBAAkB;AACnC,cAAM,SAA0B;AAAA,UAC/B,MAAM;AAAA,YACL,SAAS;AAAA,cACR;AAAA,cACA;AAAA,cACA,GAAI,UAAU,EAAE,OAAO;AAAA,YACxB;AAAA,UACD;AAAA,QACD;AAEA,gBAAQ,UAAU,QAAQ,EAAE,KAAK,KAAK,CAAC;AAAA,MACxC,OAAO;AACN,gBAAQ;AAAA,UACP;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,EACD,SAAS,OAAO;AACf,YAAQ,MAAM,oCAAoC,KAAK;AAAA,EAExD;AACD;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htlkg/astro",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -54,20 +54,10 @@
54
54
  "dist",
55
55
  "src"
56
56
  ],
57
- "scripts": {
58
- "build": "tsup",
59
- "dev": "tsup --watch",
60
- "test": "vitest run",
61
- "test:watch": "vitest",
62
- "prepublishOnly": "pnpm build",
63
- "version:patch": "pnpm version patch --no-git-tag-version",
64
- "version:minor": "pnpm version minor --no-git-tag-version",
65
- "version:major": "pnpm version major --no-git-tag-version"
66
- },
67
57
  "dependencies": {
68
- "@htlkg/core": "^0.0.9",
69
- "@htlkg/components": "^0.0.9",
70
- "@hotelinking/ui": "^16.49.16"
58
+ "@hotelinking/ui": "^16.49.16",
59
+ "@htlkg/components": "0.0.9",
60
+ "@htlkg/core": "0.0.9"
71
61
  },
72
62
  "peerDependencies": {
73
63
  "@astrojs/tailwind": "^6.0.0",
@@ -101,5 +91,14 @@
101
91
  },
102
92
  "publishConfig": {
103
93
  "access": "public"
94
+ },
95
+ "scripts": {
96
+ "build": "tsup",
97
+ "dev": "tsup --watch",
98
+ "test": "vitest run",
99
+ "test:watch": "vitest",
100
+ "version:patch": "pnpm version patch --no-git-tag-version",
101
+ "version:minor": "pnpm version minor --no-git-tag-version",
102
+ "version:major": "pnpm version major --no-git-tag-version"
104
103
  }
105
- }
104
+ }
@@ -16,18 +16,12 @@
16
16
 
17
17
  import DefaultLayout from "../layouts/DefaultLayout.astro";
18
18
  import LoginForm from "./LoginForm.vue";
19
+ import { getHtlkgConfig } from "../runtime-config.js";
19
20
 
20
21
  export const prerender = false;
21
22
 
22
- // Lazy load configuration to avoid build-time evaluation
23
- let loginPageConfig;
24
- try {
25
- const config = await import("virtual:htlkg-config");
26
- loginPageConfig = config.loginPageConfig;
27
- } catch (e) {
28
- // During build analysis, virtual modules may not be available
29
- loginPageConfig = null;
30
- }
23
+ // Get login page configuration from runtime config
24
+ const { loginPageConfig } = getHtlkgConfig();
31
25
 
32
26
  const {
33
27
  logo = "https://images.hotelinking.com/ui/WiFiBot.svg",
@@ -32,32 +32,31 @@ export default defineConfig({
32
32
  - Route guard configuration
33
33
  - Login page injection
34
34
  - Brand route handling
35
- - Virtual module configuration
35
+ - Runtime configuration
36
36
 
37
37
  ## Options
38
38
 
39
39
  ```typescript
40
40
  interface HtlkgIntegrationOptions {
41
41
  auth?: {
42
- enabled: boolean;
43
- loginPage?: string;
44
42
  publicRoutes?: RoutePattern[];
45
- protectedRoutes?: RoutePattern[];
46
- };
47
- brandRoutes?: {
48
- enabled: boolean;
49
- pattern?: string;
43
+ authenticatedRoutes?: RoutePattern[];
44
+ adminRoutes?: RoutePattern[];
45
+ brandRoutes?: BrandRouteConfig[];
46
+ loginUrl?: string;
50
47
  };
48
+ loginPage?: {
49
+ path?: string;
50
+ logo?: string;
51
+ title?: string;
52
+ redirectUrl?: string;
53
+ } | false;
54
+ amplify?: Record<string, unknown>;
55
+ vueAppSetup?: 'auto' | 'basic' | 'full';
51
56
  }
52
57
  ```
53
58
 
54
- ## Virtual Modules
55
-
56
- The integration provides virtual modules for configuration:
59
+ ## Configuration
57
60
 
58
- ```typescript
59
- // Access config in your code
60
- import config from 'virtual:htlkg-config';
61
-
62
- console.log(config.auth.loginPage);
63
- ```
61
+ Configuration is passed to the integration and made available to middleware
62
+ and pages at runtime via the internal `runtime-config` module.
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * htlkg Astro Integration
3
- *
3
+ *
4
4
  * Supports static, hybrid, and full SSR output modes.
5
5
  */
6
6
 
@@ -8,7 +8,7 @@ import tailwind from '@astrojs/tailwind';
8
8
  import vue from '@astrojs/vue';
9
9
  import type { AstroIntegration } from 'astro';
10
10
  import type { HtlkgIntegrationOptions } from './config.js';
11
- import { createVirtualModulePlugin, virtualModuleTypes } from './virtual-modules.js';
11
+ import { setHtlkgConfig } from '../runtime-config.js';
12
12
 
13
13
  const DEFAULT_ENV_VARS = [
14
14
  'PUBLIC_COGNITO_USER_POOL_ID',
@@ -74,24 +74,25 @@ export function htlkg(
74
74
  }
75
75
  }
76
76
 
77
- // Create Vite virtual module plugin
78
- const virtualModulePlugin = createVirtualModulePlugin(auth, loginPage, amplify || null);
79
- const vitePlugins: any[] = [virtualModulePlugin];
77
+ // Set runtime configuration (replaces virtual module approach)
78
+ setHtlkgConfig({
79
+ routeGuard: auth,
80
+ loginPage: loginPage || null,
81
+ amplify: amplify || null,
82
+ });
80
83
 
81
84
  // Only load Vue DevTools during development (dynamic import to avoid bundling in production)
82
85
  const isDev = process.env.NODE_ENV !== 'production';
83
86
  if (isDev) {
84
87
  try {
85
88
  const vueDevTools = (await import('vite-plugin-vue-devtools')).default;
86
- vitePlugins.push(vueDevTools());
89
+ updateConfig({ vite: { plugins: [vueDevTools()] } });
87
90
  logger.info('Vue DevTools enabled');
88
91
  } catch {
89
92
  // DevTools not available, skip silently
90
93
  }
91
94
  }
92
95
 
93
- updateConfig({ vite: { plugins: vitePlugins } });
94
-
95
96
  // Inject middleware (only for full setup)
96
97
  if (useFullVueSetup) {
97
98
  addMiddleware({ entrypoint: '@htlkg/astro/middleware', order: 'pre' });
@@ -130,8 +131,6 @@ declare global {
130
131
  }
131
132
  }
132
133
 
133
- ${virtualModuleTypes}
134
-
135
134
  export {};
136
135
  `,
137
136
  });
@@ -12,7 +12,7 @@ import type { MiddlewareHandler } from 'astro';
12
12
  import { Amplify } from 'aws-amplify';
13
13
  import { getUser } from '@htlkg/core/auth';
14
14
  import { globalSettings } from '@htlkg/core/amplify-astro-adapter';
15
- import { amplifyConfig } from 'virtual:htlkg-config';
15
+ import { getHtlkgConfig } from '../runtime-config.js';
16
16
 
17
17
  // Track if Amplify has been configured
18
18
  let amplifyConfigured = false;
@@ -24,6 +24,7 @@ function ensureAmplifyConfigured(): void {
24
24
  if (amplifyConfigured) return;
25
25
 
26
26
  try {
27
+ const { amplifyConfig } = getHtlkgConfig();
27
28
  if (!amplifyConfig) {
28
29
  console.warn('[htlkg Auth] No Amplify configuration provided');
29
30
  return;
@@ -1,18 +1,12 @@
1
1
  /**
2
2
  * Unit tests for route guard helper functions
3
3
  *
4
- * Note: The routeGuard middleware itself cannot be tested in isolation because it
5
- * depends on the virtual:htlkg-config module which is generated at build time by Vite.
6
- * These tests focus on the exported helper functions that don't depend on the virtual module.
4
+ * These tests focus on the exported helper functions logic.
5
+ * The tests recreate the function logic to ensure isolation.
7
6
  */
8
7
 
9
8
  import { describe, it, expect, vi } from 'vitest';
10
9
 
11
- // Since the module imports from 'virtual:htlkg-config' at the top level,
12
- // we need to test the helper functions by recreating their logic here.
13
- // The actual helper functions (requireAuth, requireAdminAccess, requireBrandAccess)
14
- // have their implementation logic tested below.
15
-
16
10
  describe('Route Guard Helper Functions Logic', () => {
17
11
  /**
18
12
  * requireAuth implementation logic test
@@ -10,12 +10,7 @@
10
10
 
11
11
  import type { MiddlewareHandler } from 'astro';
12
12
  import type { RoutePattern, RouteGuardConfig } from '../htlkg/config.js';
13
-
14
- // Import configuration from virtual module
15
- import { routeGuardConfig } from 'virtual:htlkg-config';
16
-
17
- // Type assertion for the imported config
18
- const config = routeGuardConfig as RouteGuardConfig;
13
+ import { getHtlkgConfig } from '../runtime-config.js';
19
14
 
20
15
  /**
21
16
  * Helper: Check if pathname matches any of the provided patterns
@@ -67,13 +62,14 @@ export const routeGuard: MiddlewareHandler = async (context, next) => {
67
62
  const { locals, url, redirect } = context;
68
63
  const pathname = url.pathname;
69
64
 
65
+ const { routeGuardConfig } = getHtlkgConfig();
70
66
  const {
71
67
  publicRoutes = [],
72
68
  authenticatedRoutes = [],
73
69
  adminRoutes = [],
74
70
  brandRoutes = [],
75
71
  loginUrl = '/login',
76
- } = config;
72
+ } = routeGuardConfig || {};
77
73
 
78
74
  try {
79
75
  // Public routes - no auth required
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Runtime Configuration for @htlkg/astro
3
+ *
4
+ * This module stores configuration set during Astro's config setup phase
5
+ * and makes it available to middleware and pages at request time.
6
+ *
7
+ * This replaces the virtual:htlkg-config virtual module approach,
8
+ * which required bundling to resolve virtual imports.
9
+ *
10
+ * @internal This module is internal to @htlkg/astro
11
+ */
12
+
13
+ import type { RouteGuardConfig, LoginPageConfig } from './htlkg/config.js';
14
+
15
+ // Module-level storage (persists across requests in the same Node.js process)
16
+ let _routeGuardConfig: RouteGuardConfig | null = null;
17
+ let _loginPageConfig: LoginPageConfig | null = null;
18
+ let _amplifyConfig: Record<string, unknown> | null = null;
19
+
20
+ /**
21
+ * Set the runtime configuration
22
+ * Called by the htlkg integration during astro:config:setup
23
+ */
24
+ export function setHtlkgConfig(config: {
25
+ routeGuard: RouteGuardConfig;
26
+ loginPage: LoginPageConfig | null;
27
+ amplify: Record<string, unknown> | null;
28
+ }): void {
29
+ _routeGuardConfig = config.routeGuard;
30
+ _loginPageConfig = config.loginPage;
31
+ _amplifyConfig = config.amplify;
32
+ }
33
+
34
+ /**
35
+ * Get the runtime configuration
36
+ * Called by middleware and pages at request time
37
+ */
38
+ export function getHtlkgConfig(): {
39
+ routeGuardConfig: RouteGuardConfig | null;
40
+ loginPageConfig: LoginPageConfig | null;
41
+ amplifyConfig: Record<string, unknown> | null;
42
+ } {
43
+ return {
44
+ routeGuardConfig: _routeGuardConfig,
45
+ loginPageConfig: _loginPageConfig,
46
+ amplifyConfig: _amplifyConfig,
47
+ };
48
+ }
@@ -8,10 +8,10 @@
8
8
 
9
9
  /// <reference types="vite/client" />
10
10
 
11
- import { amplifyConfig } from "virtual:htlkg-config";
12
11
  import type { App } from "vue";
13
12
  import type { ResourcesConfig } from "aws-amplify";
14
13
  import { Amplify } from "aws-amplify";
14
+ import { getHtlkgConfig } from "./runtime-config.js";
15
15
 
16
16
  /**
17
17
  * Setup function called by Astro's Vue integration
@@ -36,6 +36,7 @@ export default function setupVueApp(app: App): void {
36
36
 
37
37
  // Setup Amplify
38
38
  try {
39
+ const { amplifyConfig } = getHtlkgConfig();
39
40
  if (!amplifyConfig) {
40
41
  console.warn("[htlkg] No Amplify configuration provided");
41
42
  return;
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/htlkg/index.ts","../src/htlkg/virtual-modules.ts"],"sourcesContent":["/**\n * htlkg Astro Integration\n * \n * Supports static, hybrid, and full SSR output modes.\n */\n\nimport tailwind from '@astrojs/tailwind';\nimport vue from '@astrojs/vue';\nimport type { AstroIntegration } from 'astro';\nimport type { HtlkgIntegrationOptions } from './config.js';\nimport { createVirtualModulePlugin, virtualModuleTypes } from './virtual-modules.js';\n\nconst DEFAULT_ENV_VARS = [\n\t'PUBLIC_COGNITO_USER_POOL_ID',\n\t'PUBLIC_COGNITO_USER_POOL_CLIENT_ID'\n];\n\nexport function htlkg(\n\toptions: HtlkgIntegrationOptions = {},\n): AstroIntegration | AstroIntegration[] {\n\tconst {\n\t\tauth = {},\n\t\tloginPage = { path: '/login', title: 'Sign In', redirectUrl: '/admin' },\n\t\tvalidateEnv = true,\n\t\trequiredEnvVars = DEFAULT_ENV_VARS,\n\t\ttailwind: tailwindOptions,\n\t\tamplify,\n\t\tvueAppSetup = 'auto',\n\t} = options;\n\n\tconst integrations: AstroIntegration[] = [];\n\n\t// Add Tailwind integration (enabled by default)\n\tif (tailwindOptions !== false) {\n\t\tconst tailwindConfig =\n\t\t\ttypeof tailwindOptions === 'object' ? tailwindOptions : undefined;\n\t\tintegrations.push(\n\t\t\ttailwind(tailwindConfig as Parameters<typeof tailwind>[0]),\n\t\t);\n\t}\n\n\t// Determine Vue setup mode:\n\t// - 'full': Use Amplify app entrypoint (requires SSR)\n\t// - 'basic': Basic Vue without app entrypoint (works with static)\n\t// - 'auto': Default to 'basic' for compatibility with static builds\n\tconst useFullVueSetup = vueAppSetup === 'full';\n\n\t// Add Vue integration\n\tif (useFullVueSetup) {\n\t\tintegrations.push(vue({ appEntrypoint: '@htlkg/astro/vue-app-setup' }));\n\t} else {\n\t\tintegrations.push(vue());\n\t}\n\n\t// Add the main htlkg integration\n\tintegrations.push({\n\t\tname: '@htlkg/astro',\n\t\thooks: {\n\t\t\t'astro:config:setup': async ({ updateConfig, addMiddleware, injectRoute, logger }) => {\n\t\t\t\ttry {\n\t\t\t\t\tlogger.info(useFullVueSetup \n\t\t\t\t\t\t? 'Vue configured with Amplify app setup' \n\t\t\t\t\t\t: 'Vue configured (basic mode)');\n\n\t\t\t\t\tif (amplify) {\n\t\t\t\t\t\tlogger.info('Amplify configuration provided');\n\t\t\t\t\t}\n\n\t\t\t\t\t// Validate env vars (only for full setup)\n\t\t\t\t\tif (validateEnv && !amplify && useFullVueSetup) {\n\t\t\t\t\t\tconst missing = requiredEnvVars.filter(v => !process.env[v]);\n\t\t\t\t\t\tif (missing.length > 0) {\n\t\t\t\t\t\t\tlogger.warn(`Missing env vars: ${missing.join(', ')}`);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Create Vite virtual module plugin\n\t\t\t\t\tconst virtualModulePlugin = createVirtualModulePlugin(auth, loginPage, amplify || null);\n\t\t\t\t\tconst vitePlugins: any[] = [virtualModulePlugin];\n\n\t\t\t\t\t// Only load Vue DevTools during development (dynamic import to avoid bundling in production)\n\t\t\t\t\tconst isDev = process.env.NODE_ENV !== 'production';\n\t\t\t\t\tif (isDev) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst vueDevTools = (await import('vite-plugin-vue-devtools')).default;\n\t\t\t\t\t\t\tvitePlugins.push(vueDevTools());\n\t\t\t\t\t\t\tlogger.info('Vue DevTools enabled');\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// DevTools not available, skip silently\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tupdateConfig({ vite: { plugins: vitePlugins } });\n\n\t\t\t\t\t// Inject middleware (only for full setup)\n\t\t\t\t\tif (useFullVueSetup) {\n\t\t\t\t\t\taddMiddleware({ entrypoint: '@htlkg/astro/middleware', order: 'pre' });\n\t\t\t\t\t\tlogger.info('Authentication middleware injected');\n\t\t\t\t\t}\n\n\t\t\t\t\t// Inject login page (only for full setup)\n\t\t\t\t\tif (loginPage !== false && useFullVueSetup) {\n\t\t\t\t\t\tconst loginPath = loginPage.path || '/login';\n\t\t\t\t\t\tinjectRoute({\n\t\t\t\t\t\t\tpattern: loginPath,\n\t\t\t\t\t\t\tentrypoint: '@htlkg/astro/auth/LoginPage.astro',\n\t\t\t\t\t\t\tprerender: false,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tlogger.info(`Login page injected at ${loginPath}`);\n\t\t\t\t\t}\n\n\t\t\t\t\tlogger.info('htlkg integration configured');\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst msg = error instanceof Error ? error.message : 'Unknown error';\n\t\t\t\t\tlogger.error(`htlkg configuration failed: ${msg}`);\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t\t'astro:config:done': ({ injectTypes }) => {\n\t\t\t\tinjectTypes({\n\t\t\t\t\tfilename: 'htlkg.d.ts',\n\t\t\t\t\tcontent: `\nimport type { AuthUser } from '@htlkg/core/types';\n\ndeclare global {\n namespace App {\n interface Locals {\n user: AuthUser | null;\n }\n }\n}\n\n${virtualModuleTypes}\n\nexport {};\n`,\n\t\t\t\t});\n\t\t\t},\n\t\t},\n\t});\n\n\treturn integrations;\n}\n","/**\n * Virtual module setup for htlkg integration\n * Creates Vite virtual modules to pass configuration to middleware and pages\n */\n\nimport type { RouteGuardConfig, LoginPageConfig, RoutePattern } from './config.js';\n\n/**\n * Serialize a route pattern (RegExp or string) to JavaScript code\n */\nfunction serializePattern(pattern: RegExp | string): string {\n\tif (pattern instanceof RegExp) {\n\t\treturn `new RegExp(${JSON.stringify(pattern.source)}, ${JSON.stringify(pattern.flags)})`;\n\t}\n\treturn JSON.stringify(pattern);\n}\n\n/**\n * Serialize an array of route patterns to JavaScript code\n */\nfunction serializePatterns(patterns: RoutePattern[]): string {\n\tif (!patterns || patterns.length === 0) return '[]';\n\treturn `[${patterns.map(serializePattern).join(', ')}]`;\n}\n\n/**\n * Create the virtual module plugin for Vite\n * This plugin provides configuration to middleware and pages at runtime\n */\nexport function createVirtualModulePlugin(\n\tauthConfig: RouteGuardConfig,\n\tloginPageConfig: LoginPageConfig | false | null,\n\tamplifyConfig: Record<string, unknown> | null\n) {\n\tconst virtualModuleId = 'virtual:htlkg-config';\n\tconst resolvedVirtualModuleId = '\\0' + virtualModuleId;\n\n\treturn {\n\t\tname: 'htlkg-config',\n\t\tenforce: 'pre' as const,\n\t\tresolveId(id: string) {\n\t\t\tif (id === virtualModuleId) {\n\t\t\t\treturn resolvedVirtualModuleId;\n\t\t\t}\n\t\t},\n\t\tload(id: string) {\n\t\t\tif (id === resolvedVirtualModuleId) {\n\t\t\t\t// Serialize auth configuration with RegExp support\n\t\t\t\tconst serializedAuthConfig = `{\n\t\t\t\t\tpublicRoutes: ${serializePatterns(authConfig.publicRoutes || [])},\n\t\t\t\t\tauthenticatedRoutes: ${serializePatterns(authConfig.authenticatedRoutes || [])},\n\t\t\t\t\tadminRoutes: ${serializePatterns(authConfig.adminRoutes || [])},\n\t\t\t\t\tbrandRoutes: ${JSON.stringify(authConfig.brandRoutes || [])},\n\t\t\t\t\tloginUrl: ${JSON.stringify(authConfig.loginUrl || '/login')}\n\t\t\t\t}`;\n\n\t\t\t\tconst serializedAmplifyConfig = amplifyConfig\n\t\t\t\t\t? JSON.stringify(amplifyConfig)\n\t\t\t\t\t: 'null';\n\n\t\t\t\tconst serializedLoginPageConfig = loginPageConfig !== false && loginPageConfig !== null\n\t\t\t\t\t? JSON.stringify(loginPageConfig)\n\t\t\t\t\t: 'null';\n\n\t\t\t\treturn `export const routeGuardConfig = ${serializedAuthConfig};\nexport const loginPageConfig = ${serializedLoginPageConfig};\nexport const amplifyConfig = ${serializedAmplifyConfig};`;\n\t\t\t}\n\t\t},\n\t};\n}\n\n/**\n * Type definitions for the virtual module\n * This should be injected into the project's type definitions\n */\nexport const virtualModuleTypes = `\ndeclare module 'virtual:htlkg-config' {\n import type { RouteGuardConfig, LoginPageConfig } from '@htlkg/astro/htlkg/config';\n \n export const routeGuardConfig: RouteGuardConfig;\n export const loginPageConfig: LoginPageConfig | null;\n export const amplifyConfig: Record<string, unknown> | null;\n}\n`;\n"],"mappings":";AAMA,OAAO,cAAc;AACrB,OAAO,SAAS;;;ACGhB,SAAS,iBAAiB,SAAkC;AAC3D,MAAI,mBAAmB,QAAQ;AAC9B,WAAO,cAAc,KAAK,UAAU,QAAQ,MAAM,CAAC,KAAK,KAAK,UAAU,QAAQ,KAAK,CAAC;AAAA,EACtF;AACA,SAAO,KAAK,UAAU,OAAO;AAC9B;AAKA,SAAS,kBAAkB,UAAkC;AAC5D,MAAI,CAAC,YAAY,SAAS,WAAW,EAAG,QAAO;AAC/C,SAAO,IAAI,SAAS,IAAI,gBAAgB,EAAE,KAAK,IAAI,CAAC;AACrD;AAMO,SAAS,0BACf,YACA,iBACA,eACC;AACD,QAAM,kBAAkB;AACxB,QAAM,0BAA0B,OAAO;AAEvC,SAAO;AAAA,IACN,MAAM;AAAA,IACN,SAAS;AAAA,IACT,UAAU,IAAY;AACrB,UAAI,OAAO,iBAAiB;AAC3B,eAAO;AAAA,MACR;AAAA,IACD;AAAA,IACA,KAAK,IAAY;AAChB,UAAI,OAAO,yBAAyB;AAEnC,cAAM,uBAAuB;AAAA,qBACZ,kBAAkB,WAAW,gBAAgB,CAAC,CAAC,CAAC;AAAA,4BACzC,kBAAkB,WAAW,uBAAuB,CAAC,CAAC,CAAC;AAAA,oBAC/D,kBAAkB,WAAW,eAAe,CAAC,CAAC,CAAC;AAAA,oBAC/C,KAAK,UAAU,WAAW,eAAe,CAAC,CAAC,CAAC;AAAA,iBAC/C,KAAK,UAAU,WAAW,YAAY,QAAQ,CAAC;AAAA;AAG5D,cAAM,0BAA0B,gBAC7B,KAAK,UAAU,aAAa,IAC5B;AAEH,cAAM,4BAA4B,oBAAoB,SAAS,oBAAoB,OAChF,KAAK,UAAU,eAAe,IAC9B;AAEH,eAAO,mCAAmC,oBAAoB;AAAA,iCACjC,yBAAyB;AAAA,+BAC3B,uBAAuB;AA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AAM,cAAqB,CAAC,mBAAmB;AAG/C,gBAAM,QAAQ,QAAQ,IAAI,aAAa;AACvC,cAAI,OAAO;AACV,gBAAI;AACH,oBAAM,eAAe,MAAM,OAAO,0BAA0B,GAAG;AAC/D,0BAAY,KAAK,YAAY,CAAC;AAC9B,qBAAO,KAAK,sBAAsB;AAAA,YACnC,QAAQ;AAAA,YAER;AAAA,UACD;AAEA,uBAAa,EAAE,MAAM,EAAE,SAAS,YAAY,EAAE,CAAC;AAG/C,cAAI,iBAAiB;AACpB,0BAAc,EAAE,YAAY,2BAA2B,OAAO,MAAM,CAAC;AACrE,mBAAO,KAAK,oCAAoC;AAAA,UACjD;AAGA,cAAI,cAAc,SAAS,iBAAiB;AAC3C,kBAAM,YAAY,UAAU,QAAQ;AACpC,wBAAY;AAAA,cACX,SAAS;AAAA,cACT,YAAY;AAAA,cACZ,WAAW;AAAA,YACZ,CAAC;AACD,mBAAO,KAAK,0BAA0B,SAAS,EAAE;AAAA,UAClD;AAEA,iBAAO,KAAK,8BAA8B;AAAA,QAC3C,SAAS,OAAO;AACf,gBAAM,MAAM,iBAAiB,QAAQ,MAAM,UAAU;AACrD,iBAAO,MAAM,+BAA+B,GAAG,EAAE;AACjD,gBAAM;AAAA,QACP;AAAA,MACD;AAAA,MACA,qBAAqB,CAAC,EAAE,YAAY,MAAM;AACzC,oBAAY;AAAA,UACX,UAAU;AAAA,UACV,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWZ,kBAAkB;AAAA;AAAA;AAAA;AAAA,QAIhB,CAAC;AAAA,MACF;AAAA,IACD;AAAA,EACD,CAAC;AAED,SAAO;AACR;","names":[]}
@@ -1,14 +0,0 @@
1
- /**
2
- * Mock for virtual:htlkg-config module used in tests
3
- */
4
-
5
- export const routeGuardConfig = {
6
- publicRoutes: [],
7
- authenticatedRoutes: [],
8
- adminRoutes: [],
9
- brandRoutes: [],
10
- loginUrl: '/login',
11
- };
12
-
13
- export const loginPageConfig = null;
14
- export const amplifyConfig = null;
@@ -1,158 +0,0 @@
1
- /**
2
- * Unit tests for virtual module setup
3
- */
4
-
5
- import { describe, it, expect } from 'vitest';
6
- import { createVirtualModulePlugin } from './virtual-modules.js';
7
-
8
- describe('Virtual Module Plugin', () => {
9
- it('should create a valid Vite plugin', () => {
10
- const authConfig = {
11
- publicRoutes: ['/login', '/'],
12
- authenticatedRoutes: ['/dashboard'],
13
- adminRoutes: [/^\/admin/],
14
- brandRoutes: [],
15
- loginUrl: '/login',
16
- };
17
-
18
- const plugin = createVirtualModulePlugin(authConfig, null, null);
19
-
20
- expect(plugin).toBeDefined();
21
- expect(plugin.name).toBe('htlkg-config');
22
- expect(typeof plugin.resolveId).toBe('function');
23
- expect(typeof plugin.load).toBe('function');
24
- });
25
-
26
- it('should resolve virtual module ID', () => {
27
- const plugin = createVirtualModulePlugin({}, null, null);
28
-
29
- const resolved = plugin.resolveId('virtual:htlkg-config');
30
- expect(resolved).toBe('\0virtual:htlkg-config');
31
- });
32
-
33
- it('should not resolve non-virtual module IDs', () => {
34
- const plugin = createVirtualModulePlugin({}, null, null);
35
-
36
- const resolved = plugin.resolveId('some-other-module');
37
- expect(resolved).toBeUndefined();
38
- });
39
-
40
- it('should generate module code with string patterns', () => {
41
- const authConfig = {
42
- publicRoutes: ['/login', '/register'],
43
- authenticatedRoutes: ['/dashboard'],
44
- adminRoutes: [],
45
- brandRoutes: [],
46
- loginUrl: '/login',
47
- };
48
-
49
- const plugin = createVirtualModulePlugin(authConfig, null, null);
50
- const code = plugin.load('\0virtual:htlkg-config');
51
-
52
- expect(code).toContain('publicRoutes: ["/login", "/register"]');
53
- expect(code).toContain('authenticatedRoutes: ["/dashboard"]');
54
- expect(code).toContain('loginUrl: "/login"');
55
- });
56
-
57
- it('should generate module code with RegExp patterns', () => {
58
- const authConfig = {
59
- publicRoutes: [],
60
- authenticatedRoutes: [],
61
- adminRoutes: [/^\/admin/],
62
- brandRoutes: [],
63
- loginUrl: '/login',
64
- };
65
-
66
- const plugin = createVirtualModulePlugin(authConfig, null, null);
67
- const code = plugin.load('\0virtual:htlkg-config');
68
-
69
- expect(code).toContain('new RegExp');
70
- // The pattern is serialized as a string, so we check for the pattern source
71
- expect(code).toContain('adminRoutes: [new RegExp(');
72
- });
73
-
74
- it('should generate module code with mixed patterns', () => {
75
- const authConfig = {
76
- publicRoutes: ['/login', /^\/public/],
77
- authenticatedRoutes: ['/dashboard', /^\/app/],
78
- adminRoutes: [],
79
- brandRoutes: [],
80
- loginUrl: '/login',
81
- };
82
-
83
- const plugin = createVirtualModulePlugin(authConfig, null, null);
84
- const code = plugin.load('\0virtual:htlkg-config');
85
-
86
- expect(code).toContain('"/login"');
87
- expect(code).toContain('new RegExp');
88
- expect(code).toContain('"/dashboard"');
89
- });
90
-
91
- it('should handle empty route arrays', () => {
92
- const authConfig = {
93
- publicRoutes: [],
94
- authenticatedRoutes: [],
95
- adminRoutes: [],
96
- brandRoutes: [],
97
- loginUrl: '/login',
98
- };
99
-
100
- const plugin = createVirtualModulePlugin(authConfig, null, null);
101
- const code = plugin.load('\0virtual:htlkg-config');
102
-
103
- expect(code).toContain('publicRoutes: []');
104
- expect(code).toContain('authenticatedRoutes: []');
105
- expect(code).toContain('adminRoutes: []');
106
- });
107
-
108
- it('should serialize amplify config when provided', () => {
109
- const amplifyConfig = {
110
- userPoolId: 'us-east-1_abc123',
111
- userPoolClientId: 'abc123def456',
112
- region: 'us-east-1',
113
- };
114
-
115
- const plugin = createVirtualModulePlugin({}, null, amplifyConfig);
116
- const code = plugin.load('\0virtual:htlkg-config');
117
-
118
- expect(code).toContain('amplifyConfig');
119
- expect(code).toContain('userPoolId');
120
- expect(code).toContain('us-east-1_abc123');
121
- });
122
-
123
- it('should set amplify config to null when not provided', () => {
124
- const plugin = createVirtualModulePlugin({}, null, null);
125
- const code = plugin.load('\0virtual:htlkg-config');
126
-
127
- expect(code).toContain('amplifyConfig = null');
128
- });
129
-
130
- it('should serialize login page config when provided', () => {
131
- const loginPageConfig = {
132
- path: '/custom-login',
133
- title: 'Custom Login',
134
- redirectUrl: '/dashboard',
135
- };
136
-
137
- const plugin = createVirtualModulePlugin({}, loginPageConfig, null);
138
- const code = plugin.load('\0virtual:htlkg-config');
139
-
140
- expect(code).toContain('loginPageConfig');
141
- expect(code).toContain('custom-login');
142
- expect(code).toContain('Custom Login');
143
- });
144
-
145
- it('should set login page config to null when disabled', () => {
146
- const plugin = createVirtualModulePlugin({}, false, null);
147
- const code = plugin.load('\0virtual:htlkg-config');
148
-
149
- expect(code).toContain('loginPageConfig = null');
150
- });
151
-
152
- it('should not load non-virtual module IDs', () => {
153
- const plugin = createVirtualModulePlugin({}, null, null);
154
- const code = plugin.load('some-other-module');
155
-
156
- expect(code).toBeUndefined();
157
- });
158
- });
@@ -1,85 +0,0 @@
1
- /**
2
- * Virtual module setup for htlkg integration
3
- * Creates Vite virtual modules to pass configuration to middleware and pages
4
- */
5
-
6
- import type { RouteGuardConfig, LoginPageConfig, RoutePattern } from './config.js';
7
-
8
- /**
9
- * Serialize a route pattern (RegExp or string) to JavaScript code
10
- */
11
- function serializePattern(pattern: RegExp | string): string {
12
- if (pattern instanceof RegExp) {
13
- return `new RegExp(${JSON.stringify(pattern.source)}, ${JSON.stringify(pattern.flags)})`;
14
- }
15
- return JSON.stringify(pattern);
16
- }
17
-
18
- /**
19
- * Serialize an array of route patterns to JavaScript code
20
- */
21
- function serializePatterns(patterns: RoutePattern[]): string {
22
- if (!patterns || patterns.length === 0) return '[]';
23
- return `[${patterns.map(serializePattern).join(', ')}]`;
24
- }
25
-
26
- /**
27
- * Create the virtual module plugin for Vite
28
- * This plugin provides configuration to middleware and pages at runtime
29
- */
30
- export function createVirtualModulePlugin(
31
- authConfig: RouteGuardConfig,
32
- loginPageConfig: LoginPageConfig | false | null,
33
- amplifyConfig: Record<string, unknown> | null
34
- ) {
35
- const virtualModuleId = 'virtual:htlkg-config';
36
- const resolvedVirtualModuleId = '\0' + virtualModuleId;
37
-
38
- return {
39
- name: 'htlkg-config',
40
- enforce: 'pre' as const,
41
- resolveId(id: string) {
42
- if (id === virtualModuleId) {
43
- return resolvedVirtualModuleId;
44
- }
45
- },
46
- load(id: string) {
47
- if (id === resolvedVirtualModuleId) {
48
- // Serialize auth configuration with RegExp support
49
- const serializedAuthConfig = `{
50
- publicRoutes: ${serializePatterns(authConfig.publicRoutes || [])},
51
- authenticatedRoutes: ${serializePatterns(authConfig.authenticatedRoutes || [])},
52
- adminRoutes: ${serializePatterns(authConfig.adminRoutes || [])},
53
- brandRoutes: ${JSON.stringify(authConfig.brandRoutes || [])},
54
- loginUrl: ${JSON.stringify(authConfig.loginUrl || '/login')}
55
- }`;
56
-
57
- const serializedAmplifyConfig = amplifyConfig
58
- ? JSON.stringify(amplifyConfig)
59
- : 'null';
60
-
61
- const serializedLoginPageConfig = loginPageConfig !== false && loginPageConfig !== null
62
- ? JSON.stringify(loginPageConfig)
63
- : 'null';
64
-
65
- return `export const routeGuardConfig = ${serializedAuthConfig};
66
- export const loginPageConfig = ${serializedLoginPageConfig};
67
- export const amplifyConfig = ${serializedAmplifyConfig};`;
68
- }
69
- },
70
- };
71
- }
72
-
73
- /**
74
- * Type definitions for the virtual module
75
- * This should be injected into the project's type definitions
76
- */
77
- export const virtualModuleTypes = `
78
- declare module 'virtual:htlkg-config' {
79
- import type { RouteGuardConfig, LoginPageConfig } from '@htlkg/astro/htlkg/config';
80
-
81
- export const routeGuardConfig: RouteGuardConfig;
82
- export const loginPageConfig: LoginPageConfig | null;
83
- export const amplifyConfig: Record<string, unknown> | null;
84
- }
85
- `;