Skip to content

Commit

Permalink
emulate context in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Nov 22, 2023
1 parent 37cb8d3 commit 53775ba
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/netlify/src/integration-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { generateEdgeMiddleware } from './middleware.ts';
import { createRedirects, type Options } from './shared.ts';
import type { Connect } from 'vite';

export const NETLIFY_EDGE_MIDDLEWARE_FILE = 'netlify-edge-middleware';

Expand Down Expand Up @@ -34,6 +35,7 @@ export function getAdapter(options: Required<Options> & InternalOptions): AstroA
interface InternalOptions {
adapterName: string;
functionType: 'lambda-compatible' | 'builders' | 'v2';
devMiddleware?: Connect.HandleFunction
dist?: URL
}

Expand All @@ -42,11 +44,12 @@ class StacklessError extends Error { trace = undefined }
export function getIntegration({
dist,
adapterName,
binaryMediaTypes = [],
functionType,
devMiddleware,
builders = false,
binaryMediaTypes = [],
functionPerRoute = false,
edgeMiddleware = false,
functionType
}: Options & InternalOptions): AstroIntegration {

if (functionType === 'v2' && builders) {
Expand Down Expand Up @@ -96,6 +99,9 @@ export function getIntegration({
logger.warn('Otherwise, this adapter is not required to deploy a static site to Netlify.');
}
},
'astro:server:setup' ({ server }) {
if (devMiddleware) server.middlewares.use(devMiddleware)
},
async 'astro:build:done' ({ routes, dir }) {
const functionsConfig = {
version: 1,
Expand Down
53 changes: 52 additions & 1 deletion packages/netlify/src/integration-v2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
import { getIntegration } from "./integration-base.ts";
import type { Options } from "./shared.ts";
import type { Connect } from "vite";

export default function (options: Omit<Options, "builders">) {
return getIntegration({
...options,
adapterName: "@astrojs/netlify/v2",
functionType: "v2"
functionType: "v2",
devMiddleware
});
}

function devMiddleware([ request ]: Parameters<Connect.SimpleHandleFunction>) {
const context = Object.assign(Object.create(duplicatedContext), demoContext, { rewrite })
Reflect.set(
request,
Symbol.for("astro.locals"),
{ context }
);
}

// these properties of netlify context have equivalents in Astro
// we want to avoid providing multiple ways of doing the same thing
const duplicatedContext = {
get cookies() { throw new Error("Please use Astro.cookies or context.cookies instead.") },
get ip () { throw new Error("Please use Astro.clientAddress or context.clientAddress instead.") },
get json() { throw new Error("Please use Response.json instead.") },
get params() { throw new Error("Please use Astro.params or context.params instead.") },
}

const demoContext = {
account: { id: '66825ac773b8b53ec7a92755' },
deploy: { id: '67a781b80398785b0bb556f2' },
geo: {
city: 'Zurich',
country: { code: 'CH', name: 'Switzerland' },
subdivision: { code: 'ZH', name: 'Zurich' },
timezone: 'Europe/Zurich',
latitude: 47.3682,
longitude: 8.5671
},
log: console.log,
requestId: '01HFT9RZXSW1GPBE2TT7HWAJPQ',
server: { region: 'us-east-1' },
site: {
id: '345f2a26-befd-45f1-ada1-655d33d6e3d8',
name: 'your-site-name',
url: 'https://your-site-name.netlify.app'
}
}

function rewrite(input: string | URL, orginalUrl: string) {

const destinationUrl =
input instanceof URL ? input :
input.startsWith("/") ? new URL(input, orginalUrl) :
new URL(input);

return fetch(destinationUrl);
}

0 comments on commit 53775ba

Please sign in to comment.