Registry / testing / rest-in-model

rest-in-model

JSON →
library2.1.1jsnpmunverified

RestInModel is a TypeScript-first model-based REST consumer library that maps API responses directly to class instances via decorators. Version 2.1.1 supports ESM and CJS, with no breaking changes from v2.0. It differentiates by allowing declarative API model definitions with automatic type coercion, relation handling, and base URL configuration. Ideal for structured API clients where type safety and model transformation are priorities.

npm install rest-in-model
INSTALL
IMPORT
SIG · REST-IN-MODEL
R
rest-in-model
testingjavascriptv2.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RestInModel
import { RestInModel } from 'rest-in-model'
const RestInModel = require('rest-in-model')
Default export is not available; use named import. CommonJS require works but destructure.
Model
import { Model } from 'rest-in-model'
import Model from 'rest-in-model'
Model is a named export, not default.
Field
import { Field } from 'rest-in-model'
const { Field } = require('rest-in-model')
CommonJS require is fine but less common in TypeScript projects.
HasMany
import { HasMany } from 'rest-in-model'
import { HasMany } from 'rest-in-model/decorators'
All decorators are exported from the main package, no subpath exports.

Shows model definition with Field and HasMany decorators, and fetching related models via async method.

import 'reflect-metadata'; import { RestInModel, Model, Field, HasMany } from 'rest-in-model'; @Model({ baseUrl: 'https://api.example.com' }) class User extends RestInModel { @Field() id!: number; @Field('full_name') name!: string; @HasMany('Post') posts!: Post[]; } @Model({ baseUrl: 'https://api.example.com' }) class Post extends RestInModel { @Field() id!: number; @Field() title!: string; } async function main() { const user = new User({ id: 1, full_name: 'John' }); const posts = await user.posts.all(); console.log(posts); } main().catch(console.error);
Debug
Known issues
gotchaDecorators require experimentalDecorators: true in tsconfig.json
fix
Add 'experimentalDecorators': true to your tsconfig.json compilerOptions.
affects: >=2.0.0
gotchaField decorator uses the property name as default JSON key; use @Field('api_key') to map different names.
fix
Explicitly pass the JSON key as first argument to @Field.
affects: >=2.0.0
breakingIn v2.0, RestInModel constructor no longer accepts raw data directly; use Model.create() or new Model(data).
fix
Use new MyModel(data) or MyModel.create(data) instead of MyModel(data).
affects: >=2.0.0
deprecatedThe method fetch() is deprecated; use action() or resource() instead.
fix
Replace fetch() with action() or resource() methods.
affects: >=2.1.0
gotchaHasMany relation expects the related model class name as a string, not the class itself.
fix
Use @HasMany('Post') rather than @HasMany(Post).
affects: >=2.0.0
gotchaRequires reflect-metadata polyfill; not importing it causes 'reflect.getMetadata' is not a function' error.
fix
Import 'reflect-metadata' at the top of your entry file.
affects: >=1.0.0
Errors
Common errors & fixes
reflect-metadata is not a function or TypeError: Reflect.getMetadata is not a function
Missing import of 'reflect-metadata' package
fix
Add import 'reflect-metadata' at the top of your application entry point.
Decorators are not valid here or Experimental support for decorators is a feature that is subject to change
tsconfig.json missing 'experimentalDecorators': true
fix
Set 'experimentalDecorators': true in compilerOptions of tsconfig.json.
Cannot find module 'rest-in-model' or 'rest-in-model' has no exported member 'Model'
Using wrong import syntax or package not installed
fix
Install the package via npm install rest-in-model and use named imports: import { Model } from 'rest-in-model'.
UnhandledPromiseRejectionWarning: TypeError: this.posts.all is not a function
HasMany relation not properly set up or model class not decorated with @Model
fix
Ensure @Model decorator is applied to the parent model and @HasMany uses the correct related model name string.
Upgrade
Version history
2.1.1latest on npm
Audit
Dependencies
reflect-metadataoptionalRequired for decorator metadata reflection in TypeScript
Agent activity
8 hits · last 30 days
node
8
Resources
rest-in-model — npm install rest-in-model · libregistry