Generate an Idea

Once you have an Idea file, you can generate code using a transformer. A transformer is a plugin that can be used to generate code based on the provided Idea file.

Declare a Transformer

There are two ways to declare a transformer. The first is to declare a plugin in the main Idea file like the following example.
Copy
The second (harder) way is to create a plugin and listen to the event like the following example.
Copy
When Stackpress calls the event, the above code will get the current transformer from the request data via . Next we parse down the schema with , then manually add the plugin to the schema.
NOTE: Make sure to add the plugin to the plugins list in package.json.

Transform

Inside of a transformer file you just need to a function and the function does not need to return anything. The function can be optionally async.
Copy
The transform function can accept an object argument that contains the following properties.
  • - The plugin specific configuration object. For example,
  • - A object provided from ts-morph.
  • - The parsed schema object from the Idea file. For example, .
  • - The terminal instance that called this transformer. from the terminal, you can access the instance. For example, .
These given properties are the toolset used to generate code. The next thing to do is create a and add the schema like the following example.
Copy
The above example creates a new and loops through the models. The class is used to organize the raw schema into an API set of methods and properties. It contains the following properties.
  • - A map of props defined in the Idea schema.
  • - A map of enums defined in the Idea schema.
  • - A map of fieldset objects (type) defined in the Idea schema. You can learn more about the Fieldset class in the Client API section.
  • - A map of model objects defined in the Idea schema. You can learn more about the Model class in the Client API section.
From here you can generate code heuristically, or use the provided ts-morph API like the following example.
Copy
The above code creates a new source file in . The method will create a new file if it does not exist. This returns a pointer to the file so we can add code to it. The following code will add the first code block.
Copy
The above code will add the statement to the top of the file via . The next example demonstrates how to add a function to the file.
Copy
The above code will add a function called to the file. The function is exported and returns a element. The and are passed to the tag. To test this transformer, you can run the following command in the terminal.
Copy
The above command will run the transformer and generate a file located at . This means you can now import this file from your project like this.
Copy
NOTE: You may need to add this to the client exports list in the stackpress-client package.json file.