Skip to content

Commit

Permalink
feat: allow customizing apiBaseURL, apiDir and routesDir (#1763)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
igorjacauna and pi0 authored Apr 29, 2024
1 parent 6b6777e commit 665f630
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
18 changes: 18 additions & 0 deletions docs/3.config/0.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ Default: `/`{lang=ts} (or `NITRO_APP_BASE_URL` environment variable if provided)

Server's main base URL.

### `apiBaseURL`

- Default : `/api`

Changes the default api base URL prefix.

### `handlers`

Server handlers and routes.
Expand Down Expand Up @@ -408,6 +414,18 @@ Project source directory. Same as `rootDir` unless specified. Helpful to move co

List of directories to scan and auto-register files, such as API routes.

### `apiDir`

- Default : `api`

Defines a different directory to scan for api route handlers.

### `routesDir`

- Default : `routes`

Defines a different directory to scan for route handlers.

### `buildDir`

- Default: `.nitro`
Expand Down
10 changes: 7 additions & 3 deletions src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ export async function scanHandlers(nitro: Nitro) {
const middleware = await scanMiddleware(nitro);

const handlers = await Promise.all([
scanServerRoutes(nitro, "api", "/api"),
scanServerRoutes(nitro, "routes", "/"),
scanServerRoutes(
nitro,
nitro.options.apiDir || "api",
nitro.options.apiBaseURL || "/api"
),
scanServerRoutes(nitro, nitro.options.routesDir || "routes"),
]).then((r) => r.flat());

nitro.scannedHandlers = [
Expand Down Expand Up @@ -49,7 +53,7 @@ export async function scanMiddleware(nitro: Nitro) {

export async function scanServerRoutes(
nitro: Nitro,
dir: "routes" | "api",
dir: string,
prefix = "/"
) {
const files = await scanFiles(nitro, dir);
Expand Down
3 changes: 3 additions & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ export interface NitroOptions extends PresetOptions {
rootDir: string;
srcDir: string;
scanDirs: string[];
apiDir: string;
routesDir: string;
buildDir: string;
output: {
dir: string;
Expand Down Expand Up @@ -372,6 +374,7 @@ export interface NitroOptions extends PresetOptions {

// Routing
baseURL: string;
apiBaseURL: string;
handlers: NitroEventHandler[];
routeRules: { [path: string]: NitroRouteRules };
devHandlers: NitroDevEventHandler[];
Expand Down

0 comments on commit 665f630

Please sign in to comment.