ditojs / dito

Dito.js is a declarative and modern web framework with a focus on API driven development, based on Objection.js, Koa.js and Vue.js – Developed at Lineto by Jürg Lehni and made available by Lineto in 2018 under the MIT license
https://lineto.com
MIT License
82 stars 8 forks source link

Typescript types #24

Closed puckey closed 4 years ago

puckey commented 4 years ago

I was looking at how Typescript types might be approached in the library using the Application configuration object as a starting point. One nicety is that you can easily import the types from underlying libraries. See for example the app.cors configuration..

Some other things I noticed while doing the initial typing:

import type { Config } from 'knex';
import type { KnexSnakeCaseMappersFactory } from 'objection';
import type { Options as CorsOptions } from '@koa/cors';
import type { CompressOptions } from 'koa-compress';
import type koaSession from 'koa-session';

export interface Configuration {
  /**
   * @defaultValue `production`
   */
  env?: 'production' | 'development';
  /**
   * The server configuration
   */
  server: {
    /**
     * The ip address or hostname used to serve requests
     */
    host: string;
    /**
     * The port to listen on for connections
     */
    port: number;
  };
  /**
   * Logging options
   */
  log: {
    /**
     * Enable logging requests to console by passing `true` or pick between
     * 'console' for logging to console and 'file' for logging to file
     * @defaultValue `false`
     */
    requests?: boolean | 'console' | 'file';
    /**
     * Whether to output route (Controller) logs
     * @defaultValue `false`
     */
    routes?: boolean;
    /**
     * Whether to log relation mappings
     * @defaultValue `false`
     */
    relations?: boolean;
    /**
     * Whether to log json schema
     * @defaultValue `false`
     */
    schema?: boolean;
    /**
     * Whether to log sql queries
     * @defaultValue `false`
     */
    sql?: boolean;
  };
  app?: {
    /**
     * Whether to normalize paths from camel case to kebab case
     * @defaultValue `false`
     */
    normalizePaths?: boolean;
    /**
     * @defaultValue `false`
     */
    proxy?: boolean;
    /**
     * Whether to include X-Response-Time header in responses
     * @defaultValue `true`
     */
    responseTime?: boolean;
    /**
     * Whether to use koa-helmet middleware which provides important security
     * headers to make your app more secure by default.
     * @defaultValue `true`
     */
    helmet?: boolean;
    /**
     * Enable or configure Cross-Origin Resource Sharing (CORS)
     * @defaultValue `true`
     */
    cors?: boolean | CorsOptions;
    /**
     * Enable or configure server response compression
     * @defaultValue `true`
     */
    compress?: boolean | CompressOptions;
    /**
     * Enable ETag headers in server responses
     * @defaultValue `true`
     */
    etag?: boolean;
    /**
     * @defaultValue `false`
     */
    session?: boolean | ({ modelClass: 'string' } & koaSession.opts);
    /**
     * Enable passport authentication middleware
     * @defaultValue `false`
     */
    passport?: boolean;

    // csrf: boolean,            // TODO: Implement

    /**
     * Keys used for session
     */
    keys?: string[];
  };
  knex: {
    /**
     * The database client to use - see http://knexjs.org/#Installation-client
     */
    client: Config['client'];
    /**
     * The database connection configuration - see http://knexjs.org/#Installation-client
     */
    connection: Config['connection'];
    /**
     * @defaultValue `false`
     */
    normalizeDbNames?: boolean | Parameters<KnexSnakeCaseMappersFactory>;
    /**
     * Whether to replace undefined keys with NULL instead of DEFAULT
     * @defaultValue `false`
     */
    useNullAsDefault?: Config['useNullAsDefault'];
  };
}
puckey commented 4 years ago

Moving this to WIP pr: https://github.com/ditojs/dito/pull/25