@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.
@@ -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
- };
@@ -1,28 +0,0 @@
1
- const statusSchema = {
2
- description: 'Returns a response with the provided status code',
3
- params: {
4
- type: 'object',
5
- properties: {
6
- code: { type: 'string', description: 'HTTP status code to be returned' },
7
- },
8
- required: ['code'],
9
- },
10
- response: {
11
- // eslint-disable-next-line @typescript-eslint/naming-convention
12
- 200: {
13
- type: 'object',
14
- properties: {
15
- status: { type: 'string' },
16
- },
17
- },
18
- },
19
- };
20
- export const statusRoute = (fastify) => {
21
- fastify.get('/status/:code', { schema: statusSchema }, async (request, reply) => {
22
- const { code } = request.params;
23
- await reply.code(Number(code));
24
- return {
25
- status: `Response with status code ${code}`,
26
- };
27
- });
28
- };
package/dist/swagger.js DELETED
@@ -1,34 +0,0 @@
1
- import { fastifySwaggerUi } from '@fastify/swagger-ui';
2
- export const fastifySwaggerConfig = {
3
- swagger: {
4
- info: {
5
- title: 'Mock HTTP API',
6
- description: 'A replacement for httpbin built using Fastify and Typescript',
7
- version: '1.0.0',
8
- },
9
- consumes: ['application/json'],
10
- produces: ['application/json'],
11
- },
12
- };
13
- export const registerSwaggerUi = async (fastify) => {
14
- await fastify.register(fastifySwaggerUi, {
15
- routePrefix: '/docs',
16
- uiConfig: {
17
- docExpansion: 'none',
18
- deepLinking: false,
19
- },
20
- uiHooks: {
21
- /* c8 ignore next 6 */
22
- onRequest(_request, _reply, next) {
23
- next();
24
- },
25
- preHandler(_request, _reply, next) {
26
- next();
27
- },
28
- },
29
- // eslint-disable-next-line @typescript-eslint/naming-convention
30
- staticCSP: true,
31
- transformSpecification: (swaggerObject, _request, _reply) => swaggerObject,
32
- transformSpecificationClone: true,
33
- });
34
- };