Skip to content

Commit 2b907f3

Browse files
authored
client (common): improve header handling (#31647)
1 parent 1c813dc commit 2b907f3

File tree

16 files changed

+159
-32
lines changed

16 files changed

+159
-32
lines changed

generators/angular/generators/bootstrap/__snapshots__/generator.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ exports[`generator - angular:bootstrap bootstrapping default config should prepa
183183
"messageBrokerKafka": false,
184184
"messageBrokerNo": true,
185185
"messageBrokerPulsar": false,
186+
"messageHeaderNameAlert": "x-jhipsterapp-alert",
187+
"messageHeaderNameError": "x-jhipsterapp-error",
188+
"messageHeaderNameParam": "x-jhipsterapp-params",
186189
"microfrontend": false,
187190
"microfrontends": undefined,
188191
"monorepository": undefined,

generators/app/__snapshots__/generator.spec.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,9 @@ exports[`generator - app with default config should match snapshot 1`] = `
640640
"messageBrokerKafka": false,
641641
"messageBrokerNo": true,
642642
"messageBrokerPulsar": false,
643+
"messageHeaderNameAlert": "x-jhipsterapp-alert",
644+
"messageHeaderNameError": "x-jhipsterapp-error",
645+
"messageHeaderNameParam": "x-jhipsterapp-params",
643646
"microfrontend": false,
644647
"microfrontends": undefined,
645648
"monorepository": undefined,
@@ -1323,6 +1326,9 @@ exports[`generator - app with gateway should match snapshot 1`] = `
13231326
"messageBrokerKafka": false,
13241327
"messageBrokerNo": true,
13251328
"messageBrokerPulsar": false,
1329+
"messageHeaderNameAlert": "x-jhipsterapp-alert",
1330+
"messageHeaderNameError": "x-jhipsterapp-error",
1331+
"messageHeaderNameParam": "x-jhipsterapp-params",
13261332
"microfrontend": undefined,
13271333
"microfrontends": undefined,
13281334
"monorepository": undefined,

generators/client/__snapshots__/application.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ exports[`application mutation test expects mutateApplication to match snapshot 1
1111
"clientThemeAny": true,
1212
"clientThemeNone": false,
1313
"frontendAppName": "baseNameApp",
14+
"messageHeaderNameAlert": "x-basenameapp-alert",
15+
"messageHeaderNameError": "x-basenameapp-error",
16+
"messageHeaderNameParam": "x-basenameapp-params",
1417
"microfrontend": "clientFrameworkAny",
1518
"webappLoginRegExp": "^[a-zA-Z0-9!$&*+=?^_\`{|}~.-]+@[a-zA-Z0-9-]+(?:\\\\.[a-zA-Z0-9-]+)*$|^[_.@A-Za-z0-9-]+$",
1619
"withAdminUi": "applicationTypeMicroservice",

generators/client/application.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export type ClientAddedApplicationProperties = {
4040
clientBundlerName: string;
4141
clientTestFrameworkName: string;
4242
withAdminUi: boolean;
43+
44+
messageHeaderNameAlert: string;
45+
messageHeaderNameError: string;
46+
messageHeaderNameParam: string;
4347
};
4448

4549
export const mutateApplication = {
@@ -64,4 +68,8 @@ export const mutateApplication = {
6468
clientBundlerName: ctx => (ctx.clientBundlerEsbuild ? 'esbuild' : startCase(ctx.clientBundler)),
6569
clientTestFrameworkName: ctx => startCase(ctx.clientTestFramework),
6670
withAdminUi: ctx => ctx.applicationTypeMicroservice,
71+
72+
messageHeaderNameAlert: ({ frontendAppName }) => `x-${frontendAppName.toLowerCase()}-alert`,
73+
messageHeaderNameError: ({ frontendAppName }) => `x-${frontendAppName.toLowerCase()}-error`,
74+
messageHeaderNameParam: ({ frontendAppName }) => `x-${frontendAppName.toLowerCase()}-params`,
6775
} as const satisfies MutateDataPropertiesWithRequiredProperties<MutateDataParam<ClientApplication>, ClientAddedApplicationProperties>;

generators/client/generators/bootstrap/__snapshots__/generator.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ exports[`generator - client:bootstrap bootstrapping default config should prepar
184184
"messageBrokerKafka": false,
185185
"messageBrokerNo": true,
186186
"messageBrokerPulsar": false,
187+
"messageHeaderNameAlert": "x-jhipsterapp-alert",
188+
"messageHeaderNameError": "x-jhipsterapp-error",
189+
"messageHeaderNameParam": "x-jhipsterapp-params",
187190
"microfrontend": false,
188191
"microfrontends": undefined,
189192
"monorepository": undefined,

generators/client/generators/common/__snapshots__/generator.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ exports[`generator - client:common with defaults options should match files snap
77
".yo-rc.json": {
88
"stateCleared": "modified",
99
},
10+
"src/main/webapp/app/shared/jhipster/constants.ts": {
11+
"stateCleared": "modified",
12+
},
1013
"src/main/webapp/app/shared/jhipster/headers.ts": {
1114
"stateCleared": "modified",
1215
},

generators/client/generators/common/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class CommonGenerator extends BaseApplicationGenerator {
3636
await this.writeFiles({
3737
blocks: [
3838
clientApplicationTemplatesBlock({
39-
templates: ['shared/jhipster/problem-details.ts', 'shared/jhipster/headers.ts'],
39+
templates: ['shared/jhipster/constants.ts', 'shared/jhipster/problem-details.ts', 'shared/jhipster/headers.ts'],
4040
}),
4141
],
4242
context: application,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%#
2+
Copyright 2013-2025 the original author or authors from the JHipster project.
3+
4+
This file is part of the JHipster project, see https://www.jhipster.tech/
5+
for more information.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
https://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
-%>
19+
export const MESSAGE_ALERT_HEADER_NAME = '<%= messageHeaderNameAlert %>';
20+
export const MESSAGE_ERROR_HEADER_NAME = '<%= messageHeaderNameError %>';
21+
export const MESSAGE_PARAM_HEADER_NAME = '<%= messageHeaderNameParam %>';
22+
<%_ if (authenticationUsesCsrf) { _%>
23+
export const CSRF_TOKEN_HEADER_NAME = 'X-XSRF-TOKEN';
24+
export const CSRF_TOKEN_COOKIE_NAME = 'XSRF-TOKEN';
25+
<%_ } _%>

generators/client/generators/common/templates/src/main/webapp/app/shared/jhipster/headers.ts.ejs

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,36 @@
1616
See the License for the specific language governing permissions and
1717
limitations under the License.
1818
-%>
19+
<%_ if (!microfrontend && !applicationTypeGateway) { _%>
20+
import { MESSAGE_ALERT_HEADER_NAME, MESSAGE_ERROR_HEADER_NAME, MESSAGE_PARAM_HEADER_NAME } from './constants';
21+
22+
<%_ } _%>
1923
export type HeaderMessage = {
20-
/** Success message */
24+
/** Success translation key or message */
2125
alert?: string;
22-
/** Error message */
26+
<%_ if (enableTranslation) { _%>
27+
/** Success translation key */
28+
alertKey?: string;
29+
<%_ } _%>
30+
/** Success message */
31+
alertMessage?: string;
32+
33+
/** Error translation key or message */
2334
error?: string;
35+
<%_ if (enableTranslation) { _%>
36+
/** Error translation key */
37+
errorKey?: string;
38+
<%_ } _%>
39+
/** Error message */
40+
errorMessage?: string;
2441
/** Entity id for success messages. Entity name for error messages. */
2542
param?: string;
2643
};
2744

28-
const headerToString = (headerValue: any): string => {
45+
const headerToString = (headerValue: any): string | undefined => {
46+
if (headerValue === undefined) {
47+
return undefined;
48+
}
2949
if (Array.isArray(headerValue)) {
3050
if (headerValue.length > 1) {
3151
throw new Error('Multiple header values found');
@@ -38,20 +58,41 @@ const headerToString = (headerValue: any): string => {
3858
return headerValue;
3959
};
4060

41-
const decodeHeaderValue = (headerValue: string): string => decodeURIComponent(headerValue.replace(/\+/g, ' '));
61+
const decodeHeaderValue = (headerValue?: string): string | undefined =>
62+
headerValue ? decodeURIComponent(headerValue.replace(/\+/g, ' ')) : headerValue;
4263

4364
export const getMessageFromHeaders = (headers: Record<string, any>): HeaderMessage => {
44-
let alert: string | undefined = undefined;
45-
let param: string | undefined = undefined;
46-
let error: string | undefined = undefined;
47-
for (const [key, value] of Object.entries(headers)) {
48-
if (key.toLowerCase().endsWith('-alert')) {
49-
alert = headerToString(value);
50-
} else if (key.toLowerCase().endsWith('-error')) {
51-
error = headerToString(value);
52-
} else if (key.toLowerCase().endsWith('-params')) {
53-
param = decodeHeaderValue(headerToString(value));
54-
}
55-
}
56-
return { alert, error, param };
65+
const alertHeader = headerToString(headers[<% if (microfrontend || applicationTypeGateway) { %>Object.keys(headers).find(key => /x-(.*)-alert/.test(key))<%_ } else { _%>MESSAGE_ALERT_HEADER_NAME<%_ } _%>]);
66+
<%_ if (enableTranslation) { _%>
67+
// Try to determine if the alertHeader is a key or a message
68+
const alertIsKey = /^[A-Za-z0-9](?:[A-Za-z0-9.]*[A-Za-z0-9])?$/.test(alertHeader ?? '');
69+
const alertKey = alertIsKey ? alertHeader : undefined;
70+
const alertMessage = alertIsKey ? undefined : alertHeader;
71+
72+
<%_ } _%>
73+
const errorHeader = headerToString(headers[<% if (microfrontend || applicationTypeGateway) { %>Object.keys(headers).find(key => /x-(.*)-error/.test(key))<%_ } else { _%>MESSAGE_ERROR_HEADER_NAME<%_ } _%>]);
74+
<%_ if (enableTranslation) { _%>
75+
// Try to determine if the errorHeader is a key or a message
76+
const errorIsKey = /^[A-Za-z0-9](?:[A-Za-z0-9.]*[A-Za-z0-9])?$/.test(errorHeader ?? '');
77+
const errorKey = errorIsKey ? errorHeader : undefined;
78+
const errorMessage = errorIsKey ? undefined : errorHeader;
79+
<%_ } _%>
80+
81+
return {
82+
alert: alertHeader,
83+
<%_ if (enableTranslation) { _%>
84+
alertKey,
85+
alertMessage,
86+
<%_ } else { _%>
87+
alertMessage: alertHeader,
88+
<%_ } _%>
89+
error: errorHeader,
90+
<%_ if (enableTranslation) { _%>
91+
errorKey,
92+
errorMessage,
93+
<%_ } else { _%>
94+
errorMessage: errorHeader,
95+
<%_ } _%>
96+
param: decodeHeaderValue(headerToString(headers[<% if (microfrontend || applicationTypeGateway) { %>Object.keys(headers).find(key => /x-(.*)-params/.test(key))<%_ } else { _%>MESSAGE_PARAM_HEADER_NAME<%_ } _%>])),
97+
};
5798
};

generators/client/generators/common/templates/src/main/webapp/app/shared/jhipster/problem-details.ts.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export type ProblemWithMessage = ProblemDetails & {
4242

4343
/** Translation message key */
4444
message?: string;
45+
/** Translation message parameter */
46+
params?: string;
4547
/** Request path */
4648
path?: string;
4749
/** Field errors */

0 commit comments

Comments
 (0)