@lowdefy/api 5.6.0 → 6.1.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/dist/context/createAuthorize.js +4 -1
- package/dist/context/createEvaluateOperators.js +3 -1
- package/dist/context/getCurrentEnvironment.js +27 -0
- package/dist/context/resolveStrategyCaller.js +56 -0
- package/dist/index.js +13 -2
- package/dist/response/buildEndpointResult.js +3 -1
- package/dist/response/createWireProjection.js +88 -0
- package/dist/response/normalizeErrorSources.js +10 -13
- package/dist/response/redactErrorResponse.js +42 -8
- package/dist/response/redactResponse.js +10 -10
- package/dist/routes/agent/callAgent.js +7 -184
- package/dist/routes/agent/prepareAgent.js +179 -0
- package/dist/routes/auth/createLogger.js +2 -5
- package/dist/routes/auth/createPrefixedCookies.js +52 -0
- package/dist/routes/auth/defaultAuthUrl.js +31 -0
- package/dist/routes/auth/{getNextAuthConfig.js → getAuthConfig.js} +31 -16
- package/dist/routes/auth/resolveCookies.js +37 -0
- package/dist/routes/auth/strategies/createAuthStrategies.js +77 -0
- package/dist/routes/auth/strategies/getAuthStrategies.js +31 -0
- package/dist/routes/endpoints/authorizeApiEndpoint.js +8 -2
- package/dist/routes/endpoints/callEndpoint.js +27 -3
- package/dist/routes/endpoints/control/controlFor.js +4 -7
- package/dist/routes/endpoints/control/controlIf.js +4 -8
- package/dist/routes/endpoints/control/controlLog.js +6 -14
- package/dist/routes/endpoints/control/controlParallelFor.js +4 -7
- package/dist/routes/endpoints/control/controlReject.js +16 -16
- package/dist/routes/endpoints/control/controlReturn.js +4 -9
- package/dist/routes/endpoints/control/controlSetState.js +4 -8
- package/dist/routes/endpoints/control/controlSwitch.js +4 -8
- package/dist/routes/endpoints/control/controlThrow.js +21 -20
- package/dist/routes/endpoints/control/controlTry.js +20 -10
- package/dist/routes/endpoints/evaluateRoutineOperators.js +26 -0
- package/dist/routes/endpoints/findSchedule.js +35 -0
- package/dist/routes/endpoints/forwardScheduledEndpoint.js +111 -0
- package/dist/routes/endpoints/getEndpointConfig.js +9 -3
- package/dist/routes/endpoints/getEnvironmentSchedules.js +27 -0
- package/dist/routes/endpoints/handleAgentCall.js +80 -0
- package/dist/routes/endpoints/handleEndpointCall.js +53 -8
- package/dist/routes/endpoints/handleRenderNotification.js +196 -0
- package/dist/routes/endpoints/handleRequest.js +1 -5
- package/dist/routes/endpoints/handleValidateSchema.js +14 -10
- package/dist/routes/endpoints/invokeEndpoint.js +1 -0
- package/dist/routes/endpoints/isUnauthenticatedHuman.js +31 -0
- package/dist/routes/endpoints/operatorScope.js +27 -0
- package/dist/routes/endpoints/resolveCronEnvironment.js +38 -0
- package/dist/routes/endpoints/runDetachedEndpoint.js +67 -0
- package/dist/routes/endpoints/runRoutine.js +12 -0
- package/dist/routes/endpoints/runScheduledEndpoint.js +105 -0
- package/dist/routes/endpoints/runWebhookEndpoint.js +84 -0
- package/dist/routes/endpoints/scheduleBackground.js +48 -0
- package/dist/routes/endpoints/test/runTest.js +17 -2
- package/dist/routes/mcp/createMcpServer.js +178 -0
- package/dist/routes/notifications/derivePreview.js +30 -0
- package/dist/routes/notifications/getNotificationConfig.js +32 -0
- package/dist/routes/notifications/resolveNotificationLinks.js +72 -0
- package/dist/routes/notifications/resolveThemeLogo.js +35 -0
- package/dist/routes/page/dynamic/resolveDynamicContent.js +172 -0
- package/dist/routes/page/dynamic/unescapeOperators.js +36 -0
- package/dist/routes/page/dynamic/validateFragment.js +104 -0
- package/dist/routes/page/getPageConfig.js +16 -6
- package/dist/routes/request/callRequest.js +9 -3
- package/dist/routes/request/callRequestResolver.js +6 -0
- package/dist/routes/request/evaluateOperators.js +8 -12
- package/dist/routes/websocket/authorizeWebsocket.js +27 -0
- package/dist/routes/websocket/createChannelRegistry.js +283 -0
- package/dist/routes/websocket/createWebSocketConnection.js +143 -0
- package/dist/routes/websocket/getWebsocketConfig.js +30 -0
- package/dist/routes/websocket/getWebsocketResolver.js +33 -0
- package/dist/routes/websocket/prepareChannel.js +72 -0
- package/dist/test/testContext.js +7 -3
- package/package.json +12 -10
- package/dist/response/omitErrorProps.js +0 -51
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { ConfigError } from '@lowdefy/errors';
|
|
16
|
+
import buildEndpointResult from '../../response/buildEndpointResult.js';
|
|
17
|
+
import createAuthorize from '../../context/createAuthorize.js';
|
|
18
|
+
import createEvaluateOperators from '../../context/createEvaluateOperators.js';
|
|
19
|
+
import getEndpointConfig from './getEndpointConfig.js';
|
|
20
|
+
import runRoutine from './runRoutine.js';
|
|
21
|
+
// Runs an endpoint declared `webhook: true` — a third-party webhook receiver
|
|
22
|
+
// (SNS, Event Grid, Stripe, ...) served on the standard /api/endpoints route,
|
|
23
|
+
// but taking the request RAW: bodies are the caller's own format, not
|
|
24
|
+
// Lowdefy's { payload } envelope, so the routine receives
|
|
25
|
+
// { body, query, headers } as its payload and its return value is sent back
|
|
26
|
+
// verbatim (handshakes require exact response shapes). Only endpoints that
|
|
27
|
+
// opt in are runnable here (a missing flag reads as a missing endpoint — no
|
|
28
|
+
// probing). The transport is public by design: authenticating the caller
|
|
29
|
+
// (shared-secret query param, signature header) is the webhook routine's own
|
|
30
|
+
// first step. Executes as a system context.
|
|
31
|
+
async function runWebhookEndpoint(context, { endpointId, body, query, headers }) {
|
|
32
|
+
const { logger } = context;
|
|
33
|
+
context.endpointId = endpointId;
|
|
34
|
+
context.evaluateOperators = createEvaluateOperators(context);
|
|
35
|
+
logger.debug({
|
|
36
|
+
event: 'debug_webhook_endpoint',
|
|
37
|
+
endpointId
|
|
38
|
+
});
|
|
39
|
+
const endpointConfig = await getEndpointConfig(context, {
|
|
40
|
+
endpointId
|
|
41
|
+
});
|
|
42
|
+
if (endpointConfig.webhook !== true) {
|
|
43
|
+
const err = new ConfigError(`API Endpoint "${endpointId}" does not exist.`);
|
|
44
|
+
logger.debug({
|
|
45
|
+
params: {
|
|
46
|
+
endpointId
|
|
47
|
+
},
|
|
48
|
+
err
|
|
49
|
+
}, err.message);
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
// Force a system context regardless of any session cookie sent with the request.
|
|
53
|
+
// system: true — nested CallApi steps are authorized like function calls (the run
|
|
54
|
+
// was already authorized at the transport layer), not re-gated on a user session.
|
|
55
|
+
context.session = undefined;
|
|
56
|
+
context.user = undefined;
|
|
57
|
+
context.system = true;
|
|
58
|
+
context.authorize = createAuthorize({
|
|
59
|
+
session: undefined,
|
|
60
|
+
system: true
|
|
61
|
+
});
|
|
62
|
+
const routineContext = {
|
|
63
|
+
steps: {},
|
|
64
|
+
payload: {
|
|
65
|
+
body: body ?? null,
|
|
66
|
+
query: query ?? {},
|
|
67
|
+
headers: headers ?? {}
|
|
68
|
+
},
|
|
69
|
+
arrayIndices: [],
|
|
70
|
+
error: null,
|
|
71
|
+
items: {},
|
|
72
|
+
state: {},
|
|
73
|
+
endpointDepth: 0
|
|
74
|
+
};
|
|
75
|
+
const { error, response, status } = await runRoutine(context, routineContext, {
|
|
76
|
+
routine: endpointConfig.routine
|
|
77
|
+
});
|
|
78
|
+
return buildEndpointResult(context, {
|
|
79
|
+
error,
|
|
80
|
+
response,
|
|
81
|
+
status
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
export default runWebhookEndpoint;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ // Run work after the response is sent. Platforms that reap the invocation once
|
|
16
|
+
// the response is flushed (Vercel fluid compute) inject context.waitUntil in
|
|
17
|
+
// their server's apiContext middleware to keep the invocation alive until the
|
|
18
|
+
// promise settles (bounded by the function's maxDuration); locally /
|
|
19
|
+
// self-hosted there is no waitUntil and the promise simply runs detached on
|
|
20
|
+
// the still-alive Node process.
|
|
21
|
+
//
|
|
22
|
+
// The outcome only exists in logs — completion and failure are logged through
|
|
23
|
+
// the request logger so they reach the platform log stream (and any log
|
|
24
|
+
// drains); a background failure must never surface as an unhandled rejection.
|
|
25
|
+
function scheduleBackground(context, { event, endpointId }, fn) {
|
|
26
|
+
const { logger } = context;
|
|
27
|
+
const promise = (async ()=>{
|
|
28
|
+
try {
|
|
29
|
+
const result = await fn();
|
|
30
|
+
logger.info({
|
|
31
|
+
event: `${event}_done`,
|
|
32
|
+
endpointId,
|
|
33
|
+
status: result?.status
|
|
34
|
+
});
|
|
35
|
+
} catch (err) {
|
|
36
|
+
logger.error({
|
|
37
|
+
event: `${event}_failed`,
|
|
38
|
+
endpointId,
|
|
39
|
+
err
|
|
40
|
+
}, err.message);
|
|
41
|
+
}
|
|
42
|
+
})();
|
|
43
|
+
if (context.waitUntil) {
|
|
44
|
+
context.waitUntil(promise);
|
|
45
|
+
}
|
|
46
|
+
return promise;
|
|
47
|
+
}
|
|
48
|
+
export default scheduleBackground;
|
|
@@ -1,4 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { jest } from '@jest/globals';
|
|
2
16
|
import { wait } from '@lowdefy/helpers';
|
|
3
17
|
import { operatorsServer } from '@lowdefy/operators-js';
|
|
4
18
|
import createEvaluateOperators from '../../../context/createEvaluateOperators.js';
|
|
@@ -7,7 +21,7 @@ import testContext from '../../../test/testContext.js';
|
|
|
7
21
|
// const { _date, _eq, _payload, _secret, _user } = operatorsServer;
|
|
8
22
|
const operators = {
|
|
9
23
|
...operatorsServer,
|
|
10
|
-
|
|
24
|
+
_throw_test: ()=>{
|
|
11
25
|
throw new Error('Test error.');
|
|
12
26
|
}
|
|
13
27
|
};
|
|
@@ -89,6 +103,7 @@ async function runTest({ routine, payload = {} }) {
|
|
|
89
103
|
const routineContext = {
|
|
90
104
|
steps: {},
|
|
91
105
|
payload,
|
|
106
|
+
error: null,
|
|
92
107
|
items: {},
|
|
93
108
|
arrayIndices: [],
|
|
94
109
|
state: {},
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
16
|
+
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
17
|
+
import { AuthenticationError } from '@lowdefy/errors';
|
|
18
|
+
import { serializer, type } from '@lowdefy/helpers';
|
|
19
|
+
import callEndpoint from '../endpoints/callEndpoint.js';
|
|
20
|
+
import createWireProjection from '../../response/createWireProjection.js';
|
|
21
|
+
import isUnauthenticatedHuman from '../endpoints/isUnauthenticatedHuman.js';
|
|
22
|
+
// LLM-safe tool names use the same rule as buildAgents tool naming.
|
|
23
|
+
function toToolName(id) {
|
|
24
|
+
return id.replaceAll('/', '__');
|
|
25
|
+
}
|
|
26
|
+
// Twin of cleanBuildArtifact in packages/utils/ai-utils/src/buildAgentTools.js -
|
|
27
|
+
// duplicated here rather than shared, since pulling in @lowdefy/ai-utils would
|
|
28
|
+
// add the ai SDK and MCP client deps to api just for this. Strips build-artifact
|
|
29
|
+
// serializer markers (~k, ~r, ~l) and unwraps { '~arr': [...] } back to a plain
|
|
30
|
+
// array, so payloadSchema reaches MCP clients as plain JSON Schema.
|
|
31
|
+
function cleanBuildArtifact(obj) {
|
|
32
|
+
return JSON.parse(JSON.stringify(serializer.deserialize(obj)));
|
|
33
|
+
}
|
|
34
|
+
// A stateless per-request MCP server exposing the configured api endpoints
|
|
35
|
+
// as tools. Built with the SDK's low-level Server: tool input schemas are
|
|
36
|
+
// config-provided JSON Schema (payloadSchema), which the low-level handlers
|
|
37
|
+
// accept directly - no zod conversion. The server is constructed with the
|
|
38
|
+
// request's context, so the caller is known at construction time: tools/list
|
|
39
|
+
// filters by context.authorize, and tools/call re-authorizes inside
|
|
40
|
+
// callEndpoint (defense in depth). Returns null when no mcp block is
|
|
41
|
+
// configured.
|
|
42
|
+
async function createMcpServer({ context }) {
|
|
43
|
+
const mcpConfig = await context.readConfigFile('mcp.json');
|
|
44
|
+
if (type.isNone(mcpConfig) || mcpConfig.configured !== true) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
// serverInfo doubles as the connector card in clients such as claude.ai:
|
|
48
|
+
// title, websiteUrl and icons are optional branding the app may configure,
|
|
49
|
+
// and are omitted (not sent as undefined) when it does not. icons is config
|
|
50
|
+
// structure, so like payloadSchema it carries build-artifact markers that
|
|
51
|
+
// must not reach the client.
|
|
52
|
+
const serverInfo = {
|
|
53
|
+
name: mcpConfig.name,
|
|
54
|
+
version: mcpConfig.version
|
|
55
|
+
};
|
|
56
|
+
for (const key of [
|
|
57
|
+
'title',
|
|
58
|
+
'websiteUrl'
|
|
59
|
+
]){
|
|
60
|
+
if (!type.isNone(mcpConfig[key])) {
|
|
61
|
+
serverInfo[key] = mcpConfig[key];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!type.isNone(mcpConfig.icons)) {
|
|
65
|
+
serverInfo.icons = cleanBuildArtifact(mcpConfig.icons);
|
|
66
|
+
}
|
|
67
|
+
const server = new Server(serverInfo, {
|
|
68
|
+
capabilities: {
|
|
69
|
+
tools: {}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
server.setRequestHandler(ListToolsRequestSchema, async ()=>{
|
|
73
|
+
const tools = [];
|
|
74
|
+
for (const endpointId of mcpConfig.endpoints){
|
|
75
|
+
const endpointConfig = await context.readConfigFile(`api/${endpointId}.json`);
|
|
76
|
+
if (type.isNone(endpointConfig) || !context.authorize(endpointConfig)) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
tools.push({
|
|
80
|
+
name: toToolName(endpointId),
|
|
81
|
+
description: endpointConfig.description,
|
|
82
|
+
inputSchema: cleanBuildArtifact(endpointConfig.payloadSchema)
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
tools
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
server.setRequestHandler(CallToolRequestSchema, async (request)=>{
|
|
90
|
+
const { name, arguments: args } = request.params;
|
|
91
|
+
context.logger.info({
|
|
92
|
+
event: 'mcp_tool_call',
|
|
93
|
+
tool: name
|
|
94
|
+
});
|
|
95
|
+
const endpointId = mcpConfig.endpoints.find((id)=>toToolName(id) === name);
|
|
96
|
+
try {
|
|
97
|
+
if (!type.isNone(endpointId)) {
|
|
98
|
+
const { error, response, success } = await callEndpoint(context, {
|
|
99
|
+
blockId: '_mcp',
|
|
100
|
+
endpointId,
|
|
101
|
+
pageId: '_mcp',
|
|
102
|
+
payload: args ?? {}
|
|
103
|
+
});
|
|
104
|
+
if (!success) {
|
|
105
|
+
const deserialized = serializer.deserialize(error);
|
|
106
|
+
let text = deserialized?.message ?? 'Endpoint failed.';
|
|
107
|
+
// The dev MCP route serves a coding agent, which is a dev tool: it gets the real
|
|
108
|
+
// message and the config location so it can go straight to the YAML. A prod MCP
|
|
109
|
+
// client is an end-user reader and keeps the wire message.
|
|
110
|
+
if (context.mode === 'dev' && !type.isNone(error?.devError)) {
|
|
111
|
+
const devError = serializer.deserialize(error.devError);
|
|
112
|
+
text = devError.message;
|
|
113
|
+
if (!type.isNone(devError.source)) {
|
|
114
|
+
text = `${text} (at ${devError.source})`;
|
|
115
|
+
}
|
|
116
|
+
if (!type.isNone(devError.hint)) {
|
|
117
|
+
text = `${text} Hint: ${devError.hint}`;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
content: [
|
|
122
|
+
{
|
|
123
|
+
type: 'text',
|
|
124
|
+
text
|
|
125
|
+
}
|
|
126
|
+
],
|
|
127
|
+
isError: true
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
type: 'text',
|
|
134
|
+
text: JSON.stringify(serializer.deserialize(response))
|
|
135
|
+
}
|
|
136
|
+
]
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
// An unknown tool answers like a gated one for an anonymous caller on an
|
|
140
|
+
// auth'd app, so tools/call cannot be used to enumerate tool names that
|
|
141
|
+
// tools/list already hides from that caller.
|
|
142
|
+
if (await isUnauthenticatedHuman(context)) {
|
|
143
|
+
throw new AuthenticationError(`Authentication required for API endpoint "${name}".`);
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
content: [
|
|
147
|
+
{
|
|
148
|
+
type: 'text',
|
|
149
|
+
text: `Unknown tool "${name}".`
|
|
150
|
+
}
|
|
151
|
+
],
|
|
152
|
+
isError: true
|
|
153
|
+
};
|
|
154
|
+
} catch (error) {
|
|
155
|
+
// Unauthenticated calls to gated tools are expected probing traffic -
|
|
156
|
+
// a warn line and the 401-shaped message, not a structured error log.
|
|
157
|
+
if (error.name === 'AuthenticationError') {
|
|
158
|
+
context.logger.warn(`Unauthenticated MCP tool call: ${name}`);
|
|
159
|
+
} else {
|
|
160
|
+
context.logger.error(error);
|
|
161
|
+
}
|
|
162
|
+
// This error never went through an endpoint result, so it has not been projected yet.
|
|
163
|
+
// The projection keeps an AuthenticationError's message.
|
|
164
|
+
const text = context.mode === 'dev' ? error.message : createWireProjection(context)(error).message;
|
|
165
|
+
return {
|
|
166
|
+
content: [
|
|
167
|
+
{
|
|
168
|
+
type: 'text',
|
|
169
|
+
text
|
|
170
|
+
}
|
|
171
|
+
],
|
|
172
|
+
isError: true
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
return server;
|
|
177
|
+
}
|
|
178
|
+
export default createMcpServer;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
// Rough inversion of the interpolation pipeline for the preview text: strip
|
|
17
|
+
// author markdown syntax and unescape the backslash-escaped interpolated values.
|
|
18
|
+
function stripMarkdown(text) {
|
|
19
|
+
return text.replace(/```[\s\S]*?```/g, ' ').replace(/`([^`]*)`/g, '$1').replace(/!\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/^#{1,6}\s+/gm, '').replace(/^\s*>\s?/gm, '').replace(/^\s*[-*+]\s+/gm, '').replace(/(\*\*|__|\*|_|~~)/g, '').replace(/\\([!-/:-@[-`{-~])/g, '$1').replace(/\s+/g, ' ').trim();
|
|
20
|
+
}
|
|
21
|
+
function derivePreview({ properties }) {
|
|
22
|
+
if (type.isString(properties.preview) && properties.preview !== '') {
|
|
23
|
+
return properties.preview;
|
|
24
|
+
}
|
|
25
|
+
if (type.isString(properties.message) && properties.message !== '') {
|
|
26
|
+
return stripMarkdown(properties.message).slice(0, 140);
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
export default derivePreview;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { ConfigError } from '@lowdefy/errors';
|
|
16
|
+
async function getNotificationConfig({ logger, readConfigFile }, { notificationId, configKey }) {
|
|
17
|
+
const notification = await readConfigFile(`notifications/${notificationId}.json`);
|
|
18
|
+
if (!notification) {
|
|
19
|
+
const err = new ConfigError(`Notification "${notificationId}" does not exist.`, {
|
|
20
|
+
configKey
|
|
21
|
+
});
|
|
22
|
+
logger.debug({
|
|
23
|
+
params: {
|
|
24
|
+
notificationId
|
|
25
|
+
},
|
|
26
|
+
err
|
|
27
|
+
}, err.message);
|
|
28
|
+
throw err;
|
|
29
|
+
}
|
|
30
|
+
return notification;
|
|
31
|
+
}
|
|
32
|
+
export default getNotificationConfig;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { serializer, type, urlQuery } from '@lowdefy/helpers';
|
|
16
|
+
function resolveLinkValue({ link, option, serverUrl, basePath, landingPage, recordId }) {
|
|
17
|
+
// Absolute URLs pass through — links into other apps or external destinations.
|
|
18
|
+
// They skip any landing page, so they carry no mark-as-read.
|
|
19
|
+
if (type.isString(link)) {
|
|
20
|
+
return link;
|
|
21
|
+
}
|
|
22
|
+
if (type.isObject(link) && !type.isNone(link.pageId)) {
|
|
23
|
+
if (type.isNone(landingPage)) {
|
|
24
|
+
// No landing page configured — link straight to the target page.
|
|
25
|
+
const query = urlQuery.stringify(link.urlQuery ?? {});
|
|
26
|
+
return `${serverUrl}${basePath}/${link.pageId}${query ? `?${query}` : ''}`;
|
|
27
|
+
}
|
|
28
|
+
// The option query param is the dot-path of the link inside the record's
|
|
29
|
+
// data — the landing page reads the original target back with
|
|
30
|
+
// get(record.data, option) after marking the record read.
|
|
31
|
+
const query = urlQuery.stringify({
|
|
32
|
+
_id: recordId,
|
|
33
|
+
option
|
|
34
|
+
});
|
|
35
|
+
return `${serverUrl}${basePath}${landingPage}?${query}`;
|
|
36
|
+
}
|
|
37
|
+
return link;
|
|
38
|
+
}
|
|
39
|
+
// Resolves link values in a copy of the data item to URLs; the stored record
|
|
40
|
+
// keeps the original { pageId, urlQuery } objects for in-app navigation.
|
|
41
|
+
// data.links is the framework convention; link fields inside arrays are
|
|
42
|
+
// resolved for the data keys the template declares (Template.dataKeys), so
|
|
43
|
+
// custom templates get the same treatment as the built-in ones.
|
|
44
|
+
function resolveNotificationLinks({ item, dataKeys, serverUrl, basePath, landingPage, recordId }) {
|
|
45
|
+
const resolved = serializer.copy(item);
|
|
46
|
+
Object.keys(resolved.links ?? {}).forEach((key)=>{
|
|
47
|
+
resolved.links[key] = resolveLinkValue({
|
|
48
|
+
link: resolved.links[key],
|
|
49
|
+
option: `links.${key}`,
|
|
50
|
+
serverUrl,
|
|
51
|
+
basePath,
|
|
52
|
+
landingPage,
|
|
53
|
+
recordId
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
(dataKeys ?? []).forEach((arrayKey)=>{
|
|
57
|
+
if (!type.isArray(resolved[arrayKey])) return;
|
|
58
|
+
resolved[arrayKey].forEach((entry, index)=>{
|
|
59
|
+
if (!type.isObject(entry) || type.isNone(entry.link)) return;
|
|
60
|
+
entry.link = resolveLinkValue({
|
|
61
|
+
link: entry.link,
|
|
62
|
+
option: `${arrayKey}.${index}.link`,
|
|
63
|
+
serverUrl,
|
|
64
|
+
basePath,
|
|
65
|
+
landingPage,
|
|
66
|
+
recordId
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
return resolved;
|
|
71
|
+
}
|
|
72
|
+
export default resolveNotificationLinks;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { type } from '@lowdefy/helpers';
|
|
16
|
+
// A theme logo written as an app-relative path ("/logo.png", a public/ asset)
|
|
17
|
+
// resolves against the deployment's serverUrl + basePath, so one config works
|
|
18
|
+
// across environments. Absolute ("https://...") and protocol-relative ("//...")
|
|
19
|
+
// URLs pass through. Without a serverUrl an email client can never fetch a
|
|
20
|
+
// relative path, so the logo is dropped — EmailLayout falls back to the
|
|
21
|
+
// companyName text header, which beats a broken image.
|
|
22
|
+
function resolveThemeLogo({ theme, serverUrl, basePath }) {
|
|
23
|
+
const { logo, ...rest } = theme;
|
|
24
|
+
if (!type.isString(logo) || !logo.startsWith('/') || logo.startsWith('//')) {
|
|
25
|
+
return theme;
|
|
26
|
+
}
|
|
27
|
+
if (type.isNone(serverUrl)) {
|
|
28
|
+
return rest;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
...rest,
|
|
32
|
+
logo: `${serverUrl}${basePath}${logo}`
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export default resolveThemeLogo;
|