1.0.8 • Published 4 years ago

autogentypes v1.0.8

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

autogentypes

Generate types & schemas from types & schemas.

Currently supports:

Check example project in src/example

If you use joi->schema4:
npm i joi @hapi/joi@npm:joi json-joi-converter

Idea

Keep single source of truth for all types & schemas.
Currently based on joi.

Example

from:

import * as Joi from 'joi';

export const WorkerData = Joi.object({
    uuid: Joi.string(),
    workingDir: Joi.string(),
    currentJobUUID: Joi.alternatives(
        Joi.string(),
        Joi.valid(false)
    ),
}).options({
    presence: 'required'
});

to:

import * as Joi from 'joi';

export const WorkerData = Joi.object({
    uuid: Joi.string(),
    workingDir: Joi.string(),
    currentJobUUID: Joi.alternatives(
        Joi.string(),
        Joi.valid(false)
    ),
}).options({
    presence: 'required'
});

export type WorkerData = {
    "uuid": string,
    "workingDir": string,
    "currentJobUUID": (string | false),
};

export const WorkerDataSchema4 = {
    "type": "object",
    "properties": {
        "uuid": {
            "type": "string"
        },
        "workingDir": {
            "type": "string"
        },
        "currentJobUUID": {
            "type": "alternatives"
        }
    }
};

Api

Create script file in your project:

import { convertGlobSync } from "autogentypes/lib/cli";

convertGlobSync('src/**/*.gen.js', {
    outputFilePath: (orig, prefer) => prefer.replace('.gen.ts', '.ts')
});

Options of convertGlobSync:

{
    outputFilePath?: (originalFilePath: string, preferedFilePath: string) => string,

    // true by default
    prettyFmt?: boolean,

    // 'append' by default
    // append - append original file with generated code
    // export - output only generated code
    convertMode?: 'append' | 'export',

    // [ 'joi' ] by default
    convertFrom?: 'joi'[] | 'joi',

    // [ 'ts', 'schema4' ] by default
    convertTo?: ('ts' | 'schema4')[] | 'ts' | 'schema4',

    // by default:  
    // joi->ts does not change name  
    // joi->schema4 append 'Schema4' to name  
    exportedSymbolNameTransform?: (originalName: string, params: {
        from: string // eg 'joi'
        to: string // eg 'ts'
    }) => string,
}
1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago