Skip to content

Commit

Permalink
support alt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Aug 26, 2024
1 parent 57bbb8b commit 8377fb9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/routes/bins/delete.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const debug = require("debug")("mockbin");

const { createCompoundId } = require("../../utils");
module.exports = (client) => (req, res, next) => {
const compoundId = req.params.uuid + req.params[0];
const compoundId = createCompoundId(
req.headers["insomnia-mock-method"],
req.params.uuid,
req.params[0],
);
client.del(`bin:${compoundId}`, (err) => {
if (err) {
debug(err);
Expand Down
8 changes: 7 additions & 1 deletion lib/routes/bins/log.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const debug = require("debug")("mockbin");
const pkg = require("../../../package.json");
const { createCompoundId } = require("../../utils");

module.exports = (client) => (req, res, next) => {
res.view = "bin/log";
const compoundId = req.params.uuid + req.params[0];
const compoundId = createCompoundId(
req.headers["insomnia-mock-method"],
req.params.uuid,
req.params[0],
);

client.lrange(`log:${compoundId}`, 0, -1, (err, history) => {
if (err) {
debug(err);
Expand Down
9 changes: 7 additions & 2 deletions lib/routes/bins/run.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const debug = require("debug")("mockbin");
const { createCompoundId } = require("../../utils");

module.exports = (client) => (req, res, next) => {
// compoundId allows us to provide paths in the id to resolve to a specific bin
const compoundId = req.params.uuid + req.params[0];
const compoundId = createCompoundId(
req.headers["insomnia-mock-method"],
req.params.uuid,
req.params[0],
);

client.get(`bin:${compoundId}`, function (err, value) {
if (err) {
debug(err);
Expand Down
14 changes: 7 additions & 7 deletions lib/routes/bins/update.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const debug = require("debug")("mockbin");
const e = require("express");
const validate = require("har-validator");
const path = require("node:path");

const { createCompoundId } = require("../../utils");
module.exports = (client) => (req, res, next) => {
const id = req.params.uuid;
const path = req.params[0];
const compoundId = id + path;
const compoundId = createCompoundId(
req.headers["insomnia-mock-method"],
req.params.uuid,
req.params[0],
);

let mock = req.jsonBody;

// overritten by application/x-www-form-urlencoded or multipart/form-data
if (req.simple.postData.text) {
try {
Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports = (client) => (req, res, next) => {
);

res.view = "redirect";
res.status(200).location(`/bin/${compoundId}`).body = id;
res.status(200).location(`/bin/${compoundId}`).body = req.params.uuid;
})
.catch((err) => {
res.body = {
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const pkg = require("../package.json");

const utils = {
// HTTP method + ID + route name: POST1234/foo OR 1234/foo
// NOTE: http method GET is ingored in the compound id in order to preserve existing mocks
createCompoundId: (method, id, path) => {
return method.toLowerCase() === "get" ? id + path : method + id + path;
},
objectToArray: (obj) => {
if (!obj || typeof obj !== "object") {
return [];
Expand Down

0 comments on commit 8377fb9

Please sign in to comment.