Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint factory fetch #204

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/client/src/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { EventName, CustomEvents, MessageEvents } from './events';
import { Params } from 'decorator-shared/params';
import { Auth } from './api';
import { AnalyticsEventArgs } from './analytics/constants';
import { DecoratorEndpointFn } from 'decorator-shared/urls';

declare global {
interface Window {
__DECORATOR_DATA__: AppState;
makeEndpoint: decoratorEndpointFn;
loginDebug: {
expireToken: (seconds: number) => void;
expireSession: (seconds: number) => void;
Expand Down
4 changes: 3 additions & 1 deletion packages/client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import { addFaroMetaData } from './faro';
import { analyticsLoaded, analyticsReady, createEvent } from './events';
import { type ParamKey } from 'decorator-shared/params';
import { param, hasParam, updateDecoratorParams, env } from './params';
import { makeEndpointFactory } from 'decorator-shared/urls';

console.log('Decorator client loaded');

import.meta.glob('./styles/*.css', { eager: true });

window.makeEndpoint = makeEndpointFactory(() => window.__DECORATOR_DATA__.params, env('APP_URL'));

// Just for testing
export const CONTEXTS = ['privatperson', 'arbeidsgiver', 'samarbeidspartner'] as const;

Expand Down
9 changes: 0 additions & 9 deletions packages/server/src/lib/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,3 @@ export class HandlerBuilder {
return this.handlers;
}
}

// function makeHeaders() {
// const headers = new Headers()
// return new Headers(headers);
// }

/**
* A helper function for creating a JSON response.
*/
68 changes: 14 additions & 54 deletions packages/server/src/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { cspHandler } from './csp';
import { clientEnv, env } from './env/server';
import { HandlerBuilder, r } from './lib/handler';
import { getMockSession, refreshToken } from './mockAuth';
import renderIndex from './render-index';
import renderIndex, { renderFooter, renderHeader } from './render-index';
import jsonIndex from './json-index';
import SearchService from './search-service';
import TaConfigService from './task-analytics-service';
Expand All @@ -27,7 +27,6 @@ import { OpsMessages } from './views/ops-messages';
import { SearchHits } from './views/search-hits';
import { SimpleUserMenu } from './views/simple-user-menu';
import { NotificationsService } from './notifications-service';
import { Footer } from './views/footer/footer';
import { isExternallyAvailable } from 'decorator-shared/utils';
import { assetsHandlers } from './handlers/assets-handler';
import { csrHandler } from './csr';
Expand Down Expand Up @@ -219,67 +218,28 @@ const requestHandler = async (
.get('/header', async ({ query }) => {
const data = validParams(query);
const localTexts = texts[data.language];
const { language, breadcrumbs, availableLanguages, utilsBackground } = data;

const decoratorUtils = DecoratorUtils({
breadcrumbs,
availableLanguages,
localTexts,
utilsBackground,
hidden: isExternallyAvailable(clientEnv.APP_URL),
const header = await renderHeader({
texts: localTexts,
data,
contentService,
});

return rewriter.transform(
r()
.html(
(data.simple || data.simpleHeader
? SimpleHeader({
texts: localTexts,
decoratorUtils,
})
: ComplexHeader({
texts: localTexts,
contextLinks: makeContextLinks(env.XP_BASE_URL),
context: data.context,
language,
decoratorUtils,
opsMessages: data.ssr ? await contentService.getOpsMessages() : [],
})
).render()
)
.build()
);

return rewriter.transform(r().html(header.render()).build());
})
.get('/footer', async ({ query }) => {
const data = validParams(query);
const localTexts = texts[data.language];
const features = unleashService.getFeatures();
const footer = await renderFooter({
features,
texts: localTexts,
contentService,
data,
});

return rewriter.transform(
r()
.html(
Footer({
...(data.simpleFooter || data.simple
? {
simple: true,
links: await contentService.getSimpleFooterLinks({
language: data.language,
}),
}
: {
simple: false,
links: await contentService.getComplexFooterLinks({
language: data.language,
context: data.context,
}),
}),
data,
features,
texts: localTexts,
}).render()
)
.build()
);
return rewriter.transform(r().html(footer.render()).build());
})
.get('/scripts', async ({ query }) => {
const json = await jsonIndex({
Expand Down
4 changes: 3 additions & 1 deletion packages/shared/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export function getIdPortenLocale(language: Language) {
return idPortenLocaleMap[language];
}

export function makeEndpointFactory(defaultParams: () => Params, origin: string) {
export type DecoratorEndpointFn = (endpoint: string, params: Partial<Params>) => string;

export function makeEndpointFactory(defaultParams: () => Params, origin: string): DecoratorEndpointFn {
return (endpoint: string, params: Partial<Params>) => {
const formattedParams = formatParams({
...defaultParams(),
Expand Down
Loading