Skip to content

Commit

Permalink
fix(typescript): add proper typeings for fastify.decorate (#58)
Browse files Browse the repository at this point in the history
* 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
SkeLLLa authored and mcollina committed Nov 1, 2018
1 parent 919cc4c commit 80e8075
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -43,3 +43,4 @@ jspm_packages
*.swp

db
package-lock.json
74 changes: 58 additions & 16 deletions index.d.ts
@@ -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;
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
},
"homepage": "https://github.com/fastify/fastify-mongodb#readme",
"devDependencies": {
"@types/mongodb": "^3.1.14",
"@types/node": "^10.7.1",
"fastify": "^1.3.1",
"pre-commit": "^1.2.2",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -4,7 +4,7 @@
"module": "commonjs",
"noEmit": true,
"strict": true,
"noImplicitAny": false,
"noImplicitAny": true,
},
"files": [
"./types.test.ts"
Expand Down
20 changes: 13 additions & 7 deletions types.test.ts
@@ -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;
});

0 comments on commit 80e8075

Please sign in to comment.