mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-02 06:29:40 +08:00
create schwifty definitions
This commit is contained in:
83
types/schwifty/index.d.1.ts
Normal file
83
types/schwifty/index.d.1.ts
Normal 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
70
types/schwifty/index.d.ts
vendored
Normal 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 };
|
||||
}
|
||||
}
|
||||
6
types/schwifty/package.json
Normal file
6
types/schwifty/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"objection": "^1.1.8"
|
||||
}
|
||||
}
|
||||
38
types/schwifty/schwifty-tests.ts
Normal file
38
types/schwifty/schwifty-tests.ts
Normal 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}!`);
|
||||
})();
|
||||
14
types/schwifty/test/dog.ts
Normal file
14
types/schwifty/test/dog.ts
Normal 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;
|
||||
}
|
||||
18
types/schwifty/test/plugin.ts
Normal file
18
types/schwifty/test/plugin.ts
Normal 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);
|
||||
}
|
||||
};
|
||||
17
types/schwifty/tsconfig.json
Normal file
17
types/schwifty/tsconfig.json
Normal 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"]
|
||||
}
|
||||
1
types/schwifty/tslint.json
Normal file
1
types/schwifty/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user