@motiadev/core 0.1.0-beta.3 → 0.1.0-beta.6
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.
|
@@ -8,13 +8,16 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
const flowsConfigEndpoint = (app, baseDir) => {
|
|
10
10
|
const configPath = path_1.default.join(baseDir, 'motia-workbench.json');
|
|
11
|
+
const getConfig = () => {
|
|
12
|
+
if (fs_1.default.existsSync(configPath)) {
|
|
13
|
+
return JSON.parse(fs_1.default.readFileSync(configPath, 'utf8'));
|
|
14
|
+
}
|
|
15
|
+
return {};
|
|
16
|
+
};
|
|
11
17
|
app.post('/flows/:id/config', (req, res) => {
|
|
12
18
|
const newFlowConfig = req.body;
|
|
13
19
|
try {
|
|
14
|
-
|
|
15
|
-
fs_1.default.writeFileSync(configPath, JSON.stringify({}, null, 2));
|
|
16
|
-
}
|
|
17
|
-
const existingConfig = JSON.parse(fs_1.default.readFileSync(configPath, 'utf8'));
|
|
20
|
+
const existingConfig = getConfig();
|
|
18
21
|
const updatedConfig = {
|
|
19
22
|
...existingConfig,
|
|
20
23
|
};
|
|
@@ -28,21 +31,20 @@ const flowsConfigEndpoint = (app, baseDir) => {
|
|
|
28
31
|
res.status(200).send({ message: 'Flow config saved successfully' });
|
|
29
32
|
}
|
|
30
33
|
catch (error) {
|
|
31
|
-
console.error('Error saving flow config:', error);
|
|
34
|
+
console.error('Error saving flow config:', error.message);
|
|
32
35
|
res.status(500).json({ error: 'Failed to save flow config' });
|
|
33
36
|
}
|
|
34
37
|
});
|
|
35
38
|
app.get('/flows/:id/config', (req, res) => {
|
|
36
39
|
const { id } = req.params;
|
|
37
40
|
try {
|
|
38
|
-
const
|
|
39
|
-
const allFlowsConfig = JSON.parse(file);
|
|
41
|
+
const allFlowsConfig = getConfig();
|
|
40
42
|
const flowConfig = allFlowsConfig[id] || {};
|
|
41
43
|
res.status(200).send(flowConfig);
|
|
42
44
|
}
|
|
43
45
|
catch (error) {
|
|
44
|
-
console.error('Error reading flow config:', error);
|
|
45
|
-
res.status(400).send({});
|
|
46
|
+
console.error('Error reading flow config:', error.message);
|
|
47
|
+
res.status(400).send({ error: 'Failed to read flow config' });
|
|
46
48
|
}
|
|
47
49
|
});
|
|
48
50
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (...middlewares) => {
|
|
4
|
+
return async (req, ctx, handler) => {
|
|
5
|
+
const composedHandler = middlewares.reduceRight((nextHandler, middleware) => () => middleware(req, ctx, nextHandler), handler);
|
|
6
|
+
return composedHandler();
|
|
7
|
+
};
|
|
8
|
+
};
|
package/dist/src/server.js
CHANGED
|
@@ -17,6 +17,7 @@ const call_step_file_1 = require("./call-step-file");
|
|
|
17
17
|
const LoggerFactory_1 = require("./LoggerFactory");
|
|
18
18
|
const generate_trace_id_1 = require("./generate-trace-id");
|
|
19
19
|
const flows_config_endpoint_1 = require("./flows-config-endpoint");
|
|
20
|
+
const middleware_composer_1 = __importDefault(require("./middleware-composer"));
|
|
20
21
|
const createServer = async (lockedData, eventManager, state, config) => {
|
|
21
22
|
const printer = lockedData.printer;
|
|
22
23
|
const app = (0, express_1.default)();
|
|
@@ -37,22 +38,45 @@ const createServer = async (lockedData, eventManager, state, config) => {
|
|
|
37
38
|
pathParams: req.params,
|
|
38
39
|
queryParams: req.query,
|
|
39
40
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
41
|
+
const ctx = {
|
|
42
|
+
emit: async (event) => {
|
|
43
|
+
await eventManager.emit({
|
|
44
|
+
topic: event.topic,
|
|
45
|
+
data: event.data,
|
|
46
|
+
traceId,
|
|
47
|
+
logger,
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
traceId,
|
|
51
|
+
state,
|
|
52
|
+
logger,
|
|
53
|
+
};
|
|
54
|
+
const finalHandler = async () => {
|
|
55
|
+
try {
|
|
56
|
+
const result = await (0, call_step_file_1.callStepFile)({
|
|
57
|
+
contextInFirstArg: false,
|
|
58
|
+
data: request,
|
|
59
|
+
step,
|
|
60
|
+
printer,
|
|
61
|
+
logger,
|
|
62
|
+
eventManager,
|
|
63
|
+
state,
|
|
64
|
+
traceId,
|
|
65
|
+
});
|
|
66
|
+
if (!result) {
|
|
67
|
+
return { status: 500, body: { error: 'Internal server error' } };
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
logger.error('[API] Internal server error', { error });
|
|
73
|
+
console.log(error);
|
|
74
|
+
return { status: 500, body: { error: 'Internal server error' } };
|
|
55
75
|
}
|
|
76
|
+
};
|
|
77
|
+
try {
|
|
78
|
+
const middleware = step.config.middleware || [];
|
|
79
|
+
const result = await (0, middleware_composer_1.default)(...middleware)(request, ctx, finalHandler);
|
|
56
80
|
if (result.headers) {
|
|
57
81
|
Object.entries(result.headers).forEach(([key, value]) => res.setHeader(key, value));
|
|
58
82
|
}
|
|
@@ -60,8 +84,7 @@ const createServer = async (lockedData, eventManager, state, config) => {
|
|
|
60
84
|
res.json(result.body);
|
|
61
85
|
}
|
|
62
86
|
catch (error) {
|
|
63
|
-
logger.error('[API]
|
|
64
|
-
console.log(error);
|
|
87
|
+
logger.error('[API] Error in middleware chain', { error });
|
|
65
88
|
res.status(500).json({ error: 'Internal server error' });
|
|
66
89
|
}
|
|
67
90
|
};
|
|
@@ -72,15 +95,15 @@ const createServer = async (lockedData, eventManager, state, config) => {
|
|
|
72
95
|
const addRoute = (step) => {
|
|
73
96
|
const { method, path } = step.config;
|
|
74
97
|
logger_1.globalLogger.debug('[API] Registering route', step.config);
|
|
75
|
-
const
|
|
98
|
+
const expressHandler = asyncHandler(step);
|
|
76
99
|
const methods = {
|
|
77
|
-
GET: () => router.get(path,
|
|
78
|
-
POST: () => router.post(path,
|
|
79
|
-
PUT: () => router.put(path,
|
|
80
|
-
DELETE: () => router.delete(path,
|
|
81
|
-
PATCH: () => router.patch(path,
|
|
82
|
-
OPTIONS: () => router.options(path,
|
|
83
|
-
HEAD: () => router.head(path,
|
|
100
|
+
GET: () => router.get(path, expressHandler),
|
|
101
|
+
POST: () => router.post(path, expressHandler),
|
|
102
|
+
PUT: () => router.put(path, expressHandler),
|
|
103
|
+
DELETE: () => router.delete(path, expressHandler),
|
|
104
|
+
PATCH: () => router.patch(path, expressHandler),
|
|
105
|
+
OPTIONS: () => router.options(path, expressHandler),
|
|
106
|
+
HEAD: () => router.head(path, expressHandler),
|
|
84
107
|
};
|
|
85
108
|
const methodHandler = methods[method];
|
|
86
109
|
if (!methodHandler) {
|
package/dist/src/types.d.ts
CHANGED
|
@@ -47,6 +47,7 @@ export type NoopConfig = {
|
|
|
47
47
|
flows?: string[];
|
|
48
48
|
};
|
|
49
49
|
export type ApiRouteMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
|
|
50
|
+
export type ApiMiddleware = (req: ApiRequest, ctx: FlowContext, next: () => Promise<ApiResponse>) => Promise<ApiResponse>;
|
|
50
51
|
export type ApiRouteConfig = {
|
|
51
52
|
type: 'api';
|
|
52
53
|
name: string;
|
|
@@ -57,6 +58,7 @@ export type ApiRouteConfig = {
|
|
|
57
58
|
virtualEmits?: Emit[];
|
|
58
59
|
virtualSubscribes?: string[];
|
|
59
60
|
flows?: string[];
|
|
61
|
+
middleware?: ApiMiddleware[];
|
|
60
62
|
bodySchema?: ZodObject<any>;
|
|
61
63
|
/**
|
|
62
64
|
* Files to include in the step bundle.
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@motiadev/core",
|
|
3
3
|
"description": "Core functionality for the Motia framework, providing the foundation for building event-driven workflows.",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
|
-
"version": "0.1.0-beta.
|
|
5
|
+
"version": "0.1.0-beta.6",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"body-parser": "^1.20.3",
|
|
8
8
|
"colors": "^1.4.0",
|