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" }