Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Anna Henningsen <[email protected]>
  • Loading branch information
paula-stacho and addaleax authored Feb 26, 2024
1 parent c467c15 commit 906985e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions packages/oidc-http-server-pages/src/create-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ function getPageTemplates({
}): ITemplate[] {
const templates: ITemplate[] = [];
for (const paramsSubset of allSubsets(parameters)) {
const propsObject = paramsSubset.reduce((obj, prop) => {
obj[prop] = placeholder(prop);
return obj;
}, {} as Record<string, string>);
const propsObject = Object.fromEntries(
paramsSubset.map((prop) => [prop, placeholder(prop)])
);
const markup = renderStylesToString(
renderToStaticMarkup(React.createElement(Component, propsObject))
);
Expand Down
8 changes: 4 additions & 4 deletions packages/oidc-http-server-pages/src/get-static-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ITemplate, HttpServerPage, PageTemplates } from './types';

function findTemplate(templates: ITemplate[], parameterKeys: string[]) {
function findTemplate(templates: ITemplate[], parameterKeys: string[]): ITemplate | undefined {
const parametersJoined = parameterKeys.sort().join('-');
for (const template of templates) {
const templateParametersJoined = Object.keys(template.parameters)
Expand All @@ -13,15 +13,15 @@ function findTemplate(templates: ITemplate[], parameterKeys: string[]) {
function replacePlaceholders(
template: ITemplate,
parameters: Record<string, string | undefined>
) {
): string {
let { html } = template;
for (const [key, placeholder] of Object.entries(template.parameters)) {
html = html.replaceAll(placeholder, escapeHTML(parameters[key] as string));
}
return html;
}

function escapeHTML(str: string) {
function escapeHTML(str: string): string {
return str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
Expand All @@ -34,7 +34,7 @@ export function getStaticPage<TPage extends string = HttpServerPage>(
page: TPage,
parameters: Record<string, string | undefined>,
templates?: PageTemplates<TPage>
) {
): string {
if (!templates) {
templates = require('./templates.json');
}
Expand Down

0 comments on commit 906985e

Please sign in to comment.