Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(typescript): add proper typeings for fastify.decorate (#58)
* fix(typescript): add proper typeings for fastify.decorate * chore(lock): remove lock file * chore(gitignore): add lockfile to ignore * test(types): add some tests
- Loading branch information
Showing
5 changed files
with
74 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,4 @@ jspm_packages | |
*.swp | ||
|
||
db | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,64 @@ | ||
import * as mongodb from "mongodb" | ||
import * as http from "http"; | ||
import * as fastify from "fastify"; | ||
import * as mongodb from 'mongodb'; | ||
import * as http from 'http'; | ||
import * as fastify from 'fastify'; | ||
|
||
declare module fastifyMongodb { | ||
interface FastifyMongodbOptions { | ||
forceClose?: boolean; | ||
database?: string; | ||
name?: string; | ||
client?: mongodb.MongoClient; | ||
url?: string; | ||
} | ||
declare namespace fastifyMongodb { | ||
interface FastifyMongoObject { | ||
/** | ||
* Mongo client instance | ||
*/ | ||
client: mongodb.MongoClient; | ||
/** | ||
* DB instance | ||
*/ | ||
db?: mongodb.Db; | ||
/** | ||
* Mongo ObjectId class | ||
*/ | ||
ObjectId: mongodb.ObjectId; | ||
} | ||
|
||
interface FastifyMongoNestedObject { | ||
[name: string]: FastifyMongoObject; | ||
} | ||
|
||
interface FastifyMongodbOptions { | ||
/** | ||
* Force to close the mongodb connection when app stopped | ||
* @default false | ||
*/ | ||
forceClose?: boolean; | ||
/** | ||
* Database name to connect | ||
*/ | ||
database?: string; | ||
name?: string; | ||
/** | ||
* Pre-configured instance of MongoClient | ||
*/ | ||
client?: mongodb.MongoClient; | ||
/** | ||
* Connection url | ||
*/ | ||
url?: string; | ||
} | ||
} | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance< | ||
HttpServer = http.Server, | ||
HttpRequest = http.IncomingMessage, | ||
HttpResponse = http.ServerResponse | ||
> { | ||
mongo: fastifyMongodb.FastifyMongoObject & fastifyMongodb.FastifyMongoNestedObject; | ||
} | ||
} | ||
|
||
declare let fastifyMongodb: fastify.Plugin< | ||
http.Server, | ||
http.IncomingMessage, | ||
http.ServerResponse, | ||
fastifyMongodb.FastifyMongodbOptions | ||
>; | ||
http.Server, | ||
http.IncomingMessage, | ||
http.ServerResponse, | ||
fastifyMongodb.FastifyMongodbOptions | ||
>; | ||
|
||
export = fastifyMongodb; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import fastify = require("fastify"); | ||
import fastifyMongodb = require("../fastify-mongodb"); | ||
import fastify = require('fastify'); | ||
import fastifyMongodb = require('../fastify-mongodb'); | ||
|
||
const app = fastify(); | ||
|
||
app.register(fastifyMongodb, { | ||
database: "testdb", | ||
name: "db", | ||
url: "mongodb://localhost:27017/testdb", | ||
}); | ||
app | ||
.register(fastifyMongodb, { | ||
database: 'testdb', | ||
name: 'db', | ||
url: 'mongodb://localhost:27017/testdb', | ||
}) | ||
.after((err) => { | ||
const dbTest = app.mongo.client.db('test'); | ||
const dbDb = app.mongo.db!; | ||
const ObjectId = app.mongo.ObjectId; | ||
}); |