Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dmock-server

Package Overview
Dependencies
Maintainers
1
Versions
114
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dmock-server - npm Package Compare versions

Comparing version 1.0.9 to 1.1.0

21

lib/index.d.ts
/// <reference types="node" />
import { Server } from 'http';
import * as core from 'express-serve-static-core';
export declare class MockServer {

@@ -15,2 +16,5 @@ private handler;

* @param parameters.routes An array of the routes the server will mock
* @param parameters.routes.method The request method, for example: get
* @param parameters.routes.path The path of the url, for example: /user
* @param parameters.routes.response The response body, for example: { id: 1 }
*/

@@ -23,2 +27,9 @@ constructor(parameters: MockServerParameters);

/**
* Handling the response
* @param req The request object
* @param res The response object
* @param route The route object
*/
private handleRequest;
/**
* Stopping the mock server

@@ -47,2 +58,3 @@ */

* @param path The path of the url, for example: /user
* @param statusCode The status code of the response
* @param response The response body, for example: { id: 1 }

@@ -53,5 +65,6 @@ */

path: string;
statusCode?: number;
response: {
[key: string]: any;
};
} | GenericFunction;
};

@@ -61,3 +74,7 @@ /**

*/
declare type RequestMethod = 'get' | 'post' | 'put' | 'delete';
declare type RequestMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
/**
* A generic flexible function for function response handling
*/
declare type GenericFunction = (request: core.Request<core.ParamsDictionary, any, any, core.Query>, response: core.Response<any>) => void;
export {};

39

lib/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = require("http");
const express = require("express");
const http_1 = require("http");
const parser = require("body-parser");
class MockServer {

@@ -12,2 +13,5 @@ /**

* @param parameters.routes An array of the routes the server will mock
* @param parameters.routes.method The request method, for example: get
* @param parameters.routes.path The path of the url, for example: /user
* @param parameters.routes.response The response body, for example: { id: 1 }
*/

@@ -28,11 +32,20 @@ constructor(parameters) {

start() {
this.handler.use(parser.urlencoded({ extended: true }));
this.handler.use(parser.json());
for (const route of this.routes) {
const scope = this;
if (route.method === 'get')
this.handler.get(route.path, function (req, res) { res.send(route.response); });
if (route.method === 'post')
this.handler.post(route.path, function (req, res) { res.send(route.response); });
if (route.method === 'put')
this.handler.put(route.path, function (req, res) { res.send(route.response); });
if (route.method === 'delete')
this.handler.delete(route.path, function (req, res) { res.send(route.response); });
this.handler.get(route.path, function (req, res) { scope.handleRequest(req, res, route); });
else if (route.method === 'post')
this.handler.post(route.path, function (req, res) { scope.handleRequest(req, res, route); });
else if (route.method === 'put')
this.handler.put(route.path, function (req, res) { scope.handleRequest(req, res, route); });
else if (route.method === 'delete')
this.handler.delete(route.path, function (req, res) { scope.handleRequest(req, res, route); });
else if (route.method === 'patch')
this.handler.patch(route.path, function (req, res) { scope.handleRequest(req, res, route); });
else if (route.method === 'options')
this.handler.options(route.path, function (req, res) { scope.handleRequest(req, res, route); });
else if (route.method === 'head')
this.handler.head(route.path, function (req, res) { scope.handleRequest(req, res, route); });
}

@@ -42,2 +55,12 @@ this.server = http_1.createServer(this.handler).listen(this.port, this.hostname);

/**
* Handling the response
* @param req The request object
* @param res The response object
* @param route The route object
*/
handleRequest(req, res, route) {
const response = (typeof route.response === 'function') ? route.response(req, res) : route.response;
res.status((route.statusCode !== undefined) ? route.statusCode : 200).send(response);
}
/**
* Stopping the mock server

@@ -44,0 +67,0 @@ */

{
"name": "dmock-server",
"version": "1.0.9",
"version": "1.1.0",
"description": "A typescript based mock server NPM module",

@@ -8,3 +8,4 @@ "main": "lib/index.js",

"scripts": {
"build": "tsc"
"build": "tsc",
"test": "mocha -r ts-node/register tests/unit-test.ts"
},

@@ -15,7 +16,15 @@ "keywords": [],

"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/express": "^4.17.6",
"ts-node": "^8.8.2",
"axios": "^0.19.2",
"@types/chai": "^4.2.7",
"@types/mocha": "^7.0.1",
"@types/node": "^13.1.1",
"chai": "^4.2.0",
"mocha": "^7.0.1",
"ts-node": "^8.5.4",
"typescript": "^3.8.3"

@@ -22,0 +31,0 @@ },

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc