Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
566 changes: 566 additions & 0 deletions bun.lock

Large diffs are not rendered by default.

Binary file removed bun.lockb
Binary file not shown.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "get",
"license": "Apache-2.0",
"private": true,
"packageManager": "[email protected].20",
"packageManager": "[email protected].21",
"scripts": {
"fmt": "biome check --write",
"lint": "bun lint:check && bun lint:types && bun lint:unused && bun lint:repo",
Expand All @@ -18,11 +18,11 @@
"test"
],
"devDependencies": {
"@biomejs/biome": "^2.2.0",
"@biomejs/biome": "^2.2.3",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.29.6",
"@types/bun": "^1.2.20",
"knip": "^5.62.0",
"@types/bun": "^1.2.21",
"knip": "^5.63.1",
"sherif": "^1.6.1",
"typescript": "^5.9.2"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/get/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"@getlang/ast": "workspace:^0.0.1",
"@getlang/lib": "workspace:^0.1.5",
"@getlang/parser": "workspace:^0.3.4",
"@getlang/utils": "workspace:^0.1.6",
"@getlang/walker": "workspace:^0.0.1",
"acorn": "^8.15.0",
"estree-toolkit": "^1.7.13",
"lodash-es": "^4.17.21"
},
"devDependencies": {
Expand Down
98 changes: 98 additions & 0 deletions packages/get/src/calls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import type { TypeInfo } from '@getlang/ast'
import { Type } from '@getlang/ast'
import type { Hooks, Inputs } from '@getlang/lib'
import { cookies, html, invariant, js, json } from '@getlang/lib'
import { ImportError, ValueReferenceError } from '@getlang/lib/errors'
import { partition } from 'lodash-es'
import type { Entry, Registry } from './registry.js'
import type { RuntimeValue } from './value.js'
import { materialize } from './value.js'

export async function callModifier(
registry: Registry,
mod: string,
args: Record<string, unknown>,
context?: RuntimeValue,
) {
const entry = await registry.importMod(mod)
if (entry) {
return entry.mod(context?.data, args)
}

invariant(context, 'Modifier requires context')

if (mod === 'link') {
invariant(typeof args.base === 'string', '@link requires base url')
const data = html.findLink(context.data)
const link = materialize({ data, typeInfo: context.typeInfo })
return new URL(link, args.base).toString()
}

const doc = materialize(context)

switch (mod) {
case 'html':
return html.parse(doc)
case 'js':
return js.parse(doc)
case 'json':
return json.parse(doc)
case 'cookies':
return cookies.parse(doc)
default:
throw new ValueReferenceError(`Unsupported modifier: ${mod}`)
}
}

export type Execute = (entry: Entry, inputs: Inputs) => Promise<any>

export async function callModule(
registry: Registry,
execute: Execute,
hooks: Required<Hooks>,
module: string,
args: RuntimeValue,
contextType?: TypeInfo,
) {
let entry: Entry
try {
entry = await registry.import(module, [], contextType)
} catch (e) {
const err = `Failed to import module: ${module}`
throw new ImportError(err, { cause: e })
}
const [inputArgs, attrArgs] = partition(Object.entries(args.data), e =>
entry.inputs.has(e[0]),
)
const inputs = Object.fromEntries(inputArgs)
let extracted = await hooks.call(module, inputs)
if (typeof extracted === 'undefined') {
extracted = await execute(entry, inputs)
}
await hooks.extract(module, inputs, extracted.data)

function dropWarning(reason: string) {
if (attrArgs.length) {
const dropped = attrArgs.map(e => e[0]).join(', ')
const err = [
`Module '${module}' ${reason}`,
`dropping view attributes: ${dropped}`,
].join(', ')
console.warn(err)
}
}

if (entry.returnType.type !== Type.Value) {
dropWarning('returned unmaterialized value')
return extracted
}

if (typeof extracted !== 'object') {
dropWarning('returned a primitive')
return extracted
}

const data = Object.fromEntries(attrArgs)
const raster = materialize({ data, typeInfo: args.typeInfo })
return { ...raster, ...extracted }
}
Loading