Skip to content

@amqp-contract/asyncapi


@amqp-contract/asyncapi ​

Classes ​

AsyncAPIGenerator ​

Defined in: index.ts:111

Generator for creating AsyncAPI 3.0 documentation from AMQP contracts.

This class converts contract definitions into AsyncAPI 3.0 specification documents, which can be used for API documentation, code generation, and tooling integration.

Example ​

typescript
import { AsyncAPIGenerator } from '@amqp-contract/asyncapi';
import { defineExchange, defineMessage, defineContract, definePublisher } from '@amqp-contract/contract';
import { zodToJsonSchema } from '@orpc/zod';
import { z } from 'zod';

const ordersExchange = defineExchange('orders');
const orderMessage = defineMessage(z.object({
  orderId: z.string(),
  amount: z.number()
}));

const contract = defineContract({
  publishers: {
    orderCreated: definePublisher(ordersExchange, orderMessage, {
      routingKey: 'order.created'
    })
  }
});

const generator = new AsyncAPIGenerator({
  schemaConverters: [zodToJsonSchema]
});

const asyncapi = await generator.generate(contract, {
  id: 'urn:com:example:order-service',
  info: {
    title: 'Order Service API',
    version: '1.0.0',
    description: 'Async API for order processing'
  },
  servers: {
    production: {
      host: 'rabbitmq.example.com',
      protocol: 'amqp',
      protocolVersion: '0.9.1'
    }
  }
});

Constructors ​

Constructor ​
ts
new AsyncAPIGenerator(options?): AsyncAPIGenerator;

Defined in: index.ts:121

Create a new AsyncAPI generator instance.

Parameters ​
ParameterTypeDescription
optionsAsyncAPIGeneratorOptionsConfiguration options including schema converters
Returns ​

AsyncAPIGenerator

Methods ​

generate() ​
ts
generate(contract, options): Promise<AsyncAPIObject>;

Defined in: index.ts:155

Generate an AsyncAPI 3.0 document from a contract definition.

Converts AMQP exchanges, queues, publishers, and consumers into AsyncAPI channels, operations, and messages with proper JSON Schema validation definitions.

Parameters ​
ParameterTypeDescription
contractContractDefinitionThe AMQP contract definition to convert
optionsAsyncAPIGeneratorGenerateOptionsAsyncAPI document metadata (id, info, servers)
Returns ​

Promise<AsyncAPIObject>

Promise resolving to a complete AsyncAPI 3.0 document

Example ​
typescript
const asyncapi = await generator.generate(contract, {
  id: 'urn:com:example:api',
  info: {
    title: 'My API',
    version: '1.0.0'
  },
  servers: {
    dev: {
      host: 'localhost:5672',
      protocol: 'amqp'
    }
  }
});

Type Aliases ​

AsyncAPIGeneratorGenerateOptions ​

ts
type AsyncAPIGeneratorGenerateOptions = Pick<AsyncAPIObject, "info"> & Partial<Pick<AsyncAPIObject, "id" | "servers">>;

Defined in: index.ts:60

Options for generating an AsyncAPI document. These correspond to the top-level AsyncAPI document fields.


AsyncAPIGeneratorOptions ​

ts
type AsyncAPIGeneratorOptions = object;

Defined in: index.ts:35

Options for configuring the AsyncAPI generator.

Example ​

typescript
import { AsyncAPIGenerator } from '@amqp-contract/asyncapi';
import { zodToJsonSchema } from '@orpc/zod';

const generator = new AsyncAPIGenerator({
  schemaConverters: [zodToJsonSchema]
});

Properties ​

PropertyTypeDescriptionDefined in
failOnMissingConverter?booleanIf true, the generator throws when a payload schema cannot be converted by any of the configured schemaConverters instead of falling back to a generic { type: "object" } placeholder. Recommended for CI pipelines that depend on the generated spec being faithful (e.g. for codegen). Defaults to false for backwards compatibility.index.ts:53
logger?objectOptional logger for warnings during generation (e.g. unmatched schema converters).index.ts:44
logger.warn(message) => void-index.ts:44
schemaConverters?ConditionalSchemaConverter[]Schema converters for transforming validation schemas to JSON Schema. Supports Zod, Valibot, ArkType, and other Standard Schema v1 compatible libraries.index.ts:40

Released under the MIT License.