@keshavsoft/kschema-api-gen-actions 1.16.2 → 1.16.3

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.
Files changed (29) hide show
  1. package/bin/v16/config/actions.json +11 -0
  2. package/bin/v16/config/actionsV3.json +35 -0
  3. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/common/readFile.js +8 -0
  4. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/common/writeFile.js +10 -0
  5. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateImports/buildImport.js +9 -0
  6. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateImports/checkDuplicate.js +8 -0
  7. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateImports/findInsertIndex.js +10 -0
  8. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateImports/index.js +47 -0
  9. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateUse/buildUseLine.js +5 -0
  10. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateUse/checkDuplicate.js +9 -0
  11. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateUse/findInsertIndex.js +10 -0
  12. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/UpdateRoutesJs/updateUse/index.js +56 -0
  13. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/announce.js +9 -0
  14. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/createFolder.js +7 -0
  15. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/createHttpFile.js +12 -0
  16. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/decideTemplate.js +3 -0
  17. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/locateDestination.js +7 -0
  18. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/locateSource.js +13 -0
  19. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/resolveFolderName.js +20 -0
  20. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/updateAppJs.js +7 -0
  21. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/updateEndPointsFile.js +30 -0
  22. package/bin/v16/tasks/actions/PostMethods/FilterColumns/steps/updateEndPointsJs.js +16 -0
  23. package/bin/v16/tasks/actions/PostMethods/FilterColumns/template/controller.js +27 -0
  24. package/bin/v16/tasks/actions/PostMethods/FilterColumns/template/errors.js +11 -0
  25. package/bin/v16/tasks/actions/PostMethods/FilterColumns/template/middleware.js +19 -0
  26. package/bin/v16/tasks/actions/PostMethods/FilterColumns/template/rest.http +4 -0
  27. package/bin/v16/tasks/actions/PostMethods/FilterColumns/template/service.js +9 -0
  28. package/bin/v16/tasks/actions/filterColumns.js +56 -0
  29. package/package.json +2 -2
@@ -31,5 +31,16 @@
31
31
  "importLine": "import distinctFunc from './Distinct/controller.js';",
32
32
  "usageLine": "router.get('/Distinct/:columnName', (req, res) => distinctFunc({ req, res, inTableName : tableName}));"
33
33
  }
34
+ },
35
+ {
36
+ "cmd": "FilterColumns",
37
+ "file": "filterColumns",
38
+ "exportFile": "FilterColumns",
39
+ "group": "PostMethods",
40
+ "description": "Generate FilterColumns POST action",
41
+ "endPointsJs": {
42
+ "importLine": "import filterColumnsFunc from './FilterColumns/controller.js';",
43
+ "usageLine": "router.post('/FilterColumns', express.json(), (req, res) => filterColumnsFunc({ req, res, inTableName : tableName}));"
44
+ }
34
45
  }
35
46
  ]
@@ -0,0 +1,35 @@
1
+ [
2
+ {
3
+ "cmd": "ShowAll",
4
+ "file": "showAll",
5
+ "exportFile": "ShowAll",
6
+ "group": "GetMethods",
7
+ "description": "Generate ShowAll GET action",
8
+ "endPointsJs": {
9
+ "importLine": "import { getFunc } from './ShowAll/controller.js';",
10
+ "usageLine": "router.get('/ShowAll', (req, res) => getFunc({ res, inTableName : tableName}));"
11
+ }
12
+ },
13
+ {
14
+ "cmd": "Insert",
15
+ "file": "insert",
16
+ "exportFile": "Insert",
17
+ "group": "PostMethods",
18
+ "description": "Generate Insert POST action",
19
+ "endPointsJs": {
20
+ "importLine": "import { postFunc } from './Insert/controller.js';",
21
+ "usageLine": "router.post('/Insert', express.json(), (req, res) => postFunc({ req, res, inTableName : tableName}));"
22
+ }
23
+ },
24
+ {
25
+ "cmd": "Distinct",
26
+ "file": "distinct",
27
+ "exportFile": "Distinct",
28
+ "group": "GetMethods",
29
+ "description": "Generate Distinct GET action",
30
+ "endPointsJs": {
31
+ "importLine": "import distinctFunc from './Distinct/controller.js';",
32
+ "usageLine": "router.get('/Distinct/:columnName', (req, res) => distinctFunc({ req, res, inTableName : tableName}));"
33
+ }
34
+ }
35
+ ]
@@ -0,0 +1,8 @@
1
+ import fs from "fs";
2
+
3
+ const readFile = (inAppJsPath) => {
4
+ const localPath = inAppJsPath;
5
+ return fs.readFileSync(localPath, "utf-8");
6
+ };
7
+
8
+ export default readFile;
@@ -0,0 +1,10 @@
1
+ import fs from "fs";
2
+
3
+ const writeFile = (inAppJsPath, inContent) => {
4
+ const localPath = inAppJsPath;
5
+ const localContent = inContent;
6
+
7
+ fs.writeFileSync(localPath, localContent);
8
+ };
9
+
10
+ export default writeFile;
@@ -0,0 +1,9 @@
1
+ const buildImport = ({ inFuncName, inEndpoint }) => {
2
+ const localEndpoint = inEndpoint;
3
+
4
+ const safeName = localEndpoint.replace(/[^a-zA-Z0-9]/g, "_");
5
+
6
+ return `import { ${inFuncName} } from "./${localEndpoint}/controller.js";`;
7
+ };
8
+
9
+ export default buildImport;
@@ -0,0 +1,8 @@
1
+ const checkUseDuplicate = ({ inContent, inFuncName }) => {
2
+ const localContent = inContent;
3
+ const toCheckLine = `import { ${inFuncName} } from`;
4
+
5
+ return localContent.includes(toCheckLine);
6
+ };
7
+
8
+ export default checkUseDuplicate;
@@ -0,0 +1,10 @@
1
+ const findInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/^\s*import.*$/gm)];
5
+ const last = matches.at(-1);
6
+
7
+ return last ? last.index + last[0].length : 0;
8
+ };
9
+
10
+ export default findInsertIndex;
@@ -0,0 +1,47 @@
1
+ import readFile from "../common/readFile.js";
2
+ import buildImport from "./buildImport.js";
3
+ import checkDuplicate from "./checkDuplicate.js";
4
+ import findInsertIndex from "./findInsertIndex.js";
5
+ import writeFile from "../common/writeFile.js";
6
+
7
+ const updateImports = ({ appJsPath, endpoint, showLog, inFuncName }) => {
8
+ const summary = {
9
+ import: { added: false, line: null },
10
+ };
11
+
12
+ const content = readFile(appJsPath);
13
+
14
+ const importLine = buildImport({
15
+ inEndpoint: endpoint,
16
+ inFuncName
17
+ });
18
+
19
+ const fromCheckDuplicate = checkDuplicate({ inContent: content, inFuncName });
20
+
21
+ if (fromCheckDuplicate) {
22
+ summary.import.skipped = true;
23
+
24
+ if (showLog) console.log(summary);
25
+
26
+ return summary;
27
+ };
28
+
29
+ const index = findInsertIndex(content);
30
+
31
+ const before = content.slice(0, index);
32
+ const lineNumber = before.split("\n").length + 1;
33
+
34
+ const updated =
35
+ before + "\n" + importLine + content.slice(index);
36
+
37
+ writeFile(appJsPath, updated);
38
+
39
+ summary.import.added = true;
40
+ summary.import.line = lineNumber;
41
+
42
+ if (showLog) console.log(summary);
43
+
44
+ return summary;
45
+ };
46
+
47
+ export default updateImports;
@@ -0,0 +1,5 @@
1
+ const buildUseLine = ({ inAppOrRouter, inEndpoint, inFuncName }) => {
2
+ return `${inAppOrRouter}.get('/${inEndpoint}', (req, res) => ${inFuncName}({ res, inTableName : tableName}));`;
3
+ };
4
+
5
+ export default buildUseLine;
@@ -0,0 +1,9 @@
1
+ const checkDuplicate = ({ inContent, inEndpoint, inAppOrRouter }) => {
2
+ const localContent = inContent;
3
+ const localEndpoint = inEndpoint;
4
+ const stringToSearch = `${inAppOrRouter}.get('/${localEndpoint}'`;
5
+
6
+ return localContent.includes(stringToSearch);
7
+ };
8
+
9
+ export default checkDuplicate;
@@ -0,0 +1,10 @@
1
+ const findUseInsertIndex = (inContent) => {
2
+ const localContent = inContent;
3
+
4
+ const matches = [...localContent.matchAll(/const\s+router\s*=\s*express\.Router\(\)/g)];
5
+ const match = matches.at(0);
6
+
7
+ return match ? match.index + match[0].length : localContent.length;
8
+ };
9
+
10
+ export default findUseInsertIndex;
@@ -0,0 +1,56 @@
1
+ import readFile from "../common/readFile.js";
2
+ import writeFile from "../common/writeFile.js";
3
+
4
+ import buildUseLine from "./buildUseLine.js";
5
+ import checkDuplicate from "./checkDuplicate.js";
6
+ import findUseInsertIndex from "./findInsertIndex.js";
7
+
8
+ const appOrRouter = "router";
9
+
10
+ const updateAppUse = ({ appJsPath, endpoint, showLog, inFuncName }) => {
11
+ const summary = {
12
+ use: { added: false, skipped: false, line: null }
13
+ };
14
+
15
+ const content = readFile(appJsPath);
16
+
17
+ const useLine = buildUseLine({
18
+ inAppOrRouter: appOrRouter,
19
+ inEndpoint: endpoint,
20
+ inFuncName
21
+ });
22
+
23
+ const fromCheckDuplicate = checkDuplicate({
24
+ inContent: content,
25
+ inEndpoint: endpoint,
26
+ inAppOrRouter: appOrRouter
27
+ });
28
+ console.log("fromCheckDuplicate : ", fromCheckDuplicate);
29
+
30
+ if (fromCheckDuplicate) {
31
+ summary.use.skipped = true;
32
+
33
+ if (showLog) console.log(summary);
34
+
35
+ return summary;
36
+ };
37
+
38
+ const index = findUseInsertIndex(content);
39
+
40
+ const before = content.slice(0, index);
41
+ const lineNumber = before.split("\n").length + 1;
42
+
43
+ const updated =
44
+ before + "\n" + useLine + content.slice(index);
45
+
46
+ writeFile(appJsPath, updated);
47
+
48
+ summary.use.added = true;
49
+ summary.use.line = lineNumber;
50
+
51
+ if (showLog) console.log(summary);
52
+
53
+ return summary;
54
+ };
55
+
56
+ export default updateAppUse;
@@ -0,0 +1,9 @@
1
+ export const announce = ({ inResolvedFolderName }) => {
2
+ console.log(`[keshavsoft] Folder created: ${inResolvedFolderName}`);
3
+
4
+ console.log("");
5
+ console.log("Next:");
6
+ console.log("");
7
+ console.log(`cd ${inResolvedFolderName}`);
8
+ console.log(`npx kschema-api-gen AddSubRoute`);
9
+ };
@@ -0,0 +1,7 @@
1
+ import fs from "fs";
2
+
3
+ export const createFolder = ({ source, destination }) => {
4
+ fs.mkdirSync(destination, { recursive: true });
5
+
6
+ fs.cpSync(source, destination, { recursive: true });
7
+ };
@@ -0,0 +1,12 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ const createHttpFile = ({ inTargetPath, toPath }) => {
5
+ const relative = inTargetPath.replace(toPath, "").replace(/\\/g, "/");
6
+
7
+ const content = `GET http://localhost:3000${relative}`;
8
+
9
+ fs.writeFileSync(path.join(inTargetPath, "rest.http"), content);
10
+ };
11
+
12
+ export default createHttpFile;
@@ -0,0 +1,3 @@
1
+ export const decideTemplate = ({ inTemplate }) => {
2
+ return inTemplate || "basic";
3
+ };
@@ -0,0 +1,7 @@
1
+ import path from "path";
2
+
3
+ export const locateDestination = ({ inResolvedFolderName, toPath }) => {
4
+ const localToPath = toPath;
5
+
6
+ return path.join(localToPath, inResolvedFolderName);
7
+ };
@@ -0,0 +1,13 @@
1
+ import path from "path";
2
+ import { fileURLToPath } from "url";
3
+
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = path.dirname(__filename);
6
+
7
+ export const locateSource = () => {
8
+ return path.join(
9
+ __dirname,
10
+ "..",
11
+ "template"
12
+ );
13
+ };
@@ -0,0 +1,20 @@
1
+ import fs from "fs";
2
+
3
+ export default function resolveFolderName({ name, inDefaultFolderName = "Insert" }) {
4
+ const defaultFolderName = inDefaultFolderName;
5
+
6
+ // case 1: force new → timestamp
7
+ if (name === null) {
8
+ name = defaultFolderName;
9
+ };
10
+
11
+ // case 2: user provided → strict
12
+ if (fs.existsSync(name)) {
13
+ return {
14
+ KTF: false,
15
+ KReason: `Folder already exists : ${name}`
16
+ };
17
+ };
18
+
19
+ return name;
20
+ };
@@ -0,0 +1,7 @@
1
+ import updateImports from "./UpdateRoutesJs/updateImports/index.js";
2
+ import updateAppUse from "./UpdateRoutesJs/updateUse/index.js";
3
+
4
+ export default ({ appJsPath, endpoint }) => {
5
+ updateImports({ appJsPath, endpoint });
6
+ updateAppUse({ appJsPath, endpoint });
7
+ };
@@ -0,0 +1,30 @@
1
+ import fs from 'fs';
2
+
3
+ function updateEndPointsFile({ filePath, inTableName }) {
4
+ try {
5
+ let content = readFile(filePath);
6
+
7
+ content = content.replace("<TABLE_NAME>", inTableName);
8
+
9
+ writeFile(filePath, content);
10
+ } catch (e) {
11
+ handleError(e);
12
+ };
13
+ };
14
+
15
+ // read
16
+ function readFile(filePath) {
17
+ return fs.readFileSync(filePath, "utf-8");
18
+ };
19
+
20
+ // write
21
+ function writeFile(filePath, content) {
22
+ fs.writeFileSync(filePath, content);
23
+ };
24
+
25
+ // error handler
26
+ function handleError(e) {
27
+ console.error("ROUTE USE ERROR:", e.message);
28
+ };
29
+
30
+ export { updateEndPointsFile };
@@ -0,0 +1,16 @@
1
+ import updateImports from "./UpdateRoutesJs/updateImports/index.js";
2
+ import updateAppUse from "./UpdateRoutesJs/updateUse/index.js";
3
+
4
+ const funcName = "getFunc";
5
+
6
+ export default ({ appJsPath, endpoint }) => {
7
+ updateImports({
8
+ appJsPath, endpoint,
9
+ inFuncName: funcName
10
+ });
11
+
12
+ updateAppUse({
13
+ appJsPath, endpoint,
14
+ inFuncName: funcName
15
+ });
16
+ };
@@ -0,0 +1,27 @@
1
+ import { filterItems } from "./service.js";
2
+ import { ConflictError, StorageError } from "./errors.js";
3
+
4
+ const filterColumnsFunc = ({ req, res, inTableName }) => {
5
+ try {
6
+ const inRequestBody = req.body;
7
+
8
+ const message = filterItems({
9
+ inRequestBody,
10
+ inTableName
11
+ });
12
+
13
+ res.type("application/json").send(message);
14
+ } catch (err) {
15
+
16
+ if (err instanceof ConflictError)
17
+ return res.status(409).send(err.message);
18
+
19
+ if (err instanceof StorageError)
20
+ return res.status(500).send("Failed to persist data");
21
+
22
+ console.error(err);
23
+ res.status(500).send("Unexpected error");
24
+ }
25
+ };
26
+
27
+ export default filterColumnsFunc;
@@ -0,0 +1,11 @@
1
+ class AppError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = this.constructor.name;
5
+ }
6
+ }
7
+
8
+ class ConflictError extends AppError { }
9
+ class StorageError extends AppError { }
10
+
11
+ export { AppError, ConflictError, StorageError };
@@ -0,0 +1,19 @@
1
+ const StartFunc = (req, res, next) => {
2
+ const LocalBody = req.body;
3
+
4
+ if (!LocalBody || Object.keys(LocalBody).length === 0) {
5
+ return res.status(400).send("Request body should not be empty.");
6
+ }
7
+
8
+ if (Array.isArray(LocalBody)) {
9
+ return res.status(400).send("Remove The Array From The Body.");
10
+ }
11
+
12
+ if (typeof LocalBody === 'object' && Object.keys(LocalBody).length === 0) {
13
+ return res.status(400).send("Request body should not be an empty object.");
14
+ }
15
+
16
+ next();
17
+ };
18
+
19
+ export { StartFunc };
@@ -0,0 +1,4 @@
1
+ POST http://localhost:3000/Api/V1/journals/FilterColumns
2
+ Content-Type: application/json
3
+
4
+ { "AccountName": "VEETI KRISHNA" }
@@ -0,0 +1,9 @@
1
+ import { kschema } from "@keshavsoft/kschema";
2
+
3
+ const filterItems = ({ inRequestBody, inTableName }) => {
4
+ const tableName = inTableName;
5
+
6
+ return kschema.table(tableName).query.filterByColumns(inRequestBody);
7
+ };
8
+
9
+ export { filterItems };
@@ -0,0 +1,56 @@
1
+ import path from "path";
2
+ import apiCheck from "@keshavsoft/kschema-api-check";
3
+
4
+ import { locateSource } from "./PostMethods/FilterColumns/steps/locateSource.js";
5
+ import { locateDestination } from "./PostMethods/FilterColumns/steps/locateDestination.js";
6
+ import { createFolder } from "../../core/createFolder.js";
7
+
8
+ import updateEndPointsJs from "./PostMethods/FilterColumns/steps/updateEndPointsJs.js";
9
+ import createHttpFile from "./PostMethods/FilterColumns/steps/createHttpFile.js";
10
+
11
+ import { announce } from "./PostMethods/FilterColumns/steps/announce.js";
12
+
13
+ import resolveFolderName from "./PostMethods/FilterColumns/steps/resolveFolderName.js";
14
+
15
+ const startFunc = async ({ cmd = "", toPath, isAnnounce = true, checkBeforeCreate = true }) => {
16
+ const localToPath = toPath;
17
+
18
+ const resolvedFolderName = resolveFolderName({
19
+ name: cmd
20
+ });
21
+
22
+ if (resolvedFolderName.KTF === false) {
23
+ console.log(resolvedFolderName.KReason);
24
+
25
+ return;
26
+ };
27
+
28
+ const source = locateSource();
29
+ const destination = locateDestination({
30
+ inResolvedFolderName: resolvedFolderName,
31
+ toPath: localToPath
32
+ });
33
+
34
+ const createFolderResponse = createFolder({
35
+ source, destination,
36
+ isAnnounce, checkBeforeCreate
37
+ });
38
+
39
+ if (createFolderResponse.KTF) {
40
+ await apiCheck({
41
+ toPath: localToPath,
42
+ action: resolvedFolderName
43
+ });
44
+
45
+ createHttpFile({
46
+ inTargetPath: path.join(localToPath, resolvedFolderName),
47
+ toPath: process.cwd()
48
+ });
49
+ };
50
+
51
+ if (isAnnounce) announce({ inResolvedFolderName: resolvedFolderName });
52
+
53
+ return resolvedFolderName;
54
+ };
55
+
56
+ export default startFunc;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keshavsoft/kschema-api-gen-actions",
3
- "version": "1.16.2",
3
+ "version": "1.16.3",
4
4
  "description": "CLI to scaffold projects using templates",
5
5
  "keywords": [
6
6
  "cli",
@@ -11,7 +11,7 @@
11
11
  ],
12
12
  "dependencies": {
13
13
  "@keshavsoft/kschema": "^1.17.8",
14
- "@keshavsoft/kschema-api-check": "^1.12.2"
14
+ "@keshavsoft/kschema-api-check": "^1.12.3"
15
15
  },
16
16
  "type": "module",
17
17
  "exports": {