### Install SvelteKit Azure Adapter Source: https://github.com/shellicar/svelte-adapter-azure-functions/blob/main/packages/svelte-adapter-azure-functions/README.md Commands to install the adapter package using popular Node.js package managers. Choose either npm or pnpm based on your project configuration. ```bash npm i --save @shellicar/svelte-adapter-azure-functions ``` ```bash pnpm add @shellicar/svelte-adapter-azure-functions ``` -------------------------------- ### Install SvelteKit Azure Adapter Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Commands to add the adapter as a development dependency to your SvelteKit project using npm or pnpm. ```bash # Using npm npm install --save-dev @shellicar/svelte-adapter-azure-functions # Using pnpm pnpm add -D @shellicar/svelte-adapter-azure-functions ``` -------------------------------- ### Bash: Build and Deploy Workflow Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Demonstrates the build and deployment process for a SvelteKit application using the adapter, including dependency installation, building, local execution, and Azure deployment. ```bash # Install dependencies pnpm install # Build the SvelteKit application pnpm build # The adapter outputs to the 'build' directory: # build/ # ├── server/ # Azure Function files # │ ├── dist/ # Bundled server code # │ ├── host.json # Azure Functions host configuration # │ ├── local.settings.json # │ └── package.json # └── static/ # Static assets (client-side JS, CSS, prerendered pages) # Run locally with Azure Functions Core Tools cd build/server func start # Deploy to Azure (example using Azure CLI) az functionapp deployment source config-zip \ --resource-group \ --name \ --src build/server.zip ``` -------------------------------- ### Configure SvelteKit Adapter Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Basic setup for the adapter within the svelte.config.js file to enable Azure Functions deployment. ```javascript // svelte.config.js import adapter from '@shellicar/svelte-adapter-azure-functions'; import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; /** @type {import('@sveltejs/kit').Config} */ const config = { preprocess: vitePreprocess(), kit: { adapter: adapter() } }; export default config; ``` -------------------------------- ### Configure SvelteKit Adapter Source: https://github.com/shellicar/svelte-adapter-azure-functions/blob/main/packages/svelte-adapter-azure-functions/README.md Integration of the adapter into the SvelteKit configuration file. This setup is required in svelte.config.js to enable the Azure Functions build output. ```javascript import adapter from '@shellicar/svelte-adapter-azure-functions'; export default { kit: { adapter: adapter() } }; ``` -------------------------------- ### Configure esbuild Options for Azure Functions Adapter Source: https://github.com/shellicar/svelte-adapter-azure-functions/blob/main/README.md This JavaScript code shows how to pass custom esbuild options to the SvelteKit Azure Functions adapter. The example disables minification by setting `minify: false`. Default esbuild options are defined in `./src/defaults.ts`. ```javascript adapter({ esbuildOptions: { minify: false } }) ``` -------------------------------- ### TypeScript: Getting Client IP Address Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Shows how to retrieve the client's IP address within a SvelteKit API route using the `getClientAddress()` function provided by the adapter. It demonstrates extracting the IP from the `x-forwarded-for` header. ```typescript // src/routes/api/ip/+server.ts import type { RequestHandler } from './$types'; export const GET: RequestHandler = async ({ getClientAddress }) => { const clientIP = getClientAddress(); return new Response(JSON.stringify({ ip: clientIP, timestamp: new Date().toISOString() }), { headers: { 'Content-Type': 'application/json' } }); }; // The adapter extracts IP from x-forwarded-for header: // "203.0.113.195, 70.41.3.18, 150.172.238.178" -> "203.0.113.195" ``` -------------------------------- ### Default esbuild Configuration Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Reference for the default build options utilized by the adapter during the bundling process. ```typescript // Default build options used by the adapter const defaults: BuildOptions = { bundle: true, platform: 'node', target: 'node20', treeShaking: true, format: 'esm', splitting: true, minify: true, keepNames: true, sourcemap: true, outExtension: { '.js': '.mjs' } }; ``` -------------------------------- ### Customize esbuild Options Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt How to override default build settings by passing an esbuildOptions object to the adapter configuration. ```javascript // svelte.config.js import adapter from '@shellicar/svelte-adapter-azure-functions'; export default { kit: { adapter: adapter({ esbuildOptions: { minify: false, // Disable minification for debugging sourcemap: false, // Disable sourcemaps in production target: 'node18', // Target a different Node.js version keepNames: true, // Preserve function names for stack traces treeShaking: true // Enable tree shaking } }) } }; ``` -------------------------------- ### Access Platform Context in SvelteKit Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Demonstrates how to retrieve the Azure Functions InvocationContext and client principal information from the platform object in server-side routes. ```typescript // src/routes/api/user/+server.ts import type { RequestHandler } from './$types'; export const GET: RequestHandler = async ({ platform }) => { // Access Azure Functions InvocationContext const context = platform?.context; context?.log('Processing request...'); // Access authenticated user information from Azure App Service const clientPrincipal = platform?.clientPrincipal; if (!clientPrincipal) { return new Response(JSON.stringify({ authenticated: false }), { status: 401, headers: { 'Content-Type': 'application/json' } }); } // Extract user claims const userClaims = clientPrincipal.claims; const identityProvider = clientPrincipal.auth_typ; return new Response(JSON.stringify({ authenticated: true, provider: identityProvider, claims: userClaims }), { headers: { 'Content-Type': 'application/json' } }); }; ``` -------------------------------- ### JSON: Local Development Settings Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Configures local development settings for running Azure Functions with Azure Functions Core Tools, specifying the worker runtime and storage. ```json { "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "node", "AzureWebJobsStorage": "UseDevelopmentStorage=true", "SERVER_AUTH_LEVEL": "anonymous" } } ``` -------------------------------- ### JSON: Azure Functions Host Configuration Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Provides optimized settings for hosting SvelteKit applications on Azure Functions, including logging, extension bundles, and HTTP route prefixes. ```json { "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } }, "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.0.0, 5.0.0)" }, "extensions": { "http": { "routePrefix": "" } } } ``` -------------------------------- ### Configure Azure Function Authentication Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Setting the authentication level via environment variables and the resulting trigger configuration in the Azure Functions runtime. ```bash # Set authentication level in local.settings.json or Azure App Settings # Anonymous access (default) - no authentication required SERVER_AUTH_LEVEL=anonymous # Function-level authentication - requires function key SERVER_AUTH_LEVEL=function # Admin-level authentication - requires master key SERVER_AUTH_LEVEL=admin ``` ```typescript import { app } from '@azure/functions'; import { handler } from './handler'; const getAuthLevel = (level: string | undefined) => { switch (level) { case 'function': return 'function'; case 'admin': return 'admin'; case 'anonymous': return 'anonymous'; } return 'anonymous'; }; app.http('server', { handler, authLevel: getAuthLevel(process.env.SERVER_AUTH_LEVEL), route: '{*url}', methods: ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'] }); ``` -------------------------------- ### TypeScript: Client Principal Type Definitions Source: https://context7.com/shellicar/svelte-adapter-azure-functions/llms.txt Defines TypeScript types for Azure App Service authentication client principal data, including claims and user information. It also augments the global App.Platform interface for SvelteKit. ```typescript // Type definitions for Azure App Service client principal type ClientPrincipalClaim = { /** Claim type */ typ: string; /** Claim value */ val: string; }; type ClientPrincipal = { /** Identity Provider (e.g., 'aad', 'github', 'google') */ auth_typ: string; /** User claims array */ claims: ClientPrincipalClaim[]; /** Name claim type identifier */ name_typ: string; /** Role claim type identifier */ role_typ: string; }; // Global type augmentation for SvelteKit declare global { namespace App { interface Platform { clientPrincipal?: ClientPrincipal; context: InvocationContext; } } } ``` -------------------------------- ### Azure Function HTTP Trigger Configuration Source: https://github.com/shellicar/svelte-adapter-azure-functions/blob/main/README.md This code defines an Azure Function HTTP trigger named 'server'. It specifies the handler function, the route to match all URLs, and the allowed HTTP methods. This is the core of how the SvelteKit app is served as an Azure Function. ```typescript import { app } from '@azure/functions'; import { handler } from './handler'; app.http('server', { handler, route: '{*url}', methods: ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'] }); ``` -------------------------------- ### Azure Function with Configurable Auth Level Source: https://github.com/shellicar/svelte-adapter-azure-functions/blob/main/README.md This TypeScript snippet configures an Azure Function HTTP trigger with a dynamic authentication level. The `SERVER_AUTH_LEVEL` environment variable determines the auth level, defaulting to 'anonymous' if not set or invalid. It imports the handler and uses the `app` object from `@azure/functions`. ```typescript import { app } from '@azure/functions'; import { handler } from './handler'; const getAuthLevel = (level: string | undefined) => { switch(level) { case 'function': return 'function'; case 'admin': return 'admin'; case 'anonymous': return 'anonymous'; } return 'anonymous'; }; app.http('server', { handler, authLevel: getAuthLevel(process.env.SERVER_AUTH_LEVEL), route: '{*url}', methods: ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT'], }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.