@lowdefy/build
Version:
84 lines (80 loc) • 3.1 kB
JavaScript
/*
Copyright 2020-2026 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import { type } from '@lowdefy/helpers';
import { ConfigError } from '@lowdefy/errors';
import validateId from '../../../utils/validateId.js';
function buildRequest(request, pageContext) {
const { auth, checkDuplicateRequestId, context, pageId, typeCounters } = pageContext;
const configKey = request['~k'];
if (type.isUndefined(request.id)) {
throw new ConfigError(`Request id missing at page "${pageId}".`, {
configKey
});
}
if (!type.isString(request.id)) {
throw new ConfigError(`Request id is not a string at page "${pageId}".`, {
received: request.id,
configKey
});
}
checkDuplicateRequestId({
id: request.id,
configKey,
pageId
});
validateId({
id: request.id,
field: 'Request id',
location: `page "${pageId}"`,
configKey
});
if (!type.isString(request.type)) {
throw new ConfigError(`Request type is not a string at request "${request.id}" at page "${pageId}".`, {
received: request.type,
configKey
});
}
typeCounters.requests.increment(request.type, configKey);
// Validate connectionId references an existing connection
if (!type.isNone(request.connectionId)) {
if (!type.isString(request.connectionId)) {
throw new ConfigError(`Request "${request.id}" at page "${pageId}" connectionId is not a string.`, {
received: request.connectionId,
configKey
});
}
if (!context.connectionIds.has(request.connectionId)) {
throw new ConfigError(`Request "${request.id}" at page "${pageId}" references non-existent connection "${request.connectionId}".`, {
configKey,
checkSlug: 'connection-refs'
});
}
}
if (type.isUndefined(request.payload)) request.payload = {};
if (!type.isObject(request.payload)) {
throw new ConfigError(`Request "${request.id}" at page "${pageId}" payload should be an object.`, {
configKey
});
}
request.auth = auth;
request.requestId = request.id;
request.pageId = pageId;
request.id = `request:${pageId}:${request.id}`;
pageContext.requests.push(request);
}
function buildRequests(block, pageContext) {
(block.requests || []).forEach((request)=>{
buildRequest(request, pageContext);
});
delete block.requests;
}
export default buildRequests;