From 9eea430c1e10d10e425b5c8a8637b0a7855858a6 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 5 Jun 2021 19:44:15 +0200 Subject: [PATCH] fix: various issues arising from oak removal (#23) --- example/api_deps.ts | 1 - example/pages/api/joke.ts | 7 +++---- src/cli/deps.ts | 6 +++++- src/cli/init.ts | 15 +++++---------- src/cli/routes.ts | 7 +++++-- src/server/router.ts | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) delete mode 100644 example/api_deps.ts diff --git a/example/api_deps.ts b/example/api_deps.ts deleted file mode 100644 index 2840fd7f..00000000 --- a/example/api_deps.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "../server.ts"; diff --git a/example/pages/api/joke.ts b/example/pages/api/joke.ts index 33c0dffa..33e02bd2 100644 --- a/example/pages/api/joke.ts +++ b/example/pages/api/joke.ts @@ -1,5 +1,3 @@ -import { oak } from "../../api_deps.ts"; - // Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/ const JOKES = [ "Why do Java developers often wear glasses? They can’t C#.", @@ -14,7 +12,8 @@ const JOKES = [ "An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.", ]; -export default (ctx: oak.Context) => { +export default (_req: Request): Response => { const randomIndex = Math.floor(Math.random() * 10); - ctx.response.body = JOKES[randomIndex]; + const body = JOKES[randomIndex]; + return new Response(body); }; diff --git a/src/cli/deps.ts b/src/cli/deps.ts index edf85fe1..c0195b87 100644 --- a/src/cli/deps.ts +++ b/src/cli/deps.ts @@ -1,5 +1,9 @@ // std -export { join, toFileUrl } from "https://deno.land/std@0.95.0/path/mod.ts"; +export { + join, + resolve, + toFileUrl, +} from "https://deno.land/std@0.95.0/path/mod.ts"; export { walk } from "https://deno.land/std@0.95.0/fs/walk.ts"; export { parse as parseArgs } from "https://deno.land/std@0.95.0/flags/mod.ts"; export { diff --git a/src/cli/init.ts b/src/cli/init.ts index 09a0dd2a..4d4e6f5f 100644 --- a/src/cli/init.ts +++ b/src/cli/init.ts @@ -61,11 +61,6 @@ async function init(directory: string) { import.meta.url, )}";\n`; await Deno.writeTextFile(join(directory, "deps.ts"), DEPS_TS); - const API_DEPS_TS = `export * from "${new URL( - "../../server.ts", - import.meta.url, - )}";\n`; - await Deno.writeTextFile(join(directory, "api_deps.ts"), API_DEPS_TS); const PAGES_INDEX_TSX = `import { h, IS_BROWSER, useState } from "../deps.ts"; export default function Home() { @@ -112,9 +107,8 @@ export default function Greet(props: Props) { join(directory, "pages", "[name].tsx"), PAGES_GREET_TSX, ); - const PAGES_API_JOKE_TS = `import { oak } from "../../api_deps.ts"; - -// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/ + const PAGES_API_JOKE_TS = + `// Jokes courtesy of https://punsandoneliners.com/randomness/programmer-jokes/ const JOKES = [ "Why do Java developers often wear glasses? They can’t C#.", "A SQL query walks into a bar, goes up to two tables and says “can I join you?", @@ -128,9 +122,10 @@ const JOKES = [ "An SEO expert walked into a bar, pub, inn, tavern, hostelry, public house.", ]; -export default (ctx: oak.Context) => { +export default (_req: Request): Response => { const randomIndex = Math.floor(Math.random() * 10); - ctx.response.body = JOKES[randomIndex]; + const body = JOKES[randomIndex]; + return new Response(body); }; `; await Deno.writeTextFile( diff --git a/src/cli/routes.ts b/src/cli/routes.ts index d17b0067..6c345a35 100644 --- a/src/cli/routes.ts +++ b/src/cli/routes.ts @@ -1,4 +1,4 @@ -import { join, toFileUrl, walk } from "./deps.ts"; +import { join, resolve, toFileUrl, walk } from "./deps.ts"; const help = `fresh routes @@ -37,6 +37,7 @@ export async function routesSubcommand(rawArgs: Record) { } export async function routes(directory: string) { + directory = resolve(directory); const files = []; const pagesDir = join(directory, "./pages"); const pagesUrl = new URL(pagesDir, "file:///"); @@ -71,5 +72,7 @@ const routes = { export default routes; `; - await Deno.writeTextFile(join(directory, "./routes.gen.ts"), output); + const routeManifestPath = join(directory, "./routes.gen.ts"); + await Deno.writeTextFile(routeManifestPath, output); + console.log("Route manifest generated and written to", routeManifestPath); } diff --git a/src/server/router.ts b/src/server/router.ts index 5c5509cb..faf97a22 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -34,7 +34,7 @@ export function installRoutes( routes[apiRoute.route] = apiRoute.handler; } - routes[`/${INTERNAL_PREFIX}/${JS_PREFIX}/${BUILD_ID}/:path*`] = + routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = internalBundleAssetRoute(bundler); return routes;