Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support alt methods #181

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const pkg = require("../package.json");

const utils = {
// HTTP method + ID + route name: POST1234/foo OR 1234/foo
// method is gathered from the insomnia-mock-method header
// id is first slug of the url and path is the rest of the url
// NOTE: http method GET is ingored in the compound id in order to preserve existing mocks
// compoundId is used to store and retrieve mocks from redis
createCompoundId: (method, id, path) => {
return method?.toUpperCase() === "GET"
? id + path
: (method?.toUpperCase() || "") + id + path;
},
objectToArray: (obj) => {
if (!obj || typeof obj !== "object") {
return [];
Expand Down
Loading