This package has been deprecated

Author message:

this version has been deprecated

mock-from-git

1.0.1-alpha • Public • Published

mock-from-git

A node module that returns JSON from public github repositories. Intended for use in Express web servers returning mock JSON for development.

How to use

npm i mock-from-git

Then in your Express web server, create a route to serve your mock.

const mfg = require('mock-from-git');

app.get(mfg.githubPath, (req, res) => {
    handleReq(req, res);
});

async function handleReq(req, res) {
    try {
        const config = {token: <your github personal access token>};
        const result = await mfg.fetch(req, config);
        res.json(result);
    } catch (error) {
        res.json({error: error.message});
    }
}

Static JSON models

Use a request like this to server a JSON file stored in Github.

http://yourhost/<github domain and path to your JSON file>

Dynamic JSON models

To get dynamic models, a javascript module can be used. Create a javascript module that exports a single function with signature function(request). The module can "require" local JSON files, but not other modules. It's meant to be used as a handler than can munge together JSON based on the values in the request. It should then return the JSON needed (See "Example mock handler" further down the page).

http://yourhost/<github domain and path to your javascript module>

What I usually do is go to Github and navigate to the file I want to serve. Then copy that url (except for the protocol) from the browser and paste it after "http://your-host/" .

Example mock handler

Take a look https://github.com/smallpattern/mock-from-git/blob/master/mock/a/a.js for an example of a simple handler. It can take the parameter "file" which can have values "a", "a1", "b", "b1" or "root". It will return different JSON files based on the "file" param value.

To get the JSON for "a1" I would use the url:

http://<your host>/github.com/smallpattern/mock-from-git/blob/master/mock/a/a.js?file=a1

Github token

Although you can start the server and make a few requests, it's expected that you use a Github token. Without a token the number of requests you can make is very limited. With a token the limit is 5,000 per hour.

In your Github account, go to Settings > Developer settings and create a "Personal access token".

Readme

Keywords

none

Package Sidebar

Install

npm i mock-from-git

Weekly Downloads

0

Version

1.0.1-alpha

License

ISC

Unpacked Size

11.2 kB

Total Files

7

Last publish

Collaborators

  • smallpattern