Skip to content

feat: add interpolate for string template transclusion#149

Merged
char0n merged 4 commits into
mainfrom
feat/interpolate-transclusion
Jul 8, 2026
Merged

feat: add interpolate for string template transclusion#149
char0n merged 4 commits into
mainfrom
feat/interpolate-transclusion

Conversation

@char0n

@char0n char0n commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Implements the transclusion mechanism for string templates (#86) as interpolate().

interpolate(template, resolver, options?) renders Arazzo string templates by replacing each embedded {expression} with a value produced by a user-supplied resolver callback:

import { interpolate } from '@swaggerexpert/arazzo-runtime-expression';

interpolate('Hello {$inputs.name}!', () => 'world'); // => 'Hello world!'

const values = { '$inputs.clientId': 'abc', '$inputs.grantType': 'authorization_code' };
interpolate('client_id={$inputs.clientId}&grant_type={$inputs.grantType}', (expr) => values[expr]);
// => 'client_id=abc&grant_type=authorization_code'

Design

  • Reuses the existing expression-string grammar/CST (same foundation as extract), so expression boundaries — including JSON Pointers in the name part — are parsed correctly rather than regex-matched.
  • Because this package is a context-agnostic parser, resolution logic is supplied by the caller via resolver, which receives the raw expression (e.g. $inputs.name). This composes cleanly with a downstream evaluator.
  • Resolved values are stringified via a configurable stringify option: defaults to '' for undefined/null, strings as-is, JSON.stringify for objects, String() otherwise.
  • Throws TypeError for non-string templates / non-function resolvers; returns the template unchanged when it can't be parsed (mirroring extract's leniency).

Changes

  • src/interpolate.js — implementation
  • src/index.js — public export
  • types/index.d.tsinterpolate, InterpolateResolver, InterpolateOptions typings
  • README.md — new Interpolation section, TOC entry, updated tagline
  • test/interpolate.js — 19 tests

Verification

Full npm run build succeeds with dual ESM/CJS output; all 188 tests pass; both built formats confirmed to export a working interpolate.

Closes #86

🤖 Generated with Claude Code

Add `interpolate(template, resolver, options?)` which renders Arazzo
string templates by replacing each embedded `{expression}` with a value
produced by a user-supplied resolver callback.

The implementation reuses the existing `expression-string` grammar/CST
(the same foundation as `extract`), so expression boundaries — including
JSON Pointers in the name part — are parsed correctly. Literal content is
preserved verbatim. Resolved values are stringified via a configurable
`stringify` option that defaults to '' for undefined/null, strings as-is,
JSON.stringify for objects and String() otherwise.

Closes #86
@char0n
char0n force-pushed the feat/interpolate-transclusion branch from cc09748 to 7d18cc2 Compare July 8, 2026 06:18
char0n added 2 commits July 8, 2026 08:34
Adds coverage for repeated expressions, verbatim splicing of values
containing replacement patterns ($&), non-re-interpolation of values that
look like expressions, and JSON-object-looking braced literals left
unchanged.
Replace the 'transclusion' framing with the spec's 'embeds' wording, and
remove the Interpolation note whose 'see the extraction notes' reference
pointed at notes that do not exist and which overstated the body-pointer
limitation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new interpolate() public API to render Arazzo “expression-string” templates by walking the existing CST for expression-string and replacing each {expression} with a caller-resolved value (with configurable stringification).

Changes:

  • Implemented src/interpolate.js using the existing expression-string grammar + CSTTranslator.
  • Exported interpolate from src/index.js and added TypeScript typings in types/index.d.ts.
  • Documented interpolation in README.md and added a dedicated test suite in test/interpolate.js.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/interpolate.js Core interpolation implementation + default stringification behavior.
src/index.js Public export of interpolate.
types/index.d.ts Adds interpolate typings plus resolver/options types.
test/interpolate.js New tests covering interpolation behavior and edge cases.
README.md Adds “Interpolation” usage docs and updates package capability list/TOC.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/interpolate.js Outdated
Comment thread src/interpolate.js Outdated
Coalesce JSON.stringify to '' so an object whose toJSON() returns
undefined does not leak the literal 'undefined' into the output, and
guard the embedded-expression's expression child, emitting node text
verbatim as a defensive fallback. Addresses Copilot review feedback.
@char0n
char0n merged commit 23c72dc into main Jul 8, 2026
4 checks passed
@char0n
char0n deleted the feat/interpolate-transclusion branch July 8, 2026 06:55
char0n pushed a commit that referenced this pull request Jul 8, 2026
# [3.1.0](v3.0.0...v3.1.0) (2026-07-08)

### Features

* add interpolate for string template transclusion ([#149](#149)) ([23c72dc](23c72dc)), closes [#86](#86)
@char0n

char0n commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

🎉 This PR is included in version 3.1.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@char0n char0n added the released label Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tranclusion mechanism for string templates

2 participants