1 HTTP Request = 1 TypeScript Class
Define HTTP requests as TypeScript classes and send them using native fetch.
Real-world APIs have different workloads and performance requirements. Define execution policies declaratively for each endpoint, with TypeScript’s type safety.
jin-frame brings these policies into the endpoint definition:
- One endpoint, one class. Keep request parameters, HTTP methods, and execution policies together in a TypeScript class, making each endpoint’s behavior visible in one place.
- Reuse policies through inheritance. Share hosts, authentication, and request settings across base classes, then override only what each endpoint needs.
- Declare policies with decorators. Configure timeouts, retries, and other request behaviors per endpoint, so call sites reuse those policies without repeating configuration.
- Catch mistakes with type safety. Check request field types and required builder fields at compile time, and narrow response data types by HTTP status code.
- Build reliability into each endpoint. Validate response data, deduplicate identical concurrent requests, and configure retry policies as part of the request definition.
- Generate classes from OpenAPI. Use the companion generators to turn API specifications into TypeScript endpoint classes and types, with options and custom templates to fit your project.
- Introduction
- Why jin-frame?
- Install
- Usage
- Inheritance
- Builder Pattern
- Response status unions
- Retry, Timeout, Validation
- Authorization
- Response Validation and Errors
- jin-frame Requirements
- Documentation
- Support
- License
npm install jin-frame --saveyarn add jin-frame --savepnpm add jin-frame --saveThe following example defines a request and fetches a Pokémon by name.
Run it in a TypeScript ESM project with experimentalDecorators enabled and top-level await support.
import { Get, Param, JinFrame } from 'jin-frame';
// Define an HTTP GET request with a decorator.
@Get({
// Set the API endpoint.
host: 'https://pokeapi.co',
path: '/api/v2/pokemon/{name}',
})
export class PokemonFrame extends JinFrame {
// Map this field to the name parameter in the URL path.
@Param()
declare public readonly name: string;
}
// Create a request instance using the static factory.
const frame = PokemonFrame.of({ name: 'pikachu' });
// Send the request and read the response.
const reply = await frame._execute();
console.log(reply.data);Define shared settings (host, auth, pathPrefix, default field values) in a base class and override only the path in each subclass.
import { Get, Post, Param, Header, Body, JinFrame } from 'jin-frame';
import { randomUUID } from 'node:crypto';
// Share the host and URL prefix across subclasses.
@Get({
host: 'https://pokeapi.co',
pathPrefix: '/api/v2',
})
class PokeApiFrame extends JinFrame {
// Map the inherited field to the X-Request-Id header.
@Header({ replaceAt: 'X-Request-Id' })
declare public readonly requestId: string;
// Generate a default request ID unless an explicit value is supplied.
protected static override getDefaultValues() {
return { requestId: randomUUID() };
}
}
// Override the path while inheriting the host and prefix.
@Get({ path: '/pokemon/{name}' })
class GetPokemonFrame extends PokeApiFrame {
// Substitute this value for {name} in the URL path.
@Param()
declare public readonly name: string;
}
// Illustrate a POST override; PokeAPI does not support creating Pokémon.
@Post({ path: '/pokemon' })
class CreatePokemonFrame extends PokeApiFrame {
// Serialize this field into the request body rather than the URL path.
@Body()
declare public readonly name: string;
}
// Supply name through the callback and use the inherited default request ID.
const frame = GetPokemonFrame.of((b) => b.set('name', 'pikachu'));
// Send the GET request with the generated X-Request-Id header.
const reply = await frame._execute();
console.log(reply.data);builder() tracks which fields have been set at the type level. build() is only available once all public fields are assigned, catching missing fields at compile time.
const frame = PokemonFrame.builder()
.set('name', 'pikachu')
.set('tid', randomUUID())
.build(); // compile error if any public field is missing
const reply = await frame._execute();of() also accepts a builder callback:
const frame = PokemonFrame.of((b) => b.set('name', 'pikachu').set('tid', randomUUID()));JinFrame takes one { status, data } response union. Checking status narrows the corresponding data type.
type PokemonResponse =
| { status: 200; data: Pokemon }
| { status: 404; data: ErrorBody };
class PokemonFrame extends JinFrame<PokemonResponse> {}
const reply = await frame._execute({ validateStatus: () => true });
if (reply.status === 200) {
console.log(reply.data);
} else {
console.error(reply.data);
}The OpenAPI generator combines documented numeric 2xx–5xx responses into a { status, data } union passed as the single JinFrame type argument. Checking status narrows data to that response's type. Responses without a body use undefined data.
Non-2xx responses throw by default. Use _execute({ validateStatus: () => true }) to receive error responses directly. The generated class always declares all documented numeric statuses. Without a call-level validator, the return type selects 2xx responses; () => true returns the full union. Use validateStatus(200, 404) to select specific statuses at runtime and in the return type. Range keys such as 2XX and default are not included in the numeric literal union.
This is a breaking type API change. Migrate JinFrame<DTO> to JinFrame<{ status: number; data: DTO }> or a status-specific union. JinPassResp and JinFailResp are replaced by JinResp<Response>. Register a single validator: new ApiValidator() receiving the complete response union, and select schemas by status inside the validator.
Retry failed requests with a configurable interval.
import { Get, JinFrame, Retry } from 'jin-frame';
// Retry up to 5 times with a 1-second interval.
@Retry({
max: 5,
interval: 1000,
})
@Get({
host: 'https://pokeapi.co',
path: '/api/v2/pokemon/pikachu',
})
class RetryPokemonFrame extends JinFrame {}Set the timeout in milliseconds.
import { Get, JinFrame, Timeout } from 'jin-frame';
// Abort each request attempt after 2 seconds.
@Timeout(2000)
@Get({
host: 'https://pokeapi.co',
path: '/api/v2/pokemon/pikachu',
})
class TimeoutPokemonFrame extends JinFrame {}Select a Zod schema by HTTP status (pnpm add zod).
import { z } from 'zod';
import { BaseValidator, Get, JinFrame, Validator } from 'jin-frame';
import type { ValidationInput, ValidationResult } from 'jin-frame';
const schemas: Partial<Record<number, z.ZodType>> = {
200: z.object({ name: z.string() }),
};
class PokemonValidator extends BaseValidator {
validator({ status, data }: ValidationInput): ValidationResult<string> {
// Select the body schema for this HTTP status.
const schema = schemas[status];
// Leave statuses without a schema to the HTTP status policy.
if (schema === undefined) {
return { valid: true };
}
const result = schema.safeParse(data);
return result.success
? { valid: true }
: { valid: false, error: result.error.issues.map((issue) => issue.message) };
}
}
// Throw JinValidationError when an accepted response fails validation.
@Validator(new PokemonValidator({ type: 'exception' }))
@Get({
host: 'https://pokeapi.co',
path: '/api/v2/pokemon/pikachu',
})
class ValidatedPokemonFrame extends JinFrame {}Use a security provider to turn credentials into request headers.
import { BearerTokenProvider, Get, JinFrame } from 'jin-frame';
@Get({
host: 'https://api.example.com', // Replace with your API host.
path: '/me',
// Read the current token whenever a request is built.
security: new BearerTokenProvider(() => process.env.API_ACCESS_TOKEN ?? ''),
})
class GetProfileFrame extends JinFrame {}
const frame = GetProfileFrame.of();
// Sends Authorization: Bearer <current token>.
const reply = await frame._execute();
console.log(reply.data);
// Override the token for this call without updating the shared provider.
await frame._execute({ dynamicAuth: 'per-request-access-token' });ApiKeyProvider(key, keyName, location) accepts a string or synchronous key supplier.
BasicAuthProvider(key) and BearerTokenProvider(key) specialize its Authorization header.
Basic keys must already be Base64-encoded, for example new BasicAuthProvider(() => btoa("user:pass")).
Token priority is dynamicAuth → provider key → decorator authorization.
BearerTokenProvider adds Bearer unless already present; without a token it adds no header.
An explicit Authorization header or call-level auth bypasses security providers.
By default, _execute() returns 2xx responses and throws JinRespError for other statuses.
Pass validateStatus to _execute() or _create() to choose which statuses to accept.
It is not a decorator option.
import { JinRespError, validateStatus } from 'jin-frame';
try {
// Accept 200 and 404; other HTTP statuses still throw JinRespError.
const reply = await frame._execute({
validateStatus: validateStatus(200, 404),
});
// A 404 is returned here as a normal response, not caught below.
console.log(reply.status, reply.data);
} catch (error) {
// Propagate errors that are not wrapped by jin-frame.
if (!(error instanceof JinRespError)) {
throw error;
}
// Check whether the error includes an HTTP response.
const response = error.resp;
if (response === undefined) {
// Inspect the original cause of a network, hook, or deserialization failure.
console.error(error.message, error.cause);
} else {
// Read the actual response, for example when its status was rejected.
console.error(response.status, response.data, response.headers);
}
}validateStatus: () => true accepts every HTTP status, but cannot prevent network or
processing failures. An aborted caller signal also stops requests and retries, rejecting
with JinRespError; inspect cause for the abort reason. Without error.resp, no HTTP response is attached; use cause
to diagnose the failure rather than treating the error's status as a server response.
Override _validateStatus in a common BaseFrame to share HTTP acceptance across subclasses. The default returns ok (2xx). Call options take precedence, and the selected policy controls retries and final response acceptance.
import { Get, JinFrame } from 'jin-frame';
class BaseFrame extends JinFrame {
public override readonly _validateStatus = (ok: boolean, status: number) =>
ok || status === 404;
}
@Get({ host: 'https://api.example.com', path: '/users/1' })
class UserFrame extends BaseFrame {}
const frame = UserFrame.of();
await frame._execute();
await frame._create()();
// A call-level policy takes precedence over the inherited member.
await frame._execute({ validateStatus: (ok) => ok });Member overrides change runtime behavior only. Without a call-level validator, return types still select declared 2xx responses. Users accepting additional statuses through an override are responsible for handling that type difference. Use validateStatus(200, 404) in call options when you need status-aware return type inference.
- Node.js >=22.23.4
- TypeScript >= 5.0
experimentalDecoratorsandemitDecoratorMetadataenabled intsconfig.json
Full documentation: https://imjuni.github.io/jin-frame/
- Getting Started
- Inheritance
- Builder Pattern
- URL Template (RFC 6570)
- Form / File Upload
- Retry
- Authorization
- Validation
- Naming Convention
If you find jin-frame useful, please consider starring the repository or sponsoring its development.
Your support is greatly appreciated!
This software is licensed under the MIT.
{ "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true } }