Skip to content

Latest commit

 

History

History
101 lines (70 loc) · 2.46 KB

File metadata and controls

101 lines (70 loc) · 2.46 KB

@typespec-tools/emitter-fetch-client

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.

Features / Roadmap

  • 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

Install

npm install @typespec-tools/emitter-fetch-client

Emitter

Usage

  1. Via the command line
tsp compile . --emit=@typespec-tools/emitter-fetch-client
  1. 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

Examples

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 },
});

Emitter options

Option Type Default Description
output-file string "output.ts" Name of the output file