@r35007/mock-server
Advanced tools
Changelog
v9.1.0
wont't set
rewriters`. - Now It will set to the global rewritersconfig.host
- Set to empty string to set the host as your Local Ip Address.Changelog
v9.0.1
Fixed
.mockServer.default
wont't set config
.mockServer.rewriter
wont't set rewriters
.Setter
methods to set any data.Changelog
v9.0.0
_getDb()
, _getStore
to getDb()
, getStore()
.index.js
file from the given folder path instead of getting all the files from the folder.db.js
const { MockServer } = require('@r35007/mock-server');
module.exports = (mockServer) => {
if (mockServer) {
// This can be undefined If you don't pass the mockServer instance.
}
return {
// Your Db here
'/post': { id: 1, name: 'foo' },
};
};
const mockServer = MockServer.Create();
const resources = mockServer.resource(db, { mockServer });
mockServer.app.use(resources);
Note: For now it cant handle async methods.
// launchServer sets all the resource data. It sets db, injectors, middlewares, store, rewriters, middlewares.
launchServer(db?: ParamTypes.Db, { injectors, middlewares, store, rewriters, router }?: LaunchServerOptions): Promise<Server | undefined>;
// Resources sets only db and it will not set injectors, middlewares or any other options provided. This db will be added to the existing db.
resources(db?: ParamTypes.Db, { injectors, middlewares, mockServer, reverse, rootPath, dbMode, router }?: ResourceOptions): express.Router;
// Setters
setData(data?: SetData, options?: SetterOptions): void;
setConfig(config?: Params.Config, { rootPath, merge, mockServer }?: SetterOptions): void;
setRewriters(rewriters?: Params.Rewriters, { rootPath, merge, mockServer }?: SetterOptions): void;
setMiddlewares(middleware?: Params.Middlewares, { rootPath, merge, mockServer }?: SetterOptions): void;
setInjectors(injectors?: Params.Injectors, { rootPath, merge, mockServer }?: SetterOptions): void;
setStore(store?: Params.Store, { rootPath, merge, mockServer }?: SetterOptions): void;
setDb(db?: Params.Db, { injectors, rootPath, reverse, dbMode, merge, mockServer }?: SetterOptions): void;
// Validators
getValidConfig(config?: ParamTypes.Config | undefined, { rootPath, mockServer }?: SetterOptions): ValidTypes.Config;
getValidMiddlewares(middlewares?: ParamTypes.Middlewares | undefined, { rootPath, mockServer }?: SetterOptions): ValidTypes.Middlewares;
getValidInjectors(injectors?: ParamTypes.Injectors | undefined, { rootPath, mockServer }?: SetterOptions): ValidTypes.Injectors;
getValidStore(store?: ParamTypes.Store | undefined, { rootPath, mockServer }?: SetterOptions): ValidTypes.Store;
getValidRewriters(rewriters?: ParamTypes.Rewriters | undefined, { rootPath, mockServer }?: SetterOptions): ValidTypes.Rewriters;
getValidDb(data?: ParamTypes.Db | undefined, { mockServer, injectors, rootPath, reverse, dbMode }?: SetterOptions): ValidTypes.Db;
Changelog
v8.0.0
added merge
param in all setters. If true it merges the data with the existing.
For Example:
mockServer.setDb({ user:{ id: 1, name: "foo" } });
mockServer.setDb({ post:{ id: 1, name: "bar" } }); // replaces the previous one
mockServer.setDb({ comment:{ id: 1, comment: "I am Happy !" } }, true); // adds to the previous one
mockServer.db
{
"/post":{_config: true, mock: { id: 1, name:"bar" } }
"/comment":{_config: true, mock: { id: 1, comment: "I am Happy !" } }
}
renamed defaultRoutes
to homePage
. Now we can use mockServer.homePage
which returns the hoePage router.
removed - mockServer.router
removed - mockServer.addDb
instead use mockServer.resource
update - mockServer.resources
will always return the new router and if repeated it will add the resource to the existing resources.
For Example:
const user = mockServer.resources({ user:{ id: 1, name: "foo" } }); // creates user resource
mockServer.app.use(user);
const post = mockServer.resources({ post:{ id: 1, name: "bar" } }); // creates post resource. this resource will be added in db with the existing resources.
mockServer.app.use(post);
mockServer.db
{
"/user":{_config: true, mock: { id: 1, name:"foo" } }
"/post":{_config: true, mock: { id: 1, name:"bar" } }
}
updated config.staticDir
- It uses ./public
directory as the default static host directory. Set it to empty string to avoid hosting static dir.
Changelog
v7.3.0
renamed mode
to dbMode
in config.
added multi
dbMode in config.
dbMode :
multi
- Only direct sting value sets to fetch
attribute. All other direct values will be set to - mock
attribute.fetch
- All other direct values will be set to fetch
attribute.mock
- All other direct values will be set to mock
attribute.Fetch not showing any error on a invalid file path - Fixed
Changelog
v7.2.0
added mockServer.port
- Gives the current running port. This will be undefined
if server is stopped.
added mockServer.address
- Gives the current running server ip address. This will be undefined
if server is stopped.
Set Port to 0
(zero) to pick a dynamic available port
Now we can do default import Example of imports :
const { MockServer } = require('@r35007/mock-server');
const MockServer = require('@r35007/mock-server').default;
import MockServer from '@r35007/mock-server';
import { MockServer } from '@r35007/mock-server';