openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

Docs: Add an example of adding a text node to a file inside the plugin.

Open c01nd01r opened this issue 6 months ago • 2 comments

Description

Hi there!

Recently, a friend of mine told me that his company was evaluating tools for generating API clients, but hey-api lost a few points in the process. He found it a bit complicated that file contents had to be built using the TypeScript AST, as shown in the handler example on the custom plugin page. For his use case, working with simple string literals would have been much more convenient.

I showed him that the file.add(...nodes) interface also supports string-based inputs, and he actually found the ability to mix both styles even more convenient than in other code generation tools.

From my perspective, the current example doesn’t clearly show that a handler can simply use strings like

file.add('export const sayHey = () =>  alert("Hey!")')

I’d like to suggest updating the example to demonstrate both approaches, highlighting the flexibility and developer-friendliness of plugin development for hey-api.

Something like:

// we're using the TypeScript Compiler API
const stringLiteral = ts.factory.createStringLiteral('Hello, world!');
const variableDeclaration = ts.factory.createVariableDeclaration(
  'foo',
  undefined,
  undefined,
  stringLiteral,
);
const tsNode = ts.factory.createVariableStatement(
  [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
  ts.factory.createVariableDeclarationList(
    [variableDeclaration],
    ts.NodeFlags.Const,
  ),
);

// or just using strings
const stringNode = `export const sayHey = () => alert("Hey!")`;

// add nodes to our file
file.add(tsNode, stringNode);

c01nd01r avatar Apr 18 '25 16:04 c01nd01r