Skip to content

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

Open
@c01nd01r

Description

@c01nd01r

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs 📃Improvements or additions to documentationfeature 🚀New feature or requestinternal ⚙️Internal development related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions