Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow customizing apiBaseURL, apiDir and routesDir #1763

Merged
merged 11 commits into from
Apr 29, 2024
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