feat: add type dts and apiKeyObject
This commit is contained in:
parent
f3c0707666
commit
9d4a4947e7
138
package-lock.json
generated
138
package-lock.json
generated
@ -15,6 +15,7 @@
|
||||
"@babel/preset-env": "^7.24.7",
|
||||
"@babel/preset-typescript": "^7.24.7",
|
||||
"dayjs": "^1.11.11",
|
||||
"dts-bundle": "^0.7.3",
|
||||
"json5": "^2.2.3",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"lodash-es": "^4.17.21",
|
||||
@ -26,6 +27,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/crypto-js": "^4.2.2",
|
||||
"@types/dts-bundle": "^0.0.35",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/jsonwebtoken": "^9.0.6",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
@ -2630,6 +2632,17 @@
|
||||
"@types/ms": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/detect-indent": {
|
||||
"version": "0.1.30",
|
||||
"resolved": "https://registry.npmmirror.com/@types/detect-indent/-/detect-indent-0.1.30.tgz",
|
||||
"integrity": "sha512-AUmj9JHuHTD94slY1WR1VulFxRGC6D1pcNCN0MCulKFyiihvV/28lLS8oRHgfmc2Cxq954J8Vmosa8qzm7PLGQ=="
|
||||
},
|
||||
"node_modules/@types/dts-bundle": {
|
||||
"version": "0.0.35",
|
||||
"resolved": "https://registry.npmmirror.com/@types/dts-bundle/-/dts-bundle-0.0.35.tgz",
|
||||
"integrity": "sha512-TZCP4CIDR3R3K2ij5UqXzKfkiAcnVBSUudPOq0zUk42hl8PirN/4q0dPiidBP3s7hZJuB7OQULxR3I2ZQsnLig==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/eslint": {
|
||||
"version": "8.56.10",
|
||||
"resolved": "https://registry.npmmirror.com/@types/eslint/-/eslint-8.56.10.tgz",
|
||||
@ -2653,6 +2666,15 @@
|
||||
"resolved": "https://registry.npmmirror.com/@types/estree/-/estree-1.0.5.tgz",
|
||||
"integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw=="
|
||||
},
|
||||
"node_modules/@types/glob": {
|
||||
"version": "5.0.30",
|
||||
"resolved": "https://registry.npmmirror.com/@types/glob/-/glob-5.0.30.tgz",
|
||||
"integrity": "sha512-ZM05wDByI+WA153sfirJyEHoYYoIuZ7lA2dB/Gl8ymmpMTR78fNRtDMqa7Z6SdH4fZdLWZNRE6mZpx3XqBOrHw==",
|
||||
"dependencies": {
|
||||
"@types/minimatch": "*",
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/graceful-fs": {
|
||||
"version": "4.1.9",
|
||||
"resolved": "https://registry.npmmirror.com/@types/graceful-fs/-/graceful-fs-4.1.9.tgz",
|
||||
@ -2731,6 +2753,16 @@
|
||||
"integrity": "sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/minimatch": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmmirror.com/@types/minimatch/-/minimatch-5.1.2.tgz",
|
||||
"integrity": "sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA=="
|
||||
},
|
||||
"node_modules/@types/mkdirp": {
|
||||
"version": "0.3.29",
|
||||
"resolved": "https://registry.npmmirror.com/@types/mkdirp/-/mkdirp-0.3.29.tgz",
|
||||
"integrity": "sha512-QRLQpFsIQGO2k8pupga9abfei85GKotAtQ+F6xuQmSGomUt6C52TyMiTFpP8kUwuPKr00gNtu3itLlC6gvI/NA=="
|
||||
},
|
||||
"node_modules/@types/ms": {
|
||||
"version": "0.7.34",
|
||||
"resolved": "https://registry.npmmirror.com/@types/ms/-/ms-0.7.34.tgz",
|
||||
@ -3342,8 +3374,7 @@
|
||||
"node_modules/balanced-match": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmmirror.com/balanced-match/-/balanced-match-1.0.2.tgz",
|
||||
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
|
||||
"dev": true
|
||||
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
|
||||
},
|
||||
"node_modules/base64id": {
|
||||
"version": "2.0.0",
|
||||
@ -3369,7 +3400,6 @@
|
||||
"version": "1.1.11",
|
||||
"resolved": "https://registry.npmmirror.com/brace-expansion/-/brace-expansion-1.1.11.tgz",
|
||||
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"balanced-match": "^1.0.0",
|
||||
"concat-map": "0.0.1"
|
||||
@ -3711,8 +3741,7 @@
|
||||
"node_modules/concat-map": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmmirror.com/concat-map/-/concat-map-0.0.1.tgz",
|
||||
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
|
||||
"dev": true
|
||||
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
|
||||
},
|
||||
"node_modules/concurrently": {
|
||||
"version": "8.2.2",
|
||||
@ -4048,6 +4077,21 @@
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/detect-indent": {
|
||||
"version": "0.2.0",
|
||||
"resolved": "https://registry.npmmirror.com/detect-indent/-/detect-indent-0.2.0.tgz",
|
||||
"integrity": "sha512-C6jyrDu/eGH4KT0ZxAzijiH+ts5YLy7DqGFoDuHGxZjMOdjzRltp3jByySnpFBVIy4Em0ZkLN8tIV6mcREdw5A==",
|
||||
"dependencies": {
|
||||
"get-stdin": "^0.1.0",
|
||||
"minimist": "^0.1.0"
|
||||
},
|
||||
"bin": {
|
||||
"detect-indent": "cli.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/detect-newline": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmmirror.com/detect-newline/-/detect-newline-3.1.0.tgz",
|
||||
@ -4090,6 +4134,48 @@
|
||||
"resolved": "https://registry.npmmirror.com/dottie/-/dottie-2.0.6.tgz",
|
||||
"integrity": "sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA=="
|
||||
},
|
||||
"node_modules/dts-bundle": {
|
||||
"version": "0.7.3",
|
||||
"resolved": "https://registry.npmmirror.com/dts-bundle/-/dts-bundle-0.7.3.tgz",
|
||||
"integrity": "sha512-EEAEuPRk8QyKhoN90NHTh+spSQujkkvOnKWUfuzpmC/fgryiWopL1SegSktx0UsoPfNidIGVDN7/AXpBDBv0WQ==",
|
||||
"dependencies": {
|
||||
"@types/detect-indent": "0.1.30",
|
||||
"@types/glob": "5.0.30",
|
||||
"@types/mkdirp": "0.3.29",
|
||||
"@types/node": "8.0.0",
|
||||
"commander": "^2.9.0",
|
||||
"detect-indent": "^0.2.0",
|
||||
"glob": "^6.0.4",
|
||||
"mkdirp": "^0.5.0"
|
||||
},
|
||||
"bin": {
|
||||
"dts-bundle": "lib/dts-bundle.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/dts-bundle/node_modules/@types/node": {
|
||||
"version": "8.0.0",
|
||||
"resolved": "https://registry.npmmirror.com/@types/node/-/node-8.0.0.tgz",
|
||||
"integrity": "sha512-j2tekvJCO7j22cs+LO6i0kRPhmQ9MXaPZ55TzOc1lzkN5b6BWqq4AFjl04s1oRRQ1v5rSe+KEvnLUSTonuls/A=="
|
||||
},
|
||||
"node_modules/dts-bundle/node_modules/glob": {
|
||||
"version": "6.0.4",
|
||||
"resolved": "https://registry.npmmirror.com/glob/-/glob-6.0.4.tgz",
|
||||
"integrity": "sha512-MKZeRNyYZAVVVG1oZeLaWie1uweH40m9AZwIwxyPbTSX4hHrVYSzLg0Ro5Z5R7XKkIX+Cc6oD1rqeDJnwsB8/A==",
|
||||
"deprecated": "Glob versions prior to v9 are no longer supported",
|
||||
"dependencies": {
|
||||
"inflight": "^1.0.4",
|
||||
"inherits": "2",
|
||||
"minimatch": "2 || 3",
|
||||
"once": "^1.3.0",
|
||||
"path-is-absolute": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/ecdsa-sig-formatter": {
|
||||
"version": "1.0.11",
|
||||
"resolved": "https://registry.npmmirror.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
|
||||
@ -4595,6 +4681,14 @@
|
||||
"node": ">=8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/get-stdin": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmmirror.com/get-stdin/-/get-stdin-0.1.0.tgz",
|
||||
"integrity": "sha512-/WBu3IaQZxE3bs3BhBmR10ipDY4pjN+U4EZgXULa1eqKA0B/Lka/MVoAqhTVYBkkRlCrEGDOU9itrzIgm9Ksng==",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/get-stream": {
|
||||
"version": "6.0.1",
|
||||
"resolved": "https://registry.npmmirror.com/get-stream/-/get-stream-6.0.1.tgz",
|
||||
@ -4853,7 +4947,6 @@
|
||||
"resolved": "https://registry.npmmirror.com/inflight/-/inflight-1.0.6.tgz",
|
||||
"integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==",
|
||||
"deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"once": "^1.3.0",
|
||||
"wrappy": "1"
|
||||
@ -4862,8 +4955,7 @@
|
||||
"node_modules/inherits": {
|
||||
"version": "2.0.4",
|
||||
"resolved": "https://registry.npmmirror.com/inherits/-/inherits-2.0.4.tgz",
|
||||
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
|
||||
},
|
||||
"node_modules/interpret": {
|
||||
"version": "3.1.1",
|
||||
@ -6083,7 +6175,6 @@
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmmirror.com/minimatch/-/minimatch-3.1.2.tgz",
|
||||
"integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"brace-expansion": "^1.1.7"
|
||||
},
|
||||
@ -6091,6 +6182,30 @@
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/minimist": {
|
||||
"version": "0.1.0",
|
||||
"resolved": "https://registry.npmmirror.com/minimist/-/minimist-0.1.0.tgz",
|
||||
"integrity": "sha512-wR5Ipl99t0mTGwLjQJnBjrP/O7zBbLZqvA3aw32DmLx+nXHfWctUjzDjnDx09pX1Po86WFQazF9xUzfMea3Cnw=="
|
||||
},
|
||||
"node_modules/mkdirp": {
|
||||
"version": "0.5.6",
|
||||
"resolved": "https://registry.npmmirror.com/mkdirp/-/mkdirp-0.5.6.tgz",
|
||||
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
|
||||
"dependencies": {
|
||||
"minimist": "^1.2.6"
|
||||
},
|
||||
"bin": {
|
||||
"mkdirp": "bin/cmd.js"
|
||||
}
|
||||
},
|
||||
"node_modules/mkdirp/node_modules/minimist": {
|
||||
"version": "1.2.8",
|
||||
"resolved": "https://registry.npmmirror.com/minimist/-/minimist-1.2.8.tgz",
|
||||
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/moment": {
|
||||
"version": "2.30.1",
|
||||
"resolved": "https://registry.npmmirror.com/moment/-/moment-2.30.1.tgz",
|
||||
@ -6262,7 +6377,6 @@
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmmirror.com/once/-/once-1.4.0.tgz",
|
||||
"integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"wrappy": "1"
|
||||
}
|
||||
@ -6376,7 +6490,6 @@
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmmirror.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
|
||||
"integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
@ -8038,8 +8151,7 @@
|
||||
"node_modules/wrappy": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmmirror.com/wrappy/-/wrappy-1.0.2.tgz",
|
||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
|
||||
},
|
||||
"node_modules/write-file-atomic": {
|
||||
"version": "4.0.2",
|
||||
|
10
package.json
10
package.json
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "code-flow",
|
||||
"name": "@abearxiong/code-flow",
|
||||
"version": "0.0.2",
|
||||
"description": "code的flow流程成图",
|
||||
"type": "module",
|
||||
@ -21,6 +21,10 @@
|
||||
"docker:push:gitea": "docker push git.xiongxiao.me/abearxiong/code-flow:v0.0.2"
|
||||
},
|
||||
"keywords": [],
|
||||
"types": "types/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
@ -30,6 +34,7 @@
|
||||
"@babel/preset-env": "^7.24.7",
|
||||
"@babel/preset-typescript": "^7.24.7",
|
||||
"dayjs": "^1.11.11",
|
||||
"dts-bundle": "^0.7.3",
|
||||
"json5": "^2.2.3",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"lodash-es": "^4.17.21",
|
||||
@ -41,6 +46,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/crypto-js": "^4.2.2",
|
||||
"@types/dts-bundle": "^0.0.35",
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/jsonwebtoken": "^9.0.6",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
@ -64,4 +70,4 @@
|
||||
"webpack-node-externals": "^3.0.0"
|
||||
},
|
||||
"packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
|
||||
}
|
||||
}
|
||||
|
16
script/dts/index.mjs
Normal file
16
script/dts/index.mjs
Normal file
@ -0,0 +1,16 @@
|
||||
import * as dts from 'dts-bundle';
|
||||
import path from 'path';
|
||||
|
||||
const currentPath = process.cwd();
|
||||
const packagePath = path.join(currentPath, 'script/package/package.json');
|
||||
|
||||
const mainPath = path.join(currentPath, 'src/type.ts');
|
||||
const outPath = path.join(currentPath, 'types/index.d.ts');
|
||||
// node script/dts/index.mjs
|
||||
dts.bundle({
|
||||
name: 'my-library',
|
||||
main: mainPath,
|
||||
out: outPath,
|
||||
removeSource: true,
|
||||
outputAsModuleFolder: true,
|
||||
});
|
@ -13,6 +13,7 @@ export type CodeManager = {
|
||||
status?: CodeStatus;
|
||||
errorMsg?: string;
|
||||
} & Partial<RouterCode>;
|
||||
|
||||
const codeDemoRun = `async function run(ctx) {
|
||||
ctx.body = 'test js';
|
||||
return ctx;
|
||||
|
@ -170,7 +170,19 @@ getRouterApi.run = async (ctx) => {
|
||||
validator: item.validator,
|
||||
};
|
||||
});
|
||||
ctx.body = apiList;
|
||||
const apiKeyObject = apiList.reduce((pre: any, cur: any) => {
|
||||
pre[cur.key] = {
|
||||
path: cur.path,
|
||||
key: cur.key,
|
||||
description: cur.description || '',
|
||||
validator: cur.validator || {},
|
||||
};
|
||||
return pre;
|
||||
}, {});
|
||||
ctx.body = {
|
||||
list: apiList,
|
||||
keyObject: apiKeyObject,
|
||||
};
|
||||
return ctx;
|
||||
};
|
||||
getRouterApi.validator = {
|
||||
|
3
src/type.ts
Normal file
3
src/type.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { CodeManager } from './admin/dashboard/load.ts';
|
||||
|
||||
export { CodeManager };
|
5
src/utils/filter.ts
Normal file
5
src/utils/filter.ts
Normal file
@ -0,0 +1,5 @@
|
||||
// 过滤掉值为 null 的字段
|
||||
export const filterNull = (updateData: Record<string, any>) => {
|
||||
const filteredData = Object.fromEntries(Object.entries(updateData).filter(([_, v]) => v !== null));
|
||||
return filteredData;
|
||||
};
|
248
types/index.d.ts
vendored
Normal file
248
types/index.d.ts
vendored
Normal file
@ -0,0 +1,248 @@
|
||||
// Generated by dts-bundle v0.7.3
|
||||
// Dependencies for this module:
|
||||
// ../@abearxiong/router
|
||||
// ../sequelize
|
||||
// ../@abearxiong/use-config
|
||||
|
||||
import { Route } from '@abearxiong/router';
|
||||
import { QueryRouter, Server } from '@abearxiong/router';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
import { Sequelize } from 'sequelize';
|
||||
|
||||
|
||||
export { CodeManager };
|
||||
|
||||
|
||||
export enum CodeStatus {
|
||||
running = 'running',
|
||||
stop = 'stop',
|
||||
fail = 'fail',
|
||||
}
|
||||
|
||||
export type CodeManager = {
|
||||
fn?: any;
|
||||
status?: CodeStatus;
|
||||
errorMsg?: string;
|
||||
} & Partial<RouterCode>;
|
||||
|
||||
const codeDemoRun = `async function run(ctx) {
|
||||
ctx.body = 'test js';
|
||||
return ctx;
|
||||
}`;
|
||||
const templateFn = (codeStr: string) => {
|
||||
return `
|
||||
${codeStr}
|
||||
if(run) {
|
||||
return run(ctx);
|
||||
}
|
||||
if(main) {
|
||||
return main(ctx);
|
||||
}
|
||||
return 'no run or main function';
|
||||
`;
|
||||
};
|
||||
export const loadOne = async (item: RouterCodeModel) => {
|
||||
const { path, key, id, code, exec, project } = item.toJSON();
|
||||
const codeStr = exec || code;
|
||||
try {
|
||||
const fn: any = new Function('ctx', templateFn(codeStr));
|
||||
// run code
|
||||
const codeRunRoute = new Route(path, key, { id });
|
||||
codeRunRoute.run = fn;
|
||||
router.removeById(id); // TODO:
|
||||
router.add(codeRunRoute);
|
||||
return {
|
||||
...item.toJSON(),
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
project,
|
||||
fn,
|
||||
status: CodeStatus.running,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('error id:', id, '\n', e);
|
||||
return {
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
project,
|
||||
status: CodeStatus.fail,
|
||||
errorMsg: e.message.toString(),
|
||||
};
|
||||
}
|
||||
};
|
||||
export const load = async function () {
|
||||
const codes = await RouterCodeModel.findAll();
|
||||
const codeManager: CodeManager[] = codes.map((item) => {
|
||||
const { path, key, id, code, exec, project, active } = item.toJSON();
|
||||
if (!active) {
|
||||
return {
|
||||
...item.toJSON(),
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
code,
|
||||
project,
|
||||
status: CodeStatus.stop,
|
||||
};
|
||||
}
|
||||
try {
|
||||
const codeStr = exec || code;
|
||||
const fn: any = new Function('ctx', templateFn(codeStr));
|
||||
// run code
|
||||
const codeRunRoute = new Route(path, key, { id });
|
||||
codeRunRoute.run = fn;
|
||||
router.add(codeRunRoute);
|
||||
return {
|
||||
...item.toJSON(),
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
code,
|
||||
project,
|
||||
type: item.type,
|
||||
fn,
|
||||
status: CodeStatus.running,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('error id:', id, '\n', e);
|
||||
return {
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
code,
|
||||
project,
|
||||
type: item.type,
|
||||
status: CodeStatus.fail,
|
||||
errorMsg: e.message.toString(),
|
||||
};
|
||||
}
|
||||
});
|
||||
return codeManager;
|
||||
};
|
||||
|
||||
export const router = new QueryRouter();
|
||||
export const server = new Server({
|
||||
path: '/api/router',
|
||||
});
|
||||
|
||||
|
||||
export type RouterCode = {
|
||||
id: string;
|
||||
path: string;
|
||||
key: string;
|
||||
active: boolean;
|
||||
project: string;
|
||||
code: string;
|
||||
exec: string;
|
||||
type: RouterCodeType;
|
||||
middleware: string[];
|
||||
next: string;
|
||||
data: any;
|
||||
validator: any;
|
||||
};
|
||||
|
||||
export enum RouterCodeType {
|
||||
route = 'route',
|
||||
middleware = 'middleware',
|
||||
}
|
||||
|
||||
export class RouterCodeModel extends Model {
|
||||
declare id: string;
|
||||
declare path: string;
|
||||
declare key: string;
|
||||
declare active: boolean;
|
||||
declare project: string;
|
||||
declare code: string;
|
||||
declare exec: string;
|
||||
declare type: RouterCodeType;
|
||||
declare middleware: string[];
|
||||
declare next: string; // 如果是中间件,不存在
|
||||
declare data: any; // 内容
|
||||
declare validator: any;
|
||||
}
|
||||
RouterCodeModel.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: '用户id',
|
||||
},
|
||||
path: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
key: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
},
|
||||
project: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: 'default',
|
||||
},
|
||||
code: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
exec: {
|
||||
type: DataTypes.STRING, // 对代码进行编译后的代码
|
||||
defaultValue: '',
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.ENUM(RouterCodeType.route, RouterCodeType.middleware),
|
||||
defaultValue: RouterCodeType.route,
|
||||
},
|
||||
middleware: {
|
||||
type: DataTypes.ARRAY(DataTypes.STRING),
|
||||
defaultValue: [],
|
||||
},
|
||||
next: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
validator: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'cf_router_code',
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
type PostgresConfig = {
|
||||
postgres: {
|
||||
username: string;
|
||||
password: string;
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
};
|
||||
};
|
||||
const config = useConfig<PostgresConfig>();
|
||||
|
||||
const postgresConfig = config.postgres;
|
||||
|
||||
if (!postgresConfig) {
|
||||
console.error('postgres config is required');
|
||||
process.exit(1);
|
||||
}
|
||||
export const sequelize = new Sequelize({
|
||||
dialect: 'postgres',
|
||||
...postgresConfig,
|
||||
// logging: false,
|
||||
});
|
||||
|
92
yarn.lock
92
yarn.lock
@ -1423,6 +1423,16 @@
|
||||
dependencies:
|
||||
"@types/ms" "*"
|
||||
|
||||
"@types/detect-indent@0.1.30":
|
||||
version "0.1.30"
|
||||
resolved "https://registry.npmmirror.com/@types/detect-indent/-/detect-indent-0.1.30.tgz"
|
||||
integrity sha512-AUmj9JHuHTD94slY1WR1VulFxRGC6D1pcNCN0MCulKFyiihvV/28lLS8oRHgfmc2Cxq954J8Vmosa8qzm7PLGQ==
|
||||
|
||||
"@types/dts-bundle@^0.0.35":
|
||||
version "0.0.35"
|
||||
resolved "https://registry.npmmirror.com/@types/dts-bundle/-/dts-bundle-0.0.35.tgz"
|
||||
integrity sha512-TZCP4CIDR3R3K2ij5UqXzKfkiAcnVBSUudPOq0zUk42hl8PirN/4q0dPiidBP3s7hZJuB7OQULxR3I2ZQsnLig==
|
||||
|
||||
"@types/eslint-scope@^3.7.3":
|
||||
version "3.7.7"
|
||||
resolved "https://registry.npmmirror.com/@types/eslint-scope/-/eslint-scope-3.7.7.tgz"
|
||||
@ -1444,6 +1454,14 @@
|
||||
resolved "https://registry.npmmirror.com/@types/estree/-/estree-1.0.5.tgz"
|
||||
integrity sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==
|
||||
|
||||
"@types/glob@5.0.30":
|
||||
version "5.0.30"
|
||||
resolved "https://registry.npmmirror.com/@types/glob/-/glob-5.0.30.tgz"
|
||||
integrity sha512-ZM05wDByI+WA153sfirJyEHoYYoIuZ7lA2dB/Gl8ymmpMTR78fNRtDMqa7Z6SdH4fZdLWZNRE6mZpx3XqBOrHw==
|
||||
dependencies:
|
||||
"@types/minimatch" "*"
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/graceful-fs@^4.1.3":
|
||||
version "4.1.9"
|
||||
resolved "https://registry.npmmirror.com/@types/graceful-fs/-/graceful-fs-4.1.9.tgz"
|
||||
@ -1507,6 +1525,16 @@
|
||||
resolved "https://registry.npmmirror.com/@types/methods/-/methods-1.1.4.tgz"
|
||||
integrity sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==
|
||||
|
||||
"@types/minimatch@*":
|
||||
version "5.1.2"
|
||||
resolved "https://registry.npmmirror.com/@types/minimatch/-/minimatch-5.1.2.tgz"
|
||||
integrity sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==
|
||||
|
||||
"@types/mkdirp@0.3.29":
|
||||
version "0.3.29"
|
||||
resolved "https://registry.npmmirror.com/@types/mkdirp/-/mkdirp-0.3.29.tgz"
|
||||
integrity sha512-QRLQpFsIQGO2k8pupga9abfei85GKotAtQ+F6xuQmSGomUt6C52TyMiTFpP8kUwuPKr00gNtu3itLlC6gvI/NA==
|
||||
|
||||
"@types/ms@*":
|
||||
version "0.7.34"
|
||||
resolved "https://registry.npmmirror.com/@types/ms/-/ms-0.7.34.tgz"
|
||||
@ -1519,6 +1547,11 @@
|
||||
dependencies:
|
||||
undici-types "~5.26.4"
|
||||
|
||||
"@types/node@8.0.0":
|
||||
version "8.0.0"
|
||||
resolved "https://registry.npmmirror.com/@types/node/-/node-8.0.0.tgz"
|
||||
integrity sha512-j2tekvJCO7j22cs+LO6i0kRPhmQ9MXaPZ55TzOc1lzkN5b6BWqq4AFjl04s1oRRQ1v5rSe+KEvnLUSTonuls/A==
|
||||
|
||||
"@types/stack-utils@^2.0.0":
|
||||
version "2.0.3"
|
||||
resolved "https://registry.npmmirror.com/@types/stack-utils/-/stack-utils-2.0.3.tgz"
|
||||
@ -2154,7 +2187,7 @@ commander@^10.0.1:
|
||||
resolved "https://registry.npmmirror.com/commander/-/commander-10.0.1.tgz"
|
||||
integrity sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==
|
||||
|
||||
commander@^2.20.0:
|
||||
commander@^2.20.0, commander@^2.9.0:
|
||||
version "2.20.3"
|
||||
resolved "https://registry.npmmirror.com/commander/-/commander-2.20.3.tgz"
|
||||
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
|
||||
@ -2313,6 +2346,14 @@ delayed-stream@~1.0.0:
|
||||
resolved "https://registry.npmmirror.com/delayed-stream/-/delayed-stream-1.0.0.tgz"
|
||||
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==
|
||||
|
||||
detect-indent@^0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.npmmirror.com/detect-indent/-/detect-indent-0.2.0.tgz"
|
||||
integrity sha512-C6jyrDu/eGH4KT0ZxAzijiH+ts5YLy7DqGFoDuHGxZjMOdjzRltp3jByySnpFBVIy4Em0ZkLN8tIV6mcREdw5A==
|
||||
dependencies:
|
||||
get-stdin "^0.1.0"
|
||||
minimist "^0.1.0"
|
||||
|
||||
detect-newline@^3.0.0:
|
||||
version "3.1.0"
|
||||
resolved "https://registry.npmmirror.com/detect-newline/-/detect-newline-3.1.0.tgz"
|
||||
@ -2341,6 +2382,20 @@ dottie@^2.0.6:
|
||||
resolved "https://registry.npmmirror.com/dottie/-/dottie-2.0.6.tgz"
|
||||
integrity sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==
|
||||
|
||||
dts-bundle@^0.7.3:
|
||||
version "0.7.3"
|
||||
resolved "https://registry.npmmirror.com/dts-bundle/-/dts-bundle-0.7.3.tgz"
|
||||
integrity sha512-EEAEuPRk8QyKhoN90NHTh+spSQujkkvOnKWUfuzpmC/fgryiWopL1SegSktx0UsoPfNidIGVDN7/AXpBDBv0WQ==
|
||||
dependencies:
|
||||
"@types/detect-indent" "0.1.30"
|
||||
"@types/glob" "5.0.30"
|
||||
"@types/mkdirp" "0.3.29"
|
||||
"@types/node" "8.0.0"
|
||||
commander "^2.9.0"
|
||||
detect-indent "^0.2.0"
|
||||
glob "^6.0.4"
|
||||
mkdirp "^0.5.0"
|
||||
|
||||
ecdsa-sig-formatter@1.0.11:
|
||||
version "1.0.11"
|
||||
resolved "https://registry.npmmirror.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz"
|
||||
@ -2658,6 +2713,11 @@ get-package-type@^0.1.0:
|
||||
resolved "https://registry.npmmirror.com/get-package-type/-/get-package-type-0.1.0.tgz"
|
||||
integrity sha512-pjzuKtY64GYfWizNAJ0fr9VqttZkNiK2iS430LtIHzjBEr6bX8Am2zm4sW4Ro5wjWW5cAlRL1qAMTcXbjNAO2Q==
|
||||
|
||||
get-stdin@^0.1.0:
|
||||
version "0.1.0"
|
||||
resolved "https://registry.npmmirror.com/get-stdin/-/get-stdin-0.1.0.tgz"
|
||||
integrity sha512-/WBu3IaQZxE3bs3BhBmR10ipDY4pjN+U4EZgXULa1eqKA0B/Lka/MVoAqhTVYBkkRlCrEGDOU9itrzIgm9Ksng==
|
||||
|
||||
get-stream@^6.0.0:
|
||||
version "6.0.1"
|
||||
resolved "https://registry.npmmirror.com/get-stream/-/get-stream-6.0.1.tgz"
|
||||
@ -2689,6 +2749,17 @@ glob-to-regexp@^0.4.1:
|
||||
resolved "https://registry.npmmirror.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz"
|
||||
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==
|
||||
|
||||
glob@^6.0.4:
|
||||
version "6.0.4"
|
||||
resolved "https://registry.npmmirror.com/glob/-/glob-6.0.4.tgz"
|
||||
integrity sha512-MKZeRNyYZAVVVG1oZeLaWie1uweH40m9AZwIwxyPbTSX4hHrVYSzLg0Ro5Z5R7XKkIX+Cc6oD1rqeDJnwsB8/A==
|
||||
dependencies:
|
||||
inflight "^1.0.4"
|
||||
inherits "2"
|
||||
minimatch "2 || 3"
|
||||
once "^1.3.0"
|
||||
path-is-absolute "^1.0.0"
|
||||
|
||||
glob@^7.1.3, glob@^7.1.4:
|
||||
version "7.2.3"
|
||||
resolved "https://registry.npmmirror.com/glob/-/glob-7.2.3.tgz"
|
||||
@ -3578,13 +3649,30 @@ mimic-fn@^2.1.0:
|
||||
resolved "https://registry.npmmirror.com/mimic-fn/-/mimic-fn-2.1.0.tgz"
|
||||
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==
|
||||
|
||||
minimatch@^3.0.4, minimatch@^3.1.1, minimatch@^3.1.2:
|
||||
minimatch@^3.0.4, minimatch@^3.1.1, minimatch@^3.1.2, "minimatch@2 || 3":
|
||||
version "3.1.2"
|
||||
resolved "https://registry.npmmirror.com/minimatch/-/minimatch-3.1.2.tgz"
|
||||
integrity sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==
|
||||
dependencies:
|
||||
brace-expansion "^1.1.7"
|
||||
|
||||
minimist@^0.1.0:
|
||||
version "0.1.0"
|
||||
resolved "https://registry.npmmirror.com/minimist/-/minimist-0.1.0.tgz"
|
||||
integrity sha512-wR5Ipl99t0mTGwLjQJnBjrP/O7zBbLZqvA3aw32DmLx+nXHfWctUjzDjnDx09pX1Po86WFQazF9xUzfMea3Cnw==
|
||||
|
||||
minimist@^1.2.6:
|
||||
version "1.2.8"
|
||||
resolved "https://registry.npmmirror.com/minimist/-/minimist-1.2.8.tgz"
|
||||
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
|
||||
|
||||
mkdirp@^0.5.0:
|
||||
version "0.5.6"
|
||||
resolved "https://registry.npmmirror.com/mkdirp/-/mkdirp-0.5.6.tgz"
|
||||
integrity sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==
|
||||
dependencies:
|
||||
minimist "^1.2.6"
|
||||
|
||||
moment-timezone@^0.5.43:
|
||||
version "0.5.45"
|
||||
resolved "https://registry.npmmirror.com/moment-timezone/-/moment-timezone-0.5.45.tgz"
|
||||
|
Loading…
x
Reference in New Issue
Block a user