Custom Generator

Create Client provides support for many of the popular JS frameworks, but you may be using another framework or language and may need a solution adapted to your specific needs. For this cenario, you can write your own generator and pass it to the CLI using a path as the -g argument.

Create Client は、一般的な JS フレームワークの多くをサポートしていますが、別のフレームワークや言語を使用している可能性があり、特定のニーズに合わせたソリューションが必要になる場合があります。このシナリオでは、独自のジェネレーターを作成し、パスを -g 引数として使用して CLI に渡すことができます。

You will probably want to extend or, at least, take a look at BaseGenerator.js, since the library expects some methods to be available, as well as one of the included generators to make your own.

ライブラリは、いくつかのメソッドが利用可能であり、含まれているジェネレーターの 1 つが独自のものを作成することを想定しているため、拡張するか、少なくとも BaseGenerator.js を確認することをお勧めします。

Usage

npm init @api-platform/client -- --generator "$(pwd)/path/to/custom/generator.js" -t "$(pwd)/path/to/templates"

The -g argument can point to any resolvable node module which means it can be a package dependency of the current project as well as any js file.

-g 引数は、任意の解決可能なノード モジュールを指すことができます。つまり、現在のプロジェクトおよび任意の js ファイルのパッケージ依存関係になる可能性があります。

Example

Create Client makes use of the Handlebars template engine. You can use any programming language or file type. Your generator can also pass data to your templates in any shape you want.

Create Client は、Handlebars テンプレート エンジンを利用します。任意のプログラミング言語またはファイル タイプを使用できます。ジェネレーターは、データを必要な形でテンプレートに渡すこともできます。

In this example, we'll create a simple Rust file defining a new struct and creating some instances of this struct.

この例では、新しい構造体を定義し、この構造体のいくつかのインスタンスを作成する単純な Rust ファイルを作成します。

Generator

// ./Generator.js
import BaseGenerator from "@api-platform/create-client/lib/generators/BaseGenerator";

export default class extends BaseGenerator {
    constructor(params) {
        super(params);

        this.registerTemplates("", ["main.rs"]);
    }

    help() {}

    generate(api, resource, dir) {
        const context = {
            type: "Tilia",
            structure: [
                { name: "name", type: "String" },
                { name: "min_size", type: "u8" },
                { name: "max_size", type: "u8" },
            ],
            list: [
                {
                    name: "Tilia cordata",
                    minSize: 50,
                    maxSize: 80,
                },
                {
                    name: "Tilia platyphyllos",
                    minSize: 50,
                    maxSize: 70,
                },
                {
                    name: "Tilia tomentosa",
                    minSize: 50,
                    maxSize: 70,
                },
                {
                    name: "Tilia intermedia",
                    minSize: 50,
                    maxSize: 165,
                },
            ],
        };

        this.createDir(dir);

        this.createFile("main.rs", `${dir}/main.rs`, context, false);
    }
}

Template

// template/main.rs
struct {{{type}}} {
  {{#each structure}}
  {{{name}}}: {{{type}}}
  {{/each}}
}

fn main() {
  let tilias = [
  {{#each list}}
    Tilia { name: "{{{name}}}", min_size: {{{minSize}}}, max_size: {{{maxSize}}}, },
  {{/each}}
  ];
}

Then we can use our generator:

次に、ジェネレーターを使用できます。

npm init @api-platform/client https://demo.api-platform.com out/ -g "$(pwd)/Generator.js" -t "$(pwd)/template"

which will produces:

これは以下を生成します:

struct Tilia {
  name: String
  min_size: u8
  max_size: u8
}

fn main() {
  let tilias = [
    Tilia { name: "Tilia cordata", min_size: 50, max_size: 80, },
    Tilia { name: "Tilia platyphyllos", min_size: 50, max_size: 70, },
    Tilia { name: "Tilia tomentosa", min_size: 50, max_size: 70, },
    Tilia { name: "Tilia intermedia", min_size: 50, max_size: 165, },
  ];
}