Skip to content

Examples

Fourteen worked examples live in the repository, under examples/. Each directory holds three files: the TypeSpec source, the tspconfig.yaml it was compiled with, and the asyncapi.yaml the emitter wrote from it.

The output is committed, so you can read an input and its output side by side without running anything. Every one of the fourteen passes the official AsyncAPI parser.

The examples

ExampleWhat it shows
Hello worldThe smallest document the emitter can produce. @service and @info, and nothing else.
Payload schemasThe shapes of the schema layer: models, scalars, enums, arrays, records, and the constraints on them.
Schema compositionFour ways to build one schema out of others, plus the raw-schema escape hatch.
Message metadataEverything that sits around a payload: headers, correlation ids, examples, tags and links.
Channels and parametersChannel ids, templated addresses, and the parameters an address declares.
Servers and securityServers, server variables, and the security schemes a server offers.
Request and replyThree operations, and the two shapes an AsyncAPI reply can take.
Kafka user signupOne realistic Kafka contract, with all four Kafka bindings on one document.
MQTT bindingsThe three MQTT decorators, and the one kind of name the generic @binding still carries.
StreetlightsThe canonical AsyncAPI example, written in TypeSpec.
Multiple protocolsOne application over Kafka, WebSocket and SQS, from one payload model.
HTTP callbacksThe HTTP bindings on a webhook, including the statusCode of a reply.
Enterprise brokersAMQP, JMS, IBM MQ and Anypoint MQ describing one destination four ways.
Streaming platformsNATS, Pulsar, Google Cloud Pub/Sub and Solace, and the fields each one requires.
Specification extensionsThe x- fields the specification leaves to the author, on all four objects that take one.

Running one

Clone the repository and compile inside the directory you want:

bash
git clone https://github.com/marvin-hsu/tsp-asyncapi.git
cd tsp-asyncapi
pnpm install && pnpm build
cd examples/01-hello-world
pnpm exec tsp compile .

The emitter writes asyncapi.yaml next to main.tsp, overwriting the copy in the repository. git diff then shows whether your build produces what was committed.

TIP

Every main.tsp starts with import "../..";, which points at the root of this repository. In your own project, depend on the package and write import "tsp-asyncapi"; instead.