poad 2.24.0 → 2.25.0
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/package.json +2 -2
- package/project/cli.js +1 -1
- package/project/functions.js +68 -20
package/package.json
CHANGED
package/project/cli.js
CHANGED
|
@@ -29,7 +29,7 @@ module.exports = {
|
|
|
29
29
|
},
|
|
30
30
|
migrate: 'Apply pending migrations of poad/migrations and named queries of poad/queries (poad start does it too)',
|
|
31
31
|
migration: 'Database migrations: migration new <name>, migration list',
|
|
32
|
-
functions: 'Functions of the project (JS/TS, served on /functions/v1/<name>): functions new <name> (or create), functions list',
|
|
32
|
+
functions: 'Functions of the project (JS/TS, served on /functions/v1/<name>): functions new <name> (or create), functions list, functions types (editor autocompletion)',
|
|
33
33
|
function: 'Same as functions: function create <name>, function list',
|
|
34
34
|
/* status: `[AI] Vérification du statut de l'environnement`,
|
|
35
35
|
gen: {
|
package/project/functions.js
CHANGED
|
@@ -1,44 +1,52 @@
|
|
|
1
1
|
// poad functions new <nom> crée poad/functions/<nom>/index.ts
|
|
2
2
|
// poad functions list fonctions du projet et leur état dans l'engine lancé
|
|
3
|
+
// poad functions types types pour l'éditeur (autocomplétion), lus sur l'engine lancé
|
|
3
4
|
//
|
|
4
5
|
// Une fonction est un module qui exporte { fetch(request, env, ctx) }, au
|
|
5
6
|
// format de Cloudflare Workers. L'engine la sert sur /functions/v1/<nom> et
|
|
6
7
|
// la recharge à chaque enregistrement (engine 0.16.0).
|
|
7
8
|
const NAME_RE = /^[a-z][a-z0-9-]{0,62}$/;
|
|
8
9
|
|
|
9
|
-
const TEMPLATE = (name) => `import { poadFromRequest, caller } from '@poadio/functions';
|
|
10
|
+
const TEMPLATE = (name) => `import { poadFromRequest, caller, type PoadEnv } from '@poadio/functions';
|
|
10
11
|
|
|
11
12
|
// Servie sur /functions/v1/${name}. Depuis votre application :
|
|
12
13
|
// const res = await poad.fetch('/functions/v1/${name}', { method: 'POST', body: JSON.stringify({ name: 'poad' }) });
|
|
13
14
|
export default {
|
|
14
|
-
async fetch(request: Request, env:
|
|
15
|
+
async fetch(request: Request, env: PoadEnv): Promise<Response> {
|
|
15
16
|
// L'appelant, lu dans son jeton (null sans jeton)
|
|
16
17
|
const me = caller(request);
|
|
17
18
|
// Client poad au nom de l'appelant : les politiques RLS s'appliquent
|
|
18
19
|
const poad = poadFromRequest(request, env);
|
|
19
20
|
|
|
20
|
-
const body = request.method === 'POST' ? await request.json().catch(() => ({})) : {};
|
|
21
|
+
const body: { name?: string } = request.method === 'POST' ? await request.json().catch(() => ({})) : {};
|
|
21
22
|
console.log('${name} called', { role: me?.role });
|
|
22
23
|
return Response.json({ message: \`Hello \${body.name ?? me?.email ?? 'world'}\` });
|
|
23
24
|
},
|
|
24
25
|
};
|
|
25
26
|
`;
|
|
26
27
|
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
// tsconfig.json de poad/functions : API web (celles de workerd) et @poadio/*
|
|
29
|
+
// relié aux types déposés par l'engine, pour l'autocomplétion sans node_modules
|
|
30
|
+
const TSCONFIG = `{
|
|
31
|
+
"compilerOptions": {
|
|
32
|
+
"target": "ES2022",
|
|
33
|
+
"module": "ESNext",
|
|
34
|
+
"moduleResolution": "bundler",
|
|
35
|
+
"lib": ["ES2022", "WebWorker"],
|
|
36
|
+
"types": [],
|
|
37
|
+
"strict": true,
|
|
38
|
+
"noEmit": true,
|
|
39
|
+
"skipLibCheck": true,
|
|
40
|
+
"allowImportingTsExtensions": true,
|
|
41
|
+
"paths": {
|
|
42
|
+
"@poadio/*": ["./.poad/types/@poadio/*/index.d.ts"]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"include": ["**/*.ts", "**/*.tsx"],
|
|
46
|
+
"exclude": ["node_modules"]
|
|
40
47
|
}
|
|
41
48
|
`;
|
|
49
|
+
const TYPE_FILE_RE = /^@poadio\/[a-z][a-z0-9-]*(\/[A-Za-z0-9_-]+)*\/[A-Za-z0-9_.-]+\.d\.ts$/;
|
|
42
50
|
|
|
43
51
|
module.exports = async (args = {}) => {
|
|
44
52
|
const chalk = require('chalk-v2');
|
|
@@ -54,6 +62,48 @@ module.exports = async (args = {}) => {
|
|
|
54
62
|
const rel = (p) => path.relative(process.cwd(), p) || p;
|
|
55
63
|
const words = Object.keys(args);
|
|
56
64
|
|
|
65
|
+
// Types pour l'éditeur : lus sur l'engine lancé (accordés à son client poad)
|
|
66
|
+
// et déposés dans poad/functions/.poad/types. Renvoie le nombre de fichiers, ou null.
|
|
67
|
+
const installTypes = async () => {
|
|
68
|
+
const config = readConfig(root);
|
|
69
|
+
if (!(await isDockerExist(config.project_uuid))) return null;
|
|
70
|
+
let bundle;
|
|
71
|
+
try {
|
|
72
|
+
const res = await fetch(`http://localhost:${config.web.port}/functions/v1/types.json`, { signal: AbortSignal.timeout(5000) });
|
|
73
|
+
if (!res.ok) return null;
|
|
74
|
+
bundle = await res.json();
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const files = Object.entries((bundle && bundle.files) || {}).filter(([name, text]) => TYPE_FILE_RE.test(name) && !name.includes('..') && typeof text === 'string');
|
|
79
|
+
if (!files.length) return null;
|
|
80
|
+
const typesDir = path.join(dir, '.poad', 'types');
|
|
81
|
+
fs.rmSync(typesDir, { recursive: true, force: true });
|
|
82
|
+
for (const [name, text] of files) {
|
|
83
|
+
const target = path.join(typesDir, name);
|
|
84
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
85
|
+
fs.writeFileSync(target, text);
|
|
86
|
+
}
|
|
87
|
+
// L'ancien fichier de types (client en « any ») masquerait les nouveaux
|
|
88
|
+
const old = path.join(dir, 'poad-functions.d.ts');
|
|
89
|
+
if (fs.existsSync(old) && fs.readFileSync(old, 'utf8').startsWith('// Généré par poad')) fs.rmSync(old);
|
|
90
|
+
const tsconfig = path.join(dir, 'tsconfig.json');
|
|
91
|
+
if (!fs.existsSync(tsconfig)) fs.writeFileSync(tsconfig, TSCONFIG);
|
|
92
|
+
return files.length;
|
|
93
|
+
};
|
|
94
|
+
const reportTypes = (count) => {
|
|
95
|
+
if (count) console.log(chalk.grey(` Editor types: ${rel(path.join(dir, '.poad', 'types'))} (${count} files) and ${rel(path.join(dir, 'tsconfig.json'))}. No install needed.`));
|
|
96
|
+
else console.log(chalk.yellowBright(' Editor types not installed: start the project (engine 0.16.4+), then "poad functions types".'));
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
if (words[0] === 'types') {
|
|
100
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
101
|
+
const count = await installTypes();
|
|
102
|
+
if (!count) process.exitCode = 1;
|
|
103
|
+
else console.log(`${chalk.greenBright('✔')} ${chalk.bold('Types of @poadio/functions and the poad clients')}`);
|
|
104
|
+
return reportTypes(count);
|
|
105
|
+
}
|
|
106
|
+
|
|
57
107
|
if (words[0] === 'new' || words[0] === 'create') {
|
|
58
108
|
const name = String(words[1] || '');
|
|
59
109
|
if (!NAME_RE.test(name)) {
|
|
@@ -67,11 +117,9 @@ module.exports = async (args = {}) => {
|
|
|
67
117
|
}
|
|
68
118
|
fs.mkdirSync(fnDir, { recursive: true });
|
|
69
119
|
fs.writeFileSync(path.join(fnDir, 'index.ts'), TEMPLATE(name));
|
|
70
|
-
const types = path.join(dir, 'poad-functions.d.ts');
|
|
71
|
-
if (!fs.existsSync(types)) fs.writeFileSync(types, TYPES);
|
|
72
120
|
console.log(`${chalk.greenBright('✔')} ${chalk.bold(rel(path.join(fnDir, 'index.ts')))}`);
|
|
73
121
|
console.log(chalk.grey(` Served on /functions/v1/${name}, reloaded on save. Options in ${rel(path.join(fnDir, 'function.json'))}: { "verify_jwt": false, "service_role": true }.`));
|
|
74
|
-
return;
|
|
122
|
+
return reportTypes(await installTypes());
|
|
75
123
|
}
|
|
76
124
|
|
|
77
125
|
if (words[0] === 'list' || !words.length) {
|
|
@@ -103,5 +151,5 @@ module.exports = async (args = {}) => {
|
|
|
103
151
|
}
|
|
104
152
|
|
|
105
153
|
process.exitCode = 1;
|
|
106
|
-
console.log(chalk.redBright('Usage: poad functions new <name> | poad functions list'));
|
|
154
|
+
console.log(chalk.redBright('Usage: poad functions new <name> | poad functions list | poad functions types'));
|
|
107
155
|
};
|