@dyanet/nextjs-config-aws 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +458 -0
  2. package/dist/cjs/client/env.js +104 -0
  3. package/dist/cjs/client/env.js.map +1 -0
  4. package/dist/cjs/client/index.js +15 -0
  5. package/dist/cjs/client/index.js.map +1 -0
  6. package/dist/cjs/components/index.js +11 -0
  7. package/dist/cjs/components/index.js.map +1 -0
  8. package/dist/cjs/components/public-env-script.js +145 -0
  9. package/dist/cjs/components/public-env-script.js.map +1 -0
  10. package/dist/cjs/index.js +46 -0
  11. package/dist/cjs/index.js.map +1 -0
  12. package/dist/cjs/server/config-provider.js +99 -0
  13. package/dist/cjs/server/config-provider.js.map +1 -0
  14. package/dist/cjs/server/get-config.js +153 -0
  15. package/dist/cjs/server/get-config.js.map +1 -0
  16. package/dist/cjs/server/index.js +17 -0
  17. package/dist/cjs/server/index.js.map +1 -0
  18. package/dist/esm/client/env.js +98 -0
  19. package/dist/esm/client/env.js.map +1 -0
  20. package/dist/esm/client/index.js +8 -0
  21. package/dist/esm/client/index.js.map +1 -0
  22. package/dist/esm/components/index.js +5 -0
  23. package/dist/esm/components/index.js.map +1 -0
  24. package/dist/esm/components/public-env-script.js +107 -0
  25. package/dist/esm/components/public-env-script.js.map +1 -0
  26. package/dist/esm/index.js +15 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/server/config-provider.js +95 -0
  29. package/dist/esm/server/config-provider.js.map +1 -0
  30. package/dist/esm/server/get-config.js +145 -0
  31. package/dist/esm/server/get-config.js.map +1 -0
  32. package/dist/esm/server/index.js +6 -0
  33. package/dist/esm/server/index.js.map +1 -0
  34. package/dist/types/client/env.d.ts +116 -0
  35. package/dist/types/client/env.d.ts.map +1 -0
  36. package/dist/types/client/index.d.ts +8 -0
  37. package/dist/types/client/index.d.ts.map +1 -0
  38. package/dist/types/components/index.d.ts +5 -0
  39. package/dist/types/components/index.d.ts.map +1 -0
  40. package/dist/types/components/public-env-script.d.ts +91 -0
  41. package/dist/types/components/public-env-script.d.ts.map +1 -0
  42. package/dist/types/index.d.ts +13 -0
  43. package/dist/types/index.d.ts.map +1 -0
  44. package/dist/types/server/config-provider.d.ts +80 -0
  45. package/dist/types/server/config-provider.d.ts.map +1 -0
  46. package/dist/types/server/get-config.d.ts +86 -0
  47. package/dist/types/server/get-config.d.ts.map +1 -0
  48. package/dist/types/server/index.d.ts +6 -0
  49. package/dist/types/server/index.d.ts.map +1 -0
  50. package/package.json +90 -0
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ /**
3
+ * PublicEnvScript - Server component for exposing environment variables to the client.
4
+ *
5
+ * This component renders a script tag that injects server-side environment variables
6
+ * into the client-side JavaScript context, enabling runtime environment variable access
7
+ * without requiring NEXT_PUBLIC_ prefixes at build time.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * // In your root layout.tsx
12
+ * import { PublicEnvScript } from '@dyanet/nextjs-config-aws';
13
+ *
14
+ * export default function RootLayout({ children }) {
15
+ * return (
16
+ * <html>
17
+ * <head>
18
+ * <PublicEnvScript
19
+ * publicVars={['API_URL', 'APP_NAME']}
20
+ * variableName="__ENV"
21
+ * />
22
+ * </head>
23
+ * <body>{children}</body>
24
+ * </html>
25
+ * );
26
+ * }
27
+ * ```
28
+ */
29
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
30
+ if (k2 === undefined) k2 = k;
31
+ var desc = Object.getOwnPropertyDescriptor(m, k);
32
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
33
+ desc = { enumerable: true, get: function() { return m[k]; } };
34
+ }
35
+ Object.defineProperty(o, k2, desc);
36
+ }) : (function(o, m, k, k2) {
37
+ if (k2 === undefined) k2 = k;
38
+ o[k2] = m[k];
39
+ }));
40
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
41
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
42
+ }) : function(o, v) {
43
+ o["default"] = v;
44
+ });
45
+ var __importStar = (this && this.__importStar) || (function () {
46
+ var ownKeys = function(o) {
47
+ ownKeys = Object.getOwnPropertyNames || function (o) {
48
+ var ar = [];
49
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
50
+ return ar;
51
+ };
52
+ return ownKeys(o);
53
+ };
54
+ return function (mod) {
55
+ if (mod && mod.__esModule) return mod;
56
+ var result = {};
57
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
58
+ __setModuleDefault(result, mod);
59
+ return result;
60
+ };
61
+ })();
62
+ Object.defineProperty(exports, "__esModule", { value: true });
63
+ exports.filterEnvVars = filterEnvVars;
64
+ exports.generateScriptContent = generateScriptContent;
65
+ exports.PublicEnvScript = PublicEnvScript;
66
+ const React = __importStar(require("react"));
67
+ /**
68
+ * Filters environment variables based on allowlist or prefix.
69
+ *
70
+ * @param env - The environment variables object (typically process.env)
71
+ * @param publicVars - Optional explicit list of variable names to include
72
+ * @param publicPrefix - Optional prefix to filter variables
73
+ * @returns Filtered environment variables object
74
+ */
75
+ function filterEnvVars(env, publicVars, publicPrefix) {
76
+ const result = {};
77
+ // If explicit allowlist is provided, use it
78
+ if (publicVars && publicVars.length > 0) {
79
+ for (const key of publicVars) {
80
+ const value = env[key];
81
+ if (value !== undefined) {
82
+ result[key] = value;
83
+ }
84
+ }
85
+ return result;
86
+ }
87
+ // If prefix is provided, filter by prefix
88
+ if (publicPrefix) {
89
+ for (const [key, value] of Object.entries(env)) {
90
+ if (key.startsWith(publicPrefix) && value !== undefined) {
91
+ result[key] = value;
92
+ }
93
+ }
94
+ return result;
95
+ }
96
+ // If neither is provided, return empty object (safe default)
97
+ return result;
98
+ }
99
+ /**
100
+ * Generates the script content for injecting environment variables.
101
+ *
102
+ * @param filteredEnv - The filtered environment variables to expose
103
+ * @param variableName - The global variable name to use
104
+ * @returns The script content as a string
105
+ */
106
+ function generateScriptContent(filteredEnv, variableName) {
107
+ const jsonString = JSON.stringify(filteredEnv);
108
+ return `window.${variableName}=${jsonString};`;
109
+ }
110
+ /**
111
+ * Server component that renders a script tag exposing environment variables to the client.
112
+ *
113
+ * This enables runtime environment variable access in Next.js applications without
114
+ * requiring NEXT_PUBLIC_ prefixes at build time. The same build artifact can be
115
+ * deployed to different environments with different configuration.
116
+ *
117
+ * Security considerations:
118
+ * - Only expose variables that are safe for public access
119
+ * - Use the publicVars allowlist for explicit control
120
+ * - Never expose secrets, API keys, or sensitive data
121
+ *
122
+ * @param props - Component props
123
+ * @returns A script element with environment variables, or null if no variables to expose
124
+ */
125
+ function PublicEnvScript({ publicVars, publicPrefix, variableName = '__ENV', nonce, }) {
126
+ // Filter environment variables based on allowlist or prefix
127
+ const filteredEnv = filterEnvVars(process.env, publicVars, publicPrefix);
128
+ // If no variables to expose, render nothing
129
+ if (Object.keys(filteredEnv).length === 0) {
130
+ return null;
131
+ }
132
+ // Generate the script content
133
+ const scriptContent = generateScriptContent(filteredEnv, variableName);
134
+ // Build script props
135
+ const scriptProps = {
136
+ dangerouslySetInnerHTML: { __html: scriptContent },
137
+ };
138
+ // Add nonce if provided for CSP compliance
139
+ if (nonce) {
140
+ scriptProps.nonce = nonce;
141
+ }
142
+ return React.createElement('script', scriptProps);
143
+ }
144
+ exports.default = PublicEnvScript;
145
+ //# sourceMappingURL=public-env-script.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"public-env-script.js","sourceRoot":"","sources":["../../../src/components/public-env-script.tsx"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CH,sCA8BC;AASD,sDAMC;AAiBD,0CA4BC;AApID,6CAA+B;AAkC/B;;;;;;;GAOG;AACH,SAAgB,aAAa,CAC3B,GAAuC,EACvC,UAAqB,EACrB,YAA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AC,WAAW,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,8BAA8B;IAC9B,MAAM,aAAa,GAAG,qBAAqB,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;IAEvE,qBAAqB;IACrB,MAAM,WAAW,GAAkD;QACjE,uBAAuB,EAAE,EAAE,MAAM,EAAE,aAAa,EAAE;KACnD,CAAC;IAEF,2CAA2C;IAC3C,IAAI,KAAK,EAAE,CAAC;QACV,WAAW,CAAC,KAAK,GAAG,KAAK,CAAC;IAC5B,CAAC;IAED,OAAO,KAAK,CAAC,aAAa,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;AACpD,CAAC;AAED,kBAAe,eAAe,CAAC"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /**
3
+ * @dyanet/nextjs-config-aws
4
+ *
5
+ * Next.js adapter for AWS configuration management.
6
+ * Provides server-side configuration loading, React context providers,
7
+ * and runtime environment variable support for Next.js applications.
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.hasEnv = exports.getAllEnv = exports.envFrom = exports.env = exports.generateScriptContent = exports.filterEnvVars = exports.PublicEnvScript = exports.ConfigContext = exports.useConfig = exports.ConfigProvider = exports.createConfigProvider = exports.invalidateConfig = exports.getConfigCacheSize = exports.clearConfigCache = exports.getConfig = exports.EnvFileParser = exports.ConfigValidationUtil = exports.ConfigManager = exports.SSMParameterStoreLoader = exports.SecretsManagerLoader = exports.S3Loader = exports.EnvFileLoader = exports.EnvironmentLoader = exports.MissingConfigurationError = exports.ConfigurationLoadError = exports.AWSServiceError = exports.ValidationError = exports.ConfigurationError = void 0;
11
+ var config_aws_1 = require("@dyanet/config-aws");
12
+ Object.defineProperty(exports, "ConfigurationError", { enumerable: true, get: function () { return config_aws_1.ConfigurationError; } });
13
+ Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return config_aws_1.ValidationError; } });
14
+ Object.defineProperty(exports, "AWSServiceError", { enumerable: true, get: function () { return config_aws_1.AWSServiceError; } });
15
+ Object.defineProperty(exports, "ConfigurationLoadError", { enumerable: true, get: function () { return config_aws_1.ConfigurationLoadError; } });
16
+ Object.defineProperty(exports, "MissingConfigurationError", { enumerable: true, get: function () { return config_aws_1.MissingConfigurationError; } });
17
+ Object.defineProperty(exports, "EnvironmentLoader", { enumerable: true, get: function () { return config_aws_1.EnvironmentLoader; } });
18
+ Object.defineProperty(exports, "EnvFileLoader", { enumerable: true, get: function () { return config_aws_1.EnvFileLoader; } });
19
+ Object.defineProperty(exports, "S3Loader", { enumerable: true, get: function () { return config_aws_1.S3Loader; } });
20
+ Object.defineProperty(exports, "SecretsManagerLoader", { enumerable: true, get: function () { return config_aws_1.SecretsManagerLoader; } });
21
+ Object.defineProperty(exports, "SSMParameterStoreLoader", { enumerable: true, get: function () { return config_aws_1.SSMParameterStoreLoader; } });
22
+ Object.defineProperty(exports, "ConfigManager", { enumerable: true, get: function () { return config_aws_1.ConfigManager; } });
23
+ Object.defineProperty(exports, "ConfigValidationUtil", { enumerable: true, get: function () { return config_aws_1.ConfigValidationUtil; } });
24
+ Object.defineProperty(exports, "EnvFileParser", { enumerable: true, get: function () { return config_aws_1.EnvFileParser; } });
25
+ // Server-side exports
26
+ var server_1 = require("./server");
27
+ Object.defineProperty(exports, "getConfig", { enumerable: true, get: function () { return server_1.getConfig; } });
28
+ Object.defineProperty(exports, "clearConfigCache", { enumerable: true, get: function () { return server_1.clearConfigCache; } });
29
+ Object.defineProperty(exports, "getConfigCacheSize", { enumerable: true, get: function () { return server_1.getConfigCacheSize; } });
30
+ Object.defineProperty(exports, "invalidateConfig", { enumerable: true, get: function () { return server_1.invalidateConfig; } });
31
+ Object.defineProperty(exports, "createConfigProvider", { enumerable: true, get: function () { return server_1.createConfigProvider; } });
32
+ Object.defineProperty(exports, "ConfigProvider", { enumerable: true, get: function () { return server_1.ConfigProvider; } });
33
+ Object.defineProperty(exports, "useConfig", { enumerable: true, get: function () { return server_1.useConfig; } });
34
+ Object.defineProperty(exports, "ConfigContext", { enumerable: true, get: function () { return server_1.ConfigContext; } });
35
+ // Components for runtime environment variables
36
+ var components_1 = require("./components");
37
+ Object.defineProperty(exports, "PublicEnvScript", { enumerable: true, get: function () { return components_1.PublicEnvScript; } });
38
+ Object.defineProperty(exports, "filterEnvVars", { enumerable: true, get: function () { return components_1.filterEnvVars; } });
39
+ Object.defineProperty(exports, "generateScriptContent", { enumerable: true, get: function () { return components_1.generateScriptContent; } });
40
+ // Client-side environment variable access
41
+ var client_1 = require("./client");
42
+ Object.defineProperty(exports, "env", { enumerable: true, get: function () { return client_1.env; } });
43
+ Object.defineProperty(exports, "envFrom", { enumerable: true, get: function () { return client_1.envFrom; } });
44
+ Object.defineProperty(exports, "getAllEnv", { enumerable: true, get: function () { return client_1.getAllEnv; } });
45
+ Object.defineProperty(exports, "hasEnv", { enumerable: true, get: function () { return client_1.hasEnv; } });
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAoBH,iDAc4B;AAb1B,gHAAA,kBAAkB,OAAA;AAClB,6GAAA,eAAe,OAAA;AACf,6GAAA,eAAe,OAAA;AACf,oHAAA,sBAAsB,OAAA;AACtB,uHAAA,yBAAyB,OAAA;AACzB,+GAAA,iBAAiB,OAAA;AACjB,2GAAA,aAAa,OAAA;AACb,sGAAA,QAAQ,OAAA;AACR,kHAAA,oBAAoB,OAAA;AACpB,qHAAA,uBAAuB,OAAA;AACvB,2GAAA,aAAa,OAAA;AACb,kHAAA,oBAAoB,OAAA;AACpB,2GAAA,aAAa,OAAA;AAGf,sBAAsB;AACtB,mCAUkB;AAThB,mGAAA,SAAS,OAAA;AACT,0GAAA,gBAAgB,OAAA;AAChB,4GAAA,kBAAkB,OAAA;AAClB,0GAAA,gBAAgB,OAAA;AAEhB,8GAAA,oBAAoB,OAAA;AACpB,wGAAA,cAAc,OAAA;AACd,mGAAA,SAAS,OAAA;AACT,uGAAA,aAAa,OAAA;AAGf,+CAA+C;AAC/C,2CAKsB;AAJpB,6GAAA,eAAe,OAAA;AACf,2GAAA,aAAa,OAAA;AACb,mHAAA,qBAAqB,OAAA;AAIvB,0CAA0C;AAC1C,mCAA2D;AAAlD,6FAAA,GAAG,OAAA;AAAE,iGAAA,OAAO,OAAA;AAAE,mGAAA,SAAS,OAAA;AAAE,gGAAA,MAAM,OAAA"}
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConfigContext = exports.useConfig = exports.ConfigProvider = void 0;
4
+ exports.createConfigProvider = createConfigProvider;
5
+ const jsx_runtime_1 = require("react/jsx-runtime");
6
+ /**
7
+ * React context provider for server-side configuration in Next.js applications.
8
+ * Provides a way to access configuration values in server components.
9
+ */
10
+ const react_1 = require("react");
11
+ /**
12
+ * Creates a typed configuration provider and hook for accessing configuration
13
+ * in React server components.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { createConfigProvider, getConfig } from '@dyanet/nextjs-config-aws';
18
+ * import { z } from 'zod';
19
+ *
20
+ * const schema = z.object({
21
+ * DATABASE_URL: z.string(),
22
+ * API_KEY: z.string(),
23
+ * });
24
+ *
25
+ * type AppConfig = z.infer<typeof schema>;
26
+ *
27
+ * // Create the provider and hook
28
+ * const { ConfigProvider, useConfig } = createConfigProvider<AppConfig>();
29
+ *
30
+ * // In your layout.tsx (Server Component)
31
+ * export default async function RootLayout({ children }) {
32
+ * const config = await getConfig({ schema });
33
+ *
34
+ * return (
35
+ * <html>
36
+ * <body>
37
+ * <ConfigProvider config={config}>
38
+ * {children}
39
+ * </ConfigProvider>
40
+ * </body>
41
+ * </html>
42
+ * );
43
+ * }
44
+ *
45
+ * // In a child component
46
+ * function MyComponent() {
47
+ * const config = useConfig();
48
+ * return <div>API Key: {config.API_KEY}</div>;
49
+ * }
50
+ * ```
51
+ *
52
+ * @returns An object containing the ConfigProvider component and useConfig hook
53
+ */
54
+ function createConfigProvider() {
55
+ // Create a context with null as default (will be provided by ConfigProvider)
56
+ const ConfigContext = (0, react_1.createContext)(null);
57
+ /**
58
+ * Provider component that wraps children with configuration context
59
+ */
60
+ function ConfigProvider({ children, config }) {
61
+ const value = { config };
62
+ return ((0, jsx_runtime_1.jsx)(ConfigContext.Provider, { value: value, children: children }));
63
+ }
64
+ /**
65
+ * Hook to access configuration from the context
66
+ * @throws Error if used outside of ConfigProvider
67
+ */
68
+ function useConfig() {
69
+ const context = (0, react_1.useContext)(ConfigContext);
70
+ if (context === null) {
71
+ throw new Error('useConfig must be used within a ConfigProvider. ' +
72
+ 'Make sure to wrap your component tree with ConfigProvider.');
73
+ }
74
+ return context.config;
75
+ }
76
+ return {
77
+ ConfigProvider,
78
+ useConfig,
79
+ ConfigContext,
80
+ };
81
+ }
82
+ /**
83
+ * Default configuration context and provider for simple use cases.
84
+ * For typed configuration, use createConfigProvider<T>() instead.
85
+ */
86
+ const defaultProvider = createConfigProvider();
87
+ /**
88
+ * Default ConfigProvider for untyped configuration
89
+ */
90
+ exports.ConfigProvider = defaultProvider.ConfigProvider;
91
+ /**
92
+ * Default useConfig hook for untyped configuration
93
+ */
94
+ exports.useConfig = defaultProvider.useConfig;
95
+ /**
96
+ * Default ConfigContext for untyped configuration
97
+ */
98
+ exports.ConfigContext = defaultProvider.ConfigContext;
99
+ //# sourceMappingURL=config-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-provider.js","sourceRoot":"","sources":["../../../src/server/config-provider.tsx"],"names":[],"mappings":";;;AAiEA,oDA2CC;;AA5GD;;;GAGG;AAEH,iCAAyE;AAiBzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,SAAgB,oBAAoB;IAKlC,6EAA6E;IAC7E,MAAM,aAAa,GAAG,IAAA,qBAAa,EAA+B,IAAI,CAAC,CAAC;IAExE;;OAEG;IACH,SAAS,cAAc,CAAC,EAAE,QAAQ,EAAE,MAAM,EAA0B;QAClE,MAAM,KAAK,GAA0B,EAAE,MAAM,EAAE,CAAC;QAEhD,OAAO,CACL,uBAAC,aAAa,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,YACjC,QAAQ,GACc,CAC1B,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,SAAS,SAAS;QAChB,MAAM,OAAO,GAAG,IAAA,kBAAU,EAAC,aAAa,CAAC,CAAC;QAE1C,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CACb,kDAAkD;gBAClD,4DAA4D,CAC7D,CAAC;QACJ,CAAC;QAED,OAAO,OAAO,CAAC,MAAM,CAAC;IACxB,CAAC;IAED,OAAO;QACL,cAAc;QACd,SAAS;QACT,aAAa;KACd,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,eAAe,GAAG,oBAAoB,EAA2B,CAAC;AAExE;;GAEG;AACU,QAAA,cAAc,GAAG,eAAe,CAAC,cAAc,CAAC;AAE7D;;GAEG;AACU,QAAA,SAAS,GAAG,eAAe,CAAC,SAAS,CAAC;AAEnD;;GAEG;AACU,QAAA,aAAa,GAAG,eAAe,CAAC,aAAa,CAAC"}
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ /**
3
+ * Server-side configuration loading for Next.js applications.
4
+ * Provides a cached getConfig() function that uses ConfigManager from @dyanet/config-aws.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.getConfig = getConfig;
8
+ exports.clearConfigCache = clearConfigCache;
9
+ exports.getConfigCacheSize = getConfigCacheSize;
10
+ exports.getAwsApiCallCount = getAwsApiCallCount;
11
+ exports.resetAwsApiCallCount = resetAwsApiCallCount;
12
+ exports.invalidateConfig = invalidateConfig;
13
+ const config_aws_1 = require("@dyanet/config-aws");
14
+ /**
15
+ * Cache storage for configuration
16
+ * Uses a Map to support multiple cache keys (for different option combinations)
17
+ */
18
+ const configCache = new Map();
19
+ /**
20
+ * Track AWS API call counts for testing purposes
21
+ * @internal
22
+ */
23
+ let awsApiCallCount = 0;
24
+ /**
25
+ * Generate a cache key from options
26
+ * @internal
27
+ */
28
+ function generateCacheKey(options) {
29
+ const loaderNames = options.loaders?.map((l) => l.getName()).join(',') ?? '';
30
+ const precedence = Array.isArray(options.precedence)
31
+ ? options.precedence.map((p) => `${p.loader}:${p.priority}`).join(',')
32
+ : options.precedence ?? 'aws-first';
33
+ return `${loaderNames}|${precedence}`;
34
+ }
35
+ /**
36
+ * Check if a cache entry is still valid
37
+ * @internal
38
+ */
39
+ function isCacheValid(entry) {
40
+ if (!entry)
41
+ return false;
42
+ return Date.now() < entry.expiresAt;
43
+ }
44
+ /**
45
+ * Load configuration for Next.js server-side use.
46
+ *
47
+ * This function provides caching to avoid repeated AWS API calls during request handling.
48
+ * Configuration is cached based on the loader configuration and precedence strategy.
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import { getConfig, EnvironmentLoader, SecretsManagerLoader } from '@dyanet/nextjs-config-aws';
53
+ * import { z } from 'zod';
54
+ *
55
+ * const schema = z.object({
56
+ * DATABASE_URL: z.string(),
57
+ * API_KEY: z.string(),
58
+ * });
59
+ *
60
+ * // In a Server Component or API route
61
+ * export default async function Page() {
62
+ * const config = await getConfig({
63
+ * schema,
64
+ * loaders: [
65
+ * new EnvironmentLoader(),
66
+ * new SecretsManagerLoader({ secretName: '/my-app/config' }),
67
+ * ],
68
+ * precedence: 'aws-first',
69
+ * });
70
+ *
71
+ * return <div>DB: {config.DATABASE_URL}</div>;
72
+ * }
73
+ * ```
74
+ *
75
+ * @param options Configuration options
76
+ * @returns Promise resolving to the validated configuration object
77
+ */
78
+ async function getConfig(options = {}) {
79
+ const { schema, loaders = [], precedence = 'aws-first', cache = true, cacheTTL = 60000, // 1 minute default
80
+ enableLogging = false, } = options;
81
+ // Generate cache key
82
+ const cacheKey = generateCacheKey(options);
83
+ // Check cache if enabled
84
+ if (cache) {
85
+ const cached = configCache.get(cacheKey);
86
+ if (isCacheValid(cached)) {
87
+ return cached.config;
88
+ }
89
+ }
90
+ // Increment API call counter (for testing)
91
+ awsApiCallCount++;
92
+ // Create ConfigManager with provided options
93
+ const managerOptions = {
94
+ loaders,
95
+ schema,
96
+ precedence,
97
+ validateOnLoad: true,
98
+ enableLogging,
99
+ };
100
+ const manager = new config_aws_1.ConfigManager(managerOptions);
101
+ await manager.load();
102
+ const config = manager.getAll();
103
+ const loadResult = manager.getLoadResult();
104
+ // Store in cache if enabled
105
+ if (cache && loadResult) {
106
+ const entry = {
107
+ config,
108
+ loadResult,
109
+ expiresAt: Date.now() + cacheTTL,
110
+ };
111
+ configCache.set(cacheKey, entry);
112
+ }
113
+ return config;
114
+ }
115
+ /**
116
+ * Clear the configuration cache.
117
+ * Useful for testing or when configuration needs to be reloaded.
118
+ */
119
+ function clearConfigCache() {
120
+ configCache.clear();
121
+ }
122
+ /**
123
+ * Get the current cache size.
124
+ * @returns Number of cached configurations
125
+ */
126
+ function getConfigCacheSize() {
127
+ return configCache.size;
128
+ }
129
+ /**
130
+ * Get the AWS API call count.
131
+ * This is primarily for testing the caching behavior.
132
+ * @internal
133
+ */
134
+ function getAwsApiCallCount() {
135
+ return awsApiCallCount;
136
+ }
137
+ /**
138
+ * Reset the AWS API call count.
139
+ * This is primarily for testing the caching behavior.
140
+ * @internal
141
+ */
142
+ function resetAwsApiCallCount() {
143
+ awsApiCallCount = 0;
144
+ }
145
+ /**
146
+ * Invalidate a specific cache entry by regenerating with the same options.
147
+ * @param options The options used to create the cache entry
148
+ */
149
+ function invalidateConfig(options) {
150
+ const cacheKey = generateCacheKey(options);
151
+ configCache.delete(cacheKey);
152
+ }
153
+ //# sourceMappingURL=get-config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-config.js","sourceRoot":"","sources":["../../../src/server/get-config.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAyGH,8BAoDC;AAMD,4CAEC;A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CAAC;AAED;;;GAGG;AACH,SAAgB,gBAAgB,CAAI,OAA6B;IAC/D,MAAM,QAAQ,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAC3C,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ /**
3
+ * Server-side exports for Next.js configuration management.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ConfigContext = exports.useConfig = exports.ConfigProvider = exports.createConfigProvider = exports.invalidateConfig = exports.getConfigCacheSize = exports.clearConfigCache = exports.getConfig = void 0;
7
+ var get_config_1 = require("./get-config");
8
+ Object.defineProperty(exports, "getConfig", { enumerable: true, get: function () { return get_config_1.getConfig; } });
9
+ Object.defineProperty(exports, "clearConfigCache", { enumerable: true, get: function () { return get_config_1.clearConfigCache; } });
10
+ Object.defineProperty(exports, "getConfigCacheSize", { enumerable: true, get: function () { return get_config_1.getConfigCacheSize; } });
11
+ Object.defineProperty(exports, "invalidateConfig", { enumerable: true, get: function () { return get_config_1.invalidateConfig; } });
12
+ var config_provider_1 = require("./config-provider");
13
+ Object.defineProperty(exports, "createConfigProvider", { enumerable: true, get: function () { return config_provider_1.createConfigProvider; } });
14
+ Object.defineProperty(exports, "ConfigProvider", { enumerable: true, get: function () { return config_provider_1.ConfigProvider; } });
15
+ Object.defineProperty(exports, "useConfig", { enumerable: true, get: function () { return config_provider_1.useConfig; } });
16
+ Object.defineProperty(exports, "ConfigContext", { enumerable: true, get: function () { return config_provider_1.ConfigContext; } });
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,2CAMsB;AALpB,uGAAA,SAAS,OAAA;AACT,8GAAA,gBAAgB,OAAA;AAChB,gHAAA,kBAAkB,OAAA;AAClB,8GAAA,gBAAgB,OAAA;AAIlB,qDAK2B;AAJzB,uHAAA,oBAAoB,OAAA;AACpB,iHAAA,cAAc,OAAA;AACd,4GAAA,SAAS,OAAA;AACT,gHAAA,aAAa,OAAA"}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Client-side environment variable access for Next.js applications.
3
+ *
4
+ * This module provides a function to read runtime environment variables
5
+ * that were injected by the PublicEnvScript server component.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * 'use client';
10
+ *
11
+ * import { env } from '@dyanet/nextjs-config-aws/client';
12
+ *
13
+ * function MyComponent() {
14
+ * const apiUrl = env('API_URL');
15
+ * const appName = env('APP_NAME', 'Default App');
16
+ *
17
+ * return <div>API: {apiUrl}, App: {appName}</div>;
18
+ * }
19
+ * ```
20
+ */
21
+ /**
22
+ * Default variable name used by PublicEnvScript.
23
+ */
24
+ const DEFAULT_VARIABLE_NAME = '__ENV';
25
+ /**
26
+ * Gets the environment variables object from the window.
27
+ *
28
+ * @param variableName - The global variable name to read from
29
+ * @returns The environment variables object, or an empty object if not found
30
+ */
31
+ function getEnvObject(variableName = DEFAULT_VARIABLE_NAME) {
32
+ if (typeof window === 'undefined') {
33
+ // Server-side or during SSR - return empty object
34
+ return {};
35
+ }
36
+ const envObj = window[variableName];
37
+ if (envObj && typeof envObj === 'object' && !Array.isArray(envObj)) {
38
+ return envObj;
39
+ }
40
+ return {};
41
+ }
42
+ /**
43
+ * Implementation of the env function.
44
+ */
45
+ export function env(key, defaultValue) {
46
+ const envObj = getEnvObject();
47
+ const value = envObj[key];
48
+ if (value !== undefined) {
49
+ return value;
50
+ }
51
+ return defaultValue;
52
+ }
53
+ /**
54
+ * Implementation of the envFrom function.
55
+ */
56
+ export function envFrom(variableName, key, defaultValue) {
57
+ const envObj = getEnvObject(variableName);
58
+ const value = envObj[key];
59
+ if (value !== undefined) {
60
+ return value;
61
+ }
62
+ return defaultValue;
63
+ }
64
+ /**
65
+ * Gets all runtime environment variables.
66
+ *
67
+ * @param variableName - The global variable name to read from (default: '__ENV')
68
+ * @returns A copy of all environment variables
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * const allEnv = getAllEnv();
73
+ * console.log(allEnv); // { API_URL: '...', APP_NAME: '...' }
74
+ * ```
75
+ */
76
+ export function getAllEnv(variableName = DEFAULT_VARIABLE_NAME) {
77
+ return { ...getEnvObject(variableName) };
78
+ }
79
+ /**
80
+ * Checks if a runtime environment variable exists.
81
+ *
82
+ * @param key - The environment variable name
83
+ * @param variableName - The global variable name to read from (default: '__ENV')
84
+ * @returns True if the variable exists, false otherwise
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * if (hasEnv('FEATURE_FLAG')) {
89
+ * // Feature is enabled
90
+ * }
91
+ * ```
92
+ */
93
+ export function hasEnv(key, variableName = DEFAULT_VARIABLE_NAME) {
94
+ const envObj = getEnvObject(variableName);
95
+ return key in envObj;
96
+ }
97
+ export default env;
98
+ //# sourceMappingURL=env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.js","sourceRoot":"","sources":["../../../src/client/env.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAYH;;GAEG;AACH,MAAM,qBAAqB,GAAG,OAAO,CAAC;AAEtC;;;;;GAKG;AACH,SAAS,YAAY,CAAC,eAAuB,qBAAqB;IAChE,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,kDAAkD;QAClD,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IACpC,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACnE,OAAO,MAAgC,CAAC;IAC1C,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAoCD;;GAEG;AACH,MAAM,UAAU,GAAG,CAAI,GAAW,EAAE,YAAgB;IAClD,MAAM,MAAM,GAAG,YAAY,EAAE,CAAC;IAC9B,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IAE1B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC;AA6BD;;GAEG;AACH,MAAM,UAAU,OAAO,CAAI,YAAoB,EAAE,GAAW,EAAE,YAAgB;IAC5E,MAAM,MAAM,GAAG,YAAY,CAAC,YAAY,CAAC,CAAC;IAC1C,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IAE1B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,YAAY,CAAC;AACtB,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,SAAS,CAAC,eAAuB,qBAAqB;IACpE,OAAO,EAAE,GAAG,YAAY,CAAC,YAAY,CAAC,EAAE,CAAC;AAC3C,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,MAAM,CAAC,GAAW,EAAE,eAAuB,qBAAqB;IAC9E,MAAM,MAAM,GAAG,YAAY,CAAC,YAAY,CAAC,CAAC;IAC1C,OAAO,GAAG,IAAI,MAAM,CAAC;AACvB,CAAC;AAED,eAAe,GAAG,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Client-side exports for Next.js configuration management.
3
+ *
4
+ * These utilities are designed for use in client components ('use client')
5
+ * to access runtime environment variables injected by PublicEnvScript.
6
+ */
7
+ export { env, envFrom, getAllEnv, hasEnv } from './env';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Client-side components for Next.js configuration management.
3
+ */
4
+ export { PublicEnvScript, filterEnvVars, generateScriptContent, } from './public-env-script';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/components/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,eAAe,EACf,aAAa,EACb,qBAAqB,GAEtB,MAAM,qBAAqB,CAAC"}