UNPKG

kintone-as-code

Version:

A CLI tool for managing kintone applications as code with type-safe TypeScript schemas

60 lines 2.32 kB
import { toString } from './expression.js'; import { validateExpressionDepth, validateQueryStringLength, } from './validator.js'; export const createQueryState = () => ({}); // Helpers to construct new immutable states const withProp = (state, key, value) => ({ ...state, [key]: value }); // Curried, FP-first operators over QueryState export const where = (expr) => (state) => withProp(state, 'where', expr); export const orderBy = (field, direction = 'asc') => (state) => { const next = [...(state.orderBy ?? []), { field, direction }]; return withProp(state, 'orderBy', next); }; export const limit = (value) => (state) => { if (value < 1) { throw new Error('Limit must be at least 1'); } if (value > 500) { throw new Error('kintone API limit: maximum 500 records per request'); } return withProp(state, 'limit', value); }; export const offset = (value) => (state) => { if (value < 0) { throw new Error('Offset must be non-negative'); } return withProp(state, 'offset', value); }; export const setValidationOptions = (options) => (state) => withProp(state, 'validationOptions', { ...options }); // Build final query string (pure) export const build = (state) => { const parts = []; if (state.where) { parts.push(toString(state.where)); } if (state.orderBy && state.orderBy.length > 0) { const order = state.orderBy .map(({ field, direction }) => `${field} ${direction}`) .join(', '); parts.push(`order by ${order}`); } if (state.limit !== undefined) { parts.push(`limit ${state.limit}`); } if (state.offset !== undefined) { parts.push(`offset ${state.offset}`); } const built = parts.join(' '); // Validation (pure, derived from state) if (state.where) { const depthOpts = state.validationOptions?.maxDepth !== undefined ? { maxDepth: state.validationOptions.maxDepth } : undefined; validateExpressionDepth(state.where, depthOpts); } const lengthOpts = state.validationOptions?.maxLength !== undefined ? { maxLength: state.validationOptions.maxLength } : undefined; validateQueryStringLength(built, lengthOpts); return built; }; //# sourceMappingURL=builder-fp.js.map