|
1 | | -# Codama ➤ Renderers ➤ Demo |
| 1 | +# Codama ➤ Renderers ➤ JavaScript |
2 | 2 |
|
3 | 3 | [![npm][npm-image]][npm-url] |
4 | 4 | [![npm-downloads][npm-downloads-image]][npm-url] |
5 | 5 |
|
6 | | -[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/renderers-demo.svg?style=flat |
7 | | -[npm-image]: https://img.shields.io/npm/v/@codama/renderers-demo.svg?style=flat&label=%40codama%2Frenderers-demo |
8 | | -[npm-url]: https://www.npmjs.com/package/@codama/renderers-demo |
| 6 | +[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/renderers-js.svg?style=flat |
| 7 | +[npm-image]: https://img.shields.io/npm/v/@codama/renderers-js.svg?style=flat&label=%40codama%2Frenderers-js |
| 8 | +[npm-url]: https://www.npmjs.com/package/@codama/renderers-js |
9 | 9 |
|
10 | | -This package provides a demo implementation of a Codama renderer to help developers create their own. |
| 10 | +This package generates JavaScript clients from your Codama IDLs. The generated clients are compatible with [`@solana/kit`](https://github.com/anza-xyz/kit). |
11 | 11 |
|
12 | 12 | ## Installation |
13 | 13 |
|
14 | 14 | ```sh |
15 | | -pnpm install @codama/renderers-demo |
| 15 | +pnpm install @codama/renderers-js |
16 | 16 | ``` |
17 | 17 |
|
| 18 | +> [!NOTE] |
| 19 | +> This package is **not** included in the main [`codama`](../library) package. |
| 20 | +> |
| 21 | +> However, note that the [`renderers`](../renderers) package re-exports the `renderVisitor` function of this package as `renderJavaScriptVisitor`. |
| 22 | +
|
18 | 23 | ## Usage |
19 | 24 |
|
20 | | -Add the following script to your Codama configuration file. |
21 | | - |
22 | | -```json |
23 | | -{ |
24 | | - "scripts": { |
25 | | - "demo": { |
26 | | - "from": "@codama/renderers-demo", |
27 | | - "args": ["docs"] |
28 | | - } |
29 | | - } |
30 | | -} |
| 25 | +Once you have a Codama IDL, you can use the `renderVisitor` of this package to generate JavaScript clients. You will need to provide the base directory where the generated files will be saved and an optional set of options to customize the output. |
| 26 | + |
| 27 | +```ts |
| 28 | +// node ./codama.mjs |
| 29 | +import { renderVisitor } from '@codama/renderers-js'; |
| 30 | + |
| 31 | +const pathToGeneratedFolder = path.join(__dirname, 'clients', 'js', 'src', 'generated'); |
| 32 | +const options = {}; // See below. |
| 33 | +codama.accept(renderVisitor(pathToGeneratedFolder, options)); |
31 | 34 | ``` |
| 35 | + |
| 36 | +## Options |
| 37 | + |
| 38 | +The `renderVisitor` accepts the following options. |
| 39 | + |
| 40 | +| Name | Type | Default | Description | |
| 41 | +| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 42 | +| `deleteFolderBeforeRendering` | `boolean` | `true` | Whether the base directory should be cleaned before generating new files. | |
| 43 | +| `formatCode` | `boolean` | `true` | Whether we should use Prettier to format the generated code. | |
| 44 | +| `prettierOptions` | `PrettierOptions` | `{}` | The options to use when formatting the code using Prettier. | |
| 45 | +| `asyncResolvers` | `string[]` | `[]` | The exhaustive list of `ResolverValueNode`'s names whose implementation is asynchronous in JavaScript. | |
| 46 | +| `customAccountData` | `string[]` | `[]` | The names of all `AccountNodes` whose data should be manually written in JavaScript. | |
| 47 | +| `customInstructionData` | `string[]` | `[]` | The names of all `InstructionNodes` whose data should be manually written in JavaScript. | |
| 48 | +| `linkOverrides` | `Record<'accounts' \| 'definedTypes' \| 'instructions' \| 'pdas' \| 'programs' \| 'resolvers', Record<string, string>>` | `{}` | A object that overrides the import path of link nodes. For instance, `{ definedTypes: { counter: 'hooked' } }` uses the `hooked` folder to import any link node referring to the `counter` type. | |
| 49 | +| `dependencyMap` | `Record<string, string>` | `{}` | A mapping between import aliases and their actual package name or path in JavaScript. | |
| 50 | +| `internalNodes` | `string[]` | `[]` | The names of all nodes that should be generated but not exported by the `index.ts` files. | |
| 51 | +| `nameTransformers` | `Partial<NameTransformers>` | `{}` | An object that enables us to override the names of any generated type, constant or function. | |
| 52 | +| `nonScalarEnums` | `string[]` | `[]` | The names of enum variants with no data that should be treated as a data union instead of a native `enum` type. This is only useful if you are referencing an enum value in your Codama IDL. | |
| 53 | +| `renderParentInstructions` | `boolean` | `false` | When using nested instructions, whether the parent instructions should also be rendered. When set to `false` (default), only the instruction leaves are being rendered. | |
| 54 | +| `useGranularImports` | `boolean` | `false` | Whether to import the `@solana/kit` library using sub-packages such as `@solana/addresses` or `@solana/codecs-strings`. When set to `true`, the main `@solana/kit` library is used which enables generated clients to install it as a `peerDependency`. | |
0 commit comments