diff --git a/README.md b/README.md index 86acc27..9897eff 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ npm i fastify-http-context Next, set up the plugin: ```js -const { fastifyHttpContextPlugin } = fastify-http-context +const { fastifyHttpContextPlugin } = require('fastify-http-context'); const fastify = require('fastify'); fastify.register(fastifyHttpContextPlugin, { defaults: user: { id: 'system' } }; diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..b047ba7 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,33 @@ +import { FastifyRequest, FastifyReply, Plugin } from "fastify"; +import { IncomingMessage, Server, ServerResponse } from "http"; + +export type HttpContext = { + get: (key: string) => T | undefined; + set: (key: string, value: T) => void; +}; + +export type Hook = + | "onRequest" + | "preParsing" + | "preValidation" + | "preHandler" + | "preSerialization" + | "onError" + | "onSend" + | "onResponse"; + +export type HttpContextOptions = { + defaults?: Record; + hook?: Hook; +}; + +declare const fastifyHttpContextPlugin: Plugin< + Server, + IncomingMessage, + ServerResponse, + HttpContextOptions +>; + +declare const httpContext: HttpContext; + +export { fastifyHttpContextPlugin, httpContext }; diff --git a/package.json b/package.json index e5482d9..c7638bb 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,14 @@ "version": "2.0.0", "description": "Simulates a thread of execution to allow for true session context to take place per api call within the fastify lifecycle of calls.", "main": "index.js", + "types": "index.d.ts", "scripts": { "test": "nyc mocha test/**/*.js", "benchmark": "concurrently -k -s first \"node ./examples/baseline.js\" \"sleep 10 && autocannon -c 100 -d 5 -p 10 localhost:3000/\"" }, "files": [ "index.js", + "index.d.ts", "async-local-storage.js" ], "repository": {