json-api-builder-add-route 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# json-api-builder-add-route
|
|
2
|
+
|
|
3
|
+
> Localized Route Subtree Generator & AST Router Hooker for JSON API Builder.
|
|
4
|
+
|
|
5
|
+
`json-api-builder-add-route` lets you add new API versions (e.g. `v2`) or resource subtrees to an existing project **without touching or risking `app.js` or older versions (like `v1`)**.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **AST-Driven:** Uses ESTree AST JSON templates and `astring` for clean, syntax-guaranteed code generation.
|
|
12
|
+
- **Localized Execution:** Run directly inside your router folder (e.g., `api/`).
|
|
13
|
+
- **Non-Destructive & Safe:** Only generates the new subtree (e.g. `v2/`) and injects the route mount into the immediate parent router (`routes.js`). Older routes and `app.js` remain completely untouched.
|
|
14
|
+
- **Idempotent:** Running multiple times will never duplicate imports or route mounts.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Quick Start via `npx`
|
|
19
|
+
|
|
20
|
+
1. Navigate to your router directory:
|
|
21
|
+
```bash
|
|
22
|
+
cd api
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
2. Create a scoped specification file (`v2.json`):
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"name": "v2",
|
|
29
|
+
"routes": {
|
|
30
|
+
"appointments": {
|
|
31
|
+
"resource": "appointments",
|
|
32
|
+
"idField": "id",
|
|
33
|
+
"dataFile": "../../data/appointments.json"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
3. Run with `npx`:
|
|
40
|
+
```bash
|
|
41
|
+
npx json-api-builder-add-route v2.json
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Result
|
|
47
|
+
|
|
48
|
+
- Generates `v2/routes.js`
|
|
49
|
+
- Generates `v2/appointments/routes.js` with full CRUD handlers (GET, POST, GET/:id, PUT/:id, DELETE/:id)
|
|
50
|
+
- Initializes `data/appointments.json`
|
|
51
|
+
- Safely injects the following into `api/routes.js`:
|
|
52
|
+
```javascript
|
|
53
|
+
import v2Routes from "./v2/routes.js";
|
|
54
|
+
routes.route("/v2", v2Routes);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Programmatic Usage
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
import { addRoute } from "json-api-builder-add-route";
|
|
63
|
+
|
|
64
|
+
addRoute({
|
|
65
|
+
targetDir: process.cwd(),
|
|
66
|
+
specFile: "v2.json"
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT © KeshavSoft
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-api-builder-add-route",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Scoped subtree generator and parent router hooker for JSON API Builder.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"json-api-builder-add-route": "./bin/index.js"
|
|
9
9
|
},
|
|
10
10
|
"exports": {
|
|
11
|
-
".": "./src/index.js"
|
|
11
|
+
".": "./src/index.js",
|
|
12
|
+
"./v1": "./src/v1/index.js"
|
|
12
13
|
},
|
|
13
14
|
"files": [
|
|
14
15
|
"bin",
|
|
@@ -39,4 +40,4 @@
|
|
|
39
40
|
"url": "https://github.com/keshavsoft/json-api-builder-add-route/issues"
|
|
40
41
|
},
|
|
41
42
|
"homepage": "https://keshavsoft.github.io/json-api-builder-add-route/"
|
|
42
|
-
}
|
|
43
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,41 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { generateSubtree } from "./generate-subtree.js";
|
|
4
|
-
import { hookParentRouter } from "./hook-parent-router.js";
|
|
5
|
-
|
|
6
|
-
export function addRoute(options = {}) {
|
|
7
|
-
const targetDir = path.resolve(options.targetDir ?? process.cwd());
|
|
8
|
-
let spec = options.spec;
|
|
9
|
-
|
|
10
|
-
if (!spec) {
|
|
11
|
-
const specFile = options.specFile ?? "v2.json";
|
|
12
|
-
const specPath = path.resolve(targetDir, specFile);
|
|
13
|
-
if (!fs.existsSync(specPath)) {
|
|
14
|
-
throw new Error(`Route specification file not found: ${specPath}`);
|
|
15
|
-
}
|
|
16
|
-
spec = JSON.parse(fs.readFileSync(specPath, "utf8"));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const parentRouterName = options.parentRouterName ?? "routes.js";
|
|
20
|
-
const parentRouterPath = path.join(targetDir, parentRouterName);
|
|
21
|
-
|
|
22
|
-
// 1. Generate new subtree
|
|
23
|
-
const subtreeResult = generateSubtree(targetDir, spec);
|
|
24
|
-
|
|
25
|
-
// 2. Hook into parent router if it exists
|
|
26
|
-
let hookResult = null;
|
|
27
|
-
if (fs.existsSync(parentRouterPath)) {
|
|
28
|
-
hookResult = hookParentRouter(parentRouterPath, subtreeResult.routeName);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
targetDir,
|
|
33
|
-
routeName: subtreeResult.routeName,
|
|
34
|
-
subtreeDir: subtreeResult.subtreeDir,
|
|
35
|
-
createdFiles: subtreeResult.createdFiles,
|
|
36
|
-
parentRouterPath: fs.existsSync(parentRouterPath) ? parentRouterPath : null,
|
|
37
|
-
hookResult
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export default { addRoute };
|
|
1
|
+
export * from "./v1/index.js";
|
|
@@ -28,9 +28,10 @@ export function hookParentRouter(parentRouterPath, routeName) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
// 2. Check if mount already exists
|
|
31
|
+
// 2. Check if mount already exists and detect router variable name
|
|
32
32
|
let hasMount = false;
|
|
33
33
|
let lastMountIndex = -1;
|
|
34
|
+
let routerVarName = "routes";
|
|
34
35
|
|
|
35
36
|
for (let i = 0; i < ast.body.length; i++) {
|
|
36
37
|
const node = ast.body[i];
|
|
@@ -40,6 +41,7 @@ export function hookParentRouter(parentRouterPath, routeName) {
|
|
|
40
41
|
node.expression.callee?.type === "MemberExpression" &&
|
|
41
42
|
node.expression.callee.property?.name === "route"
|
|
42
43
|
) {
|
|
44
|
+
routerVarName = node.expression.callee.object?.name ?? "routes";
|
|
43
45
|
lastMountIndex = i;
|
|
44
46
|
const firstArg = node.expression.arguments?.[0];
|
|
45
47
|
if (firstArg?.value === routeMountPath) {
|
|
@@ -84,7 +86,7 @@ export function hookParentRouter(parentRouterPath, routeName) {
|
|
|
84
86
|
type: "CallExpression",
|
|
85
87
|
callee: {
|
|
86
88
|
type: "MemberExpression",
|
|
87
|
-
object: { type: "Identifier", name:
|
|
89
|
+
object: { type: "Identifier", name: routerVarName },
|
|
88
90
|
property: { type: "Identifier", name: "route" }
|
|
89
91
|
},
|
|
90
92
|
arguments: [
|
package/src/v1/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { generateSubtree } from "./generate-subtree.js";
|
|
4
|
+
import { hookParentRouter } from "./hook-parent-router.js";
|
|
5
|
+
|
|
6
|
+
export function addRoute(options = {}) {
|
|
7
|
+
const targetDir = path.resolve(options.targetDir ?? process.cwd());
|
|
8
|
+
let spec = options.spec;
|
|
9
|
+
|
|
10
|
+
if (!spec) {
|
|
11
|
+
const specFile = options.specFile ?? "v2.json";
|
|
12
|
+
const specPath = path.resolve(targetDir, specFile);
|
|
13
|
+
if (!fs.existsSync(specPath)) {
|
|
14
|
+
throw new Error(`Route specification file not found: ${specPath}`);
|
|
15
|
+
}
|
|
16
|
+
spec = JSON.parse(fs.readFileSync(specPath, "utf8"));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const parentRouterName = options.parentRouterName ?? "routes.js";
|
|
20
|
+
const parentRouterPath = path.join(targetDir, parentRouterName);
|
|
21
|
+
|
|
22
|
+
// 1. Generate new subtree
|
|
23
|
+
const subtreeResult = generateSubtree(targetDir, spec);
|
|
24
|
+
|
|
25
|
+
// 2. Hook into parent router if it exists
|
|
26
|
+
let hookResult = null;
|
|
27
|
+
if (fs.existsSync(parentRouterPath)) {
|
|
28
|
+
hookResult = hookParentRouter(parentRouterPath, subtreeResult.routeName);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
targetDir,
|
|
33
|
+
routeName: subtreeResult.routeName,
|
|
34
|
+
subtreeDir: subtreeResult.subtreeDir,
|
|
35
|
+
createdFiles: subtreeResult.createdFiles,
|
|
36
|
+
parentRouterPath: fs.existsSync(parentRouterPath) ? parentRouterPath : null,
|
|
37
|
+
hookResult
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default { addRoute };
|
|
File without changes
|