@jaredwray/mockhttp 0.1.0 → 0.2.1

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.
Files changed (2) hide show
  1. package/dist/index.js +186 -4
  2. package/package.json +4 -2
package/dist/index.js CHANGED
@@ -90,7 +90,8 @@ var fastifyConfig = {
90
90
  options: {
91
91
  colorize: true,
92
92
  translateTime: true,
93
- ignore: "pid,hostname"
93
+ ignore: "pid,hostname",
94
+ singleLine: true
94
95
  }
95
96
  }
96
97
  }
@@ -102,7 +103,7 @@ import { fastifySwaggerUi } from "@fastify/swagger-ui";
102
103
  // package.json
103
104
  var package_default = {
104
105
  name: "@jaredwray/mockhttp",
105
- version: "0.1.0",
106
+ version: "0.2.1",
106
107
  description: "Mock Http - Easy way to mock http with httpbin replacement",
107
108
  type: "module",
108
109
  main: "dist/index.js",
@@ -138,6 +139,7 @@ var package_default = {
138
139
  homepage: "https://github.com/jaredwray/mockhttp#readme",
139
140
  devDependencies: {
140
141
  "@swc/core": "^1.10.1",
142
+ "@types/html-escaper": "^3.0.2",
141
143
  "@vitest/coverage-v8": "^2.1.8",
142
144
  rimraf: "^6.0.1",
143
145
  tsup: "^8.3.5",
@@ -151,8 +153,9 @@ var package_default = {
151
153
  "@fastify/static": "^8.0.3",
152
154
  "@fastify/swagger": "^9.4.0",
153
155
  "@fastify/swagger-ui": "^5.2.0",
154
- "@scalar/api-reference": "^1.25.80",
156
+ "@scalar/api-reference": "^1.25.82",
155
157
  fastify: "^5.1.0",
158
+ "html-escaper": "^3.0.3",
156
159
  pino: "^9.5.0",
157
160
  "pino-pretty": "^13.0.0"
158
161
  },
@@ -865,6 +868,182 @@ var statusCodeRoute = /* @__PURE__ */ __name((fastify2) => {
865
868
  }, handler);
866
869
  }, "statusCodeRoute");
867
870
 
871
+ // src/routes/response-inspection/cache.ts
872
+ var cacheGetSchema = {
873
+ description: "Handles cache validation and retrieval.",
874
+ tags: [
875
+ "Response Inspection"
876
+ ],
877
+ response: {
878
+ // eslint-disable-next-line @typescript-eslint/naming-convention
879
+ 200: {
880
+ type: "string",
881
+ description: "The cached content or resource."
882
+ },
883
+ // eslint-disable-next-line @typescript-eslint/naming-convention
884
+ 304: {
885
+ type: "null",
886
+ description: "Indicates the resource has not been modified."
887
+ }
888
+ }
889
+ };
890
+ var cacheSetSchema = {
891
+ description: "Sets a Cache-Control header for n seconds.",
892
+ tags: [
893
+ "Response Inspection"
894
+ ],
895
+ params: {
896
+ type: "object",
897
+ properties: {
898
+ value: {
899
+ type: "integer",
900
+ description: "Number of seconds to cache the resource."
901
+ }
902
+ },
903
+ required: [
904
+ "value"
905
+ ]
906
+ },
907
+ response: {
908
+ // eslint-disable-next-line @typescript-eslint/naming-convention
909
+ 200: {
910
+ type: "string",
911
+ description: "Confirmation message for setting cache."
912
+ }
913
+ }
914
+ };
915
+ var cacheRoutes = /* @__PURE__ */ __name((fastify2) => {
916
+ fastify2.get("/cache", {
917
+ schema: cacheGetSchema
918
+ }, async (request, reply) => {
919
+ const ifModifiedSince = request.headers["if-modified-since"];
920
+ const ifNoneMatch = request.headers["if-none-match"];
921
+ if (ifModifiedSince ?? ifNoneMatch) {
922
+ await reply.status(304).send();
923
+ return;
924
+ }
925
+ await reply.send("Cache content or resource response here.");
926
+ });
927
+ fastify2.get("/cache/:value", {
928
+ schema: cacheSetSchema
929
+ }, async (request, reply) => {
930
+ const { value } = request.params;
931
+ await reply.header("Cache-Control", `max-age=${value}`).send(`Cache-Control set for ${value} seconds.`);
932
+ });
933
+ }, "cacheRoutes");
934
+
935
+ // src/routes/response-inspection/etag.ts
936
+ var etagSchema = {
937
+ description: "Handles ETag-based conditional requests.",
938
+ tags: [
939
+ "Response Inspection"
940
+ ],
941
+ params: {
942
+ type: "object",
943
+ properties: {
944
+ etag: {
945
+ type: "string",
946
+ description: "The ETag value for the resource."
947
+ }
948
+ },
949
+ required: [
950
+ "etag"
951
+ ]
952
+ },
953
+ response: {
954
+ // eslint-disable-next-line @typescript-eslint/naming-convention
955
+ 200: {
956
+ type: "string",
957
+ description: "Resource content for matching ETag."
958
+ },
959
+ // eslint-disable-next-line @typescript-eslint/naming-convention
960
+ 304: {
961
+ type: "null",
962
+ description: "Indicates the resource has not been modified."
963
+ },
964
+ // eslint-disable-next-line @typescript-eslint/naming-convention
965
+ 412: {
966
+ type: "null",
967
+ description: "Indicates a precondition failed due to mismatched ETag."
968
+ }
969
+ }
970
+ };
971
+ var etagRoutes = /* @__PURE__ */ __name((fastify2) => {
972
+ fastify2.get("/etag/:etag", {
973
+ schema: etagSchema
974
+ }, async (request, reply) => {
975
+ const { etag } = request.params;
976
+ const ifNoneMatch = request.headers["if-none-match"];
977
+ const ifMatch = request.headers["if-match"];
978
+ if (ifNoneMatch === etag) {
979
+ await reply.status(304).send();
980
+ return;
981
+ }
982
+ if (ifMatch && ifMatch !== etag) {
983
+ await reply.status(412).send();
984
+ return;
985
+ }
986
+ await reply.header("ETag", etag).send("Resource content for matching ETag.");
987
+ });
988
+ }, "etagRoutes");
989
+
990
+ // src/routes/response-inspection/response-headers.ts
991
+ import { escape } from "html-escaper";
992
+ var responseHeadersSchema = {
993
+ description: "Returns a set of response headers based on the query string.",
994
+ tags: [
995
+ "Response Inspection"
996
+ ],
997
+ querystring: {
998
+ type: "object",
999
+ additionalProperties: true,
1000
+ description: "Freeform query string parameters to be added as response headers."
1001
+ },
1002
+ response: {
1003
+ // eslint-disable-next-line @typescript-eslint/naming-convention
1004
+ 200: {
1005
+ type: "object",
1006
+ additionalProperties: true,
1007
+ description: "Response headers as per query string parameters."
1008
+ }
1009
+ }
1010
+ };
1011
+ var responseHeadersRoutes = /* @__PURE__ */ __name((fastify2) => {
1012
+ fastify2.get("/response-headers", {
1013
+ schema: responseHeadersSchema
1014
+ }, async (request, reply) => {
1015
+ const queryParameters = request.query;
1016
+ for (const [key, value] of Object.entries(queryParameters)) {
1017
+ reply.header(key, escape(value));
1018
+ }
1019
+ const cleanedResponse = Object.fromEntries(Object.entries(queryParameters).map(([key, value]) => [
1020
+ key,
1021
+ escape(value)
1022
+ ]));
1023
+ await reply.send(cleanedResponse);
1024
+ });
1025
+ fastify2.post("/response-headers", {
1026
+ schema: responseHeadersSchema
1027
+ }, async (request, reply) => {
1028
+ const queryParameters = request.query ?? {};
1029
+ const body = request.body ?? {};
1030
+ for (const [key, value] of Object.entries(queryParameters)) {
1031
+ reply.header(key, escape(value));
1032
+ }
1033
+ for (const [key, value] of Object.entries(body)) {
1034
+ reply.header(key, escape(value));
1035
+ }
1036
+ const cleanedResponse = Object.fromEntries(Object.entries({
1037
+ ...queryParameters,
1038
+ ...body
1039
+ }).map(([key, value]) => [
1040
+ key,
1041
+ escape(value)
1042
+ ]));
1043
+ await reply.send(cleanedResponse);
1044
+ });
1045
+ }, "responseHeadersRoutes");
1046
+
868
1047
  // src/index.ts
869
1048
  var fastify = Fastify(fastifyConfig);
870
1049
  await fastify.register(fastifyStatic, {
@@ -884,10 +1063,13 @@ await fastify.register(postRoute);
884
1063
  await fastify.register(deleteRoute);
885
1064
  await fastify.register(putRoute);
886
1065
  await fastify.register(patchRoute);
1066
+ await fastify.register(statusCodeRoute);
887
1067
  await fastify.register(ipRoute);
888
1068
  await fastify.register(headersRoute);
889
1069
  await fastify.register(userAgentRoute);
890
- await fastify.register(statusCodeRoute);
1070
+ await fastify.register(cacheRoutes);
1071
+ await fastify.register(etagRoutes);
1072
+ await fastify.register(responseHeadersRoutes);
891
1073
  var start = /* @__PURE__ */ __name(async () => {
892
1074
  try {
893
1075
  let port = 3e3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaredwray/mockhttp",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Mock Http - Easy way to mock http with httpbin replacement",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -36,6 +36,7 @@
36
36
  "homepage": "https://github.com/jaredwray/mockhttp#readme",
37
37
  "devDependencies": {
38
38
  "@swc/core": "^1.10.1",
39
+ "@types/html-escaper": "^3.0.2",
39
40
  "@vitest/coverage-v8": "^2.1.8",
40
41
  "rimraf": "^6.0.1",
41
42
  "tsup": "^8.3.5",
@@ -49,8 +50,9 @@
49
50
  "@fastify/static": "^8.0.3",
50
51
  "@fastify/swagger": "^9.4.0",
51
52
  "@fastify/swagger-ui": "^5.2.0",
52
- "@scalar/api-reference": "^1.25.80",
53
+ "@scalar/api-reference": "^1.25.82",
53
54
  "fastify": "^5.1.0",
55
+ "html-escaper": "^3.0.3",
54
56
  "pino": "^9.5.0",
55
57
  "pino-pretty": "^13.0.0"
56
58
  },