From 5380cf2a252452830789e2951fe2ee4ef791d5d1 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 5 Dec 2018 11:15:50 +0100 Subject: [PATCH 1/6] Copy mongoose-paginate declaration to mongoose-paginate-v2. --- types/mongoose-paginate-v2/index.d.ts | 51 ++++++++++++ .../mongoose-paginate-tests.ts | 71 +++++++++++++++++ types/mongoose-paginate-v2/tsconfig.json | 23 ++++++ types/mongoose-paginate-v2/tslint.json | 79 +++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 types/mongoose-paginate-v2/index.d.ts create mode 100644 types/mongoose-paginate-v2/mongoose-paginate-tests.ts create mode 100644 types/mongoose-paginate-v2/tsconfig.json create mode 100644 types/mongoose-paginate-v2/tslint.json diff --git a/types/mongoose-paginate-v2/index.d.ts b/types/mongoose-paginate-v2/index.d.ts new file mode 100644 index 0000000000..ad54786f53 --- /dev/null +++ b/types/mongoose-paginate-v2/index.d.ts @@ -0,0 +1,51 @@ +// Type definitions for mongoose-paginate 5.0.0 +// Project: https://github.com/edwardhotchkiss/mongoose-paginate +// Definitions by: Linus Brolin , simonxca +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +declare module 'mongoose' { + export interface PaginateOptions { + select?: Object | string; + sort?: Object | string; + populate?: Array | Array | Object | string; + lean?: boolean; + leanWithId?: boolean; + offset?: number; + page?: number; + limit?: number; + } + + export interface PaginateResult { + docs: Array; + total: number; + limit: number; + page?: number; + pages?: number; + offset?: number; + } + + interface PaginateModel extends Model { + paginate(query?: Object, options?: PaginateOptions, callback?: (err: any, result: PaginateResult) => void): Promise>; + } + + export function model( + name: string, + schema?: Schema, + collection?: string, + skipInit?: boolean): PaginateModel; + + export function model>( + name: string, + schema?: Schema, + collection?: string, + skipInit?: boolean): U; +} + +declare module 'mongoose-paginate' { + import mongoose = require('mongoose'); + var _: (schema: mongoose.Schema) => void; + export = _; +} diff --git a/types/mongoose-paginate-v2/mongoose-paginate-tests.ts b/types/mongoose-paginate-v2/mongoose-paginate-tests.ts new file mode 100644 index 0000000000..d55f8a454e --- /dev/null +++ b/types/mongoose-paginate-v2/mongoose-paginate-tests.ts @@ -0,0 +1,71 @@ +/** + * Created by Linus Brolin . + */ + +import { + Schema, + model, + PaginateModel, + PaginateOptions, + PaginateResult, + Document +} from 'mongoose'; +import mongoosePaginate = require('mongoose-paginate'); +import { Router, Request, Response } from 'express'; + + +//#region Test Models +interface User extends Document { + email: string; + username: string; + password: string; +} + +const UserSchema: Schema = new Schema({ + email: String, + username: String, + password: String +}); + +UserSchema.plugin(mongoosePaginate); + +interface UserModel extends PaginateModel {}; + +let UserModel: UserModel = model('User', UserSchema) as UserModel; +//#endregion + + +//#region Test Paginate +let router: Router = Router(); + +router.get('/users.json', function(req: Request, res: Response) { + let descending: boolean = true; + let options: PaginateOptions = {} as PaginateOptions; + options.select = 'email username'; + options.sort = { 'username': (descending ? -1 : 1) }; + options.populate = ''; + options.lean = true; + options.leanWithId = false; + options.offset = 0; + options.page = 1; + options.limit = 10; + + UserModel + .paginate({}, options, (err: any, value: PaginateResult) => { + if (err) { + console.log(err); + return res.status(500).send(err); + } + + console.log('total: ' + value.total); + console.log('limit: ' + value.limit); + console.log('page: ' + value.page); + console.log('pages: ' + value.pages); + console.log('offset: ' + value.offset); + console.log('docs: '); + console.dir(value.docs); + return res.json(value); + }); + +}); +//#endregion diff --git a/types/mongoose-paginate-v2/tsconfig.json b/types/mongoose-paginate-v2/tsconfig.json new file mode 100644 index 0000000000..3194368f11 --- /dev/null +++ b/types/mongoose-paginate-v2/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mongoose-paginate-tests.ts" + ] +} \ No newline at end of file diff --git a/types/mongoose-paginate-v2/tslint.json b/types/mongoose-paginate-v2/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/mongoose-paginate-v2/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +} From ad776bc550c59a14f387157c2ea1dc733790fe49 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 5 Dec 2018 11:40:07 +0100 Subject: [PATCH 2/6] Update declarations to correspond with mongoose-paginate-v2 module. --- types/mongoose-paginate-v2/index.d.ts | 23 ++++++++++++++++--- ...tests.ts => mongoose-paginate-v2-tests.ts} | 22 ++++++++++++++---- types/mongoose-paginate-v2/tsconfig.json | 4 ++-- 3 files changed, 39 insertions(+), 10 deletions(-) rename types/mongoose-paginate-v2/{mongoose-paginate-tests.ts => mongoose-paginate-v2-tests.ts} (68%) diff --git a/types/mongoose-paginate-v2/index.d.ts b/types/mongoose-paginate-v2/index.d.ts index ad54786f53..6c9103f852 100644 --- a/types/mongoose-paginate-v2/index.d.ts +++ b/types/mongoose-paginate-v2/index.d.ts @@ -1,15 +1,31 @@ -// Type definitions for mongoose-paginate 5.0.0 +// Type definitions for mongoose-paginate-v2 1.0.13 // Project: https://github.com/edwardhotchkiss/mongoose-paginate -// Definitions by: Linus Brolin , simonxca +// Definitions by: Linus Brolin +// simonxca +// woutgg // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 +// +// Based on type declarations for mongoose-paginate 5.0.0. /// declare module 'mongoose' { + export interface CustomLabels { + totalDocs?: string, + limit?: string, + page?: string, + totalPages?: string, + docs?: string, + nextPage?: string, + prevPage?: string, + } + export interface PaginateOptions { select?: Object | string; sort?: Object | string; + customLabels?: CustomLabels, + collation?: CollationOptions, populate?: Array | Array | Object | string; lean?: boolean; leanWithId?: boolean; @@ -25,6 +41,7 @@ declare module 'mongoose' { page?: number; pages?: number; offset?: number; + [customLabel: string]: Array | number | undefined; } interface PaginateModel extends Model { @@ -44,7 +61,7 @@ declare module 'mongoose' { skipInit?: boolean): U; } -declare module 'mongoose-paginate' { +declare module 'mongoose-paginate-v2' { import mongoose = require('mongoose'); var _: (schema: mongoose.Schema) => void; export = _; diff --git a/types/mongoose-paginate-v2/mongoose-paginate-tests.ts b/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts similarity index 68% rename from types/mongoose-paginate-v2/mongoose-paginate-tests.ts rename to types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts index d55f8a454e..8410c740e5 100644 --- a/types/mongoose-paginate-v2/mongoose-paginate-tests.ts +++ b/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts @@ -43,12 +43,22 @@ router.get('/users.json', function(req: Request, res: Response) { let options: PaginateOptions = {} as PaginateOptions; options.select = 'email username'; options.sort = { 'username': (descending ? -1 : 1) }; + options.collation = { locale: 'en_US', strength: 1 }; options.populate = ''; options.lean = true; options.leanWithId = false; options.offset = 0; options.page = 1; options.limit = 10; + options.customLabels = { + totalDocs: 'totalDocsCustom', + limit: 'limitCustom', + page: 'pageCustom', + totalPages: 'totalPagesCustom', + docs: 'docsCustom', + nextPage: 'nextPageCustom', + prevPage: 'prevPageCustom' + }; UserModel .paginate({}, options, (err: any, value: PaginateResult) => { @@ -57,13 +67,15 @@ router.get('/users.json', function(req: Request, res: Response) { return res.status(500).send(err); } - console.log('total: ' + value.total); - console.log('limit: ' + value.limit); - console.log('page: ' + value.page); - console.log('pages: ' + value.pages); + console.log('totalDocs: ' + value.totalDocsCustom); + console.log('limit: ' + value.limitCustom); + console.log('page: ' + value.pageCustom); + console.log('nextPage: ' + value.nextPageCustom); + console.log('prevPage: ' + value.prevPageCustom); + console.log('totalPages: ' + value.totalPagesCustom); console.log('offset: ' + value.offset); console.log('docs: '); - console.dir(value.docs); + console.dir(value.docsCustom); return res.json(value); }); diff --git a/types/mongoose-paginate-v2/tsconfig.json b/types/mongoose-paginate-v2/tsconfig.json index 3194368f11..1e367240a9 100644 --- a/types/mongoose-paginate-v2/tsconfig.json +++ b/types/mongoose-paginate-v2/tsconfig.json @@ -18,6 +18,6 @@ }, "files": [ "index.d.ts", - "mongoose-paginate-tests.ts" + "mongoose-paginate-v2-tests.ts" ] -} \ No newline at end of file +} From b8afa6b51d275d3902b34aa2fcc66c4fb87cbdb6 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Tue, 18 Dec 2018 07:57:24 +0100 Subject: [PATCH 3/6] Fix project URL. --- types/mongoose-paginate-v2/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/mongoose-paginate-v2/index.d.ts b/types/mongoose-paginate-v2/index.d.ts index 6c9103f852..242cbdcd9a 100644 --- a/types/mongoose-paginate-v2/index.d.ts +++ b/types/mongoose-paginate-v2/index.d.ts @@ -1,5 +1,5 @@ // Type definitions for mongoose-paginate-v2 1.0.13 -// Project: https://github.com/edwardhotchkiss/mongoose-paginate +// Project: https://github.com/aravindnc/mongoose-paginate-v2 // Definitions by: Linus Brolin // simonxca // woutgg From a623516aa78e67b507e5135869f4404bd9f91ef6 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 24 Dec 2018 12:39:12 +0100 Subject: [PATCH 4/6] Remove model type parameter and overload. --- types/mongoose-paginate-v2/index.d.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/types/mongoose-paginate-v2/index.d.ts b/types/mongoose-paginate-v2/index.d.ts index 242cbdcd9a..3c178c5908 100644 --- a/types/mongoose-paginate-v2/index.d.ts +++ b/types/mongoose-paginate-v2/index.d.ts @@ -48,17 +48,11 @@ declare module 'mongoose' { paginate(query?: Object, options?: PaginateOptions, callback?: (err: any, result: PaginateResult) => void): Promise>; } - export function model( + export function model( name: string, schema?: Schema, collection?: string, - skipInit?: boolean): PaginateModel; - - export function model>( - name: string, - schema?: Schema, - collection?: string, - skipInit?: boolean): U; + skipInit?: boolean): PaginateModel; } declare module 'mongoose-paginate-v2' { From 2f2146a324a4667cd4f669caaf158b06a89bff30 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 3 Jan 2019 14:34:30 +0100 Subject: [PATCH 5/6] Remove tslint rule overrides and fix resulting issues. The 'ban-types' rule has not been removed because required changes conflict with other declarations (mongodb?). --- types/mongoose-paginate-v2/index.d.ts | 45 +++++------ .../mongoose-paginate-v2-tests.ts | 17 ++--- types/mongoose-paginate-v2/tslint.json | 75 +------------------ 3 files changed, 28 insertions(+), 109 deletions(-) diff --git a/types/mongoose-paginate-v2/index.d.ts b/types/mongoose-paginate-v2/index.d.ts index 3c178c5908..dfd70cc0cb 100644 --- a/types/mongoose-paginate-v2/index.d.ts +++ b/types/mongoose-paginate-v2/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for mongoose-paginate-v2 1.0.13 +// Type definitions for mongoose-paginate-v2 1.0 // Project: https://github.com/aravindnc/mongoose-paginate-v2 // Definitions by: Linus Brolin // simonxca @@ -8,25 +8,23 @@ // // Based on type declarations for mongoose-paginate 5.0.0. -/// - declare module 'mongoose' { - export interface CustomLabels { - totalDocs?: string, - limit?: string, - page?: string, - totalPages?: string, - docs?: string, - nextPage?: string, - prevPage?: string, + interface CustomLabels { + totalDocs?: string; + limit?: string; + page?: string; + totalPages?: string; + docs?: string; + nextPage?: string; + prevPage?: string; } - export interface PaginateOptions { + interface PaginateOptions { select?: Object | string; sort?: Object | string; - customLabels?: CustomLabels, - collation?: CollationOptions, - populate?: Array | Array | Object | string; + customLabels?: CustomLabels; + collation?: CollationOptions; + populate?: Object[] | string[] | Object | string; lean?: boolean; leanWithId?: boolean; offset?: number; @@ -34,29 +32,26 @@ declare module 'mongoose' { limit?: number; } - export interface PaginateResult { - docs: Array; + interface PaginateResult { + docs: T[]; total: number; limit: number; page?: number; pages?: number; offset?: number; - [customLabel: string]: Array | number | undefined; + [customLabel: string]: T[] | number | undefined; } interface PaginateModel extends Model { - paginate(query?: Object, options?: PaginateOptions, callback?: (err: any, result: PaginateResult) => void): Promise>; + paginate(query?: object, options?: PaginateOptions, callback?: (err: any, result: PaginateResult) => void): Promise>; } - export function model( + function model( name: string, schema?: Schema, collection?: string, skipInit?: boolean): PaginateModel; } -declare module 'mongoose-paginate-v2' { - import mongoose = require('mongoose'); - var _: (schema: mongoose.Schema) => void; - export = _; -} +import mongoose = require('mongoose'); +export function _(schema: mongoose.Schema): void; diff --git a/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts b/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts index 8410c740e5..200ca4e12a 100644 --- a/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts +++ b/types/mongoose-paginate-v2/mongoose-paginate-v2-tests.ts @@ -13,7 +13,6 @@ import { import mongoosePaginate = require('mongoose-paginate'); import { Router, Request, Response } from 'express'; - //#region Test Models interface User extends Document { email: string; @@ -29,20 +28,19 @@ const UserSchema: Schema = new Schema({ UserSchema.plugin(mongoosePaginate); -interface UserModel extends PaginateModel {}; +interface UserModel extends PaginateModel {} -let UserModel: UserModel = model('User', UserSchema) as UserModel; +const UserModel: UserModel = model('User', UserSchema) as UserModel; //#endregion - //#region Test Paginate -let router: Router = Router(); +const router: Router = Router(); -router.get('/users.json', function(req: Request, res: Response) { - let descending: boolean = true; - let options: PaginateOptions = {} as PaginateOptions; +router.get('/users.json', (req: Request, res: Response) => { + const descending = true; + const options: PaginateOptions = {}; options.select = 'email username'; - options.sort = { 'username': (descending ? -1 : 1) }; + options.sort = { username: (descending ? -1 : 1) }; options.collation = { locale: 'en_US', strength: 1 }; options.populate = ''; options.lean = true; @@ -78,6 +76,5 @@ router.get('/users.json', function(req: Request, res: Response) { console.dir(value.docsCustom); return res.json(value); }); - }); //#endregion diff --git a/types/mongoose-paginate-v2/tslint.json b/types/mongoose-paginate-v2/tslint.json index a41bf5d19a..a62d0d4e68 100644 --- a/types/mongoose-paginate-v2/tslint.json +++ b/types/mongoose-paginate-v2/tslint.json @@ -1,79 +1,6 @@ { "extends": "dtslint/dt.json", "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false + "ban-types": false } } From e50421936ea216ae862fb88afe78a502900553da Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 3 Jan 2019 19:34:43 +0100 Subject: [PATCH 6/6] Remove tslint ban-types config override in favor of inline overrides. --- types/mongoose-paginate-v2/index.d.ts | 3 +++ types/mongoose-paginate-v2/tslint.json | 5 +---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/types/mongoose-paginate-v2/index.d.ts b/types/mongoose-paginate-v2/index.d.ts index dfd70cc0cb..ca8b7eac91 100644 --- a/types/mongoose-paginate-v2/index.d.ts +++ b/types/mongoose-paginate-v2/index.d.ts @@ -20,10 +20,13 @@ declare module 'mongoose' { } interface PaginateOptions { + /* tslint:disable-next-line: ban-types */ select?: Object | string; + /* tslint:disable-next-line: ban-types */ sort?: Object | string; customLabels?: CustomLabels; collation?: CollationOptions; + /* tslint:disable-next-line: ban-types */ populate?: Object[] | string[] | Object | string; lean?: boolean; leanWithId?: boolean; diff --git a/types/mongoose-paginate-v2/tslint.json b/types/mongoose-paginate-v2/tslint.json index a62d0d4e68..f93cf8562a 100644 --- a/types/mongoose-paginate-v2/tslint.json +++ b/types/mongoose-paginate-v2/tslint.json @@ -1,6 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "ban-types": false - } + "extends": "dtslint/dt.json" }