Skip to content

Templates

Swaggie ships with seven built-in templates covering the most common TypeScript HTTP client libraries. You can also provide a custom template directory if none of them fit your codebase.

Built-in templates

TemplateHTTP ClientBest for
axiosAxiosReact, Vue, Node.js — the most widely used default
fetchNative fetchBrowser apps with no extra dependencies
xiorxiorLightweight Axios-compatible alternative
swr-axiosSWR + AxiosReact apps using SWR for data fetching
tsq-xiorTanStack Query + xiorReact apps using TanStack Query
ng1Angular 1 $httpLegacy Angular 1.x applications
ng2Angular HttpClientAngular 2+ applications (uses InjectionToken)

axios (default)

The default template. Generates client objects with methods that return AxiosPromise<T>. Includes a shared Axios instance configured with your baseUrl and query parameter serialization settings.

Dependencies you need in your project:

bash
npm install axios

Generated output (excerpt):

typescript
import Axios, { AxiosPromise } from 'axios';

const axios = Axios.create({ baseURL: '/api' });

export const petClient = {
  getPetById(petId: number): AxiosPromise<Pet> {
    return axios.request<Pet>({ url: `/pet/${petId}`, method: 'GET' });
  },
};

fetch

Uses the native browser (or Node 18+) fetch API. No runtime dependencies required.

Generated output (excerpt):

typescript
export const petClient = {
  async getPetById(petId: number): Promise<Pet> {
    const response = await fetch(`/api/pet/${petId}`);
    return response.json() as Promise<Pet>;
  },
};

xior

xior is a lightweight, modern alternative to Axios with a compatible API surface. Use this if you want Axios-style interceptors without the Axios bundle size.

Dependencies:

bash
npm install xior

swr-axios

Generates SWR hooks for GET operations and standard Axios methods for mutations (POST, PUT, PATCH, DELETE). Best for React apps that use SWR for server state.

Dependencies:

bash
npm install axios swr

Generated output (excerpt):

typescript
import useSWR from 'swr';

export const petClient = {
  useGetPetById(petId: number) {
    return useSWR<Pet>(`/pet/${petId}`, fetcher);
  },

  addPet(body: Pet): AxiosPromise<Pet> {
    return axios.request<Pet>({ url: '/pet', method: 'POST', data: body });
  },
};

tsq-xior

Generates TanStack Query hooks for GET operations, backed by xior for the actual HTTP calls. Mutations use plain xior calls.

Dependencies:

bash
npm install @tanstack/react-query xior

ng1

Angular 1 template. Generates a service using $http and Angular 1 dependency injection.


ng2

Angular 2+ template. Generates injectable services using HttpClient and InjectionToken for configuration. Requires @angular/common/http.

Choosing the right template

  • No framework / Node.js backendfetch (zero deps) or axios (interceptors, retries)
  • React with SWRswr-axios
  • React with TanStack Querytsq-xior
  • Prefer minimal bundle sizexior or fetch
  • Angularng2 (Angular 2+) or ng1 (legacy)

Custom templates

If none of the built-in templates fit your existing HTTP client setup, you can provide your own template directory:

bash
swaggie -s ./openapi.json -o ./client.ts --template ./my-template/

Or in your config file:

json
{
  "template": "./my-template/"
}

Template directory structure

A custom template directory should contain EJS (.ejs) files. Swaggie will look for these files:

FilePurpose
baseClient.ejsTop of the output file — imports, shared HTTP client setup
client.ejsOne client object per tag group
operation.ejsOne method per API operation
barrel.ejsRe-export barrel file (optional)

Available template variables

Inside each template, Swaggie provides the following variables:

In operation.ejs:

VariableTypeDescription
operationIOperationParsed operation data (method, path, params, responses)
optionsAppOptionsResolved generation options

In client.ejs:

VariableTypeDescription
groupstringTag name for this client group
operationsIOperation[]All operations belonging to this group
optionsAppOptionsResolved generation options

In baseClient.ejs:

VariableTypeDescription
optionsAppOptionsResolved generation options
groupsstring[]All tag group names

The best way to get started with a custom template is to copy one of the built-in templates and modify it for your needs.

Released under the MIT License.