AsyncAPI mode (qraft asyncapi)
qraft asyncapi is the AsyncAPI-focused mode of the unified @qraft/cli package.
It generates TypeScript schema types from AsyncAPI documents using AsyncAPI plugins.
Installationโ
- npm
- yarn
- pnpm
npm install -D @qraft/cli @qraft/asyncapi-typescript-plugin
yarn add --dev @qraft/cli @qraft/asyncapi-typescript-plugin
pnpm add -D @qraft/cli @qraft/asyncapi-typescript-plugin
Usageโ
The command below generates AsyncAPI TypeScript schema types into the src/events directory:
- npm
- yarn
- pnpm
npx qraft asyncapi --plugin asyncapi-typescript ./asyncapi.yaml --output-dir src/events
yarn exec qraft asyncapi --plugin asyncapi-typescript ./asyncapi.yaml --output-dir src/events
pnpm exec qraft asyncapi --plugin asyncapi-typescript ./asyncapi.yaml --output-dir src/events
Generated output exampleโ
Below is a real excerpt of generated code (from plugin snapshots):
/**
* This file was auto-generated by @qraft/cli.
* Do not make direct changes to the file.
*/
export interface servers {
"events-test": components["servers"]["events-test"];
/** @description Production WebSocket server for events (lighting measurements) secured with TLS */
"events-prod": {
host: "prod-events.example.com";
protocol: "wss";
};
}
export interface channels {
lightingMeasured: {
address: "/events/streetlights/{streetlightId}/lighting/measured";
messages: {
lightMeasured: components["messages"]["lightMeasured"];
lightMeasuredResponse: components["messages"]["lightMeasuredResponse"];
};
parameters: {
streetlightId: "streetlight-1" | "streetlight-2";
};
servers: [
servers["events-test"],
servers["events-prod"]
];
};
}
export interface operations {
receiveLightMeasurement: {
action: "receive";
channel: channels["lightingMeasured"];
messages: [
channels["lightingMeasured"]["messages"]["lightMeasured"]
];
reply: {
channel: channels["lightingMeasured"];
messages: [
channels["lightingMeasured"]["messages"]["lightMeasuredResponse"]
];
};
summary: "Server receives light measurement data from a sensor and responds with acknowledgment.";
};
}
Optionsโ
Requiredโ
-o <path>, --output-dir <path>: Specify where to output generated files.- Example:
--output-dir src/events
- Example:
Optionalโ
-c, --clean: Clean the output directory before generation.--file-header <string>: Add a custom header to each generated file, useful for disabling linting rules or adding file comments.- Example:
--file-header '/* eslint-disable */'
- Example:
--redocly [config]: Use the Redocly configuration to generate multiple API clients. If the[config]parameter is not specified, the default Redocly configuration will be used:[redocly.yaml | redocly.yml | .redocly.yaml | .redocly.yml].- Examples:
--redocly--redocly ./my-redocly-config.yamlqraft asyncapi events --redocly
- For more information about this option, use the command:
--redocly-helpor read the docs.
- Examples:
--plugin <name_1> --plugin <name_2>: Generator plugins to be used. (choices:asyncapi-typescript)- Example:
--plugin asyncapi-typescript
- Example:
-h, --help: Display help for the command.
Plugin Systemโ
The following plugin is currently supported:
asyncapi-typescript- Generates TypeScript types from an AsyncAPI Document.
tip
Plugin must be provided with the --plugin <name> option.
--plugin asyncapi-typescript optionsโ
--asyncapi-types-file-name <path>: AsyncAPI Schema types file name, e.g.,schema.d.ts(default:schema.ts).- Must end with
.tsor.d.ts. - Must not include path separators (
/or\). - Must not start with
..
- Must end with
--enum: Export true TypeScript enums instead of unions.--enum-values: Export enum values as arrays.--dedupe-enums: Dedupe enum types when--enumis set.-t, --export-type: Export top-leveltypeinstead ofinterface.--immutable: Generate readonly types.--additional-properties: Treat schema objects as ifadditionalProperties: trueis set.--empty-objects-unknown: Generateunknowninstead ofRecord<string, never>for empty objects.--default-non-nullable: Set tofalseto ignore default values when generating non-nullable types.--properties-required-by-default: Treat schema objects as ifrequiredis set to all properties by default.--alphabetize: Sort object keys alphabetically.--exclude-deprecated: Exclude deprecated fields from types.
Redocly config supportโ
AsyncAPI generation can be configured in redocly.yaml via x-asyncapi-qraft:
apis:
events:
root: ./asyncapi.yaml
x-asyncapi-qraft:
plugin:
asyncapi-typescript: true
output-dir: src/events
clean: true
asyncapi-types-file-name: asyncapi.ts
Then run:
- npm
- yarn
- pnpm
npx qraft asyncapi --redocly
# or generate both OpenAPI + AsyncAPI entries
npx qraft redocly
yarn exec qraft asyncapi --redocly
# or generate both OpenAPI + AsyncAPI entries
yarn exec qraft redocly
pnpm exec qraft asyncapi --redocly
# or generate both OpenAPI + AsyncAPI entries
pnpm exec qraft redocly