Getting To Start
Installation
npm
sh
npm install jin-frame --save
yarn
sh
yarn add jin-frame --save
pnpm
sh
pnpm add jin-frame --save
Requirements
- TypeScript
- Decorator
- Enable the
experimentalDecorators
andemitDecoratorMetadata
options in yourtsconfig.json
.
- Enable the
jsonc
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
// enable experimentalDecorators, emitDecoratorMetadata for using decorator
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
},
}
Quick Example
ts
@Get({ host: 'https://pokeapi.co/api/v2/pokemon/:name' })
export class PokemonFrame extends JinFrame {
@Param()
declare public readonly name: string;
@Query()
declare public readonly tid: string;
}
Customization Examples
You can apply different settings for each endpoint.
ts
@Get({
host: 'https://pokeapi.co',
path: '/api/v2/pokemon',
timeout: 10_000, // 10s timeout
})
class PokemonPagingFrame extends JinFrame {
@Query()
declare public readonly limit: number;
@Query()
declare public readonly offset: number;
}
@Get({
host: 'https://pokeapi.co',
path: '/api/v2/pokemon/:name',
timeout: 2_000, // 2s timeout
retry: { max: 3, interval: 1000 }, // retry up to 3 times, 1s interval
})
export class PokemonDetailFrame extends JinFrame {
@Param()
declare public readonly name: string;
}
Why declare public readonly
?
When defining Frame classes in jin-frame, fields typically use declare public readonly
.
ts
@Get({ path: '/api/v2/pokemon/:name' })
class PokemonFrame extends JinFrame {
@Param()
declare public readonly name: string;
@Query()
declare public readonly limit?: number;
}
Reasons
declare
- In TypeScript, the
declare
keyword means “this field has no value assigned directly, but only its type is declared.” - The actual value is injected at runtime by
jin-frame
, so there’s no need to initialize it inside the class.
- In TypeScript, the
public
- Explicitly specifies that the API field is an input value accessible from outside.
readonly
- Ensures immutability since the value should not change once defined.
- Example:
@Param() name
is injected at creation time and will not change during execution.
Summary
declare public readonly
ensures type safety and immutability, while letting jin-frame populate values automatically at runtime. This is the idiomatic pattern in jin-frame, and it is recommended to always use this syntax when defining fields.