@rabstack/rab-api-spec
v0.1.0
Published
Type-safe API endpoint specification helpers for TypeScript
Downloads
1,180
Readme
@rabstack/rab-api-spec
Type-safe API endpoint specification helpers for TypeScript.
Installation
npm install @rabstack/rab-api-specUsage
import { GetApi, PostApi, PutApi, PatchApi, DeleteApi } from '@rabstack/rab-api-spec';
// Define your types
interface User {
id: string;
name: string;
email: string;
}
interface CreateUserBody {
name: string;
email: string;
}
// Define endpoints with full type safety
const endpoints = {
getUsers: GetApi<User[]>('/users'),
getUser: GetApi<User, unknown, { id: string }>('/users/:id'),
createUser: PostApi<User, CreateUserBody>('/users'),
updateUser: PutApi<User, Partial<CreateUserBody>, { id: string }>('/users/:id'),
deleteUser: DeleteApi<void, { id: string }>('/users/:id'),
};API
EndpointSpec<RESPONSE, BODY, QUERY, PARAMS>
Core interface with properties:
url: string- Endpoint URL pathmethod: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'- HTTP method
Helper Functions
GetApi<RESPONSE, QUERY, PARAMS>(url)- GET endpointPostApi<RESPONSE, BODY, PARAMS>(url)- POST endpointPutApi<RESPONSE, BODY, PARAMS>(url)- PUT endpointPatchApi<RESPONSE, BODY, PARAMS>(url)- PATCH endpointDeleteApi<RESPONSE, PARAMS>(url)- DELETE endpoint
License
MIT
