module

Noir::JSObjectConfigExtractor

Extracts declarative object-literal configs out of JS/TS sources.

Several frameworks describe their HTTP surface as a plain object rather than as route calls — Payload CMS collections ({ slug, fields }) and Strapi route files ({ routes: [...] }) among them. Both are read the same way: find the object literals that carry a known set of keys and decode them into Crystal data.

The search is a depth-first sweep for any object node holding the required keys, deliberately not a match on the export const X = {…} declaration spine. That makes it indifferent to how the object is declared, which in the wild means all of:

export const Posts: CollectionConfig = { slug: 'posts', fields: [] }
export const Posts = { slug: 'posts', fields: [] } satisfies CollectionConfig
export default buildConfig({ collections: [{ slug: 'posts', fields: [] }] })
module.exports = { slug: 'posts', fields: [] }

TypeScript is parsed with its own grammar (parse_js_family), so as / satisfies wrappers are peeled off values, not stripped from the source.

Constants

MAX_VALUE_DEPTH = 12

Guards against pathological nesting in generated configs.

Instance methods

extract(source : String, required_keys : Array(String), path : String = "") : Array(ConfigObject)

Returns every object literal in source carrying all of required_keys. Once an object matches, its own subtree is not searched again — otherwise a nested config would surface twice.

Source

Nested types