|
1 | 1 | # Configuration |
2 | 2 |
|
3 | | -Most projects work with **zero config**. When you need to customize directories, add `statikapi.config.js` at the project root: |
| 3 | +StatikAPI works with **zero config**. When you need to customize paths, create a **`statikapi.config.js`** in your project root. |
4 | 4 |
|
5 | 5 | ```js |
6 | 6 | // statikapi.config.js |
7 | 7 | export default { |
8 | | - srcDir: "src-api", |
9 | | - outDir: "api-out" |
| 8 | + srcDir: 'src-api', |
| 9 | + outDir: 'api-out', |
10 | 10 | }; |
| 11 | +``` |
| 12 | + |
| 13 | +> Both directories are **relative to the project root**, must be **non-empty strings**, and **cannot be the same path**. Absolute paths and parent traversal (e.g. `../outside`) are not allowed. |
| 14 | +
|
| 15 | +--- |
| 16 | + |
| 17 | +## Fields |
| 18 | + |
| 19 | +### `srcDir` (string, default: `"src-api"`) |
| 20 | + |
| 21 | +- Folder that contains your **route modules**. |
| 22 | +- Supported extensions: **.js, .mjs, .cjs, .ts, .tsx**. |
| 23 | +- Files/folders that **start with `_`** are ignored (e.g. `_private/`). |
| 24 | +- `index.js` collapses to the folder route (e.g. `blog/index.js` → `/blog`). |
| 25 | + |
| 26 | +### `outDir` (string, default: `"api-out"`) |
| 27 | + |
| 28 | +- Folder where **generated JSON** is written. |
| 29 | +- Each URL path becomes a folder with **`index.json`** inside. |
| 30 | +- A build **manifest** is also written to `api-out/.statikapi/manifest.json` containing route → file mappings and sizes (handy for tooling/preview UIs). |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## Path resolution & validation |
| 35 | + |
| 36 | +The CLI normalizes and validates configuration: |
| 37 | + |
| 38 | +- Paths are **normalized** with POSIX separators. |
| 39 | +- Must be **relative** (no absolute paths). |
| 40 | +- Must **not traverse** outside the project (no leading `../`). |
| 41 | +- `srcDir` and `outDir` must be **different**. |
| 42 | + |
| 43 | +If validation fails, the CLI throws a helpful **ConfigError** before building. |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Override via CLI flags |
| 48 | + |
| 49 | +You can override config values per-invocation. The current CLI reads **`--srcDir`** and **`--outDir`**: |
| 50 | + |
| 51 | +<Tabs group="flags"> |
| 52 | + <Tab label="pnpm"> |
| 53 | + |
| 54 | +```bash |
| 55 | +pnpm statikapi build --srcDir api --outDir public/api |
| 56 | +``` |
| 57 | + |
| 58 | + </Tab> |
| 59 | + <Tab label="yarn"> |
| 60 | + |
| 61 | +```bash |
| 62 | +yarn statikapi build --srcDir api --outDir public/api |
| 63 | +``` |
| 64 | + |
| 65 | + </Tab> |
| 66 | + <Tab label="npm"> |
| 67 | + |
| 68 | +```bash |
| 69 | +npx statikapi build --srcDir api --outDir public/api |
| 70 | +``` |
| 71 | + |
| 72 | + </Tab> |
| 73 | +</Tabs> |
| 74 | + |
| 75 | +> Heads‑up: older notes might mention `--src` / `--out`. In the current CLI, use **`--srcDir`** and **`--outDir`** to affect configuration. |
| 76 | +
|
| 77 | +--- |
11 | 78 |
|
| 79 | +## Dev server notes |
| 80 | + |
| 81 | +When running **`statikapi dev`**: |
| 82 | + |
| 83 | +- Serves your built files and an interactive UI at `/_ui`. |
| 84 | +- Writes/updates `api-out/.statikapi/manifest.json` on changes. |
| 85 | +- Environment overrides (advanced): |
| 86 | + - **`STATIKAPI_UI_DIR`** — serve a custom, prebuilt UI directory (must contain `index.html`). |
| 87 | + - **`STATIKAPI_FORCE_DEV=1`** — keep dev running in non‑TTY environments (e.g., CI runners, `concurrently`). |
| 88 | + |
| 89 | +<Tabs group="dev-flags"> |
| 90 | + <Tab label="pnpm"> |
| 91 | + |
| 92 | +```bash |
| 93 | +pnpm statikapi dev --port 8788 --srcDir src-api --outDir api-out |
| 94 | +``` |
| 95 | + |
| 96 | + </Tab> |
| 97 | + <Tab label="yarn"> |
| 98 | + |
| 99 | +```bash |
| 100 | +yarn statikapi dev --port 8788 --srcDir src-api --outDir api-out |
| 101 | +``` |
| 102 | + |
| 103 | + </Tab> |
| 104 | + <Tab label="npm"> |
| 105 | + |
| 106 | +```bash |
| 107 | +npx statikapi dev --port 8788 --srcDir src-api --outDir api-out |
| 108 | +``` |
| 109 | + |
| 110 | + </Tab> |
| 111 | +</Tabs> |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +## Recipes |
| 116 | + |
| 117 | +### 1) Customizing directories |
| 118 | + |
| 119 | +```js |
| 120 | +// statikapi.config.js |
| 121 | +export default { |
| 122 | + srcDir: 'api', // move sources into /api |
| 123 | + outDir: 'public/api', // emit static JSON inside your public/ folder |
| 124 | +}; |
12 | 125 | ``` |
13 | 126 |
|
14 | | -You can override these from the CLI with `--src` and `--out`. |
| 127 | +### 2) Monorepo (package-local config) |
| 128 | + |
| 129 | +Place `statikapi.config.js` in the **package root** that owns the `srcDir`. The CLI resolves paths **relative to the CWD** (where you run it). |
| 130 | + |
| 131 | +### 3) TypeScript routes |
| 132 | + |
| 133 | +You can freely use **.ts / .tsx** route files. The CLI transpiles on the fly and imports them, then enforces **JSON‑serializable** outputs. |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +## Troubleshooting |
| 138 | + |
| 139 | +- **Config error “must be a relative path”** — remove any leading `/` or drive letter; use relative paths like `src-api`, `api-out`. |
| 140 | +- **“cannot traverse outside the project”** — avoid `../` path segments. |
| 141 | +- **`srcDir` and `outDir` must differ** — point them at different folders. |
| 142 | +- **Dynamic routes not emitting** — ensure the module exports `paths()` that returns **strings** (or arrays of strings for catch‑all). See **Routes** docs. |
| 143 | +- **Non‑serializable value** — only plain objects/arrays, finite numbers, strings, booleans, or `null`. No functions, symbols, BigInt, Dates, classes, or cycles. |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## See also |
15 | 148 |
|
16 | | -> **Tip:** The scaffolders also generate `.nvmrc` and `.node-version` for Node 22, and can drop deploy files like `wrangler.toml`, `netlify.toml`, or a GitHub Pages workflow when selected. |
| 149 | +- **[CLI → StatikAPI (Core)](./cli/statikapi)** for flags and dev/build usage |
| 150 | +- **[Routes](./routes/index)** for file → URL mapping rules |
| 151 | +- **[Deployments](./deployments/index)** for hosting static JSON |
0 commit comments