← Public packages
@danj/parallel
Parallel web search, page extraction, and deep research for Kody. Research is capped at ultra, and paid calls at a daily budget.
Files
import { array, number, object, parseSafe, string } from '@remix-run/data-schema'
export const text = () => string().refine((s) => s.trim() !== '', 'Expected a non-empty string')
export const texts = () => array(text()).refine((a) => a.length > 0, 'Expected at least one item')
export const positiveInteger = () =>
number().refine((n) => Number.isInteger(n) && n > 0, 'Expected a positive integer')
export const mills = () =>
number().refine((n) => Number.isInteger(n) && n >= 0, 'Expected an integer of at least 0')
/**
* Build a parser for one export's input that rejects unknown keys.
* Errors name the export, the path, and for an unknown key the accepted keys.
*
* @param {string} exportName such as `@danj/parallel/search`
* @param {Record<string, import('@remix-run/data-schema').Schema<any, any>>} shape
* @returns {(value: unknown) => any}
*/
export function strictInput(exportName, shape, nestedKeys = {}) {
const schema = object(shape, { unknownKeys: 'error' })
const accepted = (path) => {
const keys = path.length > 1 ? nestedKeys[path[0]] : Object.keys(shape)
return keys.length ? keys.join(', ') : 'none'
}
const errorMap = ({ code, path }) =>
code === 'object.unknown_key' ? `unknown key. Accepted keys: ${accepted(path)}` : undefined
return (value) => {
const result = parseSafe(schema, value, { errorMap })
if (result.success) return result.value
const problems = result.issues.map(({ path = [], message }) =>
path.length ? `${path.join('.')}: ${message}` : message,
)
throw new Error(`${exportName}: ${problems.join('; ')}`)
}
}