@trapi/metadata 📚

This is a library to generate decorator metadata according to a generic specification, which can easily be extended.
Therefore, you can use your own decorators or decorators declared by third party libraries.
Please read the CHANGELOG.md
in the repository for breaking changes.
Table of Contents
Installation
npm install --save @trapi/metadata
Configuration
Metadata
The metadata configuration object (Top-Level) is the main configuration object of this library
and can be defined according the following type scheme:
import {Decorator, Cache} from '@trapi/metadata';
export interface Config {
entryFile: string | string[];
ignore?: string[];
cache?: string | boolean | Partial<Cache.Config>;
decorator?: Decorator.Config;
}
Cache
The Cache can be configured by providing different kind of values:
- boolean:
true
: Cache file will be saved to process.cwd() with generated hash file name and the cache will be cleared at a 10 percent chance.false
: Cache is disabled.
- string Cache will be saved to
value
directory with generated hash file name ... - object: obda
export interface Config {
enabled?: boolean,
directoryPath?: string,
fileName?: string,
clearAtRandom?: boolean
}
Decorator
The decorator configuration is relative complex and is not described in detail here yet.
Please read the source code
and the according tests
for better understanding.
- Tests:
test/unit/decorator/mapper/index.spec.ts
test/unit/decorator/representation/index.spec.ts
- Code:
If you are the author (or contributor) of a TypeScript Decorator API library and need help to set things up, feel free to open an Issue and ask for help.
export interface Config {
library?: ConfigLibrary;
internal?: TypeRepresentationConfig;
map?: Partial<TypeRepresentationMap>;
}
Limitations
At the moment only the following TypeScript UtilityTypes are supported:
- NonNullable
- Omit
- Partial
- Readonly
- Record
- Required
- Pick
Usage
import {createMetadata, Output} from "@trapi/metadata";
import * as path from "path";
import * as process from "process";
const metadata : Output = createMetadata({
entryFile: path.join(process.cwd(), 'src', 'controllers', '**', '*.ts'),
ignore: ['**/node_modules/**'],
cache: {
enabled: true,
directoryPath: path.join(process.cwd(), 'writable')
},
decorator: {
internal: true,
library: ['decorators-express', 'typescript-rest']
}
});
console.log(metadata);
Structure
coming soon
Credits
Parts of this package @trapi/metadata code rest on a continued fork repository (typescript-swagger) of the
typescript-rest-swagger library of thiagobustamante
and was also inspired by the tsoa library of lukeatury.