-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Happy-DOM version of NAV Dekoratøren
- Loading branch information
Showing
12 changed files
with
266 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { DecoratorFetchProps, DecoratorUrlProps } from '@app/nav-dekoratoren/types'; | ||
import { getDecoratorUrl } from '@app/nav-dekoratoren/urls'; | ||
|
||
export const getCsrElements = (csrProps: DecoratorFetchProps) => { | ||
const props: DecoratorUrlProps = { | ||
...csrProps, | ||
csr: true, | ||
}; | ||
|
||
const envUrl = getDecoratorUrl(props); | ||
const assetsUrl = getDecoratorUrl({ ...props, params: undefined }); | ||
const scriptSrc = `${assetsUrl}/client.js`; | ||
|
||
return { | ||
header: '<div id="decorator-header"></div>', | ||
footer: '<div id="decorator-footer"></div>', | ||
env: `<div id="decorator-env" data-src="${envUrl}"></div>`, | ||
styles: `<link href="${assetsUrl}/css/client.css" rel="stylesheet" />`, | ||
scripts: `<script src="${scriptSrc}" async></script>`, | ||
scriptSrc, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { getCsrElements } from '@app/nav-dekoratoren/csr-elements'; | ||
import type { DecoratorFetchProps } from '@app/nav-dekoratoren/types'; | ||
import { getDecoratorUrl } from '@app/nav-dekoratoren/urls'; | ||
import { Window } from 'happy-dom'; | ||
|
||
export type DecoratorElements = { | ||
DECORATOR_STYLES: string; | ||
DECORATOR_SCRIPTS: string; | ||
DECORATOR_HEADER: string; | ||
DECORATOR_FOOTER: string; | ||
}; | ||
|
||
const fetchDecorator = async (url: string, props: DecoratorFetchProps, retries = 3): Promise<DecoratorElements> => | ||
fetch(url) | ||
.then((res) => { | ||
if (res.ok) { | ||
return res.text(); | ||
} | ||
throw new Error(`${res.status} - ${res.statusText}`); | ||
}) | ||
.then((html) => { | ||
const window = new Window(); | ||
window.document.write(html); | ||
const { document } = window; | ||
|
||
const styles = document.getElementById('styles')?.innerHTML; | ||
if (!styles) { | ||
throw new Error('Decorator styles element not found!'); | ||
} | ||
|
||
const scripts = document.getElementById('scripts')?.innerHTML; | ||
if (!scripts) { | ||
throw new Error('Decorator scripts element not found!'); | ||
} | ||
|
||
const header = document.getElementById('header-withmenu')?.innerHTML; | ||
if (!header) { | ||
throw new Error('Decorator header element not found!'); | ||
} | ||
|
||
const footer = document.getElementById('footer-withmenu')?.innerHTML; | ||
if (!footer) { | ||
throw new Error('Decorator footer element not found!'); | ||
} | ||
|
||
const elements = { | ||
DECORATOR_STYLES: styles.trim(), | ||
DECORATOR_SCRIPTS: scripts.trim(), | ||
DECORATOR_HEADER: header.trim(), | ||
DECORATOR_FOOTER: footer.trim(), | ||
}; | ||
|
||
return elements; | ||
}) | ||
.catch((e) => { | ||
if (retries > 0) { | ||
console.warn(`Failed to fetch decorator, retrying ${retries} more times - Url: ${url} - Error: ${e}`); | ||
return fetchDecorator(url, props, retries - 1); | ||
} | ||
|
||
throw e; | ||
}); | ||
|
||
export const fetchDecoratorHtml = async (props: DecoratorFetchProps): Promise<DecoratorElements> => { | ||
const url = getDecoratorUrl(props); | ||
|
||
return fetchDecorator(url, props).catch((e) => { | ||
console.error( | ||
`Failed to fetch decorator, falling back to elements for client-side rendering - Url: ${url} - Error: ${e}`, | ||
); | ||
|
||
const csrElements = getCsrElements(props); | ||
|
||
return { | ||
DECORATOR_STYLES: csrElements.styles, | ||
DECORATOR_SCRIPTS: `${csrElements.env}${csrElements.scripts}`, | ||
DECORATOR_HEADER: csrElements.header, | ||
DECORATOR_FOOTER: csrElements.footer, | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
export type DecoratorLocale = 'nb' | 'nn' | 'en' | 'se' | 'pl' | 'uk' | 'ru'; | ||
|
||
export type DecoratorLanguageOption = | ||
| { | ||
url?: string; | ||
locale: DecoratorLocale; | ||
handleInApp: true; | ||
} | ||
| { | ||
url: string; | ||
locale: DecoratorLocale; | ||
handleInApp?: false; | ||
}; | ||
|
||
export type DecoratorBreadcrumb = { | ||
url: string; | ||
title: string; | ||
analyticsTitle?: string; | ||
handleInApp?: boolean; | ||
}; | ||
|
||
export type DecoratorNaisEnv = 'prod' | 'dev' | 'beta' | 'betaTms' | 'devNext' | 'prodNext'; | ||
|
||
export type DecoratorEnvProps = | ||
| { env: 'localhost'; localUrl: string } | ||
| { env: DecoratorNaisEnv; serviceDiscovery?: boolean }; | ||
|
||
export type DecoratorFetchProps = { | ||
params?: DecoratorParams; | ||
noCache?: boolean; | ||
} & DecoratorEnvProps; | ||
|
||
export type DecoratorUrlProps = { | ||
csr?: boolean; | ||
} & DecoratorFetchProps; | ||
|
||
export type DecoratorParams = Partial<{ | ||
context: 'privatperson' | 'arbeidsgiver' | 'samarbeidspartner'; | ||
simple: boolean; | ||
simpleHeader: boolean; | ||
simpleFooter: boolean; | ||
enforceLogin: boolean; | ||
redirectToApp: boolean; | ||
redirectToUrl: string; | ||
redirectToUrlLogout: string; | ||
level: string; | ||
language: DecoratorLocale; | ||
availableLanguages: DecoratorLanguageOption[]; | ||
breadcrumbs: DecoratorBreadcrumb[]; | ||
utilsBackground: 'white' | 'gray' | 'transparent'; | ||
feedback: boolean; | ||
chatbot: boolean; | ||
chatbotVisible: boolean; | ||
urlLookupTable: boolean; | ||
shareScreen: boolean; | ||
logoutUrl: string; | ||
logoutWarning: boolean; | ||
}>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { NAIS_CLUSTER_NAME } from '@app/config/config'; | ||
import type { | ||
DecoratorBreadcrumb, | ||
DecoratorLanguageOption, | ||
DecoratorNaisEnv, | ||
DecoratorUrlProps, | ||
} from '@app/nav-dekoratoren/types'; | ||
|
||
type NaisUrls = Record<DecoratorNaisEnv, string>; | ||
|
||
const externalUrls: NaisUrls = { | ||
prod: 'https://www.nav.no/dekoratoren', | ||
dev: 'https://dekoratoren.ekstern.dev.nav.no', | ||
beta: 'https://dekoratoren-beta.intern.dev.nav.no', | ||
betaTms: 'https://dekoratoren-beta-tms.intern.dev.nav.no', | ||
devNext: 'https://decorator-next.ekstern.dev.nav.no', | ||
prodNext: 'https://www.nav.no/decorator-next', | ||
}; | ||
|
||
const serviceUrls: NaisUrls = { | ||
prod: 'http://nav-dekoratoren.personbruker', | ||
dev: 'http://nav-dekoratoren.personbruker', | ||
beta: 'http://nav-dekoratoren-beta.personbruker', | ||
betaTms: 'http://nav-dekoratoren-beta-tms.personbruker', | ||
devNext: 'http://decorator-next.personbruker', | ||
prodNext: 'http://decorator-next.personbruker', | ||
}; | ||
|
||
const naisGcpClusters: Record<string, true> = { | ||
'dev-gcp': true, | ||
'prod-gcp': true, | ||
}; | ||
|
||
const objectToQueryString = ( | ||
params: Record<string, boolean | string | DecoratorLanguageOption[] | DecoratorBreadcrumb[]>, | ||
) => | ||
params | ||
? Object.entries(params).reduce( | ||
(acc, [k, v], i) => | ||
v !== undefined | ||
? `${acc}${i ? '&' : '?'}${k}=${encodeURIComponent(typeof v === 'object' ? JSON.stringify(v) : v)}` | ||
: acc, | ||
'', | ||
) | ||
: ''; | ||
|
||
const isNaisApp = () => typeof process !== 'undefined' && NAIS_CLUSTER_NAME && naisGcpClusters[NAIS_CLUSTER_NAME]; | ||
|
||
const getNaisUrl = (env: DecoratorNaisEnv, csr = false, serviceDiscovery = true) => { | ||
const shouldUseServiceDiscovery = serviceDiscovery && !csr && isNaisApp(); | ||
|
||
return (shouldUseServiceDiscovery ? serviceUrls[env] : externalUrls[env]) || externalUrls.prod; | ||
}; | ||
|
||
export const getDecoratorUrl = (props: DecoratorUrlProps) => { | ||
const { env, params, csr } = props; | ||
const baseUrl = env === 'localhost' ? props.localUrl : getNaisUrl(env, csr, props.serviceDiscovery); | ||
|
||
if (!params) { | ||
return baseUrl; | ||
} | ||
|
||
return `${baseUrl}/${csr ? 'env' : ''}${objectToQueryString(params)}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters