Skip to content

Commit

Permalink
more progress on the API sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Oct 22, 2017
1 parent 64957a4 commit 2d981e4
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# What is MemServer?
MemServer is an in-memory database/ORM and http mock server you can run in-browser and node environments. Very useful library for large frontend teams, fast tests and rapid prototyping.
MemServer is an in-memory database/ORM and http mock server you can run in-browser and node environments. Extremely useful library for fast tests, rapid prototyping, single-file SPA demo deployments.
4 changes: 4 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- create a cli
- fixtures
- serialization embedding
- factories
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/photo-album/memserver/fixtures/photos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default [
];
5 changes: 5 additions & 0 deletions examples/photo-album/memserver/models/photo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Model } from 'memserver';

export default Model.extend({

});
10 changes: 10 additions & 0 deletions examples/photo-album/memserver/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function() {
// this.namespace = '';

this.get('/photos', () => {
return {
authors: [
]
};
});
}
11 changes: 11 additions & 0 deletions examples/photo-album/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "photo-album",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
42 changes: 31 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const ENVIRONMENT_IS_NODE = typeof global === 'object';
const ENVIRONMENT_IS_BROWSER = !ENVIRONMENT_IS_NODE;
const chalk = require('chalk');

const JSDOM = require('jsdom').JSDOM;
const dom = new JSDOM(`<p>Hello</p>`, { url: 'http://localhost' });
Expand All @@ -12,13 +13,23 @@ global.document = window.document;

window.FakeXMLHttpRequest = require('fake-xml-http-request');
window.RouteRecognizer = require('route-recognizer');
window.$ = require("jquery");
window.$ = require('jquery');

window.self = window;
global.self = window.self;

require('pretender');

// register models

// register fixtures

// scenario starts

// configurations

// register routes

let PHOTOS = {
'10': {
id: 10,
Expand All @@ -39,27 +50,36 @@ let server = new window.Pretender(function(){
this.get('/photos/:id', (request) => {
return [200, {'Content-Type': 'application/json'}, JSON.stringify(PHOTOS[request.params.id])];
});

this.get('/lol', this.passthrough);
});

server.handledRequest = function(verb, path, request) {
console.log('[PRETENDER - HANDLED REQUEST]', verb, path);
console.log(chalk.cyan('MemServer'), chalk.green('[HANDLED]'), verb, path, colorStatusCode(request.status));
console.log(JSON.parse(request.responseText));
}

server.passthroughRequest = function(verb, path, request) {
console.log(chalk.cyan('MemServer'), chalk.yellow('[PASSTHROUGH]'), verb, path);
}

server.unhandledRequest = function(verb, path, request) {
console.log('[PRETENDER - UNHANDLED REQUEST]', verb, path);
console.log(chalk.cyan('MemServer'), chalk.red('[UNHANDLED REQUEST]', verb, path));
console.log('REQUEST:');
console.log(request);
}

window.$.getJSON('/photos/10', (a) => {
console.log(a);
}).done(function() {
console.log('second success');
}).fail(function() {
console.log('error');
});
function colorStatusCode(statusCode) {
if (statusCode === 200 || statusCode === 201) {
return chalk.green(statusCode);
}

return chalk.red(statusCode);
}


// axiso.get('/photos/12')
window.$.getJSON('/photos/10');
window.$.getJSON('/lol');

// setTimeout(() => console.log('done'), 10000);

Expand Down
24 changes: 24 additions & 0 deletions lib/mem-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function start(options) {
// read current path to see if there is memserver

const MemServer = {
DB: {},
Models: {},
Pretender: {},
Routes: [],
start() {

},
shutdown() {

}
}



return MemServer
}



// BUILD A CLI
29 changes: 29 additions & 0 deletions lib/mem-server/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default {
insert(options) {

},
bulkInsert(count, options) {

},
find(id) {

},
where(options) {

},
findBy(options) {

},
update(record) {

},
destroy(record) {

},
all() {

},
serialize() {

}
}
Empty file added test/index.js
Empty file.

0 comments on commit 2d981e4

Please sign in to comment.