diff --git a/index.html b/index.html index 50fce15..aacc32c 100644 --- a/index.html +++ b/index.html @@ -5,8 +5,7 @@ Fastlege - %VITE_INTERNARBEIDSFLATEDECORATOR_CSS% - %VITE_INTERNARBEIDSFLATEDECORATOR_JS% + %VITE_INTERNARBEIDSFLATEDECORATOR_CSS% %VITE_INTERNARBEIDSFLATEDECORATOR_JS% diff --git a/package.json b/package.json index 9dfdb67..e0d705a 100644 --- a/package.json +++ b/package.json @@ -85,4 +85,4 @@ "public" ] } -} \ No newline at end of file +} diff --git a/public/mockServiceWorker.js b/public/mockServiceWorker.js index b65d2b9..22dc000 100644 --- a/public/mockServiceWorker.js +++ b/public/mockServiceWorker.js @@ -8,128 +8,128 @@ * - Please do NOT serve this file on production. */ -const PACKAGE_VERSION = '2.4.8' -const INTEGRITY_CHECKSUM = '26357c79639bfa20d64c0efca2a87423' -const IS_MOCKED_RESPONSE = Symbol('isMockedResponse') -const activeClientIds = new Set() +const PACKAGE_VERSION = "2.4.8"; +const INTEGRITY_CHECKSUM = "26357c79639bfa20d64c0efca2a87423"; +const IS_MOCKED_RESPONSE = Symbol("isMockedResponse"); +const activeClientIds = new Set(); -self.addEventListener('install', function () { - self.skipWaiting() -}) +self.addEventListener("install", function () { + self.skipWaiting(); +}); -self.addEventListener('activate', function (event) { - event.waitUntil(self.clients.claim()) -}) +self.addEventListener("activate", function (event) { + event.waitUntil(self.clients.claim()); +}); -self.addEventListener('message', async function (event) { - const clientId = event.source.id +self.addEventListener("message", async function (event) { + const clientId = event.source.id; if (!clientId || !self.clients) { - return + return; } - const client = await self.clients.get(clientId) + const client = await self.clients.get(clientId); if (!client) { - return + return; } const allClients = await self.clients.matchAll({ - type: 'window', - }) + type: "window", + }); switch (event.data) { - case 'KEEPALIVE_REQUEST': { + case "KEEPALIVE_REQUEST": { sendToClient(client, { - type: 'KEEPALIVE_RESPONSE', - }) - break + type: "KEEPALIVE_RESPONSE", + }); + break; } - case 'INTEGRITY_CHECK_REQUEST': { + case "INTEGRITY_CHECK_REQUEST": { sendToClient(client, { - type: 'INTEGRITY_CHECK_RESPONSE', + type: "INTEGRITY_CHECK_RESPONSE", payload: { packageVersion: PACKAGE_VERSION, checksum: INTEGRITY_CHECKSUM, }, - }) - break + }); + break; } - case 'MOCK_ACTIVATE': { - activeClientIds.add(clientId) + case "MOCK_ACTIVATE": { + activeClientIds.add(clientId); sendToClient(client, { - type: 'MOCKING_ENABLED', + type: "MOCKING_ENABLED", payload: true, - }) - break + }); + break; } - case 'MOCK_DEACTIVATE': { - activeClientIds.delete(clientId) - break + case "MOCK_DEACTIVATE": { + activeClientIds.delete(clientId); + break; } - case 'CLIENT_CLOSED': { - activeClientIds.delete(clientId) + case "CLIENT_CLOSED": { + activeClientIds.delete(clientId); const remainingClients = allClients.filter((client) => { - return client.id !== clientId - }) + return client.id !== clientId; + }); // Unregister itself when there are no more clients if (remainingClients.length === 0) { - self.registration.unregister() + self.registration.unregister(); } - break + break; } } -}) +}); -self.addEventListener('fetch', function (event) { - const { request } = event +self.addEventListener("fetch", function (event) { + const { request } = event; // Bypass navigation requests. - if (request.mode === 'navigate') { - return + if (request.mode === "navigate") { + return; } // Opening the DevTools triggers the "only-if-cached" request // that cannot be handled by the worker. Bypass such requests. - if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { - return + if (request.cache === "only-if-cached" && request.mode !== "same-origin") { + return; } // Bypass all requests when there are no active clients. // Prevents the self-unregistered worked from handling requests // after it's been deleted (still remains active until the next reload). if (activeClientIds.size === 0) { - return + return; } // Generate unique request ID. - const requestId = crypto.randomUUID() - event.respondWith(handleRequest(event, requestId)) -}) + const requestId = crypto.randomUUID(); + event.respondWith(handleRequest(event, requestId)); +}); async function handleRequest(event, requestId) { - const client = await resolveMainClient(event) - const response = await getResponse(event, client, requestId) + const client = await resolveMainClient(event); + const response = await getResponse(event, client, requestId); // Send back the response clone for the "response:*" life-cycle events. // Ensure MSW is active and ready to handle the message, otherwise // this message will pend indefinitely. if (client && activeClientIds.has(client.id)) { - ;(async function () { - const responseClone = response.clone() + (async function () { + const responseClone = response.clone(); sendToClient( client, { - type: 'RESPONSE', + type: "RESPONSE", payload: { requestId, isMockedResponse: IS_MOCKED_RESPONSE in response, @@ -140,12 +140,12 @@ async function handleRequest(event, requestId) { headers: Object.fromEntries(responseClone.headers.entries()), }, }, - [responseClone.body], - ) - })() + [responseClone.body] + ); + })(); } - return response + return response; } // Resolve the main client for the given event. @@ -153,49 +153,49 @@ async function handleRequest(event, requestId) { // that registered the worker. It's with the latter the worker should // communicate with during the response resolving phase. async function resolveMainClient(event) { - const client = await self.clients.get(event.clientId) + const client = await self.clients.get(event.clientId); - if (client?.frameType === 'top-level') { - return client + if (client?.frameType === "top-level") { + return client; } const allClients = await self.clients.matchAll({ - type: 'window', - }) + type: "window", + }); return allClients .filter((client) => { // Get only those clients that are currently visible. - return client.visibilityState === 'visible' + return client.visibilityState === "visible"; }) .find((client) => { // Find the client ID that's recorded in the // set of clients that have registered the worker. - return activeClientIds.has(client.id) - }) + return activeClientIds.has(client.id); + }); } async function getResponse(event, client, requestId) { - const { request } = event + const { request } = event; // Clone the request because it might've been already used // (i.e. its body has been read and sent to the client). - const requestClone = request.clone() + const requestClone = request.clone(); function passthrough() { - const headers = Object.fromEntries(requestClone.headers.entries()) + const headers = Object.fromEntries(requestClone.headers.entries()); // Remove internal MSW request header so the passthrough request // complies with any potential CORS preflight checks on the server. // Some servers forbid unknown request headers. - delete headers['x-msw-intention'] + delete headers["x-msw-intention"]; - return fetch(requestClone, { headers }) + return fetch(requestClone, { headers }); } // Bypass mocking when the client is not active. if (!client) { - return passthrough() + return passthrough(); } // Bypass initial page load requests (i.e. static assets). @@ -203,15 +203,15 @@ async function getResponse(event, client, requestId) { // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet // and is not ready to handle requests. if (!activeClientIds.has(client.id)) { - return passthrough() + return passthrough(); } // Notify the client that a request has been intercepted. - const requestBuffer = await request.arrayBuffer() + const requestBuffer = await request.arrayBuffer(); const clientMessage = await sendToClient( client, { - type: 'REQUEST', + type: "REQUEST", payload: { id: requestId, url: request.url, @@ -229,39 +229,39 @@ async function getResponse(event, client, requestId) { keepalive: request.keepalive, }, }, - [requestBuffer], - ) + [requestBuffer] + ); switch (clientMessage.type) { - case 'MOCK_RESPONSE': { - return respondWithMock(clientMessage.data) + case "MOCK_RESPONSE": { + return respondWithMock(clientMessage.data); } - case 'PASSTHROUGH': { - return passthrough() + case "PASSTHROUGH": { + return passthrough(); } } - return passthrough() + return passthrough(); } function sendToClient(client, message, transferrables = []) { return new Promise((resolve, reject) => { - const channel = new MessageChannel() + const channel = new MessageChannel(); channel.port1.onmessage = (event) => { if (event.data && event.data.error) { - return reject(event.data.error) + return reject(event.data.error); } - resolve(event.data) - } + resolve(event.data); + }; client.postMessage( message, - [channel.port2].concat(transferrables.filter(Boolean)), - ) - }) + [channel.port2].concat(transferrables.filter(Boolean)) + ); + }); } async function respondWithMock(response) { @@ -270,15 +270,15 @@ async function respondWithMock(response) { // instance will have status code set to 0. Since it's not possible to create // a Response instance with status code 0, handle that use-case separately. if (response.status === 0) { - return Response.error() + return Response.error(); } - const mockedResponse = new Response(response.body, response) + const mockedResponse = new Response(response.body, response); Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, { value: true, enumerable: true, - }) + }); - return mockedResponse + return mockedResponse; } diff --git a/server/config.ts b/server/config.ts index 06f0774..8ca87db 100644 --- a/server/config.ts +++ b/server/config.ts @@ -1,4 +1,3 @@ - import dotenv from "dotenv"; dotenv.config(); diff --git a/src/index.tsx b/src/index.tsx index 6b3a7ee..4648048 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -48,16 +48,16 @@ function renderApp() { - ) + ); } async function setupMocking() { - const { worker } = await import('./mocks/browser'); + const { worker } = await import("./mocks/browser"); return worker.start(); } if (erLokal()) { setupMocking().then(() => renderApp()); } else { - renderApp() + renderApp(); } diff --git a/src/mocks/browser.ts b/src/mocks/browser.ts index bc2beec..e40e6c2 100644 --- a/src/mocks/browser.ts +++ b/src/mocks/browser.ts @@ -1,4 +1,4 @@ import { setupWorker } from "msw/browser"; import handlers from "@/mocks/handlers"; -export const worker = setupWorker(...handlers); \ No newline at end of file +export const worker = setupWorker(...handlers); diff --git a/src/mocks/handlers.ts b/src/mocks/handlers.ts index d53762a..be7aeb1 100644 --- a/src/mocks/handlers.ts +++ b/src/mocks/handlers.ts @@ -8,6 +8,6 @@ const handlers = [ mockIstilgangskontroll, ...mockModiacontextholder, ...mockSyfoperson, -] +]; -export default handlers; \ No newline at end of file +export default handlers; diff --git a/src/mocks/mockFastlegerest.ts b/src/mocks/mockFastlegerest.ts index 226c589..0f7039d 100644 --- a/src/mocks/mockFastlegerest.ts +++ b/src/mocks/mockFastlegerest.ts @@ -85,9 +85,11 @@ const fastlegeList = [ }, ]; -const mockFastlegerest = - http.get("/fastlegerest/api/v2/fastlege/fastleger", () => { +const mockFastlegerest = http.get( + "/fastlegerest/api/v2/fastlege/fastleger", + () => { return HttpResponse.json(fastlegeList); - }); + } +); export default mockFastlegerest; diff --git a/src/mocks/mockIstilgangskontroll.ts b/src/mocks/mockIstilgangskontroll.ts index 59895f5..23283d9 100644 --- a/src/mocks/mockIstilgangskontroll.ts +++ b/src/mocks/mockIstilgangskontroll.ts @@ -1,13 +1,12 @@ import { http, HttpResponse } from "msw"; -const mockIstilgangskontroll = - http.get( - "/istilgangskontroll/api/tilgang/navident/syfo", - () => { - return HttpResponse.json({ - erGodkjent: true, - }); - } - ); +const mockIstilgangskontroll = http.get( + "/istilgangskontroll/api/tilgang/navident/syfo", + () => { + return HttpResponse.json({ + erGodkjent: true, + }); + } +); export default mockIstilgangskontroll; diff --git a/src/mocks/mockModiacontextholder.ts b/src/mocks/mockModiacontextholder.ts index 3d90e9c..652a5ac 100644 --- a/src/mocks/mockModiacontextholder.ts +++ b/src/mocks/mockModiacontextholder.ts @@ -32,26 +32,17 @@ const mockModiacontextholder = [ return HttpResponse.json(saksbehandler); }), - http.get( - "/modiacontextholder/api/context/aktivbruker", - () => { - return HttpResponse.json(aktivBruker); - } - ), - - http.get( - "/modiacontextholder/api/context/aktivenhet", - () => { - return HttpResponse.json(aktivEnhet); - } - ), - - http.post( - "/modiacontextholder/api/context", - () => { - return new HttpResponse(null, { status: 204 }); - } - ), + http.get("/modiacontextholder/api/context/aktivbruker", () => { + return HttpResponse.json(aktivBruker); + }), + + http.get("/modiacontextholder/api/context/aktivenhet", () => { + return HttpResponse.json(aktivEnhet); + }), + + http.post("/modiacontextholder/api/context", () => { + return new HttpResponse(null, { status: 204 }); + }), ]; export default mockModiacontextholder; diff --git a/src/mocks/mockSyfoperson.ts b/src/mocks/mockSyfoperson.ts index 7236f90..9722b5b 100644 --- a/src/mocks/mockSyfoperson.ts +++ b/src/mocks/mockSyfoperson.ts @@ -6,37 +6,31 @@ const diskresjonskode = "7"; const isEgenAnsatt = true; const mockSyfoperson = [ - http.get( - "/syfoperson/api/v2/person/diskresjonskode", - ({ request }) => { - if ( - request.headers.has(NAV_PERSONIDENT_HEADER) && - request.headers.get(NAV_PERSONIDENT_HEADER)?.length === 11 - ) { - return HttpResponse.json(diskresjonskode); - } else { - return HttpResponse.text("Did not find PersonIdent in headers", { - status: 400, - }); - } + http.get("/syfoperson/api/v2/person/diskresjonskode", ({ request }) => { + if ( + request.headers.has(NAV_PERSONIDENT_HEADER) && + request.headers.get(NAV_PERSONIDENT_HEADER)?.length === 11 + ) { + return HttpResponse.json(diskresjonskode); + } else { + return HttpResponse.text("Did not find PersonIdent in headers", { + status: 400, + }); } - ), + }), - http.get( - "/syfoperson/api/v2/person/egenansatt", - ({ request }) => { - if ( - request.headers.has(NAV_PERSONIDENT_HEADER) && - request.headers.get(NAV_PERSONIDENT_HEADER)?.length === 11 - ) { - return HttpResponse.json(isEgenAnsatt); - } else { - return HttpResponse.text("Did not find PersonIdent in headers", { - status: 400, - }); - } + http.get("/syfoperson/api/v2/person/egenansatt", ({ request }) => { + if ( + request.headers.has(NAV_PERSONIDENT_HEADER) && + request.headers.get(NAV_PERSONIDENT_HEADER)?.length === 11 + ) { + return HttpResponse.json(isEgenAnsatt); + } else { + return HttpResponse.text("Did not find PersonIdent in headers", { + status: 400, + }); } - ) -] + }), +]; export default mockSyfoperson; diff --git a/vite.config.ts b/vite.config.ts index a96c0a2..58fad35 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,5 +1,5 @@ -import { defineConfig } from 'vite'; -import react from '@vitejs/plugin-react'; +import { defineConfig } from "vite"; +import react from "@vitejs/plugin-react"; import tsconfigPaths from "vite-tsconfig-paths"; export default defineConfig({ @@ -10,5 +10,5 @@ export default defineConfig({ server: { open: true, port: 8080, - } + }, });