Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Jun 12, 2023
1 parent 498d1c8 commit 70d50b9
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 150 deletions.
8 changes: 3 additions & 5 deletions src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function createNitroApp(): NitroApp {
fetchWithEvent(event, req as any, init, { fetch: $fetch });
})
);

const renderer = handlers.find(({ route }) => route === "/**");
const rendererHandler = lazyEventHandler(renderer.handler);

Expand All @@ -84,16 +84,14 @@ function createNitroApp(): NitroApp {

for (const h of formActionsHandlers) {
let handler = h.lazy ? lazyEventHandler(h.handler) : h.handler;
const routeRules = getRouteRulesForPath(
h.route.replace(/:\w+|\*\*/g, "_")
);
const routeRules = getRouteRulesForPath(h.route.replace(/:\w+|\*\*/g, "_"));
if (routeRules.cache) {
handler = cachedEventHandler(handler, {
group: "nitro/routes",
...routeRules.cache,
});
}
formActionsRouter.use(h.route, handler, "post")
formActionsRouter.use(h.route, handler, "post");
formActionsRouter.use(h.route, rendererHandler, "get");
}

Expand Down
8 changes: 3 additions & 5 deletions src/runtime/newapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function createNitroApp(): NitroApp {
fetchWithEvent(event, req as any, init, { fetch: $fetch });
})
);

const renderer = handlers.find(({ route }) => route === "/**");
const rendererHandler = lazyEventHandler(renderer.handler);

Expand All @@ -84,16 +84,14 @@ function createNitroApp(): NitroApp {

for (const h of formActionsHandlers) {
let handler = h.lazy ? lazyEventHandler(h.handler) : h.handler;
const routeRules = getRouteRulesForPath(
h.route.replace(/:\w+|\*\*/g, "_")
);
const routeRules = getRouteRulesForPath(h.route.replace(/:\w+|\*\*/g, "_"));
if (routeRules.cache) {
handler = cachedEventHandler(handler, {
group: "nitro/routes",
...routeRules.cache,
});
}
formActionsRouter.use(h.route, handler, "post")
formActionsRouter.use(h.route, handler, "post");
formActionsRouter.use(h.route, rendererHandler, "get");
}

Expand Down
279 changes: 144 additions & 135 deletions src/runtime/oldapp.ts
Original file line number Diff line number Diff line change
@@ -1,142 +1,151 @@
import {
App as H3App,
createApp,
createRouter,
eventHandler,
lazyEventHandler,
Router,
toNodeListener,
fetchWithEvent,
} from "h3";
import { createFetch, Headers } from "ofetch";
import destr from "destr";
import {
createCall,
createFetch as createLocalFetch,
} from "unenv/runtime/fetch/index";
import { createHooks, Hookable } from "hookable";
import { useRuntimeConfig } from "./config";
import { cachedEventHandler } from "./cache";
import { createRouteRulesHandler, getRouteRulesForPath } from "./route-rules";
import { plugins } from "#internal/nitro/virtual/plugins";
import errorHandler from "#internal/nitro/virtual/error-handler";
import { type HandlerDefinition, handlers } from "#internal/nitro/virtual/server-handlers";

export interface NitroApp {
h3App: H3App;
router: Router;
// TODO: Type hooks and allow extending
hooks: Hookable;
localCall: ReturnType<typeof createCall>;
localFetch: ReturnType<typeof createLocalFetch>;
App as H3App,
createApp,
createRouter,
eventHandler,
lazyEventHandler,
Router,
toNodeListener,
fetchWithEvent,
} from "h3";
import { createFetch, Headers } from "ofetch";
import destr from "destr";
import {
createCall,
createFetch as createLocalFetch,
} from "unenv/runtime/fetch/index";
import { createHooks, Hookable } from "hookable";
import { useRuntimeConfig } from "./config";
import { cachedEventHandler } from "./cache";
import { createRouteRulesHandler, getRouteRulesForPath } from "./route-rules";
import { plugins } from "#internal/nitro/virtual/plugins";
import errorHandler from "#internal/nitro/virtual/error-handler";
import {
type HandlerDefinition,
handlers,
} from "#internal/nitro/virtual/server-handlers";

export interface NitroApp {
h3App: H3App;
router: Router;
// TODO: Type hooks and allow extending
hooks: Hookable;
localCall: ReturnType<typeof createCall>;
localFetch: ReturnType<typeof createLocalFetch>;
}

function createNitroApp(): NitroApp {
const config = useRuntimeConfig();

const hooks = createHooks();

const h3App = createApp({
debug: destr(process.env.DEBUG),
onError: errorHandler,
});

const router = createRouter();

h3App.use(createRouteRulesHandler());

// Create local fetch callers
const localCall = createCall(toNodeListener(h3App) as any);
const localFetch = createLocalFetch(localCall, globalThis.fetch);
const $fetch = createFetch({
fetch: localFetch,
Headers,
defaults: { baseURL: config.app.baseURL },
});
// @ts-ignore
globalThis.$fetch = $fetch;

// A generic event handler give nitro acess to the requests
h3App.use(
eventHandler((event) => {
// Init nitro context
event.context.nitro = event.context.nitro || {};
// Support platform context provided by local fetch
const envContext = (event.node.req as any).__unenv__;
if (envContext) {
Object.assign(event.context, envContext);
}
// Assign bound fetch to context
event.fetch = (req, init) =>
fetchWithEvent(event, req as any, init, { fetch: localFetch });
event.$fetch = (req, init) =>
fetchWithEvent(event, req as any, init, { fetch: $fetch });
})
);

interface HandlerWithFallback extends HandlerDefinition {
fallback?: boolean;
fallbackTo?: string;
fallbackMethod?: HandlerDefinition["method"];
}

function createNitroApp(): NitroApp {
const config = useRuntimeConfig();

const hooks = createHooks();

const h3App = createApp({
debug: destr(process.env.DEBUG),
onError: errorHandler,
});

const router = createRouter();

h3App.use(createRouteRulesHandler());

// Create local fetch callers
const localCall = createCall(toNodeListener(h3App) as any);
const localFetch = createLocalFetch(localCall, globalThis.fetch);
const $fetch = createFetch({
fetch: localFetch,
Headers,
defaults: { baseURL: config.app.baseURL },
});
// @ts-ignore
globalThis.$fetch = $fetch;

// A generic event handler give nitro acess to the requests
h3App.use(
eventHandler((event) => {
// Init nitro context
event.context.nitro = event.context.nitro || {};
// Support platform context provided by local fetch
const envContext = (event.node.req as any).__unenv__;
if (envContext) {
Object.assign(event.context, envContext);
}
// Assign bound fetch to context
event.fetch = (req, init) =>
fetchWithEvent(event, req as any, init, { fetch: localFetch });
event.$fetch = (req, init) =>
fetchWithEvent(event, req as any, init, { fetch: $fetch });
})
);

interface HandlerWithFallback extends HandlerDefinition {
fallback?: boolean;
fallbackTo?: string;
fallbackMethod?: HandlerDefinition["method"];

const getHandlersWithFallbacks = (
handlers: HandlerDefinition[]
): HandlerWithFallback[] => {
const routesWithFallbacks = handlers
.filter((h) => h.formAction)
.map((h) => {
return {
...h,
fallback: true,
fallbackTo: "/**",
fallbackMethod: "get" as const,
};
});
return [...handlers, ...routesWithFallbacks];
};

for (const h of getHandlersWithFallbacks(handlers)) {
let handler = h.lazy ? lazyEventHandler(h.handler) : h.handler;
if (h.middleware || !h.route) {
const middlewareBase = (config.app.baseURL + (h.route || "/")).replace(
/\/+/g,
"/"
);
h3App.use(middlewareBase, handler);
} else {
const routeRules = getRouteRulesForPath(
h.route.replace(/:\w+|\*\*/g, "_")
);
if (routeRules.cache) {
handler = cachedEventHandler(handler, {
group: "nitro/routes",
...routeRules.cache,
});
}

const getHandlersWithFallbacks = (handlers: HandlerDefinition[]): HandlerWithFallback[] => {
const routesWithFallbacks = handlers
.filter((h) => h.formAction)
.map((h) => {
return { ...h, fallback: true, fallbackTo: "/**", fallbackMethod: "get" as const };
});
return [...handlers, ...routesWithFallbacks];
};

for (const h of getHandlersWithFallbacks(handlers)) {
let handler = h.lazy ? lazyEventHandler(h.handler) : h.handler;
if (h.middleware || !h.route) {
const middlewareBase = (config.app.baseURL + (h.route || "/")).replace(
/\/+/g,
"/"
);
h3App.use(middlewareBase, handler);
if (h.fallback === true) {
const fallback = handlers.find(({ route }) => route === h.fallbackTo);
const fallbackHandler = h.lazy
? lazyEventHandler(fallback.handler)
: fallback.handler;
router.use(h.route, fallbackHandler, h.fallbackMethod);
} else {
const routeRules = getRouteRulesForPath(
h.route.replace(/:\w+|\*\*/g, "_")
);
if (routeRules.cache) {
handler = cachedEventHandler(handler, {
group: "nitro/routes",
...routeRules.cache,
});
}
if (h.fallback === true) {
const fallback = handlers.find(({ route }) => route === h.fallbackTo);
const fallbackHandler = h.lazy
? lazyEventHandler(fallback.handler)
: fallback.handler;
router.use(h.route, fallbackHandler, h.fallbackMethod);
} else {
router.use(h.route, handler, h.method);
}
router.use(h.route, handler, h.method);
}
}

h3App.use(config.app.baseURL, router);

const app: NitroApp = {
hooks,
h3App,
router,
localCall,
localFetch,
};

for (const plugin of plugins) {
plugin(app);
}

return app;
}

export const nitroApp: NitroApp = createNitroApp();

export const useNitroApp = () => nitroApp;


h3App.use(config.app.baseURL, router);

const app: NitroApp = {
hooks,
h3App,
router,
localCall,
localFetch,
};

for (const plugin of plugins) {
plugin(app);
}

return app;
}

export const nitroApp: NitroApp = createNitroApp();

export const useNitroApp = () => nitroApp;
20 changes: 15 additions & 5 deletions test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve } from "pathe";
import { listen, Listener } from "listhen";
import destr from "destr";
import { fetch, FetchOptions } from "ofetch";
import { expect, it, afterAll, beforeAll } from "vitest";
import { expect, it, afterAll, beforeAll, describe } from "vitest";
import { fileURLToPath } from "mlly";
import { joinURL } from "ufo";
import * as _nitro from "../src";
Expand All @@ -21,6 +21,14 @@ export interface Context {
isDev: boolean;
}

// https://github.com/unjs/nitro/pull/1240
export const describeIf = (condition, title, factory) =>
condition
? describe(title, factory)
: describe(title, () => {
it.skip("skipped", () => {});
});

export async function setupTest(preset: string) {
const fixtureDir = fileURLToPath(new URL("fixture", import.meta.url).href);

Expand Down Expand Up @@ -150,12 +158,12 @@ export function testNitro(
});

it("Form Action Works", async () => {
const {data: actionData } = await callHandler({
const { data: actionData } = await callHandler({
url: "/action",
method: "POST",
body: JSON.stringify({hello: "world"})
body: JSON.stringify({ hello: "world" })
})
expect(actionData).to.toMatchObject({ data: { hello: "world" }});
expect(actionData).to.toMatchObject({ data: { hello: "world" } });
});

it("handles route rules - redirects", async () => {
Expand Down Expand Up @@ -346,7 +354,9 @@ export function testNitro(
sharedRuntimeConfig: {
dynamic:
// TODO
ctx.preset.includes("cloudflare") || ctx.preset === "nitro-dev"
ctx.preset.includes("cloudflare") ||
ctx.preset === "vercel-edge" ||
ctx.preset === "nitro-dev"
? "initial"
: "from-env",
app: {
Expand Down

0 comments on commit 70d50b9

Please sign in to comment.