Skip to content

Commit

Permalink
Refactor + add typings (#88)
Browse files Browse the repository at this point in the history
* move example into examples folder

* move tests into test folder, configuration into .taprc

* disable package-lock generation

* add typings

* remove husky in favor of pre-commit

* remove new line
  • Loading branch information
Uzlopak authored Aug 8, 2022
1 parent 6cc8a9a commit 392e919
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
2 changes: 2 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
files:
- test/**/*.test.js
2 changes: 1 addition & 1 deletion example.js → examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fastify = Fastify({
logger: true
})

fastify.register(require('.'))
fastify.register(require('..'))

fastify.get('/', function (request, reply) {
reply.send({ hello: 'world' })
Expand Down
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
"version": "3.1.0",
"description": "provide stats for routes using perf_hooks, for fastify",
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard | snazzy",
"test": "tap test.js"
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
},
"repository": {
"type": "git",
Expand All @@ -27,22 +30,23 @@
},
"homepage": "https://github.com/fastify/fastify-routes-stats#readme",
"devDependencies": {
"@fastify/pre-commit": "2.0.2",
"@sinonjs/fake-timers": "^9.1.2",
"@types/node": "^18.6.4",
"fastify": "^4.2.1",
"husky": "^8.0.1",
"snazzy": "^9.0.0",
"standard": "^17.0.0",
"tap": "^16.2.0"
"tap": "^16.2.0",
"tsd": "^0.22.0"
},
"dependencies": {
"fastify-plugin": "^4.0.0",
"summary": "^2.1.0"
},
"husky": {
"hooks": {
"pre-commit": "npm test"
}
},
"pre-commit": [
"lint",
"test"
],
"publishConfig": {
"access": "public"
}
Expand Down
2 changes: 1 addition & 1 deletion test.js → test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { beforeEach, test } = require('tap')
const { performance } = require('perf_hooks')
const { Transform } = require('stream')
const Fastify = require('fastify')
const Stats = require('.')
const Stats = require('..')
const fakeTimer = require('@sinonjs/fake-timers')
const setTimeoutPromise = require('util').promisify(setTimeout)

Expand Down
27 changes: 27 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FastifyPluginAsync, HTTPMethods } from "fastify";

interface Stats {
mean: number;
mode: number;
median: number;
max: number;
min: number;
sd: number;
}

declare module 'fastify' {
interface FastifyInstance {
stats(): Partial<Record<HTTPMethods, Record<string, Stats>>>;
measurements(): Partial<Record<HTTPMethods, Record<string, Array<number>>>>;
}
}

interface FastifyRoutesStatsOptions {
/**
* @default 30000
*/
printInterval?: number;
}

declare const fastifyRoutesStats: FastifyPluginAsync<FastifyRoutesStatsOptions>
export default fastifyRoutesStats
25 changes: 25 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Fastify from 'fastify'
import { expectAssignable, expectError, expectType } from 'tsd'
import fastifyRoutesStats from '..'

const fastify = Fastify();

fastify.register(fastifyRoutesStats, { printInterval: 1000 })
expectError(fastify.register(fastifyRoutesStats, { printInterval: 'a' }))

expectAssignable<Function>(fastify.measurements)
expectAssignable<Function>(fastify.stats)

fastify.get('/stats', function (req, reply) {
expectAssignable<Function>(this.measurements)
expectAssignable<Function>(this.stats)
})

expectType<Array<number>>(fastify.measurements().GET!['test'])
expectAssignable<Record<string, {}> | undefined>(fastify.stats().GET)
expectType<number>(fastify.stats().GET!['/'].max)
expectType<number>(fastify.stats().GET!['/'].mean)
expectType<number>(fastify.stats().GET!['/'].median)
expectType<number>(fastify.stats().GET!['/'].min)
expectType<number>(fastify.stats().GET!['/'].mode)
expectType<number>(fastify.stats().GET!['/'].sd)

0 comments on commit 392e919

Please sign in to comment.