create-erxes 0.1.15 → 0.1.16

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/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import { mkdir, readFile, readdir, symlink, writeFile } from "node:fs/promises";
9
9
  import { homedir } from "node:os";
10
10
  import { fileURLToPath } from "node:url";
11
11
  // package.json
12
- var version = "0.1.15";
12
+ var version = "0.1.16";
13
13
  // src/existing-plugins.ts
14
14
  /** The old erxes plugins a new-core plugin can be migrated from (`backend/plugins/<name>_api`). */
15
15
  const OLD_PLUGINS = [
@@ -54,7 +54,10 @@ without line-by-line review.
54
54
  through `@erxes/core-contract/client` with the member's session. Neither reads
55
55
  `core_*` tables or imports core's app code.
56
56
  - Members are signed in by core: the UI sends a session token, the API checks it
57
- with `verifyPluginSession`. A caller outside erxes (a webhook) reaches a path
57
+ with `verifyPluginSession`. The same procedures are REST at `/api/v1` for
58
+ scripts, which get the token from core with an API key; `/api/openapi.json`
59
+ documents them (`pluginOpenApi`), and core's Settings › Developer › API reference
60
+ shows it. Renaming a procedure breaks those scripts. A caller outside erxes (a webhook) reaches a path
58
61
  the manifest's `publicPaths` lists through core, which names the organisation:
59
62
  check it with `verifyPublicCall` (`@erxes/plugin-sdk`'s overview has the route).
60
63
  - Skills in `.agents/skills/`: `endpoint` for a procedure, `schema-change` for a
@@ -21,13 +21,14 @@
21
21
  "db:check": "drizzle-kit check"
22
22
  },
23
23
  "dependencies": {
24
- "@erxes/core-contract": "0.1.0-v2.13",
25
- "@erxes/plugin-sdk": "0.1.0-v2.11",
24
+ "@erxes/core-contract": "0.1.0-v2.15",
25
+ "@erxes/plugin-sdk": "0.1.0-v2.13",
26
26
  "@opentelemetry/instrumentation-express": "^0.73.0",
27
27
  "@opentelemetry/instrumentation-http": "^0.301.1",
28
28
  "@opentelemetry/instrumentation-pg": "^0.78.0",
29
29
  "@opentelemetry/instrumentation-pino": "^0.71.0",
30
30
  "@opentelemetry/sdk-node": "^0.301.1",
31
+ "@orpc/openapi": "1.16.2",
31
32
  "@orpc/server": "1.16.2",
32
33
  "cors": "^2.8.6",
33
34
  "drizzle-orm": "0.45.4",
@@ -30,7 +30,8 @@ export const manifest = {
30
30
  description: '__description__',
31
31
  version: config.version,
32
32
  ui: { remote: '__id___ui', entry: config.uiEntryUrl },
33
- api: { url: `${config.publicUrl}/api` },
33
+ // `server.ts` serves the REST routes at /api/v1 and their document here.
34
+ api: { url: `${config.publicUrl}/api`, openapi: '/openapi.json' },
34
35
  scopes: [],
35
36
  permissions: [
36
37
  { action: PERMISSIONS.view, label: 'View items', group: '__title__' },
@@ -1,20 +1,32 @@
1
1
  import { bearerOf, verifyCoreJob, verifyPluginSession } from '@erxes/plugin-sdk';
2
+ import { pluginOpenApi } from '@erxes/plugin-sdk/openapi';
3
+ import { OpenAPIHandler } from '@orpc/openapi/node';
2
4
  import { onError, ORPCError } from '@orpc/server';
3
5
  import { RPCHandler } from '@orpc/server/node';
4
6
  import cors from 'cors';
5
7
  import express from 'express';
6
8
  import { pinoHttp } from 'pino-http';
7
9
  import { config, PLUGIN } from '#/config.ts';
10
+ import { manifest } from '#/core.ts';
8
11
  import { runJob } from '#/jobs.ts';
9
12
  import { logger } from '#/logger.ts';
10
13
  import { router } from '#/router.ts';
11
14
 
12
- const rpc = new RPCHandler(router, {
13
- interceptors: [
14
- onError((error) => {
15
- if (!(error instanceof ORPCError) || error.status >= 500) logger.error(error);
16
- }),
17
- ],
15
+ const logFailure = onError((error) => {
16
+ if (!(error instanceof ORPCError) || error.status >= 500) logger.error(error);
17
+ });
18
+
19
+ // The same procedures twice: RPC for the plugin UI, REST (documented below) for scripts.
20
+ const rpc = new RPCHandler(router, { interceptors: [logFailure] });
21
+ const rest = new OpenAPIHandler(router, { interceptors: [logFailure] });
22
+
23
+ /** Settings › Developer › API reference shows it (the manifest's `api.openapi`). */
24
+ const openApi = pluginOpenApi(router, {
25
+ plugin: PLUGIN,
26
+ title: manifest.title,
27
+ version: manifest.version,
28
+ description: manifest.description,
29
+ prefix: '/v1',
18
30
  });
19
31
 
20
32
  /** The member behind a request from the plugin UI, or null without a valid session token. */
@@ -35,26 +47,39 @@ export const createApp = () => {
35
47
  // uses (acme.app.erxes.io, its own domain). Any origin may: access is the
36
48
  // Bearer session token core issues, never a cookie, so CORS guards nothing here.
37
49
  // Routes for websites (an API key, customer tokens) set their own CORS.
38
- app.use(['/api/rpc', '/ui'], cors({ origin: true, credentials: false, maxAge: 600 }));
50
+ app.use(
51
+ ['/api/rpc', '/api/v1', '/api/openapi.json', '/ui'],
52
+ cors({ origin: true, credentials: false, maxAge: 600 }),
53
+ );
39
54
 
40
55
  app.get('/health', (_req, res) => {
41
56
  res.json({ ok: true });
42
57
  });
43
58
 
44
- // erxes-ui's pluginFetch adds a 5-minute session token core issued for this member and plugin.
45
- app.use('/api/rpc', async (req, res, next) => {
46
- const member = await memberOf(req.headers.authorization);
59
+ app.get('/api/openapi.json', async (_req, res) => {
60
+ res.json(await openApi);
61
+ });
47
62
 
48
- if (!member) {
49
- res.status(401).json({ message: 'Open this plugin inside erxes to sign in' });
63
+ // A 5-minute session token core issued for this member and plugin: erxes-ui's pluginFetch
64
+ // adds it, and a script gets one from core with an API key.
65
+ for (const [prefix, handler] of [
66
+ ['/api/rpc', rpc],
67
+ ['/api/v1', rest],
68
+ ] as const) {
69
+ app.use(prefix, async (req, res, next) => {
70
+ const member = await memberOf(req.headers.authorization);
50
71
 
51
- return;
52
- }
72
+ if (!member) {
73
+ res.status(401).json({ message: 'Send a session token for this plugin from core' });
53
74
 
54
- const { matched } = await rpc.handle(req, res, { prefix: '/api/rpc', context: { member } });
75
+ return;
76
+ }
55
77
 
56
- if (!matched) next();
57
- });
78
+ const { matched } = await handler.handle(req, res, { prefix, context: { member } });
79
+
80
+ if (!matched) next();
81
+ });
82
+ }
58
83
 
59
84
  // Core's jobs (`api/src/jobs.ts`). The token signs the raw body, so it stays text.
60
85
  app.post('/api/jobs/:name', express.text({ type: '*/*' }), async (req, res) => {
@@ -8,7 +8,7 @@
8
8
  "build": "rsbuild build"
9
9
  },
10
10
  "dependencies": {
11
- "@erxes/core-contract": "0.1.0-v2.13",
11
+ "@erxes/core-contract": "0.1.0-v2.15",
12
12
  "@orpc/client": "1.16.2",
13
13
  "@orpc/tanstack-query": "1.16.2",
14
14
  "@tabler/icons-react": "3.34.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-erxes",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "private": false,
5
5
  "description": "Create an erxes plugin for the new core, or migrate an old erxes plugin onto it: pnpm create erxes",
6
6
  "license": "MIT",