From 6dd24cc7e2a9de8036d054d62b83287f16dc62e8 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 5 Dec 2022 01:30:00 +0100 Subject: [PATCH] restructure and nodenext --- .github/workflows/ci.yml | 1 + .taprc | 2 + index.js | 2 + package.json | 6 +- test/types.test.ts | 168 ----------------------- tsconfig.json | 13 -- index.d.ts => types/index.d.ts | 17 ++- index.test-d.ts => types/index.test-d.ts | 2 +- 8 files changed, 23 insertions(+), 188 deletions(-) create mode 100644 .taprc delete mode 100644 test/types.test.ts delete mode 100644 tsconfig.json rename index.d.ts => types/index.d.ts (80%) rename index.test-d.ts => types/index.test-d.ts (99%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 325921f..079a8f0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,3 +15,4 @@ jobs: uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 with: license-check: true + lint: true diff --git a/.taprc b/.taprc new file mode 100644 index 0000000..eb6eb3e --- /dev/null +++ b/.taprc @@ -0,0 +1,2 @@ +files: + - test/**/*.test.js diff --git a/index.js b/index.js index 8a09cc6..1dc359c 100644 --- a/index.js +++ b/index.js @@ -62,3 +62,5 @@ module.exports = fp(fastifySensible, { name: '@fastify/sensible', fastify: '4.x' }) +module.exports.default = fastifySensible +module.exports.fastifySensible = fastifySensible diff --git a/package.json b/package.json index 9aafb86..88130a1 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,12 @@ "version": "5.1.1", "description": "Defaults for Fastify that everyone can agree on", "main": "index.js", + "types": "types/index.d.ts", "scripts": { - "test": "standard && tap test/*.test.js && tsd" + "lint": "standard", + "test": "npm run test:unit && npm run test:typescript", + "test:typescript": "tsd", + "test:unit": "tap" }, "repository": { "type": "git", diff --git a/test/types.test.ts b/test/types.test.ts deleted file mode 100644 index 2d2595e..0000000 --- a/test/types.test.ts +++ /dev/null @@ -1,168 +0,0 @@ -import * as fastify from "fastify"; -import * as fastifySensible from ".."; - -const app = fastify(); - -app.register(fastifySensible); - -// 'assert' types test -app.ready(err => { - try { - app.assert(true); - app.assert(true, 400); - app.assert(true, "400"); - app.assert(true, 400, "Bad request"); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.ok(true); - app.assert.ok(true, 400); - app.assert.ok(true, "400"); - app.assert.ok(true, 400, "Bad request"); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.equal(1, "1"); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.notEqual(1, "2"); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.strictEqual(1, 1); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.notStrictEqual(1, 2); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.deepEqual({ hello: "world" }, { hello: "world" }); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.notDeepEqual({ hello: "world" }, { hello: "dlrow" }); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.deepEqual({ hello: "world" }, { hello: "world" }); - } catch (err) { - throw err; - } -}); - -app.ready(err => { - try { - app.assert.deepEqual({ hello: "world" }, { hello: "world" }); - } catch (err) { - throw err; - } -}); - -// 'errorHandler' types test -app.get("/", (req, reply) => { - reply.send(new Error("kaboom")); -}); - -// 'forwarded' types test -app.get("/", (req, reply) => { - reply.send(req.forwarded()); -}); - -// 'httpError' types test -app.addHook("preHandler", async (request, reply) => { - try { - await app.httpErrors.getHttpError(400); - await app.httpErrors.badGateway(); - } catch { - const err = app.httpErrors["imateapot"]("custom"); - reply.send(err); - } -}); - -app.addHook("onError", (request, reply, error, next) => { - const err = app.httpErrors["imateapot"]("custom"); - reply.send(err); -}); - -// 'is' types test -app.get("/", (req, reply) => { - reply.send(req.is("json")); -}); - -app.get("/", (req, reply) => { - reply.send(req.is(["html", "json"])); -}); - -// 'proxyaddr' types test -app.get("/", (req, reply) => { - reply.send(req.proxyaddr(addr => addr === "127.0.0.1")); -}); - -app.get("/test", (req, reply) => { - reply.send(req.proxyaddr(["127.0.0.0/8", "10.0.0.0/8"])); -}); - -// 'to' types test -app.ready(err => { - app.to(promise(true)).then(val => {}); -}); - -function promise(bool: boolean) { - return new Promise((resolve, reject) => { - if (bool) { - resolve(true); - } else { - reject(new Error("kaboom")); - } - }); -} - -// 'vary' types test -app.get("/", (req, reply) => { - reply.vary("Accept"); - reply.vary("Origin"); - reply.vary("User-Agent"); - reply.send("ok"); -}); - -app.get("/", (req, reply) => { - app.assert.strictEqual( - reply.vary.append("", ["Accept", "Accept-Language"]), - "Accept, Accept-Language" - ); - reply.send("ok"); -}); diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 33d0fad..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "compilerOptions": { - "target": "es6", - "module": "commonjs", - "noEmit": true, - "strict": true, - "noImplicitAny": true, - "extendedDiagnostics": true - }, - "files": [ - "./test/types.test.ts" - ] -} diff --git a/index.d.ts b/types/index.d.ts similarity index 80% rename from index.d.ts rename to types/index.d.ts index ea61087..bfe7779 100644 --- a/index.d.ts +++ b/types/index.d.ts @@ -1,5 +1,7 @@ -import { FastifyPlugin } from 'fastify' -import { HttpErrors, HttpErrorReplys } from "./lib/httpError" +import { FastifyPluginCallback } from 'fastify' +import { HttpErrors, HttpErrorReplys } from "../lib/httpError" + +type FastifySensible = FastifyPluginCallback type singleValueTypes = 'must-revalidate' | 'no-cache' | @@ -60,8 +62,13 @@ declare module 'fastify' { } } -export interface SensibleOptions { +declare namespace fastifySensible { + export interface SensibleOptions { + } + + export const fastifySensible: FastifySensible + export { fastifySensible as default } } -declare const fastifySensible: FastifyPlugin -export default fastifySensible; +declare function fastifySensible(...params: Parameters): ReturnType +export = fastifySensible \ No newline at end of file diff --git a/index.test-d.ts b/types/index.test-d.ts similarity index 99% rename from index.test-d.ts rename to types/index.test-d.ts index 8903ad8..6ea0dfb 100644 --- a/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,6 +1,6 @@ import { expectType, expectAssignable, expectError } from 'tsd' import fastify from 'fastify' -import fastifySensible from '.' +import fastifySensible from '..' const app = fastify()