@jaredwray/mockhttp 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -3
- package/dist/index.d.ts +3 -0
- package/dist/index.js +896 -31
- package/package.json +23 -13
- package/dist/fastify-config.js +0 -12
- package/dist/routes/delete-route.js +0 -27
- package/dist/routes/get-route.js +0 -34
- package/dist/routes/http-methods/delete.js +0 -28
- package/dist/routes/http-methods/get.js +0 -35
- package/dist/routes/http-methods/index.js +0 -5
- package/dist/routes/http-methods/patch.js +0 -28
- package/dist/routes/http-methods/post.js +0 -28
- package/dist/routes/http-methods/put.js +0 -28
- package/dist/routes/index-route.js +0 -67
- package/dist/routes/ip-route.js +0 -25
- package/dist/routes/post-route.js +0 -27
- package/dist/routes/put-route.js +0 -27
- package/dist/routes/request-inspection/headers.js +0 -26
- package/dist/routes/request-inspection/index.js +0 -3
- package/dist/routes/request-inspection/ip.js +0 -26
- package/dist/routes/request-inspection/user-agent.js +0 -25
- package/dist/routes/status-codes/get.js +0 -29
- package/dist/routes/status-codes/index.js +0 -102
- package/dist/routes/status-route.js +0 -28
- package/dist/swagger.js +0 -34
package/package.json
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jaredwray/mockhttp",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Mock Http - Easy way to mock http with httpbin replacement",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"directories": {
|
|
8
8
|
"test": "test"
|
|
9
9
|
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "xo --fix && vitest run --coverage",
|
|
12
|
+
"dev": "xo --fix && npx tsx src/index.ts --watch",
|
|
13
|
+
"build": "rimraf ./dist && tsup src/index.ts --format esm --dts --clean",
|
|
14
|
+
"docker:build": "docker build -t jaredwray/mockhttp .",
|
|
15
|
+
"docker:run": "docker run -p 3000:3000 jaredwray/mockhttp",
|
|
16
|
+
"docker:push": "docker push jaredwray/mockhttp",
|
|
17
|
+
"docker:stop": "docker stop $(docker ps -a -q --filter ancestor=jaredwray/mockhttp --format=\"{{.ID}}\")",
|
|
18
|
+
"prepare": "npm run build",
|
|
19
|
+
"start": "node dist/index.js"
|
|
20
|
+
},
|
|
10
21
|
"repository": {
|
|
11
22
|
"type": "git",
|
|
12
23
|
"url": "git+https://github.com/jaredwray/mockhttp.git"
|
|
@@ -24,16 +35,21 @@
|
|
|
24
35
|
},
|
|
25
36
|
"homepage": "https://github.com/jaredwray/mockhttp#readme",
|
|
26
37
|
"devDependencies": {
|
|
27
|
-
"@
|
|
38
|
+
"@swc/core": "^1.10.1",
|
|
39
|
+
"@vitest/coverage-v8": "^2.1.8",
|
|
40
|
+
"rimraf": "^6.0.1",
|
|
41
|
+
"tsup": "^8.3.5",
|
|
28
42
|
"tsx": "^4.19.2",
|
|
29
43
|
"typescript": "^5.7.2",
|
|
30
|
-
"vitest": "^2.1.
|
|
31
|
-
"xo": "^0.
|
|
44
|
+
"vitest": "^2.1.8",
|
|
45
|
+
"xo": "^0.60.0"
|
|
32
46
|
},
|
|
33
47
|
"dependencies": {
|
|
48
|
+
"@fastify/helmet": "^13.0.0",
|
|
34
49
|
"@fastify/static": "^8.0.3",
|
|
35
50
|
"@fastify/swagger": "^9.4.0",
|
|
36
|
-
"@fastify/swagger-ui": "^5.
|
|
51
|
+
"@fastify/swagger-ui": "^5.2.0",
|
|
52
|
+
"@scalar/api-reference": "^1.25.80",
|
|
37
53
|
"fastify": "^5.1.0",
|
|
38
54
|
"pino": "^9.5.0",
|
|
39
55
|
"pino-pretty": "^13.0.0"
|
|
@@ -42,11 +58,5 @@
|
|
|
42
58
|
"dist",
|
|
43
59
|
"public",
|
|
44
60
|
"LICENCE"
|
|
45
|
-
]
|
|
46
|
-
|
|
47
|
-
"test": "xo --fix && vitest run --coverage",
|
|
48
|
-
"dev": "xo --fix && npx tsx src/index.ts --watch",
|
|
49
|
-
"build": "tsc",
|
|
50
|
-
"start": "node dist/index.js"
|
|
51
|
-
}
|
|
52
|
-
}
|
|
61
|
+
]
|
|
62
|
+
}
|
package/dist/fastify-config.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const deleteSchema = {
|
|
2
|
-
description: 'Handles a DELETE request and returns request information',
|
|
3
|
-
body: {
|
|
4
|
-
type: 'object',
|
|
5
|
-
additionalProperties: true,
|
|
6
|
-
description: 'The body of the DELETE request',
|
|
7
|
-
},
|
|
8
|
-
response: {
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
-
200: {
|
|
11
|
-
type: 'object',
|
|
12
|
-
properties: {
|
|
13
|
-
method: { type: 'string' },
|
|
14
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
15
|
-
body: { type: 'object', additionalProperties: true },
|
|
16
|
-
},
|
|
17
|
-
required: ['method', 'headers', 'body'],
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
export const deleteRoute = (fastify) => {
|
|
22
|
-
fastify.delete('/delete', { schema: deleteSchema }, async (request) => ({
|
|
23
|
-
method: 'DELETE',
|
|
24
|
-
headers: request.headers,
|
|
25
|
-
body: request.body,
|
|
26
|
-
}));
|
|
27
|
-
};
|
package/dist/routes/get-route.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export const getSchema = {
|
|
2
|
-
description: 'Returns request information for GET requests',
|
|
3
|
-
querystring: {
|
|
4
|
-
type: 'object',
|
|
5
|
-
additionalProperties: true,
|
|
6
|
-
properties: {},
|
|
7
|
-
description: 'Query parameters sent in the request',
|
|
8
|
-
},
|
|
9
|
-
headers: {
|
|
10
|
-
type: 'object',
|
|
11
|
-
additionalProperties: true,
|
|
12
|
-
properties: {},
|
|
13
|
-
description: 'Headers sent in the request',
|
|
14
|
-
},
|
|
15
|
-
response: {
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
17
|
-
200: {
|
|
18
|
-
type: 'object',
|
|
19
|
-
properties: {
|
|
20
|
-
method: { type: 'string' },
|
|
21
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
22
|
-
queryParams: { type: 'object', additionalProperties: { type: 'string' } },
|
|
23
|
-
},
|
|
24
|
-
required: ['method', 'headers', 'queryParams'],
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
export const getRoute = (fastify) => {
|
|
29
|
-
fastify.get('/get', { schema: getSchema }, async (request, _reply) => ({
|
|
30
|
-
method: 'GET',
|
|
31
|
-
headers: request.headers,
|
|
32
|
-
queryParams: request.query,
|
|
33
|
-
}));
|
|
34
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const deleteSchema = {
|
|
2
|
-
description: 'Handles a DELETE request and returns request information',
|
|
3
|
-
tags: ['HTTP Methods'],
|
|
4
|
-
body: {
|
|
5
|
-
type: 'object',
|
|
6
|
-
additionalProperties: true,
|
|
7
|
-
description: 'The body of the DELETE request',
|
|
8
|
-
},
|
|
9
|
-
response: {
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
11
|
-
200: {
|
|
12
|
-
type: 'object',
|
|
13
|
-
properties: {
|
|
14
|
-
method: { type: 'string' },
|
|
15
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
16
|
-
body: { type: 'object', additionalProperties: true },
|
|
17
|
-
},
|
|
18
|
-
required: ['method', 'headers', 'body'],
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
export const deleteRoute = (fastify) => {
|
|
23
|
-
fastify.delete('/delete', { schema: deleteSchema }, async (request) => ({
|
|
24
|
-
method: 'DELETE',
|
|
25
|
-
headers: request.headers,
|
|
26
|
-
body: request.body,
|
|
27
|
-
}));
|
|
28
|
-
};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export const getSchema = {
|
|
2
|
-
description: 'Returns request information for GET requests',
|
|
3
|
-
tags: ['HTTP Methods'],
|
|
4
|
-
querystring: {
|
|
5
|
-
type: 'object',
|
|
6
|
-
additionalProperties: true,
|
|
7
|
-
properties: {},
|
|
8
|
-
description: 'Query parameters sent in the request',
|
|
9
|
-
},
|
|
10
|
-
headers: {
|
|
11
|
-
type: 'object',
|
|
12
|
-
additionalProperties: true,
|
|
13
|
-
properties: {},
|
|
14
|
-
description: 'Headers sent in the request',
|
|
15
|
-
},
|
|
16
|
-
response: {
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
18
|
-
200: {
|
|
19
|
-
type: 'object',
|
|
20
|
-
properties: {
|
|
21
|
-
method: { type: 'string' },
|
|
22
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
23
|
-
queryParams: { type: 'object', additionalProperties: { type: 'string' } },
|
|
24
|
-
},
|
|
25
|
-
required: ['method', 'headers', 'queryParams'],
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
export const getRoute = (fastify) => {
|
|
30
|
-
fastify.get('/get', { schema: getSchema }, async (request, _reply) => ({
|
|
31
|
-
method: 'GET',
|
|
32
|
-
headers: request.headers,
|
|
33
|
-
queryParams: request.query,
|
|
34
|
-
}));
|
|
35
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const patchSchema = {
|
|
2
|
-
description: 'Handles a PATCH request and returns request information',
|
|
3
|
-
tags: ['HTTP Methods'],
|
|
4
|
-
body: {
|
|
5
|
-
type: 'object',
|
|
6
|
-
additionalProperties: true,
|
|
7
|
-
description: 'The body of the PATCH request',
|
|
8
|
-
},
|
|
9
|
-
response: {
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
11
|
-
200: {
|
|
12
|
-
type: 'object',
|
|
13
|
-
properties: {
|
|
14
|
-
method: { type: 'string' },
|
|
15
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
16
|
-
body: { type: 'object', additionalProperties: true },
|
|
17
|
-
},
|
|
18
|
-
required: ['method', 'headers', 'body'],
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
export const patchRoute = (fastify) => {
|
|
23
|
-
fastify.patch('/patch', { schema: patchSchema }, async (request) => ({
|
|
24
|
-
method: 'PATCH',
|
|
25
|
-
headers: request.headers,
|
|
26
|
-
body: request.body,
|
|
27
|
-
}));
|
|
28
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const postSchema = {
|
|
2
|
-
description: 'Handles a POST request and returns request information',
|
|
3
|
-
tags: ['HTTP Methods'],
|
|
4
|
-
body: {
|
|
5
|
-
type: 'object',
|
|
6
|
-
additionalProperties: true,
|
|
7
|
-
description: 'The body of the POST request',
|
|
8
|
-
},
|
|
9
|
-
response: {
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
11
|
-
200: {
|
|
12
|
-
type: 'object',
|
|
13
|
-
properties: {
|
|
14
|
-
method: { type: 'string' },
|
|
15
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
16
|
-
body: { type: 'object', additionalProperties: true },
|
|
17
|
-
},
|
|
18
|
-
required: ['method', 'headers', 'body'],
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
export const postRoute = (fastify) => {
|
|
23
|
-
fastify.post('/post', { schema: postSchema }, async (request) => ({
|
|
24
|
-
method: 'POST',
|
|
25
|
-
headers: request.headers,
|
|
26
|
-
body: request.body,
|
|
27
|
-
}));
|
|
28
|
-
};
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const putSchema = {
|
|
2
|
-
description: 'Handles a PUT request and returns request information',
|
|
3
|
-
tags: ['HTTP Methods'],
|
|
4
|
-
body: {
|
|
5
|
-
type: 'object',
|
|
6
|
-
additionalProperties: true,
|
|
7
|
-
description: 'The body of the PUT request',
|
|
8
|
-
},
|
|
9
|
-
response: {
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
11
|
-
200: {
|
|
12
|
-
type: 'object',
|
|
13
|
-
properties: {
|
|
14
|
-
method: { type: 'string' },
|
|
15
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
16
|
-
body: { type: 'object', additionalProperties: true },
|
|
17
|
-
},
|
|
18
|
-
required: ['method', 'headers', 'body'],
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
export const putRoute = (fastify) => {
|
|
23
|
-
fastify.put('/put', { schema: putSchema }, async (request) => ({
|
|
24
|
-
method: 'PUT',
|
|
25
|
-
headers: request.headers,
|
|
26
|
-
body: request.body,
|
|
27
|
-
}));
|
|
28
|
-
};
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
export const indexRoute = (fastify) => {
|
|
2
|
-
fastify.get('/', { schema: { hide: true } }, async (_request, reply) => {
|
|
3
|
-
const redocHtml = `
|
|
4
|
-
<!doctype html>
|
|
5
|
-
<html>
|
|
6
|
-
<head>
|
|
7
|
-
<title>{ mockhttp } </title>
|
|
8
|
-
<meta charset="utf-8" />
|
|
9
|
-
<meta
|
|
10
|
-
name="viewport"
|
|
11
|
-
content="width=device-width, initial-scale=1" />
|
|
12
|
-
<style>
|
|
13
|
-
:root {
|
|
14
|
-
--scalar-custom-header-height: 70px;
|
|
15
|
-
}
|
|
16
|
-
.custom-header {
|
|
17
|
-
height: var(--scalar-custom-header-height);
|
|
18
|
-
background-color: var(--scalar-background-1);
|
|
19
|
-
box-shadow: inset 0 -1px 0 var(--scalar-border-color);
|
|
20
|
-
color: var(--scalar-color-1);
|
|
21
|
-
font-size: var(--scalar-font-size-2);
|
|
22
|
-
padding: 0 18px;
|
|
23
|
-
position: sticky;
|
|
24
|
-
justify-content: space-between;
|
|
25
|
-
top: 0;
|
|
26
|
-
z-index: 100;
|
|
27
|
-
}
|
|
28
|
-
.custom-header,
|
|
29
|
-
.custom-header nav {
|
|
30
|
-
display: flex;
|
|
31
|
-
align-items: center;
|
|
32
|
-
gap: 18px;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
.custom-header img {
|
|
36
|
-
height: var(--scalar-custom-header-height);
|
|
37
|
-
object-fit: contain;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
.custom-header a:hover {
|
|
41
|
-
color: var(--scalar-color-2);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
.github-corner {
|
|
45
|
-
fill: var(--scalar-border-color) !important;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
</style>
|
|
49
|
-
</head>
|
|
50
|
-
<body>
|
|
51
|
-
<header class="custom-header scalar-app">
|
|
52
|
-
<img src="/logo.svg" alt="{ mockhttp }" />
|
|
53
|
-
<a href="https://github.com/jaredwray/mockhttp" class="github-corner" aria-label="View source on GitHub">
|
|
54
|
-
<svg width="70" height="70" viewBox="0 0 250 250" style="color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
|
|
55
|
-
</header>
|
|
56
|
-
|
|
57
|
-
<script
|
|
58
|
-
id="api-reference"
|
|
59
|
-
data-url="/docs/json"></script>
|
|
60
|
-
|
|
61
|
-
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
|
|
62
|
-
</body>
|
|
63
|
-
</html>
|
|
64
|
-
`;
|
|
65
|
-
await reply.type('text/html').send(redocHtml);
|
|
66
|
-
});
|
|
67
|
-
};
|
package/dist/routes/ip-route.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const ipSchema = {
|
|
2
|
-
description: 'Returns the IP address of the client',
|
|
3
|
-
response: {
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
5
|
-
200: {
|
|
6
|
-
type: 'object',
|
|
7
|
-
properties: {
|
|
8
|
-
ip: { type: 'string', description: 'The IP address of the client' },
|
|
9
|
-
},
|
|
10
|
-
required: ['ip'],
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
};
|
|
14
|
-
export const ipRoute = (fastify) => {
|
|
15
|
-
fastify.get('/ip', { schema: ipSchema }, async (request) => {
|
|
16
|
-
const cloudflareIp = request.headers['cf-connecting-ip'];
|
|
17
|
-
const forwardedIp = request.headers['x-forwarded-for'];
|
|
18
|
-
const remoteIp = request.ip;
|
|
19
|
-
// Determine the client's IP address, considering Cloudflare and other proxies
|
|
20
|
-
const ip = cloudflareIp ?? (typeof forwardedIp === 'string' ? forwardedIp.split(',')[0].trim() : remoteIp);
|
|
21
|
-
return {
|
|
22
|
-
ip,
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const postSchema = {
|
|
2
|
-
description: 'Handles a POST request and returns request information',
|
|
3
|
-
body: {
|
|
4
|
-
type: 'object',
|
|
5
|
-
additionalProperties: true,
|
|
6
|
-
description: 'The body of the POST request',
|
|
7
|
-
},
|
|
8
|
-
response: {
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
-
200: {
|
|
11
|
-
type: 'object',
|
|
12
|
-
properties: {
|
|
13
|
-
method: { type: 'string' },
|
|
14
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
15
|
-
body: { type: 'object', additionalProperties: true },
|
|
16
|
-
},
|
|
17
|
-
required: ['method', 'headers', 'body'],
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
export const postRoute = (fastify) => {
|
|
22
|
-
fastify.post('/post', { schema: postSchema }, async (request) => ({
|
|
23
|
-
method: 'POST',
|
|
24
|
-
headers: request.headers,
|
|
25
|
-
body: request.body,
|
|
26
|
-
}));
|
|
27
|
-
};
|
package/dist/routes/put-route.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
const putSchema = {
|
|
2
|
-
description: 'Handles a PUT request and returns request information',
|
|
3
|
-
body: {
|
|
4
|
-
type: 'object',
|
|
5
|
-
additionalProperties: true,
|
|
6
|
-
description: 'The body of the PUT request',
|
|
7
|
-
},
|
|
8
|
-
response: {
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
-
200: {
|
|
11
|
-
type: 'object',
|
|
12
|
-
properties: {
|
|
13
|
-
method: { type: 'string' },
|
|
14
|
-
headers: { type: 'object', additionalProperties: { type: 'string' } },
|
|
15
|
-
body: { type: 'object', additionalProperties: true },
|
|
16
|
-
},
|
|
17
|
-
required: ['method', 'headers', 'body'],
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
export const putRoute = (fastify) => {
|
|
22
|
-
fastify.put('/put', { schema: putSchema }, async (request) => ({
|
|
23
|
-
method: 'PUT',
|
|
24
|
-
headers: request.headers,
|
|
25
|
-
body: request.body,
|
|
26
|
-
}));
|
|
27
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const headersSchema = {
|
|
2
|
-
description: 'Returns all headers of the client request',
|
|
3
|
-
tags: ['Request Inspection'],
|
|
4
|
-
response: {
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
6
|
-
200: {
|
|
7
|
-
type: 'object',
|
|
8
|
-
properties: {
|
|
9
|
-
headers: {
|
|
10
|
-
type: 'object',
|
|
11
|
-
additionalProperties: { type: 'string' },
|
|
12
|
-
description: 'All headers of the client request',
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
required: ['headers'],
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
export const headersRoute = (fastify) => {
|
|
20
|
-
fastify.get('/headers', { schema: headersSchema }, async (request) => {
|
|
21
|
-
const headers = request.headers;
|
|
22
|
-
return {
|
|
23
|
-
headers,
|
|
24
|
-
};
|
|
25
|
-
});
|
|
26
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const ipSchema = {
|
|
2
|
-
description: 'Returns the IP address of the client',
|
|
3
|
-
tags: ['Request Inspection'],
|
|
4
|
-
response: {
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
6
|
-
200: {
|
|
7
|
-
type: 'object',
|
|
8
|
-
properties: {
|
|
9
|
-
ip: { type: 'string', description: 'The IP address of the client' },
|
|
10
|
-
},
|
|
11
|
-
required: ['ip'],
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
export const ipRoute = (fastify) => {
|
|
16
|
-
fastify.get('/ip', { schema: ipSchema }, async (request) => {
|
|
17
|
-
const cloudflareIp = request.headers['cf-connecting-ip'];
|
|
18
|
-
const forwardedIp = request.headers['x-forwarded-for'];
|
|
19
|
-
const remoteIp = request.ip;
|
|
20
|
-
// Determine the client's IP address, considering Cloudflare and other proxies
|
|
21
|
-
const ip = cloudflareIp ?? (typeof forwardedIp === 'string' ? forwardedIp.split(',')[0].trim() : remoteIp);
|
|
22
|
-
return {
|
|
23
|
-
ip,
|
|
24
|
-
};
|
|
25
|
-
});
|
|
26
|
-
};
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
const userAgentSchema = {
|
|
2
|
-
description: 'Returns the User-Agent header of the client request',
|
|
3
|
-
tags: ['Request Inspection'],
|
|
4
|
-
response: {
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
6
|
-
200: {
|
|
7
|
-
type: 'object',
|
|
8
|
-
properties: {
|
|
9
|
-
userAgent: {
|
|
10
|
-
type: 'string',
|
|
11
|
-
description: 'The User-Agent header of the client request',
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
required: ['userAgent'],
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
};
|
|
18
|
-
export const userAgentRoute = (fastify) => {
|
|
19
|
-
fastify.get('/user-agent', { schema: userAgentSchema }, async (request) => {
|
|
20
|
-
const userAgent = request.headers['user-agent'];
|
|
21
|
-
return {
|
|
22
|
-
userAgent,
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
const statusSchema = {
|
|
2
|
-
description: 'Returns a response with the provided status code',
|
|
3
|
-
tags: ['Status Codes'],
|
|
4
|
-
params: {
|
|
5
|
-
type: 'object',
|
|
6
|
-
properties: {
|
|
7
|
-
code: { type: 'string', description: 'HTTP status code to be returned' },
|
|
8
|
-
},
|
|
9
|
-
required: ['code'],
|
|
10
|
-
},
|
|
11
|
-
response: {
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
13
|
-
200: {
|
|
14
|
-
type: 'object',
|
|
15
|
-
properties: {
|
|
16
|
-
status: { type: 'string' },
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
export const getStatusCodeRoute = (fastify) => {
|
|
22
|
-
fastify.get('/status/:code', { schema: statusSchema }, async (request, reply) => {
|
|
23
|
-
const { code } = request.params;
|
|
24
|
-
await reply.code(Number(code));
|
|
25
|
-
return {
|
|
26
|
-
status: `Response with status code ${code}`,
|
|
27
|
-
};
|
|
28
|
-
});
|
|
29
|
-
};
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { randomInt } from 'node:crypto';
|
|
2
|
-
const statusCodes = [
|
|
3
|
-
{ code: 100, message: 'Continue' },
|
|
4
|
-
{ code: 101, message: 'Switching Protocols' },
|
|
5
|
-
{ code: 102, message: 'Processing' },
|
|
6
|
-
{ code: 103, message: 'Early Hints' },
|
|
7
|
-
{ code: 200, message: 'OK' },
|
|
8
|
-
{ code: 201, message: 'Created' },
|
|
9
|
-
{ code: 202, message: 'Accepted' },
|
|
10
|
-
{ code: 203, message: 'Non-Authoritative Information' },
|
|
11
|
-
{ code: 204, message: 'No Content' },
|
|
12
|
-
{ code: 205, message: 'Reset Content' },
|
|
13
|
-
{ code: 206, message: 'Partial Content' },
|
|
14
|
-
{ code: 207, message: 'Multi-Status' },
|
|
15
|
-
{ code: 208, message: 'Already Reported' },
|
|
16
|
-
{ code: 226, message: 'IM Used' },
|
|
17
|
-
{ code: 300, message: 'Multiple Choices' },
|
|
18
|
-
{ code: 301, message: 'Moved Permanently' },
|
|
19
|
-
{ code: 302, message: 'Found' },
|
|
20
|
-
{ code: 303, message: 'See Other' },
|
|
21
|
-
{ code: 304, message: 'Not Modified' },
|
|
22
|
-
{ code: 305, message: 'Use Proxy' },
|
|
23
|
-
{ code: 306, message: 'Switch Proxy' },
|
|
24
|
-
{ code: 307, message: 'Temporary Redirect' },
|
|
25
|
-
{ code: 308, message: 'Permanent Redirect' },
|
|
26
|
-
{ code: 400, message: 'Bad Request' },
|
|
27
|
-
{ code: 401, message: 'Unauthorized' },
|
|
28
|
-
{ code: 402, message: 'Payment Required' },
|
|
29
|
-
{ code: 403, message: 'Forbidden' },
|
|
30
|
-
{ code: 404, message: 'Not Found' },
|
|
31
|
-
{ code: 405, message: 'Method Not Allowed' },
|
|
32
|
-
{ code: 406, message: 'Not Acceptable' },
|
|
33
|
-
{ code: 407, message: 'Proxy Authentication Required' },
|
|
34
|
-
{ code: 408, message: 'Request Timeout' },
|
|
35
|
-
{ code: 409, message: 'Conflict' },
|
|
36
|
-
{ code: 410, message: 'Gone' },
|
|
37
|
-
{ code: 411, message: 'Length Required' },
|
|
38
|
-
{ code: 412, message: 'Precondition Failed' },
|
|
39
|
-
{ code: 413, message: 'Payload Too Large' },
|
|
40
|
-
{ code: 414, message: 'URI Too Long' },
|
|
41
|
-
{ code: 415, message: 'Unsupported Media Type' },
|
|
42
|
-
{ code: 416, message: 'Range Not Satisfiable' },
|
|
43
|
-
{ code: 417, message: 'Expectation Failed' },
|
|
44
|
-
{ code: 418, message: 'I\'m a teapot' },
|
|
45
|
-
{ code: 421, message: 'Misdirected Request' },
|
|
46
|
-
{ code: 422, message: 'Unprocessable Entity' },
|
|
47
|
-
{ code: 423, message: 'Locked' },
|
|
48
|
-
{ code: 424, message: 'Failed Dependency' },
|
|
49
|
-
{ code: 425, message: 'Too Early' },
|
|
50
|
-
{ code: 426, message: 'Upgrade Required' },
|
|
51
|
-
{ code: 428, message: 'Precondition Required' },
|
|
52
|
-
{ code: 429, message: 'Too Many Requests' },
|
|
53
|
-
{ code: 431, message: 'Request Header Fields Too Large' },
|
|
54
|
-
{ code: 451, message: 'Unavailable For Legal Reasons' },
|
|
55
|
-
{ code: 500, message: 'Internal Server Error' },
|
|
56
|
-
{ code: 501, message: 'Not Implemented' },
|
|
57
|
-
{ code: 502, message: 'Bad Gateway' },
|
|
58
|
-
{ code: 503, message: 'Service Unavailable' },
|
|
59
|
-
{ code: 504, message: 'Gateway Timeout' },
|
|
60
|
-
{ code: 505, message: 'HTTP Version Not Supported' },
|
|
61
|
-
{ code: 506, message: 'Variant Also Negotiates' },
|
|
62
|
-
{ code: 507, message: 'Insufficient Storage' },
|
|
63
|
-
{ code: 508, message: 'Loop Detected' },
|
|
64
|
-
{ code: 510, message: 'Not Extended' },
|
|
65
|
-
{ code: 511, message: 'Network Authentication Required' },
|
|
66
|
-
];
|
|
67
|
-
const statusSchema = {
|
|
68
|
-
description: 'Return status code or random status code if more than one are given ',
|
|
69
|
-
tags: ['Status Codes'],
|
|
70
|
-
params: {
|
|
71
|
-
type: 'object',
|
|
72
|
-
properties: {
|
|
73
|
-
code: { type: 'string', description: 'HTTP status code to be returned' },
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
response: {
|
|
77
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
78
|
-
200: {
|
|
79
|
-
type: 'object',
|
|
80
|
-
properties: {
|
|
81
|
-
status: { type: 'string' },
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
export const statusCodeRoute = (fastify) => {
|
|
87
|
-
const handler = async (request, reply) => {
|
|
88
|
-
let { code } = request.params;
|
|
89
|
-
// Check if the code is a valid HTTP status code, otherwise pick a random one
|
|
90
|
-
if (!code || !statusCodes.map(status => status.code).includes(Number(code))) {
|
|
91
|
-
code = statusCodes[randomInt(0, statusCodes.length)].code.toString();
|
|
92
|
-
}
|
|
93
|
-
await reply.code(Number(code)).send({
|
|
94
|
-
status: `Response with status code ${code}`,
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
fastify.get('/status/:code?', { schema: statusSchema }, handler);
|
|
98
|
-
fastify.post('/status/:code?', { schema: statusSchema }, handler);
|
|
99
|
-
fastify.put('/status/:code?', { schema: statusSchema }, handler);
|
|
100
|
-
fastify.patch('/status/:code?', { schema: statusSchema }, handler);
|
|
101
|
-
fastify.delete('/status/:code?', { schema: statusSchema }, handler);
|
|
102
|
-
};
|