diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..b2fee43 --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,164 @@ +declare module 'epilogue' { + import { Express, Request, Response } from 'express'; + import { Instance, Sequelize } from 'sequelize'; + + function initialize(options: InitializeOptions): Epilogue; + + function resource(options: ResourceOptions): Resource; + + interface Epilogue { + } + + class Resource implements ResourceOptions { + app?: Express; + sequelize?: Sequelize; + model: any; + endpoints?: string[]; + actions?: Array<'create' | 'read' | 'update' | 'delete' | 'list'>; + include?: any[]; + pagination?: boolean; + updateMethod?: 'POST' | 'PUT' | 'PATCH'; + search?: SearchOptions | SearchOptions[]; + sort?: SortOptions; + reloadInstances?: boolean; + associations?: boolean; + excludeAttributes?: string[]; + readOnlyAttributes?: string[]; + associationOptions: any; + attributes: any; + projects: any; + controllers: any; + create: Controllers.create; + read: Controllers.read; + update: Controllers.update; + delete: Controllers.update; + list: Controllers.list; + all: Controllers.all; + associationsInfo: any; + use: any; + } + + export namespace Controllers { + class base { + static hooks: string[]; + static milestones: string[]; + + start: Hook; + auth: Hook; + data: Hook; + send: Hook; + complete: Hook; + + constructor(args: any); + } + + class create extends base { + write: Hook; + + constructor(args: any); + } + + class list extends base { + fetch: Hook; + + constructor(args: any); + } + + class read extends base { + fetch: Hook; + + constructor(args: any); + } + + class update extends base { + fetch: Hook; + write: Hook; + + constructor(args: any); + } + + class all extends base { + fetch: Hook; + write: Hook; + + constructor(args: any); + } + } + + interface Hook { + before: (hook: Handler) => void; + after: (hook: Handler) => void; + + (hook: Handler): void + } + + type Handler = (req: Request, res: Response, context: Context) => Promise + + export interface Context { + skip: Promise; + stop: Promise; + continue: Promise; + error: Promise; + criteria: any; + attributes: any; + options: any; + instance: Instance | Instance[] | any; + } + + export namespace Errors { + class EpilogueError { + constructor(status: any, message: any, errors: any, cause: any); + } + + class BadRequestError { + constructor(message: any, errors: any, cause: any); + } + + class ForbiddenError { + constructor(message: any, errors: any, cause: any); + } + + class NotFoundError { + constructor(message: any, errors: any, cause: any); + } + + class RequestCompleted { + } + } + + interface InitializeOptions { + app: Express; + sequelize: Sequelize; + base?: string; + updateMethod?: 'POST' | 'PUT' | 'PATCH'; + } + + interface ResourceOptions { + app?: Express; + sequelize?: Sequelize; + model: any; + endpoints?: string[]; + actions?: Array<'create' | 'read' | 'update' | 'delete' | 'list'>; + include?: any[]; + pagination?: boolean; + updateMethod?: 'POST' | 'PUT' | 'PATCH'; + search?: SearchOptions | SearchOptions[]; + sort?: SortOptions; + reloadInstances?: boolean; + associations?: boolean; + excludeAttributes?: string[]; + readOnlyAttributes?: string[]; + } + + interface SearchOptions { + operator: '$like' | '$ilike' | '$iLike' | '$notLike' | '$notILike' | '$ne' | '$eq' | '$not' | '$gte' | '$gt' | '$lte' | '$lt'; + param: string; + attributes: string[]; + } + + interface SortOptions { + param: string; + attributes: string[]; + default: string; + } +} diff --git a/package.json b/package.json index 0311c6b..9a91131 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ } ], "main": "lib/", + "types": "lib/index.d.ts", "scripts": { "jshint": "jshint ./lib ./tests", "mocha": "mocha --check-leaks -R spec -u exports tests/*.test.js tests/**/*.test.js", @@ -39,6 +40,9 @@ "lodash": "^4.8.2" }, "devDependencies": { + "@types/body-parser": "^1.16.5", + "@types/express": "^4.0.37", + "@types/sequelize": "^4.0.74", "body-parser": "^1.13.3", "chai": "^3.2.0", "express": "^4.13.3", @@ -48,6 +52,6 @@ "request": "^2.60.0", "restify": "^4.0.3", "sequelize": "^3.5.1", - "sqlite3": "^3.0.9" + "sqlite3": "^3.1.12" } }