diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..b96383a --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,117 @@ +// Type definitions for @hapipal/schwifty 6 +// Project: https://github.com/hapipal/schwifty +// Definitions by: ozum +// timcosta +// damusix +// TypeScript Version: 4 + +import * as Joi from 'joi'; +import { Plugin } from '@hapi/hapi'; +import { Knex } from 'knex'; +import { Model as ObjectionModel } from 'objection'; + +export type ModelClass = typeof Model | typeof ObjectionModel; + +export class Model extends ObjectionModel { + static getJoiSchema(patch?: boolean): Joi.Schema; + static field(name: string): Joi.Schema; + static joiSchema: Joi.Schema; + static joiSchemaPatch: Joi.Schema; +} + +export interface RegistrationOptions { + knex?: Knex | Knex.Config | undefined; + models?: ModelClass[] | string | undefined; + migrationsDir?: string | undefined; + teardownOnStop?: boolean | undefined; + migrateOnStart?: boolean | 'latest' | 'rollback' | undefined; +} + +export function assertCompatible( + ModelA: typeof Model, + ModelB: typeof Model, + message?: string +): void | Error; +export const plugin: Plugin; + +export const migrationsStubPath: string; + +export const sandbox: Symbol; +export const bindKnex: Symbol; +export interface RegisteredModels { + [key: string]: ModelClass; +} + +/* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hapi Decorations + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ + +/** + * Server decorator for getting models scoped to the + * current plugin realm using `server.models()`, + * or models registered on the server using `server.models(true)`, + * or models scoped to plugin namespace using `server.models('namespace')`. + * + * + * + * This interface can be overwritten to modify what you want your namespace + * to actually return. For example: + * + * @example + * + * declare module '@hapipal/schwifty' { + * type AuthModels = { + * Members: Schwifty.Model + * Admin: Schwifty.Model + * Mananger: Schwifty.Model + * } + * + * type OathModels = { + * Witness: Schwifty.Model + * Promissory: Schwifty.Model + * CrownCourt: Schwifty.Model + * } + * + * interface SchwiftyDecorator { + * (namespace: 'auth'): AuthModels + * (namespace: 'oath'): OathModels + * } + * } + * + */ +export interface SchwiftyDecorator { + + (all?: boolean): RegisteredModels + (namespace?: string): RegisteredModels +} + + + +declare module '@hapi/hapi' { + + interface Server { + schwifty: ( + config: + | ModelClass + | ModelClass[] + | { + knex: Knex | Knex.Config; + models: ModelClass[]; + migrationsDir: string; + } + ) => void; + knex: () => Knex; + models: SchwiftyDecorator; + + } + + interface Request { + knex: () => Knex; + models: SchwiftyDecorator; + } + + interface ResponseToolkit { + knex: () => Knex; + models: SchwiftyDecorator; + } +} diff --git a/package.json b/package.json index a33f490..5714a94 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "6.2.0", "description": "A model layer for hapi integrating Objection ORM", "main": "lib/index.js", + "types": "lib/index.d.ts", "scripts": { "test": "lab -a @hapi/code -t 100 -L", "coveralls": "lab -r lcov | coveralls"