Skip to content

Commit 1e6c830

Browse files
committed
chore: adjust code to typescript 6
1 parent 784e7e1 commit 1e6c830

4 files changed

Lines changed: 34 additions & 37 deletions

File tree

lib/server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import { requestFormatter, dilate, parsePort } from './utils';
66
import config from './config';
77
import { StocazzoRequest, StocazzoResponse } from './types';
88

9-
const sticazzi = config.routes;
9+
const sticazzi: Record<string, { value: string; big: string }> = config.routes;
1010

1111
const server = fastify();
1212

1313
server.register(cors);
1414

15-
server.get('/:format', (request: StocazzoRequest, reply: FastifyReply) => {
16-
const {
17-
params: { format },
18-
} = request;
15+
server.get<StocazzoRequest>(
16+
'/:format',
17+
(request, reply: FastifyReply) => {
18+
const { format } = request.params;
1919
const fallback = !(format && sticazzi[format]);
2020
const obj = fallback ? sticazzi.root : sticazzi[format];
2121
const response = { response: obj.value };

lib/types.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
import { FastifyRequest } from 'fastify';
2-
3-
type StocazzoRequestContent = {
1+
export interface StocazzoRequestContent {
42
params?: {
53
format: string;
64
};
7-
query?: {
5+
query: {
86
big?: number;
97
q: string;
108
};
11-
};
9+
}
1210

13-
export type StocazzoRequest =
14-
| StocazzoRequestContent
15-
| FastifyRequest<{
16-
Params: {
17-
format: string;
18-
};
19-
Querystring: {
20-
big: number;
21-
q: string;
22-
};
23-
}>;
11+
export interface StocazzoRequest {
12+
Params: {
13+
format: string;
14+
};
15+
Querystring: {
16+
big: number;
17+
q: string;
18+
};
19+
}
2420

2521
export interface StocazzoResponse {
2622
response: string;

lib/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { substr } from 'runes';
2-
import { StocazzoRequest, StocazzoResponse } from './types';
2+
import { StocazzoRequestContent, StocazzoResponse } from './types';
33

44
type Port = string | number;
55

66
export const requestFormatter =
7-
(req: StocazzoRequest) =>
7+
(req: StocazzoRequestContent) =>
88
(res: StocazzoResponse): StocazzoResponse => {
99
if (req.query.q) {
1010
res.query = req.query.q;
@@ -16,7 +16,7 @@ export const requestFormatter =
1616
};
1717

1818
export const dilate =
19-
(req: StocazzoRequest) =>
19+
(req: StocazzoRequestContent) =>
2020
(s: string) =>
2121
(res: StocazzoResponse): StocazzoResponse => {
2222
const { response } = res;

test/stocazzo.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import { FastifyInstance } from 'fastify';
12
import { init } from '../lib/server';
23

34
describe("Testing stocazzo's", () => {
4-
let server;
5+
let server: FastifyInstance;
56

67
beforeEach(async () => {
78
server = await init();
89
});
910

1011
it('GET /', async () => {
1112
const options = {
12-
method: 'GET',
13+
method: 'GET' as const,
1314
url: '/',
1415
};
1516

@@ -20,7 +21,7 @@ describe("Testing stocazzo's", () => {
2021

2122
it('GET / with request body', async () => {
2223
const options = {
23-
method: 'GET',
24+
method: 'GET' as const,
2425
url: '/?q=chi%20e',
2526
};
2627

@@ -32,7 +33,7 @@ describe("Testing stocazzo's", () => {
3233

3334
it('GET /caps', async () => {
3435
const options = {
35-
method: 'GET',
36+
method: 'GET' as const,
3637
url: '/caps',
3738
};
3839

@@ -43,7 +44,7 @@ describe("Testing stocazzo's", () => {
4344

4445
it('GET /caps with request body', async () => {
4546
const options = {
46-
method: 'GET',
47+
method: 'GET' as const,
4748
url: '/caps?q=chi%20e',
4849
};
4950

@@ -55,7 +56,7 @@ describe("Testing stocazzo's", () => {
5556

5657
it('GET /camel', async () => {
5758
const options = {
58-
method: 'GET',
59+
method: 'GET' as const,
5960
url: '/camel',
6061
};
6162

@@ -66,7 +67,7 @@ describe("Testing stocazzo's", () => {
6667

6768
it('GET /ascii', async () => {
6869
const options = {
69-
method: 'GET',
70+
method: 'GET' as const,
7071
url: '/ascii',
7172
};
7273

@@ -77,7 +78,7 @@ describe("Testing stocazzo's", () => {
7778

7879
it('GET /snake', async () => {
7980
const options = {
80-
method: 'GET',
81+
method: 'GET' as const,
8182
url: '/snake',
8283
};
8384

@@ -88,7 +89,7 @@ describe("Testing stocazzo's", () => {
8889

8990
it('GET /snake with big=true', async () => {
9091
const options = {
91-
method: 'GET',
92+
method: 'GET' as const,
9293
url: '/snake?big=1',
9394
};
9495

@@ -99,7 +100,7 @@ describe("Testing stocazzo's", () => {
99100

100101
it('GET /sto-conte', async () => {
101102
const options = {
102-
method: 'GET',
103+
method: 'GET' as const,
103104
url: '/sto-conte',
104105
};
105106

@@ -110,7 +111,7 @@ describe("Testing stocazzo's", () => {
110111

111112
it('GET /sto-conte with big=true', async () => {
112113
const options = {
113-
method: 'GET',
114+
method: 'GET' as const,
114115
url: '/sto-conte?big=1',
115116
};
116117

@@ -121,7 +122,7 @@ describe("Testing stocazzo's", () => {
121122

122123
it('GET /varg', async () => {
123124
const options = {
124-
method: 'GET',
125+
method: 'GET' as const,
125126
url: '/varg',
126127
};
127128

@@ -132,7 +133,7 @@ describe("Testing stocazzo's", () => {
132133

133134
it('GET /varg with big enabled', async () => {
134135
const options = {
135-
method: 'GET',
136+
method: 'GET' as const,
136137
url: '/varg?big=1',
137138
};
138139

0 commit comments

Comments
 (0)