TypeScript helpers for consuming openapi-typescript types
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core HTTP method and status code types for building type-safe OpenAPI integrations and web applications.
Union type representing all standard HTTP methods.
/**
* Standard HTTP methods as defined in RFC specifications
*/
type HttpMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace";Usage Examples:
import type { HttpMethod } from "openapi-typescript-helpers";
// Type-safe method validation
function isValidMethod(method: string): method is HttpMethod {
return ["get", "put", "post", "delete", "options", "head", "patch", "trace"].includes(method);
}
// Generic API client method
function makeRequest<T>(method: HttpMethod, url: string): Promise<T> {
return fetch(url, { method }).then(res => res.json());
}Union type for 2XX success status codes.
/**
* 2XX success status codes
*/
type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207 | "2XX";Usage Examples:
import type { OkStatus } from "openapi-typescript-helpers";
// Type-safe success status checking
function isSuccessStatus(status: number): status is OkStatus {
return status >= 200 && status < 300;
}
// Response type based on status
type SuccessfulResponse<T> = {
status: OkStatus;
data: T;
};Union type for 4XX and 5XX error status codes.
/**
* 4XX and 5XX error status codes including specific codes and wildcard patterns
*/
type ErrorStatus = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | '5XX' |
400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 444 | 450 | 451 | 497 | 498 | 499 | '4XX' | "default";Usage Examples:
import type { ErrorStatus } from "openapi-typescript-helpers";
// Error response type
type ErrorResponse<T = unknown> = {
status: ErrorStatus;
error: T;
};
// Type-safe error handling
function handleError(status: ErrorStatus, message: string) {
if (typeof status === 'string') {
// Handle wildcard statuses: "4XX", "5XX", "default"
console.error(`${status} error: ${message}`);
} else if (status >= 400 && status < 500) {
// Handle 4XX client errors
console.error(`Client error ${status}: ${message}`);
} else {
// Handle 5XX server errors
console.error(`Server error ${status}: ${message}`);
}
}Extract union of OK statuses from a type.
/**
* Get a union of OK statuses from a response type
*/
type OKStatusUnion<T> = FilterKeys<T, OkStatus>;Usage Examples:
import type { OKStatusUnion } from "openapi-typescript-helpers";
// Extract success statuses from response object
type ApiResponse = {
200: { data: User };
201: { data: User };
400: { error: string };
404: { error: string };
};
type SuccessStatuses = OKStatusUnion<ApiResponse>; // 200 | 201Get the first error status in priority order (5XX first, then 4XX, then default).
/**
* Get first error status, in priority order (5XX → 4XX → default)
*/
type FirstErrorStatus<T> =
T extends { 500: any } ? T[500] :
T extends { 501: any } ? T[501] :
T extends { 502: any } ? T[502] :
T extends { 503: any } ? T[503] :
T extends { 504: any } ? T[504] :
T extends { 505: any } ? T[505] :
T extends { 506: any } ? T[506] :
T extends { 507: any } ? T[507] :
T extends { 508: any } ? T[508] :
T extends { 510: any } ? T[510] :
T extends { 511: any } ? T[511] :
T extends { "5XX": any } ? T["5XX"] :
T extends { 400: any } ? T[400] :
T extends { 401: any } ? T[401] :
T extends { 402: any } ? T[402] :
T extends { 403: any } ? T[403] :
T extends { 404: any } ? T[404] :
T extends { 405: any } ? T[405] :
T extends { 406: any } ? T[406] :
T extends { 407: any } ? T[407] :
T extends { 408: any } ? T[408] :
T extends { 409: any } ? T[409] :
T extends { 410: any } ? T[410] :
T extends { 411: any } ? T[411] :
T extends { 412: any } ? T[412] :
T extends { 413: any } ? T[413] :
T extends { 414: any } ? T[414] :
T extends { 415: any } ? T[415] :
T extends { 416: any } ? T[416] :
T extends { 417: any } ? T[417] :
T extends { 418: any } ? T[418] :
T extends { 420: any } ? T[420] :
T extends { 421: any } ? T[421] :
T extends { 422: any } ? T[422] :
T extends { 423: any } ? T[423] :
T extends { 424: any } ? T[424] :
T extends { 425: any } ? T[425] :
T extends { 426: any } ? T[426] :
T extends { 427: any } ? T[427] :
T extends { 428: any } ? T[428] :
T extends { 429: any } ? T[429] :
T extends { 430: any } ? T[430] :
T extends { 431: any } ? T[431] :
T extends { 444: any } ? T[444] :
T extends { 450: any } ? T[450] :
T extends { 451: any } ? T[451] :
T extends { 497: any } ? T[497] :
T extends { 498: any } ? T[498] :
T extends { 499: any } ? T[499] :
T extends { "4XX": any } ? T["4XX"] :
T extends { default: any } ? T["default"] : never;Usage Examples:
import type { FirstErrorStatus } from "openapi-typescript-helpers";
// Extract first error from response type
type ApiResponse = {
200: { data: User };
400: { error: "Bad Request" };
404: { error: "Not Found" };
500: { error: "Internal Error" };
};
type FirstError = FirstErrorStatus<ApiResponse>; // { error: "Internal Error" } (500 takes priority)
// Use in error handling
type ErrorHandler<T> = (error: FirstErrorStatus<T>) => void;Install with Tessl CLI
npx tessl i tessl/npm-openapi-typescript-helpers