clyent
v0.1.6
Published
TypeScript Axios API client & service builder with interceptor and metadata support
Downloads
8
Maintainers
Readme
clyent
A lightweight TypeScript library for rapidly building Axios-based API clients with fully-typed service definitions, endpoint metadata, and built-in interceptor support.
Features
- Service-Centric Definition: Group endpoints under named
ApiServiceinstances with per-servicebaseUrl, config, and interceptors. - Strong Typing: Inferred method signatures and return types based on your endpoint definitions.
- Metadata Exposure: Every service client exposes
baseUrl, mergedconfig, and the underlying Axiosinstancefor advanced use cases. - Global & Service-Level Interceptors: Plug in request/response hooks at both global and per-service levels.
- Zero Runtime Overhead: Just a thin factory wrapping Axios—no heavy abstractions.
Installation
npm install clyent axios
# or
yarn add clyent axiosMake sure you also install
axios(peer dependency).
Basic Usage
1. Define Your Services
Create one or more ApiService instances, each with a unique name, optional baseUrl, and a factory for your endpoint methods.
import { ApiService } from 'clyent';
import { AxiosInstance } from 'axios';
// Example response types
interface User {
id: number;
username: string;
}
interface GetUsersResponse {
items: User[];
}
export const usersService = new ApiService({
name: 'users',
baseUrl: '/users',
endpoints: (instance: AxiosInstance) => ({
getUsers: (page: number, limit: number) => instance.get<GetUsersResponse>('/', { params: { page, limit } }),
getUser: (id: number) => instance.get<User>(`/${id}`),
}),
});2. Create the API Client
Use createApiClient with a root URL and a map of your services.
import { createApiClient } from 'clyent';
const api = createApiClient('https://api.example.com', {
config: {
headers: { 'Content-Type': 'application/json' },
},
interceptors: {
request: {
onFulfilled: (cfg) => {
/* add auth token */ return cfg;
},
},
},
services: {
users: usersService,
// ...other services
},
});3. Consume Endpoints & Metadata
// Call an endpoint
api.users.getUsers(1, 20).then(res => {
console.log(res.data.items);
});
// Access service metadata
console.log(api.users.baseUrl); // "/users"
console.log(api.users.config.baseURL); // "https://api.example.com/users"
// Use raw Axios instance
api.users.instance.interceptors.response.use(...);Advanced Topics
- Global vs. Service-Level Interceptors: Pass
interceptorsincreateApiClientfor all services, or on individualApiServicedefinitions for fine-grained control. - Extensibility: Because you own the raw Axios instance (
api[service].instance), you can add retries, caching, or custom plugins on the fly. - Type Safety: No need to cast; each endpoint’s parameters and
res.dataare fully inferred from your definitions.
API Reference
new ApiService<Endpoints>(cfg: ApiServiceConfig<Endpoints>)
cfg.name:string— service key, becomesapi[name].cfg.baseUrl:string— URL segment appended to root.cfg.config:AxiosRequestConfig— merged with global.cfg.interceptors:Interceptors— request/response hooks for this service.cfg.endpoints:(instance: AxiosInstance) => Endpoints— factory returning your typed methods.
createApiClient(rootUrl: string, init: ApiClientConfig)
rootUrl: Base URL for all services.init.config: Global Axios config.init.interceptors: Global interceptors.init.services: Map of service keys →ApiServiceinstances.
Returns an object typed as:
{ [K in keyof Services]: ServiceClient<Endpoints> }Where ServiceClient<Endpoints> exposes:
- All your
endpointsmethods baseUrl,config,instance,interceptors
Contributing
- Fork the repo
- Install dependencies:
npm install - Build:
npm run build - Create a PR—happy to review!
