|
| 1 | +import type { TypeInfo } from '@getlang/ast' |
| 2 | +import { Type } from '@getlang/ast' |
| 3 | +import type { Hooks, Inputs } from '@getlang/lib' |
| 4 | +import { cookies, html, invariant, js, json } from '@getlang/lib' |
| 5 | +import { ImportError, ValueReferenceError } from '@getlang/lib/errors' |
| 6 | +import { partition } from 'lodash-es' |
| 7 | +import type { Entry, Registry } from './registry.js' |
| 8 | +import type { RuntimeValue } from './value.js' |
| 9 | +import { materialize } from './value.js' |
| 10 | + |
| 11 | +export async function callModifier( |
| 12 | + registry: Registry, |
| 13 | + mod: string, |
| 14 | + args: Record<string, unknown>, |
| 15 | + context?: RuntimeValue, |
| 16 | +) { |
| 17 | + const entry = await registry.importMod(mod) |
| 18 | + if (entry) { |
| 19 | + return entry.mod(context?.data, args) |
| 20 | + } |
| 21 | + |
| 22 | + invariant(context, 'Modifier requires context') |
| 23 | + |
| 24 | + if (mod === 'link') { |
| 25 | + invariant(typeof args.base === 'string', '@link requires base url') |
| 26 | + const data = html.findLink(context.data) |
| 27 | + const link = materialize({ data, typeInfo: context.typeInfo }) |
| 28 | + return new URL(link, args.base).toString() |
| 29 | + } |
| 30 | + |
| 31 | + const doc = materialize(context) |
| 32 | + |
| 33 | + switch (mod) { |
| 34 | + case 'html': |
| 35 | + return html.parse(doc) |
| 36 | + case 'js': |
| 37 | + return js.parse(doc) |
| 38 | + case 'json': |
| 39 | + return json.parse(doc) |
| 40 | + case 'cookies': |
| 41 | + return cookies.parse(doc) |
| 42 | + default: |
| 43 | + throw new ValueReferenceError(`Unsupported modifier: ${mod}`) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export type Execute = (entry: Entry, inputs: Inputs) => Promise<any> |
| 48 | + |
| 49 | +export async function callModule( |
| 50 | + registry: Registry, |
| 51 | + execute: Execute, |
| 52 | + hooks: Required<Hooks>, |
| 53 | + module: string, |
| 54 | + args: RuntimeValue, |
| 55 | + contextType?: TypeInfo, |
| 56 | +) { |
| 57 | + let entry: Entry |
| 58 | + try { |
| 59 | + entry = await registry.import(module, [], contextType) |
| 60 | + } catch (e) { |
| 61 | + const err = `Failed to import module: ${module}` |
| 62 | + throw new ImportError(err, { cause: e }) |
| 63 | + } |
| 64 | + const [inputArgs, attrArgs] = partition(Object.entries(args.data), e => |
| 65 | + entry.inputs.has(e[0]), |
| 66 | + ) |
| 67 | + const inputs = Object.fromEntries(inputArgs) |
| 68 | + let extracted = await hooks.call(module, inputs) |
| 69 | + if (typeof extracted === 'undefined') { |
| 70 | + extracted = await execute(entry, inputs) |
| 71 | + } |
| 72 | + await hooks.extract(module, inputs, extracted.data) |
| 73 | + |
| 74 | + function dropWarning(reason: string) { |
| 75 | + if (attrArgs.length) { |
| 76 | + const dropped = attrArgs.map(e => e[0]).join(', ') |
| 77 | + const err = [ |
| 78 | + `Module '${module}' ${reason}`, |
| 79 | + `dropping view attributes: ${dropped}`, |
| 80 | + ].join(', ') |
| 81 | + console.warn(err) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (entry.returnType.type !== Type.Value) { |
| 86 | + dropWarning('returned unmaterialized value') |
| 87 | + return extracted |
| 88 | + } |
| 89 | + |
| 90 | + if (typeof extracted !== 'object') { |
| 91 | + dropWarning('returned a primitive') |
| 92 | + return extracted |
| 93 | + } |
| 94 | + |
| 95 | + const data = Object.fromEntries(attrArgs) |
| 96 | + const raster = materialize({ data, typeInfo: args.typeInfo }) |
| 97 | + return { ...raster, ...extracted } |
| 98 | +} |
0 commit comments