From 0496d0f94f491809a07b4f0a4cf88fdeb8f91b48 Mon Sep 17 00:00:00 2001 From: Ozum Date: Sat, 19 May 2018 12:26:06 +0300 Subject: [PATCH 1/3] create schwifty definitions --- types/schwifty/index.d.1.ts | 83 ++++++++++++++++++++++++++++++++ types/schwifty/index.d.ts | 70 +++++++++++++++++++++++++++ types/schwifty/package.json | 6 +++ types/schwifty/schwifty-tests.ts | 38 +++++++++++++++ types/schwifty/test/dog.ts | 14 ++++++ types/schwifty/test/plugin.ts | 18 +++++++ types/schwifty/tsconfig.json | 17 +++++++ types/schwifty/tslint.json | 1 + 8 files changed, 247 insertions(+) create mode 100644 types/schwifty/index.d.1.ts create mode 100644 types/schwifty/index.d.ts create mode 100644 types/schwifty/package.json create mode 100644 types/schwifty/schwifty-tests.ts create mode 100644 types/schwifty/test/dog.ts create mode 100644 types/schwifty/test/plugin.ts create mode 100644 types/schwifty/tsconfig.json create mode 100644 types/schwifty/tslint.json diff --git a/types/schwifty/index.d.1.ts b/types/schwifty/index.d.1.ts new file mode 100644 index 0000000000..0580e5e1fb --- /dev/null +++ b/types/schwifty/index.d.1.ts @@ -0,0 +1,83 @@ +// Type definitions for schwifty 4.0. +// Project: https://github.com/hapipal/schwifty +// Definitions by: ozum +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +// HELP WANTED: If possible, find a better way to define Server.models, Request.models and ResponseToolkit.models +// They are dynamic types extended from SchwiftyModel. + +import * as Objection from "objection"; +import * as Joi from "joi"; +import { + Server, + Request, + ResponseToolkit, + Plugin, + ServerRegisterPluginObject +} from "hapi"; +import * as Knex from "knex"; + +type Model = typeof SchwiftyModel | typeof Objection.Model; + +declare class SchwiftyModel extends Objection.Model { + static getJoiSchema(patch?: boolean): Joi.Schema; + joiSchema: Joi.Schema; +} + +interface RegistrationOptions { + knex?: Knex | Knex.Config; + models?: Array | string; + migrationsDir?: string; + teardownOnStop?: boolean; + migrateOnStart?: boolean | "latest" | "rollback"; +} + +interface SchwiftyExtras { + assertCompatible: ( + ModelA: typeof SchwiftyModel, + ModelB: typeof SchwiftyModel, + message?: string + ) => void | Error; + Model: typeof SchwiftyModel; +} + +declare const Schwifty: Plugin & SchwiftyExtras; + +export const plugin: Plugin; + +//export = Schwifty; + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hapi Decorations + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ + +/** + * Merge decorations into hapi objects. + */ +declare module "hapi" { + interface Server { + schwifty: ( + config: + | Model + | Array + | { + knex: Knex | Knex.Config; + models: Array; + migrationsDir: string; + } + ) => void; + knex: () => Knex; + models: (all?: boolean) => { [key: string]: typeof SchwiftyModel }; + } + + interface Request { + knex: () => Knex; + models: (all?: boolean) => { [key: string]: typeof SchwiftyModel }; + } + + interface ResponseToolkit { + knex: () => Knex; + models: (all?: boolean) => { [key: string]: typeof SchwiftyModel }; + } +} diff --git a/types/schwifty/index.d.ts b/types/schwifty/index.d.ts new file mode 100644 index 0000000000..e95de1af55 --- /dev/null +++ b/types/schwifty/index.d.ts @@ -0,0 +1,70 @@ +// Type definitions for schwifty 4.0 +// Project: https://github.com/hapipal/schwifty +// Definitions by: ozum +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +// HELP NEEDED: If possible, find a better way to define Server.models, Request.models and ResponseToolkit.models +// They are dynamic types extended from SchwiftyModel. + +import * as Objection from "objection"; +import * as Joi from "joi"; +import { Server, Request, ResponseToolkit, Plugin } from "hapi"; +import * as Knex from "knex"; + +export type ModelClass = typeof Model | typeof Objection.Model; + +export class Model extends Objection.Model { + static getJoiSchema(patch?: boolean): Joi.Schema; + joiSchema: Joi.Schema; +} + +export interface RegistrationOptions { + knex?: Knex | Knex.Config; + models?: ModelClass[] | string; + migrationsDir?: string; + teardownOnStop?: boolean; + migrateOnStart?: boolean | "latest" | "rollback"; +} + +export function assertCompatible( + ModelA: typeof Model, + ModelB: typeof Model, + message?: string +): void | Error; + +export const plugin: Plugin; + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hapi Decorations + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ + +/** + * Merge decorations into hapi objects. + */ +declare module "hapi" { + interface Server { + schwifty: ( + config: + | ModelClass + | ModelClass[] + | { + knex: Knex | Knex.Config; + models: ModelClass[]; + migrationsDir: string; + } + ) => void; + knex: () => Knex; + models: (all?: boolean) => { [key: string]: typeof Model }; + } + + interface Request { + knex: () => Knex; + models: (all?: boolean) => { [key: string]: typeof Model }; + } + + interface ResponseToolkit { + knex: () => Knex; + models: (all?: boolean) => { [key: string]: typeof Model }; + } +} diff --git a/types/schwifty/package.json b/types/schwifty/package.json new file mode 100644 index 0000000000..37b2fc1558 --- /dev/null +++ b/types/schwifty/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "objection": "^1.1.8" + } +} diff --git a/types/schwifty/schwifty-tests.ts b/types/schwifty/schwifty-tests.ts new file mode 100644 index 0000000000..afdcda5441 --- /dev/null +++ b/types/schwifty/schwifty-tests.ts @@ -0,0 +1,38 @@ +import * as Hapi from "hapi"; +import * as Joi from "joi"; +import * as Schwifty from "schwifty"; +import DogClass from "./test/dog"; + +(async () => { + const server = new Hapi.Server({ port: 3000 }); + + await server.register(Schwifty); + await server.register({ + plugin: Schwifty.plugin, + options: { + knex: { + client: "sqlite3", + useNullAsDefault: true, + connection: { + filename: ":memory:" + } + } + } + }); + + Schwifty.assertCompatible(DogClass, DogClass); + // Register a model with schwifty... + + server.schwifty(DogClass); + + await server.initialize(); + const Dog: typeof DogClass = server.models().Dog; + + await Dog.query().insert({ name: "Guinness" }); + + // ... then start the server! + + await server.start(); + + console.log(`Now, go find some dogs at ${server.info.uri}!`); +})(); diff --git a/types/schwifty/test/dog.ts b/types/schwifty/test/dog.ts new file mode 100644 index 0000000000..33fe2402e3 --- /dev/null +++ b/types/schwifty/test/dog.ts @@ -0,0 +1,14 @@ +import * as Joi from "joi"; +import * as Schwifty from "schwifty"; + +export default class Dog extends Schwifty.Model { + static tableName = "Dog"; + + joiSchema: Joi.Schema = Joi.object({ + id: Joi.number(), + name: Joi.string() + }); + + id?: number; + name?: string; +} diff --git a/types/schwifty/test/plugin.ts b/types/schwifty/test/plugin.ts new file mode 100644 index 0000000000..0b3e9b189b --- /dev/null +++ b/types/schwifty/test/plugin.ts @@ -0,0 +1,18 @@ +import * as Hapi from "hapi"; +import * as Schwifty from "schwifty"; +import DogModel from "./dog"; + +exports.plugin = { + register: async ( + server: Hapi.Server, + options: { Model: Schwifty.Model } + ) => { + await server.register(Schwifty); + + if (options.Model) { + Schwifty.assertCompatible(options.Model, DogModel); + } + + server.schwifty(options.Model || DogModel); + } +}; diff --git a/types/schwifty/tsconfig.json b/types/schwifty/tsconfig.json new file mode 100644 index 0000000000..ff73409893 --- /dev/null +++ b/types/schwifty/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "target": "es6", + "strictFunctionTypes": true + }, + "files": ["index.d.ts", "schwifty-tests.ts"] +} diff --git a/types/schwifty/tslint.json b/types/schwifty/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/schwifty/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 48fc5d3017122927680094e06d6b1e5041f1fb9f Mon Sep 17 00:00:00 2001 From: Ozum Date: Sat, 19 May 2018 12:52:04 +0300 Subject: [PATCH 2/3] delete package.json --- types/schwifty/index.d.1.ts | 83 -------------------------------- types/schwifty/package.json | 6 --- types/schwifty/schwifty-tests.ts | 8 +-- types/schwifty/test/plugin.ts | 2 +- types/schwifty/tsconfig.json | 7 ++- 5 files changed, 11 insertions(+), 95 deletions(-) delete mode 100644 types/schwifty/index.d.1.ts delete mode 100644 types/schwifty/package.json diff --git a/types/schwifty/index.d.1.ts b/types/schwifty/index.d.1.ts deleted file mode 100644 index 0580e5e1fb..0000000000 --- a/types/schwifty/index.d.1.ts +++ /dev/null @@ -1,83 +0,0 @@ -// Type definitions for schwifty 4.0. -// Project: https://github.com/hapipal/schwifty -// Definitions by: ozum -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.4 - -// HELP WANTED: If possible, find a better way to define Server.models, Request.models and ResponseToolkit.models -// They are dynamic types extended from SchwiftyModel. - -import * as Objection from "objection"; -import * as Joi from "joi"; -import { - Server, - Request, - ResponseToolkit, - Plugin, - ServerRegisterPluginObject -} from "hapi"; -import * as Knex from "knex"; - -type Model = typeof SchwiftyModel | typeof Objection.Model; - -declare class SchwiftyModel extends Objection.Model { - static getJoiSchema(patch?: boolean): Joi.Schema; - joiSchema: Joi.Schema; -} - -interface RegistrationOptions { - knex?: Knex | Knex.Config; - models?: Array | string; - migrationsDir?: string; - teardownOnStop?: boolean; - migrateOnStart?: boolean | "latest" | "rollback"; -} - -interface SchwiftyExtras { - assertCompatible: ( - ModelA: typeof SchwiftyModel, - ModelB: typeof SchwiftyModel, - message?: string - ) => void | Error; - Model: typeof SchwiftyModel; -} - -declare const Schwifty: Plugin & SchwiftyExtras; - -export const plugin: Plugin; - -//export = Schwifty; - -/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + Hapi Decorations + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ - -/** - * Merge decorations into hapi objects. - */ -declare module "hapi" { - interface Server { - schwifty: ( - config: - | Model - | Array - | { - knex: Knex | Knex.Config; - models: Array; - migrationsDir: string; - } - ) => void; - knex: () => Knex; - models: (all?: boolean) => { [key: string]: typeof SchwiftyModel }; - } - - interface Request { - knex: () => Knex; - models: (all?: boolean) => { [key: string]: typeof SchwiftyModel }; - } - - interface ResponseToolkit { - knex: () => Knex; - models: (all?: boolean) => { [key: string]: typeof SchwiftyModel }; - } -} diff --git a/types/schwifty/package.json b/types/schwifty/package.json deleted file mode 100644 index 37b2fc1558..0000000000 --- a/types/schwifty/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "private": true, - "dependencies": { - "objection": "^1.1.8" - } -} diff --git a/types/schwifty/schwifty-tests.ts b/types/schwifty/schwifty-tests.ts index afdcda5441..39d06c6f09 100644 --- a/types/schwifty/schwifty-tests.ts +++ b/types/schwifty/schwifty-tests.ts @@ -1,7 +1,7 @@ import * as Hapi from "hapi"; import * as Joi from "joi"; import * as Schwifty from "schwifty"; -import DogClass from "./test/dog"; +import DogModel from "./test/dog"; (async () => { const server = new Hapi.Server({ port: 3000 }); @@ -20,13 +20,13 @@ import DogClass from "./test/dog"; } }); - Schwifty.assertCompatible(DogClass, DogClass); + Schwifty.assertCompatible(DogModel, DogModel); // Register a model with schwifty... - server.schwifty(DogClass); + server.schwifty(DogModel); await server.initialize(); - const Dog: typeof DogClass = server.models().Dog; + const Dog: typeof DogModel = server.models().Dog; await Dog.query().insert({ name: "Guinness" }); diff --git a/types/schwifty/test/plugin.ts b/types/schwifty/test/plugin.ts index 0b3e9b189b..e2523f7161 100644 --- a/types/schwifty/test/plugin.ts +++ b/types/schwifty/test/plugin.ts @@ -5,7 +5,7 @@ import DogModel from "./dog"; exports.plugin = { register: async ( server: Hapi.Server, - options: { Model: Schwifty.Model } + options: { Model: typeof Schwifty.Model } ) => { await server.register(Schwifty); diff --git a/types/schwifty/tsconfig.json b/types/schwifty/tsconfig.json index ff73409893..d7117c5fd2 100644 --- a/types/schwifty/tsconfig.json +++ b/types/schwifty/tsconfig.json @@ -13,5 +13,10 @@ "target": "es6", "strictFunctionTypes": true }, - "files": ["index.d.ts", "schwifty-tests.ts"] + "files": [ + "index.d.ts", + "schwifty-tests.ts", + "test/dog.ts", + "test/plugin.ts" + ] } From cd3fa4c9f258b8422521c158fa4c8971f5dfa813 Mon Sep 17 00:00:00 2001 From: Ozum Date: Sat, 26 May 2018 06:59:39 +0300 Subject: [PATCH 3/3] add shcwifty package.json for external dependency --- types/schwifty/package.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 types/schwifty/package.json diff --git a/types/schwifty/package.json b/types/schwifty/package.json new file mode 100644 index 0000000000..01df4a95e3 --- /dev/null +++ b/types/schwifty/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "objection": "^1.1.9" + } +}