TypeSpec library for emitting a type-safe Fetch client based on routes from a TypeSpec DSL using the @typespec/http library.
Warning
This library uses the current emitter-framework that is set to be updated, which could cause breaking changes.
Disclaimer: Please note that this library is not officially affiliated with TypeSpec or Azure. It is an independent project started by @codingmatty.
- Emit a function that can build a type-safe Fetch client
- Add support for typed responses based on status codes
- Add support for authorization
- Add support for header types
- Add support for Zod schema validation of inputs: Path, Query, Body.
- Add support for Zod schema validation of output body
npm install @typespec-tools/emitter-fetch-client
- Via the command line
tsp compile . --emit=@typespec-tools/emitter-fetch-client
- Via the config
emit:
- "@typespec-tools/emitter-fetch-client"
The config can be extended with options as follows:
emit:
- "@typespec-tools/emitter-fetch-client"
options:
"@typespec-tools/emitter-fetch-client":
option: value
Given the following Typespec:
import "@typespec/http";
enum petType {...}
model Pet {...}
@route("/pets")
namespace Pets {
@get
op listPets(@query type?: petType): {
@body pets: Pet[];
};
}
This emitter will allow you to implement the following:
import { createTypedClient } from "tsp-output/@typespec-tools/emitter-fetch-client/output";
const client = createTypedClient("https://www.example.com/api");
const response: { pets: Pet[] } = client.Pets.listPets({
query: { type: petType.dog },
});
Alternatively, you can implement a single namespace:
import { createPetsClient } from "tsp-output/@typespec-tools/emitter-fetch-client/output";
const client = createPetsClient("https://www.example.com/api");
const response: { pets: Pet[] } = client.listPets({
query: { type: petType.dog },
});
Option | Type | Default | Description |
---|---|---|---|
output-file |
string | "output.ts" | Name of the output file |