Skip to content

TypeScript support & Validators

Choose a tag to compare

@DimaCrafter DimaCrafter released this 22 Jun 19:50
· 11 commits to master since this release

TypeScript support:

  • Built-in TypeScript runner using ts-node
  • Database connection experimental typings
  • Some core typings

To enable TypeScript support install ts-node and typescript packages and add "typescript": true to your config.json.

Validators:

  • Basic experimental validator

Special Data<Validated> and Query<Validated> types are designed to be used as action parameters in any combination. Will be documented soon (I hope). Syntax can be changed in later releases.

Example validator class:

export class TestPayload extends Validated {
    // If this.data.a not passed, ValidationError will be returned.
    @whenEmpty('Parameter is required', 'a')
    // If this getter will throw error, ValidationError with original error's message will be returned.
    // You can replace "undefined" with desired response message.
    @whenError(undefined, 'a')
    get asyncOne () {
        if (this.raw.a == 'fail') {
            throw new Error('some error');
        }
        // If getter returns Promise, it will be awaited before calling getter below
        return sleep(1000, this.raw.a);
    }

    get asyncDependent () {
        // this.ctx contains awaited values of getters/fields above
        return `Wrapped: ${this.ctx.asyncOne**2}`;
    }
}

Usage:

import { Data, HttpController, Query } from 'dc-api-core'
import { TestPayload } from '../validators/test'

export class Test extends HttpController {
    // Core will call action with validated data only after successfully awaiting all getters/fields.
    // So, if payload is { a: 42 } someData will be equal to { asyncOne: 42, asyncDependent: 'Wrapped: 1764' }
    invokeMe (someData: Data<TestPayload>) {
        return { receivedData: someData.asyncDependent };
    }
}

Bundle generator:

Use dc-api-core --ts-build to create bundle. Available only within TypeScript setup.

Optional flag Description
--keep-uws Keep all versions of uWS.js binaries (not only for current environment)
--sourcemap Generate source map
--no-minify Disable bundle minification

Misc:

  • Removed legacy config key supportOldCase
  • Loaders refactoring
  • Exposed uWS application (dc-api-core/app)
  • Removed unused init command
  • Development mode fixes
  • Small controller's context fixes
  • Added sleep utility (dc-api-core/utils)