@jaredwray/mockhttp 0.0.1 → 0.2.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/package.json CHANGED
@@ -1,12 +1,23 @@
1
1
  {
2
2
  "name": "@jaredwray/mockhttp",
3
- "version": "0.0.1",
3
+ "version": "0.2.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,17 +35,24 @@
24
35
  },
25
36
  "homepage": "https://github.com/jaredwray/mockhttp#readme",
26
37
  "devDependencies": {
27
- "@vitest/coverage-v8": "^2.1.5",
38
+ "@swc/core": "^1.10.1",
39
+ "@types/html-escaper": "^3.0.2",
40
+ "@vitest/coverage-v8": "^2.1.8",
41
+ "rimraf": "^6.0.1",
42
+ "tsup": "^8.3.5",
28
43
  "tsx": "^4.19.2",
29
44
  "typescript": "^5.7.2",
30
- "vitest": "^2.1.5",
31
- "xo": "^0.59.3"
45
+ "vitest": "^2.1.8",
46
+ "xo": "^0.60.0"
32
47
  },
33
48
  "dependencies": {
49
+ "@fastify/helmet": "^13.0.0",
34
50
  "@fastify/static": "^8.0.3",
35
51
  "@fastify/swagger": "^9.4.0",
36
- "@fastify/swagger-ui": "^5.1.0",
52
+ "@fastify/swagger-ui": "^5.2.0",
53
+ "@scalar/api-reference": "^1.25.80",
37
54
  "fastify": "^5.1.0",
55
+ "html-escaper": "^3.0.3",
38
56
  "pino": "^9.5.0",
39
57
  "pino-pretty": "^13.0.0"
40
58
  },
@@ -42,11 +60,5 @@
42
60
  "dist",
43
61
  "public",
44
62
  "LICENCE"
45
- ],
46
- "scripts": {
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
- }
63
+ ]
64
+ }
@@ -1,12 +0,0 @@
1
- export const fastifyConfig = {
2
- logger: {
3
- transport: {
4
- target: 'pino-pretty',
5
- options: {
6
- colorize: true,
7
- translateTime: true,
8
- ignore: 'pid,hostname',
9
- },
10
- },
11
- },
12
- };
@@ -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
- };
@@ -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,5 +0,0 @@
1
- export { getRoute } from './get.js';
2
- export { postRoute } from './post.js';
3
- export { deleteRoute } from './delete.js';
4
- export { putRoute } from './put.js';
5
- export { patchRoute } from './patch.js';
@@ -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
- };
@@ -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
- };
@@ -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,3 +0,0 @@
1
- export { ipRoute } from './ip.js';
2
- export { headersRoute } from './headers.js';
3
- export { userAgentRoute } from './user-agent.js';
@@ -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
- };