Skip to content

Commit 0bc7b4f

Browse files
author
Daniele Fedeli
committed
Provided typescript test
1 parent 1faebfd commit 0bc7b4f

File tree

5 files changed

+113
-108
lines changed

5 files changed

+113
-108
lines changed

index.d.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { FastifyPlugin } from "fastify";
2-
3-
declare const fastifyGetOnly: FastifyPlugin<() => string>;
1+
import { FastifyPlugin } from 'fastify'
42

53
export interface FastifyGetOnlyOptions {
6-
httpStatusCode?: number;
7-
errorPayload?: unknown;
4+
httpStatusCode?: number
5+
errorPayload?: unknown
86
}
97

10-
export default fastifyGetOnly;
8+
declare const fastifyGetOnly: FastifyPlugin<FastifyGetOnlyOptions>
9+
10+
export default fastifyGetOnly

index.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
"use strict";
1+
'use strict'
22

3-
const fp = require("fastify-plugin");
3+
const fp = require('fastify-plugin')
44

5-
const defaultPayload = { error: "Method not allowed" };
6-
async function fastifyGetOnly(
7-
fastify,
8-
{ httpStatusCode = 405, errorPayload = defaultPayload }
5+
const defaultPayload = { error: 'Method not allowed' }
6+
async function fastifyGetOnly (
7+
fastify,
8+
{ httpStatusCode = 405, errorPayload = defaultPayload }
99
) {
10-
fastify.addHook("onRequest", async (request, reply) => {
11-
if (request.method !== "GET") {
12-
reply.status(httpStatusCode).send(errorPayload);
13-
}
14-
});
10+
fastify.addHook('onRequest', async (request, reply) => {
11+
if (request.method !== 'GET') {
12+
reply.status(httpStatusCode).send(errorPayload)
13+
}
14+
})
1515
}
1616

1717
module.exports = fp(fastifyGetOnly, {
18-
fastify: "3.x",
19-
name: "fastify-get-only",
20-
});
18+
fastify: '3.x',
19+
name: 'fastify-get-only'
20+
})

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
},
99
"scripts": {
1010
"test": "npm run lint && npm run unit && npm run test:typescript",
11-
"lint": "standard && npm run lint:typescript",
12-
"lint:typescript": "ts-standard",
11+
"lint": "standard --fix && npm run lint:typescript",
12+
"lint:typescript": "ts-standard --fix",
1313
"test:typescript": "tsd",
1414
"unit": "tap test/**/*.test.js"
1515
},

test/index.test-d.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import fastify from 'fastify'
2-
import example from '..'
3-
import { expectType } from 'tsd'
4-
5-
let app
6-
try {
7-
app = fastify()
8-
await app.ready()
9-
app.register(example)
10-
expectType<() => string>(app.exampleDecorator)
11-
} catch (err) {
12-
console.error(err)
2+
import FastifyGetOnly from '..'
3+
4+
let app = fastify()
5+
6+
app = fastify()
7+
8+
async function test (): Promise<void> {
9+
await app.register(FastifyGetOnly, { httpStatusCode: 200 })
10+
await app.register(FastifyGetOnly, {
11+
errorPayload: { error: 'Custom Error' }
12+
})
13+
await app.register(FastifyGetOnly, {
14+
errorPayload: { error: 'Custom Error' },
15+
httpStatusCode: 200
16+
})
1317
}
18+
19+
test().catch(err => console.error(err))

test/index.test.js

+74-75
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,104 @@
1-
const { test } = require("tap");
1+
const { test } = require('tap')
22

3-
test("Should return 200 on GET request", async t => {
4-
t.plan(1);
3+
test('Should return 200 on GET request', async t => {
4+
t.plan(1)
55

6-
const app = require("fastify")();
6+
const app = require('fastify')()
77

8-
app.register(require(".."));
9-
app.get("/", (req, reply) => {
10-
return { hello: "world" };
11-
});
8+
app.register(require('..'))
9+
app.get('/', (req, reply) => {
10+
return { hello: 'world' }
11+
})
1212

13-
await app.ready();
13+
await app.ready()
1414

15-
const response = await app.inject({
16-
method: "GET",
17-
url: "/",
18-
});
15+
const response = await app.inject({
16+
method: 'GET',
17+
url: '/'
18+
})
1919

20-
t.equal(response.statusCode, 200);
21-
});
20+
t.equal(response.statusCode, 200)
21+
})
2222

23-
test("Should return 405 on POST request", async t => {
24-
t.plan(1);
23+
test('Should return 405 on POST request', async t => {
24+
t.plan(1)
2525

26-
const app = require("fastify")();
26+
const app = require('fastify')()
2727

28-
app.register(require(".."));
29-
app.post("/", (req, reply) => {
30-
return { hello: "world" };
31-
});
28+
app.register(require('..'))
29+
app.post('/', (req, reply) => {
30+
return { hello: 'world' }
31+
})
3232

33-
await app.ready();
33+
await app.ready()
3434

35-
const response = await app.inject({
36-
method: "POST",
37-
url: "/",
38-
});
35+
const response = await app.inject({
36+
method: 'POST',
37+
url: '/'
38+
})
3939

40-
t.equal(response.statusCode, 405);
41-
});
40+
t.equal(response.statusCode, 405)
41+
})
4242

43-
test("Should return default body if none is specified", async t => {
44-
t.plan(1);
43+
test('Should return default body if none is specified', async t => {
44+
t.plan(1)
4545

46-
const app = require("fastify")();
46+
const app = require('fastify')()
4747

48-
app.register(require(".."));
49-
app.post("/", (req, reply) => {
50-
return { hello: "world" };
51-
});
48+
app.register(require('..'))
49+
app.post('/', (req, reply) => {
50+
return { hello: 'world' }
51+
})
5252

53-
await app.ready();
53+
await app.ready()
5454

55-
const response = await app.inject({
56-
method: "POST",
57-
url: "/",
58-
});
55+
const response = await app.inject({
56+
method: 'POST',
57+
url: '/'
58+
})
5959

60-
const bodyResponse = JSON.parse(response.body);
61-
t.same(bodyResponse, { error: "Method not allowed" });
62-
});
60+
const bodyResponse = JSON.parse(response.body)
61+
t.same(bodyResponse, { error: 'Method not allowed' })
62+
})
6363

64-
test("Should return custom body if it is specified", async t => {
65-
t.plan(1);
64+
test('Should return custom body if it is specified', async t => {
65+
t.plan(1)
6666

67-
const app = require("fastify")();
67+
const app = require('fastify')()
6868

69-
const customPayload = { error: "Custom error" };
70-
app.register(require(".."), { errorPayload: customPayload });
71-
app.post("/", (req, reply) => {
72-
return { hello: "world" };
73-
});
69+
const customPayload = { error: 'Custom error' }
70+
app.register(require('..'), { errorPayload: customPayload })
71+
app.post('/', (req, reply) => {
72+
return { hello: 'world' }
73+
})
7474

75-
await app.ready();
75+
await app.ready()
7676

77-
const response = await app.inject({
78-
method: "POST",
79-
url: "/",
80-
});
77+
const response = await app.inject({
78+
method: 'POST',
79+
url: '/'
80+
})
8181

82-
const bodyResponse = JSON.parse(response.body);
83-
t.same(bodyResponse, customPayload);
84-
});
82+
const bodyResponse = JSON.parse(response.body)
83+
t.same(bodyResponse, customPayload)
84+
})
8585

86-
test("Should return custom code if it is specified", async t => {
87-
t.plan(1);
86+
test('Should return custom code if it is specified', async t => {
87+
t.plan(1)
8888

89-
const app = require("fastify")();
89+
const app = require('fastify')()
9090

91-
const customPayload = { error: "Custom error" };
92-
app.register(require(".."), { httpStatusCode: 500 });
93-
app.post("/", (req, reply) => {
94-
return { hello: "world" };
95-
});
91+
app.register(require('..'), { httpStatusCode: 500 })
92+
app.post('/', (req, reply) => {
93+
return { hello: 'world' }
94+
})
9695

97-
await app.ready();
96+
await app.ready()
9897

99-
const response = await app.inject({
100-
method: "POST",
101-
url: "/",
102-
});
98+
const response = await app.inject({
99+
method: 'POST',
100+
url: '/'
101+
})
103102

104-
t.equal(response.statusCode, 500);
105-
});
103+
t.equal(response.statusCode, 500)
104+
})

0 commit comments

Comments
 (0)