Skip to content

Commit

Permalink
feat!: make fsRouter a named export of mod.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
justinawrey committed Aug 20, 2022
1 parent fa710ea commit 68e774b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Initialize `fsrouter` with the following `mod.ts`:

```typescript
// my-app/mod.ts
import fsRouter from "https://deno.land/x/fsrouter@{VERSION}/mod.ts";
import { fsRouter } from "https://deno.land/x/fsrouter@{VERSION}/mod.ts";
import { serve } from "https://deno.land/std@{VERSION}/http/server.ts";

// Use the file system router with base directory 'pages'
Expand Down
62 changes: 54 additions & 8 deletions public/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,61 @@ function bootMessage(fileMap: FileMap, rootDir: string) {
console.log("");
}

type RouterOptions = {
/**
* A collection of options to be passed in on initialization.
*/
interface RouterOptions {
/**
* Whether or not an information message should be shown on startup.
* Defaults to true.
*/
bootMessage?: boolean;
};
}

// fsRouter creates a Handler which handles requests
// according to the shape of the filesystem at the given rootDir.
// Each file within rootDir must provide a Handler as its default export.
// The provided handler will be used to execute requests if the requested route
// matches the file's position in the filesystem.
/**
* fsRouter creates a Handler which handles requests
* according to the shape of the filesystem at the given rootDir.
* Each file within rootDir must provide a Handler as its default
* export, which will be used to execute requests if the requested
* route matches the file's position in the filesystem.
*
* Given a project with the following folder structure:
*
* ```bash
* my-app/
* ├─ pages/
* │ ├─ blog/
* │ │ ├─ post.ts
* │ │ ├─ index.ts
* │ ├─ about.ts
* │ ├─ index.ts
* ├─ mod.ts
* ```
*
* Each "route file" must export a Handler as its default export:
*
* ```typescript
* // my-app/pages/blog/post.ts
* export default (req: Request) => {
* return new Response("hello world!");
* };
* ```
*
* Initialize `fsrouter`:
*
* ```typescript
* // my-app/mod.ts
* import { fsRouter } from "https://deno.land/x/fsrouter@{VERSION}/mod.ts";
* import { serve } from "https://deno.land/std@{VERSION}/http/server.ts";
*
* // Use the file system router with base directory 'pages'
* serve(await fsRouter("pages"));
* ```
*
* @param rootDir The directory at which routes will be served
* @param opts An optional options object
* @returns A Promise which resolves to a Handler
*/
async function fsRouter(
rootDir: string,
opts: RouterOptions = {
Expand Down Expand Up @@ -113,4 +159,4 @@ async function fsRouter(
return handleRoutes(routeMap);
}

export default fsRouter;
export { fsRouter, type RouterOptions };
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { serve } from "./deps.ts";
import fsRouter from "../public/mod.ts";
import { fsRouter } from "../public/mod.ts";

if (!import.meta.main) {
Deno.exit();
Expand Down

0 comments on commit 68e774b

Please sign in to comment.