From 5801be04665005c4229642982a394efcdec5595c Mon Sep 17 00:00:00 2001 From: Daniel Lorigan Date: Tue, 14 Jan 2025 20:18:33 -0800 Subject: [PATCH] WIP carin api, fetcher and param matcher --- src/lib/components/app/FetchCARINBB.svelte | 222 +++++++++++++++++++++ src/params/carin.ts | 5 + src/routes/api/proxy/+server.ts | 48 +++++ 3 files changed, 275 insertions(+) create mode 100644 src/lib/components/app/FetchCARINBB.svelte create mode 100644 src/params/carin.ts create mode 100644 src/routes/api/proxy/+server.ts diff --git a/src/lib/components/app/FetchCARINBB.svelte b/src/lib/components/app/FetchCARINBB.svelte new file mode 100644 index 0000000..c5e42ea --- /dev/null +++ b/src/lib/components/app/FetchCARINBB.svelte @@ -0,0 +1,222 @@ + +
prepareIps()}> + + + {#each SOF_HOSTS as host} + + + {#if host.note} +

{@html host.note}

+ {/if} +
+ {/each} +
+ + + + + + + + + + + + {#if processing || loadingSample} + + + + {/if} + +
+{fetchError} diff --git a/src/params/carin.ts b/src/params/carin.ts new file mode 100644 index 0000000..1b35323 --- /dev/null +++ b/src/params/carin.ts @@ -0,0 +1,5 @@ +import type { ParamMatcher } from '@sveltejs/kit'; + +export const match = ((param: string): param is ('acentra' | 'aetna' | 'carefirst' | 'cpcds' | 'humana') => { + return param === 'acentra' || param === 'aetna' || param === 'carefirst' || param === 'cpcds' || param === 'humana'; +}) satisfies ParamMatcher; \ No newline at end of file diff --git a/src/routes/api/proxy/+server.ts b/src/routes/api/proxy/+server.ts new file mode 100644 index 0000000..a570d50 --- /dev/null +++ b/src/routes/api/proxy/+server.ts @@ -0,0 +1,48 @@ +import { json } from '@sveltejs/kit'; +import { env } from '$env/dynamic/private'; // Access private environment variables + +const CPCDS_URL = 'https://cpcds-server.lantanagroup.com/fhir'; +const SMART_LAUNCH_URL = `${CPCDS_URL}/.well-known/smart-configuration`; + +export const POST = async ({ request, url }: { request: Request; url: URL }) => { + try { + // Get the base URL path to the current route + const proxyPath = url.pathname; + + // Construct the forward URL by removing the base path (dynamic) + const forwardPath = proxyPath.replace(url.origin, ''); // Strip the origin + const forwardQuery = url.search; // Includes the "?" and any query parameters + + // Construct the full target API URL + const targetUrl = `https://api.example.com${forwardPath}${forwardQuery}`; + + // Read the incoming request body + const body = await request.text(); // Use `text()` for flexibility (JSON, form data, etc.) + + // Set up headers and include the client secret + const clientSecret = env.CLIENT_SECRET; + + const headers = new Headers(request.headers); + headers.set('Authorization', `Basic ${btoa(`client_id:${clientSecret}`)}`); + headers.set('Content-Type', request.headers.get('Content-Type') || 'application/json'); + + // Forward the request to the target API + const apiResponse = await fetch(targetUrl, { + method: request.method, + headers, + body: ['POST', 'PUT', 'PATCH'].includes(request.method) ? body : undefined, // Include body if applicable + }); + + // Return the API response to the client + const responseText = await apiResponse.text(); + return new Response(responseText, { + status: apiResponse.status, + headers: apiResponse.headers, // Pass response headers + }); + } catch (error) { + console.error('Proxy error:', error); + + // Handle and return errors + return json({ error: 'An error occurred while forwarding the request' }, { status: 500 }); + } +}; \ No newline at end of file