From 8377fb9de033cb2e6e268b211a22e5d2551c1f56 Mon Sep 17 00:00:00 2001 From: jackkav Date: Mon, 26 Aug 2024 17:01:14 +0200 Subject: [PATCH] support alt methods --- lib/routes/bins/delete.js | 8 ++++++-- lib/routes/bins/log.js | 8 +++++++- lib/routes/bins/run.js | 9 +++++++-- lib/routes/bins/update.js | 14 +++++++------- lib/utils.js | 5 +++++ 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/lib/routes/bins/delete.js b/lib/routes/bins/delete.js index 4a6a09b3..1606773c 100644 --- a/lib/routes/bins/delete.js +++ b/lib/routes/bins/delete.js @@ -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); diff --git a/lib/routes/bins/log.js b/lib/routes/bins/log.js index d54bb59a..14fff117 100644 --- a/lib/routes/bins/log.js +++ b/lib/routes/bins/log.js @@ -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); diff --git a/lib/routes/bins/run.js b/lib/routes/bins/run.js index 03d9fe78..c5d5ff5c 100644 --- a/lib/routes/bins/run.js +++ b/lib/routes/bins/run.js @@ -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); diff --git a/lib/routes/bins/update.js b/lib/routes/bins/update.js index 4c428273..9528a807 100644 --- a/lib/routes/bins/update.js +++ b/lib/routes/bins/update.js @@ -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 { @@ -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 = { diff --git a/lib/utils.js b/lib/utils.js index 4c8683c7..c611a52a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 [];