@akinon/next 2.0.135-rc.1 → 2.0.135
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/CHANGELOG.md +2 -74
- package/__tests__/theme-editor-health.test.ts +128 -0
- package/api/auth.ts +20 -97
- package/bin/pz-generate-routes.js +1 -4
- package/components/plugin-module.tsx +1 -0
- package/data/client/checkout.ts +1 -0
- package/data/client/user.ts +1 -6
- package/data/client/wishlist.ts +0 -22
- package/data/server/category.ts +2 -14
- package/data/server/list.ts +1 -13
- package/data/server/product.ts +0 -10
- package/data/server/special-page.ts +1 -14
- package/data/server/widget.ts +1 -14
- package/data/urls.ts +4 -13
- package/hooks/index.ts +0 -1
- package/hooks/use-captcha.tsx +1 -1
- package/middlewares/default.ts +258 -254
- package/middlewares/pretty-url.ts +1 -3
- package/package.json +2 -2
- package/plugins.d.ts +0 -10
- package/plugins.js +0 -1
- package/redux/middlewares/checkout.ts +3 -45
- package/redux/middlewares/pre-order/installment-option.ts +1 -9
- package/types/index.ts +9 -20
- package/utils/csrf.ts +1 -1
- package/utils/theme-editor-health.ts +141 -0
- package/with-pz-config.js +1 -6
- package/api/client-env.ts +0 -78
- package/hooks/use-client-env.ts +0 -265
- package/utils/payload-optimizer.ts +0 -481
|
@@ -86,9 +86,7 @@ const withPrettyUrl =
|
|
|
86
86
|
const matchedLanguagePrefix = url.pathname.match(
|
|
87
87
|
urlLocaleMatcherRegex
|
|
88
88
|
)?.[0];
|
|
89
|
-
const prettyUrlPathname = matchedLanguagePrefix
|
|
90
|
-
? url.pathname.replace(matchedLanguagePrefix, '')
|
|
91
|
-
: url.pathname;
|
|
89
|
+
const prettyUrlPathname = url.pathname.replace(matchedLanguagePrefix, '');
|
|
92
90
|
const isValidPrettyUrlPath = (pathname: string) => {
|
|
93
91
|
return (
|
|
94
92
|
new RegExp(/^\/[a-zA-Z0-9/]+(?:-[a-zA-Z0-9/]+)*$/).test(pathname) &&
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akinon/next",
|
|
3
3
|
"description": "Core package for Project Zero Next",
|
|
4
|
-
"version": "2.0.135
|
|
4
|
+
"version": "2.0.135",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"bin": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"set-cookie-parser": "2.6.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@akinon/eslint-plugin-projectzero": "2.0.135
|
|
39
|
+
"@akinon/eslint-plugin-projectzero": "2.0.135",
|
|
40
40
|
"@babel/core": "7.26.10",
|
|
41
41
|
"@babel/preset-env": "7.26.9",
|
|
42
42
|
"@babel/preset-typescript": "7.27.0",
|
package/plugins.d.ts
CHANGED
|
@@ -37,16 +37,6 @@ declare module '@akinon/pz-cybersource-uc/src/redux/middleware' {
|
|
|
37
37
|
export default middleware as any;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
declare module '@akinon/pz-apple-pay' {}
|
|
41
|
-
|
|
42
|
-
declare module '@akinon/pz-similar-products' {
|
|
43
|
-
export const SimilarProductsModal: any;
|
|
44
|
-
export const SimilarProductsFilterSidebar: any;
|
|
45
|
-
export const SimilarProductsResultsGrid: any;
|
|
46
|
-
export const SimilarProductsPlugin: any;
|
|
47
|
-
export const SimilarProductsButtonPlugin: any;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
40
|
declare module '@akinon/pz-cybersource-uc/src/redux/reducer' {
|
|
51
41
|
export default reducer as any;
|
|
52
42
|
}
|
package/plugins.js
CHANGED
|
@@ -26,28 +26,13 @@ import {
|
|
|
26
26
|
} from '../../redux/reducers/checkout';
|
|
27
27
|
import { RootState, TypedDispatch } from 'redux/store';
|
|
28
28
|
import { checkoutApi } from '../../data/client/checkout';
|
|
29
|
-
import { CheckoutContext,
|
|
29
|
+
import { CheckoutContext, PreOrder } from '../../types';
|
|
30
30
|
import { getCookie } from '../../utils';
|
|
31
31
|
import settings from 'settings';
|
|
32
32
|
import { LocaleUrlStrategy } from '../../localization';
|
|
33
33
|
import { showMobile3dIframe } from '../../utils/mobile-3d-iframe';
|
|
34
34
|
import { showRedirectionIframe } from '../../utils/redirection-iframe';
|
|
35
35
|
|
|
36
|
-
const IFRAME_REDIRECTION_KEY = 'pz-iframe-redirection-active';
|
|
37
|
-
|
|
38
|
-
const isIframeRedirectionActive = () =>
|
|
39
|
-
typeof window !== 'undefined' &&
|
|
40
|
-
sessionStorage.getItem(IFRAME_REDIRECTION_KEY) === 'true';
|
|
41
|
-
|
|
42
|
-
const setIframeRedirectionActive = (active: boolean) => {
|
|
43
|
-
if (typeof window === 'undefined') return;
|
|
44
|
-
if (active) {
|
|
45
|
-
sessionStorage.setItem(IFRAME_REDIRECTION_KEY, 'true');
|
|
46
|
-
} else {
|
|
47
|
-
sessionStorage.removeItem(IFRAME_REDIRECTION_KEY);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
36
|
interface CheckoutResult {
|
|
52
37
|
payload: {
|
|
53
38
|
errors?: Record<string, string[]>;
|
|
@@ -84,7 +69,7 @@ export const redirectUrlMiddleware: Middleware = () => {
|
|
|
84
69
|
const result = next(action) as CheckoutResult;
|
|
85
70
|
const redirectUrl = result?.payload?.redirect_url;
|
|
86
71
|
|
|
87
|
-
if (redirectUrl
|
|
72
|
+
if (redirectUrl) {
|
|
88
73
|
const currentLocale = getCookie('pz-locale');
|
|
89
74
|
|
|
90
75
|
let url = redirectUrl;
|
|
@@ -114,15 +99,8 @@ export const contextListMiddleware: Middleware = ({
|
|
|
114
99
|
const { isMobileApp, userPhoneNumber } = getState().root;
|
|
115
100
|
const result = next(action) as CheckoutResult;
|
|
116
101
|
const preOrder = result?.payload?.pre_order;
|
|
117
|
-
const act = action as MiddlewareAction;
|
|
118
102
|
|
|
119
103
|
if (result?.payload?.context_list) {
|
|
120
|
-
const endpointName = act.meta?.arg?.endpointName;
|
|
121
|
-
const isBinNumberResponse = endpointName === 'setBinNumber';
|
|
122
|
-
const hasCardTypeInContextList = result.payload.context_list.some(
|
|
123
|
-
(ctx) => ctx.page_context.card_type
|
|
124
|
-
);
|
|
125
|
-
|
|
126
104
|
result.payload.context_list.forEach((context) => {
|
|
127
105
|
const redirectUrl = context.page_context.redirect_url;
|
|
128
106
|
const isIframe = context.page_context.is_iframe ?? false;
|
|
@@ -156,7 +134,6 @@ export const contextListMiddleware: Middleware = ({
|
|
|
156
134
|
if (isMobileDevice && isIframePaymentOptionIncluded) {
|
|
157
135
|
showMobile3dIframe(urlObj.toString());
|
|
158
136
|
} else if (isIframe) {
|
|
159
|
-
setIframeRedirectionActive(true);
|
|
160
137
|
showRedirectionIframe(urlObj.toString());
|
|
161
138
|
} else {
|
|
162
139
|
window.location.href = urlObj.toString();
|
|
@@ -232,34 +209,15 @@ export const contextListMiddleware: Middleware = ({
|
|
|
232
209
|
(ctx) => ctx.page_name === 'DeliveryOptionSelectionPage'
|
|
233
210
|
)
|
|
234
211
|
) {
|
|
235
|
-
const isCreditCardPayment =
|
|
236
|
-
preOrder?.payment_option?.payment_type === 'credit_card' ||
|
|
237
|
-
preOrder?.payment_option?.payment_type === 'masterpass';
|
|
238
|
-
|
|
239
212
|
if (context.page_context.card_type) {
|
|
240
213
|
dispatch(setCardType(context.page_context.card_type));
|
|
241
|
-
} else if (
|
|
242
|
-
isCreditCardPayment &&
|
|
243
|
-
isBinNumberResponse &&
|
|
244
|
-
!hasCardTypeInContextList
|
|
245
|
-
) {
|
|
246
|
-
dispatch(setCardType(null));
|
|
247
|
-
dispatch(setInstallmentOptions([]));
|
|
248
214
|
}
|
|
249
215
|
|
|
250
216
|
if (
|
|
251
217
|
context.page_context.installments &&
|
|
252
218
|
preOrder?.payment_option?.payment_type !== 'masterpass_rest'
|
|
253
219
|
) {
|
|
254
|
-
|
|
255
|
-
!isCreditCardPayment ||
|
|
256
|
-
context.page_context.card_type ||
|
|
257
|
-
hasCardTypeInContextList
|
|
258
|
-
) {
|
|
259
|
-
dispatch(
|
|
260
|
-
setInstallmentOptions(context.page_context.installments)
|
|
261
|
-
);
|
|
262
|
-
}
|
|
220
|
+
dispatch(setInstallmentOptions(context.page_context.installments));
|
|
263
221
|
}
|
|
264
222
|
}
|
|
265
223
|
|
|
@@ -14,17 +14,9 @@ export const installmentOptionMiddleware: Middleware = ({
|
|
|
14
14
|
return result;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const { installmentOptions
|
|
17
|
+
const { installmentOptions } = getState().checkout;
|
|
18
18
|
const { endpoints: apiEndpoints } = checkoutApi;
|
|
19
19
|
|
|
20
|
-
const isCreditCardPayment =
|
|
21
|
-
preOrder?.payment_option?.payment_type === 'credit_card' ||
|
|
22
|
-
preOrder?.payment_option?.payment_type === 'masterpass';
|
|
23
|
-
|
|
24
|
-
if (isCreditCardPayment && !cardType) {
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
20
|
if (
|
|
29
21
|
!preOrder?.installment &&
|
|
30
22
|
preOrder?.payment_option?.payment_type !== 'saved_card' &&
|
package/types/index.ts
CHANGED
|
@@ -85,12 +85,6 @@ export interface Settings {
|
|
|
85
85
|
};
|
|
86
86
|
usePrettyUrlRoute?: boolean;
|
|
87
87
|
commerceUrl: string;
|
|
88
|
-
/**
|
|
89
|
-
* This option allows you to track Sentry events on the client side, in addition to server and edge environments.
|
|
90
|
-
*
|
|
91
|
-
* It overrides process.env.NEXT_PUBLIC_SENTRY_DSN and process.env.SENTRY_DSN.
|
|
92
|
-
*/
|
|
93
|
-
sentryDsn?: string;
|
|
94
88
|
/**
|
|
95
89
|
* CSRF cookie hardening settings.
|
|
96
90
|
*
|
|
@@ -242,7 +236,6 @@ export interface Settings {
|
|
|
242
236
|
separator?: string;
|
|
243
237
|
segments: PzSegmentDefinition[];
|
|
244
238
|
};
|
|
245
|
-
payloadOptimization?: import('../utils/payload-optimizer').PayloadOptimizationConfig;
|
|
246
239
|
}
|
|
247
240
|
|
|
248
241
|
export interface CacheOptions {
|
|
@@ -302,9 +295,7 @@ export interface PzSegmentsConfig {
|
|
|
302
295
|
}
|
|
303
296
|
|
|
304
297
|
// Search params type compatible with both Next.js resolved searchParams and URLSearchParams
|
|
305
|
-
export type SearchParams =
|
|
306
|
-
| Record<string, string | string[] | undefined>
|
|
307
|
-
| URLSearchParams;
|
|
298
|
+
export type SearchParams = Record<string, string | string[] | undefined> | URLSearchParams;
|
|
308
299
|
|
|
309
300
|
// Raw Next 16 server prop shape, used at the middleware/HOC boundary before normalization
|
|
310
301
|
export type RawSearchParams = Record<string, string | string[] | undefined>;
|
|
@@ -337,16 +328,14 @@ export interface RootLayoutProps<T = any> extends LayoutProps<T> {
|
|
|
337
328
|
|
|
338
329
|
// Async versions for Next.js 16 generateMetadata and internal use
|
|
339
330
|
export interface AsyncPageProps<T = any> {
|
|
340
|
-
params: Promise<
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
}
|
|
349
|
-
>;
|
|
331
|
+
params: Promise<T & {
|
|
332
|
+
pz?: string;
|
|
333
|
+
commerce?: string;
|
|
334
|
+
locale?: string;
|
|
335
|
+
currency?: string;
|
|
336
|
+
url?: string;
|
|
337
|
+
[key: string]: any;
|
|
338
|
+
}>;
|
|
350
339
|
searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
|
|
351
340
|
}
|
|
352
341
|
|
package/utils/csrf.ts
CHANGED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What the theme editor needs from this storefront's environment, reported to
|
|
5
|
+
* the editor so it can refuse to run instead of failing silently.
|
|
6
|
+
*
|
|
7
|
+
* The one variable the editor cannot work without is
|
|
8
|
+
* `NEXT_PUBLIC_THEME_EDITOR_ORIGINS`. The preview drops every message whose
|
|
9
|
+
* origin is not on that list (`@akinon/pz-theme`'s editor bridge), and it has
|
|
10
|
+
* no way to say so: it cannot post back to an origin it does not trust. So a
|
|
11
|
+
* production storefront without the list, or without the editor's origin on
|
|
12
|
+
* it, renders in the preview and ignores the designer completely. The editor
|
|
13
|
+
* asks here, over plain HTTP, before relying on the preview.
|
|
14
|
+
*
|
|
15
|
+
* Only variable NAMES and whether they are usable leave the server — never a
|
|
16
|
+
* value — so the answer is safe to serve to any origin.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export const THEME_EDITOR_HEALTH_PATH = '/theme-editor/health';
|
|
20
|
+
|
|
21
|
+
export type ThemeEditorEnvProblem = 'missing' | 'invalid' | 'origin-not-listed';
|
|
22
|
+
|
|
23
|
+
export interface ThemeEditorEnvCheck {
|
|
24
|
+
name: string;
|
|
25
|
+
ok: boolean;
|
|
26
|
+
problem?: ThemeEditorEnvProblem;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ThemeEditorHealth {
|
|
30
|
+
version: 1;
|
|
31
|
+
/** Variables without which the editor cannot drive the preview. */
|
|
32
|
+
required: ThemeEditorEnvCheck[];
|
|
33
|
+
/** Variables whose absence disables a feature, not the editor. */
|
|
34
|
+
optional: ThemeEditorEnvCheck[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ThemeEditorEnv {
|
|
38
|
+
editorOrigins?: string;
|
|
39
|
+
previewSecret?: string;
|
|
40
|
+
mainAppUrl?: string;
|
|
41
|
+
production: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const originOf = (value: string): string | null => {
|
|
45
|
+
try {
|
|
46
|
+
const url = new URL(value);
|
|
47
|
+
return url.protocol === 'http:' || url.protocol === 'https:'
|
|
48
|
+
? url.origin
|
|
49
|
+
: null;
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// The same two rules as `createOriginGuard` in `@akinon/theme-contract`, which
|
|
56
|
+
// the preview applies to every message: the listed origins, plus any localhost
|
|
57
|
+
// origin outside production. The answer here must match what the preview will
|
|
58
|
+
// actually accept.
|
|
59
|
+
const parseOriginList = (raw: string | undefined): string[] =>
|
|
60
|
+
(raw ?? '')
|
|
61
|
+
.split(/[\s,]+/)
|
|
62
|
+
.map((entry) => originOf(entry.trim()))
|
|
63
|
+
.filter((origin): origin is string => origin !== null);
|
|
64
|
+
|
|
65
|
+
const LOCAL_DEV_ORIGIN = /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/;
|
|
66
|
+
|
|
67
|
+
const checkEditorOrigins = (
|
|
68
|
+
env: ThemeEditorEnv,
|
|
69
|
+
editorOrigin: string | null
|
|
70
|
+
): ThemeEditorEnvCheck => {
|
|
71
|
+
const name = 'NEXT_PUBLIC_THEME_EDITOR_ORIGINS';
|
|
72
|
+
const raw = env.editorOrigins?.trim();
|
|
73
|
+
const origins = parseOriginList(raw);
|
|
74
|
+
const trustedLocally =
|
|
75
|
+
!env.production && !!editorOrigin && LOCAL_DEV_ORIGIN.test(editorOrigin);
|
|
76
|
+
|
|
77
|
+
if (editorOrigin && (origins.includes(editorOrigin) || trustedLocally)) {
|
|
78
|
+
return { name, ok: true };
|
|
79
|
+
}
|
|
80
|
+
if (!raw) {
|
|
81
|
+
// Development trusts localhost without a list; a request with no origin
|
|
82
|
+
// (curl, a server) cannot be judged beyond that.
|
|
83
|
+
return !env.production && !editorOrigin
|
|
84
|
+
? { name, ok: true }
|
|
85
|
+
: { name, ok: false, problem: 'missing' };
|
|
86
|
+
}
|
|
87
|
+
if (origins.length === 0) return { name, ok: false, problem: 'invalid' };
|
|
88
|
+
if (!editorOrigin) return { name, ok: true };
|
|
89
|
+
|
|
90
|
+
return { name, ok: false, problem: 'origin-not-listed' };
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const getThemeEditorHealth = (
|
|
94
|
+
env: ThemeEditorEnv,
|
|
95
|
+
editorOrigin: string | null
|
|
96
|
+
): ThemeEditorHealth => {
|
|
97
|
+
const mainAppUrl = env.mainAppUrl?.trim();
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
version: 1,
|
|
101
|
+
required: [checkEditorOrigins(env, editorOrigin)],
|
|
102
|
+
optional: [
|
|
103
|
+
env.previewSecret?.trim()
|
|
104
|
+
? { name: 'PREVIEW_SECRET', ok: true }
|
|
105
|
+
: { name: 'PREVIEW_SECRET', ok: false, problem: 'missing' },
|
|
106
|
+
!mainAppUrl
|
|
107
|
+
? { name: 'MAIN_APP_URL', ok: false, problem: 'missing' }
|
|
108
|
+
: originOf(mainAppUrl)
|
|
109
|
+
? { name: 'MAIN_APP_URL', ok: true }
|
|
110
|
+
: { name: 'MAIN_APP_URL', ok: false, problem: 'invalid' }
|
|
111
|
+
]
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The environment as this build sees it. `NEXT_PUBLIC_*` is read as a literal
|
|
117
|
+
* so Next.js inlines it here exactly as it does into the preview's bundle —
|
|
118
|
+
* the answer describes the list the preview was built with.
|
|
119
|
+
*/
|
|
120
|
+
export const readThemeEditorEnv = (): ThemeEditorEnv => ({
|
|
121
|
+
editorOrigins: process.env.NEXT_PUBLIC_THEME_EDITOR_ORIGINS,
|
|
122
|
+
previewSecret: process.env.PREVIEW_SECRET,
|
|
123
|
+
mainAppUrl: process.env.MAIN_APP_URL,
|
|
124
|
+
production: process.env.NODE_ENV === 'production'
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
export const themeEditorHealthResponse = (req: NextRequest): NextResponse =>
|
|
128
|
+
NextResponse.json(
|
|
129
|
+
getThemeEditorHealth(
|
|
130
|
+
readThemeEditorEnv(),
|
|
131
|
+
originOf(req.headers.get('origin') ?? '')
|
|
132
|
+
),
|
|
133
|
+
{
|
|
134
|
+
headers: {
|
|
135
|
+
// Asked for by the editor, which runs on another origin — and by
|
|
136
|
+
// design before this storefront knows that origin is its editor.
|
|
137
|
+
'Access-Control-Allow-Origin': '*',
|
|
138
|
+
'Cache-Control': 'no-store'
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
);
|
package/with-pz-config.js
CHANGED
|
@@ -30,10 +30,6 @@ const defaultConfig = {
|
|
|
30
30
|
{
|
|
31
31
|
protocol: 'https',
|
|
32
32
|
hostname: '**.akinoncdn.com'
|
|
33
|
-
},
|
|
34
|
-
{
|
|
35
|
-
protocol: 'https',
|
|
36
|
-
hostname: '**.akinoncloudcdn.com'
|
|
37
33
|
}
|
|
38
34
|
]
|
|
39
35
|
},
|
|
@@ -78,8 +74,7 @@ const defaultConfig = {
|
|
|
78
74
|
acc[`@akinon/${plugin}`] = false;
|
|
79
75
|
return acc;
|
|
80
76
|
}, {}),
|
|
81
|
-
translations: false
|
|
82
|
-
'@opentelemetry/exporter-jaeger': false
|
|
77
|
+
translations: false
|
|
83
78
|
};
|
|
84
79
|
// Ensure webpack can resolve deps from the app's node_modules when
|
|
85
80
|
// compiling transpiled packages (e.g. @akinon/next) whose imports
|
package/api/client-env.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Runtime access to public (`NEXT_PUBLIC_*`) environment variables.
|
|
5
|
-
*
|
|
6
|
-
* Next.js inlines `process.env.NEXT_PUBLIC_*` into the bundle at *build* time.
|
|
7
|
-
* On hosting platforms where the build has no access to the environment (e.g.
|
|
8
|
-
* Akinon Cloud Commerce), those references resolve to `undefined` in the
|
|
9
|
-
* browser. This route reads the variables from the *server runtime* instead —
|
|
10
|
-
* `process.env[name]` is a computed lookup, so webpack never statically inlines
|
|
11
|
-
* it — and hands them back to the client.
|
|
12
|
-
*
|
|
13
|
-
* Only `NEXT_PUBLIC_`-prefixed names are resolvable. These values are public by
|
|
14
|
-
* design (they are shipped to the browser anyway), so nothing secret can leak
|
|
15
|
-
* through here; the prefix check is what keeps server-only secrets unreadable.
|
|
16
|
-
*
|
|
17
|
-
* Query: one or more `env` params, repeated (`?env=A&env=B`) and/or comma
|
|
18
|
-
* separated (`?env=A,B`). Names must include the full prefix. Responds with a
|
|
19
|
-
* `{ [name]: value | null }` map (`null` when the variable is not set).
|
|
20
|
-
*
|
|
21
|
-
* The consuming route file must set `export const dynamic = 'force-dynamic'` so
|
|
22
|
-
* Next never statically caches a build-time (empty) response.
|
|
23
|
-
*
|
|
24
|
-
* @see `@akinon/next/hooks` — `useClientEnv` / `getClientEnv` read from here.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
const CLIENT_ENV_NAME = /^NEXT_PUBLIC_[A-Za-z0-9_]+$/;
|
|
28
|
-
|
|
29
|
-
export async function GET(request: NextRequest) {
|
|
30
|
-
try {
|
|
31
|
-
const { searchParams } = new URL(request.url);
|
|
32
|
-
|
|
33
|
-
const names = Array.from(
|
|
34
|
-
new Set(
|
|
35
|
-
searchParams
|
|
36
|
-
.getAll('env')
|
|
37
|
-
.flatMap((value) => value.split(','))
|
|
38
|
-
.map((value) => value.trim())
|
|
39
|
-
.filter(Boolean)
|
|
40
|
-
)
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
if (names.length === 0) {
|
|
44
|
-
return NextResponse.json(
|
|
45
|
-
{ message: 'At least one `env` query parameter is required.' },
|
|
46
|
-
{ status: 400 }
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const invalid = names.filter((name) => !CLIENT_ENV_NAME.test(name));
|
|
51
|
-
|
|
52
|
-
if (invalid.length > 0) {
|
|
53
|
-
return NextResponse.json(
|
|
54
|
-
{
|
|
55
|
-
message:
|
|
56
|
-
'Only NEXT_PUBLIC_-prefixed environment variables can be read.',
|
|
57
|
-
invalid
|
|
58
|
-
},
|
|
59
|
-
{ status: 400 }
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const values = names.reduce<Record<string, string | null>>((acc, name) => {
|
|
64
|
-
acc[name] = process.env[name] ?? null;
|
|
65
|
-
return acc;
|
|
66
|
-
}, {});
|
|
67
|
-
|
|
68
|
-
return NextResponse.json(values, {
|
|
69
|
-
headers: { 'Cache-Control': 'no-store' }
|
|
70
|
-
});
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.error('Error in client-env API:', error);
|
|
73
|
-
return NextResponse.json(
|
|
74
|
-
{ message: 'Internal server error' },
|
|
75
|
-
{ status: 500 }
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
}
|