create schwifty definitions

This commit is contained in:
Ozum
2018-05-19 12:26:06 +03:00
parent 0e3cd2e17c
commit 0496d0f94f
8 changed files with 247 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
// Type definitions for schwifty 4.0.
// Project: https://github.com/hapipal/schwifty
// Definitions by: ozum <https://github.com/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<Model> | 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<RegistrationOptions> & SchwiftyExtras;
export const plugin: Plugin<RegistrationOptions>;
//export = Schwifty;
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Hapi Decorations +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */
/**
* Merge decorations into hapi objects.
*/
declare module "hapi" {
interface Server {
schwifty: (
config:
| Model
| Array<Model>
| {
knex: Knex | Knex.Config;
models: Array<Model>;
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 };
}
}

70
types/schwifty/index.d.ts vendored Normal file
View File

@@ -0,0 +1,70 @@
// Type definitions for schwifty 4.0
// Project: https://github.com/hapipal/schwifty
// Definitions by: ozum <https://github.com/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<RegistrationOptions>;
/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ 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 };
}
}

View File

@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"objection": "^1.1.8"
}
}

View File

@@ -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}!`);
})();

View File

@@ -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;
}

View File

@@ -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);
}
};

View File

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

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }