Skip to content

Commit

Permalink
Merge branch 'implement-tsd' of https://github.com/Uzlopak/mongoose i…
Browse files Browse the repository at this point in the history
…nto Uzlopak-implement-tsd
  • Loading branch information
vkarpov15 committed Feb 7, 2022
2 parents ce082c6 + a27bfe4 commit 86145e6
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,4 @@
"error"
]
}
}
}
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,48 @@ jobs:

- name: Linter
run: npm run lint

test-ts-types:
runs-on: ubuntu-latest
name: Typescript Types
steps:
- uses: actions/checkout@v2

- name: Setup node
uses: actions/setup-node@v1
with:
node-version: 12

- run: npm install

- name: Typings
run: npm run test-ts-types

test-ts-integration:
runs-on: ubuntu-latest
strategy:
fail-fast: false
name: Typescript Integration
steps:
- uses: actions/checkout@v2

- name: Setup node
uses: actions/setup-node@v1
with:
node-version: 12

- run: npm install

- name: Setup
run: |
wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.2.tgz
tar xf mongodb-linux-x86_64-ubuntu2004-5.0.2.tgz
mkdir -p ./data/db/27017 ./data/db/27000
printf "\n--timeout 8000" >> ./test/mocha.opts
./mongodb-linux-x86_64-ubuntu2004-5.0.2/bin/mongod --fork --dbpath ./data/db/27017 --syslog --port 27017
sleep 2
mongod --version
echo `pwd`/mongodb-linux-x86_64-ubuntu2004-5.0.2/bin >> $GITHUB_PATH
- run: npm run sym-link-mongoose
- run: npm run test-ts-integration
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
* mongoose.isValidObjectId({ test: 42 }); // false
*
* @method isValidObjectId
* @param {Any} value
* @returns {boolean} true if it is a valid ObjectId
* @api public
*/

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"test-coverage": "nyc --reporter=html --reporter=text npm test"
},
"main": "./index.js",
"types": "./index.d.ts",
"types": "./types/index.d.ts",
"engines": {
"node": ">=12.0.0"
},
Expand All @@ -100,6 +100,12 @@
"test/**/*.js"
]
},
<<<<<<< HEAD
=======
"tsd": {
"directory": "test/types"
},
>>>>>>> a27bfe4a96ffa8f95b52cd1f391c4d3e0105900c
"config": {
"mongodbMemoryServer": {
"disablePostinstall": true
Expand Down
8 changes: 8 additions & 0 deletions test/types/Types.ObjectId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as mongoose from 'mongoose';
import { expectError, expectType } from 'tsd';

const testObjectId = new mongoose.Types.ObjectId();

expectType<true>(mongoose.isValidObjectId(testObjectId));
expectType<boolean>(mongoose.isValidObjectId('12345'));
expectError(mongoose.isValidObjectId());
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"paths": {
"mongoose" : ["./types/index.d.ts"]
}
},
"esModuleInterop": true,
"strictNullChecks": true
}
1 change: 1 addition & 0 deletions index.d.ts → types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ declare module 'mongoose' {
* Returns true if Mongoose can cast the given value to an ObjectId, or
* false otherwise.
*/
export function isValidObjectId(v: Types.ObjectId): true;
export function isValidObjectId(v: any): boolean;

export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
Expand Down

0 comments on commit 86145e6

Please sign in to comment.