diff --git a/package.json b/package.json index 597c99f..10e28d3 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "captcha" ], "main": "index.js", + "types": "types/index.d.ts", "scripts": { "lint": "standard | snazzy", "lint:fix": "standard --fix", @@ -33,6 +34,7 @@ "standard": "^17.1.0", "tap": "^18.4.0", "ts-standard": "^12.0.2", + "tsd": "^0.29.0" }, "dependencies": { "fastify-plugin": "^4.5.1" diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..7b1f853 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,42 @@ +import { + FastifyPluginCallback, +} from 'fastify'; + +declare module 'fastify' { + interface FastifyContextConfig { + cfTurnstile?: fastifyCfTurnstile.FastifyCfTurnstileOptions; + } +} + +type FastifyCfTurnstile = FastifyPluginCallback; + +/** +* Namespace for fastify-cf-turnstile plugin options. +* +* @namespace fastifyCfTurnstile +*/ +declare namespace fastifyCfTurnstile { + /** + * Options for configuring the fastify-cf-turnstile plugin. + * + * @interface FastifyCfTurnstileOptions + */ + export interface FastifyCfTurnstileOptions { + /** + * CloudFlare Turnstile sitekey + * @type {string} + */ + sitekey: string + + /** + * CloudFlare Turnstile privatekey + * @type {string} + */ + privatekey: string + } + export const fastifyCfTurnstile: FastifyCfTurnstile + export { fastifyCfTurnstile as default } +} + +declare function fastifyCfTurnstile(...params: Parameters): ReturnType +export = fastifyCfTurnstile \ No newline at end of file diff --git a/types/index.test-d.ts b/types/index.test-d.ts new file mode 100644 index 0000000..6a252e3 --- /dev/null +++ b/types/index.test-d.ts @@ -0,0 +1,20 @@ +import fastify, { + FastifyInstance, +} from 'fastify'; +import { expectError, expectAssignable } from 'tsd'; +import { FastifyCfTurnstileOptions, default as plugin } from '.'; + +const app: FastifyInstance = fastify(); + +const fastifyCfTurnstileOptions = { + sitekey: 'test', + privatekey: 'test' +} + +app.register(plugin, { + sitekey: 'test', + privatekey: 'test' +}) +expectError(app.register(plugin, {})) + +expectAssignable(fastifyCfTurnstileOptions)