@athenox/talos
Version:
Talos: a backend you describe instead of write. Declare a schema, write the rules in plain language, map URLs to sentences; a small built-in model turns them into query plans.
53 lines (52 loc) • 2.49 kB
TypeScript
/**
* Moving rows in and out: CSV import and export, and JSON APIs read into rows.
* (Mail stays the app's: its transport, templates and sending are not Talos's.)
*/
/** CSV text to rows keyed by header; `,`, `;` or tab (whichever the header uses most), quotes and "" escapes. */
export declare function parseCsv(text: string): Record<string, string>[];
/** Rows to CSV, with the columns given (header labels optional). */
export declare function toCsv(rows: Record<string, unknown>[], columns: string[] | Record<string, string>, sep?: string): string;
/**
* Import CSV rows into a table through a write function (a sentence route's
* runner, or anything taking values): headers mapped to columns (several
* spellings each), a key column deciding what is already there, and a report.
*
* await importCsv(text, { map: { email: ['email', 'e-mail', 'courriel'], name: ['nom', 'name'] },
* key: 'email', exists: email => …, write: values => … })
*/
export declare function importCsv(text: string, o: {
map: Record<string, string[] | string>;
key?: string;
/** whether a row with that key is already there (then it is skipped, or updated with `update`) */
exists?: (key: string) => Promise<boolean> | boolean;
write: (values: Record<string, string>) => Promise<unknown> | unknown;
update?: (values: Record<string, string>) => Promise<unknown> | unknown;
/** a row to leave out, with the reason */
check?: (values: Record<string, string>) => string | null | undefined;
}): Promise<{
total: number;
inserted: number;
updated: number;
skipped: number;
errors: {
line: number;
reason: string;
}[];
}>;
/**
* A JSON API read into a row: `{placeholders}` in the URL, dotted paths or
* functions for each field, a timeout, and a small cache. Returns null when
* the API has nothing (404, or `found` says so).
*
* const product = connector({ url: 'https://world.openfoodfacts.org/api/v2/product/{code}.json',
* found: j => j.status === 1, map: { name: 'product.product_name', brand: 'product.brands' } })
* await product({ code: '3017620422003' })
*/
export declare function connector<T extends Record<string, unknown>>(o: {
url: string;
map: Record<keyof T & string, string | ((json: any) => unknown)>;
found?: (json: any) => boolean;
headers?: Record<string, string>;
timeoutMs?: number;
cacheMs?: number;
}): (params: Record<string, unknown>) => Promise<T | null>;