Skip to content

Commit

Permalink
major upgrade to the MemServer initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Oct 22, 2017
1 parent 2d981e4 commit f4640c7
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 88 deletions.
7 changes: 7 additions & 0 deletions examples/photo-album/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('@std/esm');

import MemServer from '../../index.js';

MemServer.start();

window.$.getJSON('/photos');
15 changes: 15 additions & 0 deletions examples/photo-album/memserver/fixtures/photos.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
export default [
{
id: 1,
name: 'Ski trip',
href: 'ski-trip.jpeg'
},
{
id: 2,
name: 'Family photo',
href: 'family-photo.jpeg'
},
{
id: 3,
name: 'Selfie',
href: 'selfie.jpeg'
}
];
4 changes: 2 additions & 2 deletions examples/photo-album/memserver/models/photo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Model } from 'memserver';
import Model from '../../../../lib/mem-server/model';

export default Model.extend({
export default Object.assign(Model, {

});
13 changes: 12 additions & 1 deletion examples/photo-album/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"@std/esm": "^0.12.1",
"core-object": "^3.1.5",
"ember-inflector": "^2.0.1"
},
"@std/esm": {
"esm": "all"
},
"devDependencies": {
"pryjs": "^1.0.3"
}
}
111 changes: 55 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// require('@std/esm');
// pry = require('pryjs');
require('@std/esm');

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

const chalk = require('chalk');
const JSDOM = require('jsdom').JSDOM;
const dom = new JSDOM(`<p>Hello</p>`, { url: 'http://localhost' });

Expand All @@ -15,71 +14,71 @@ window.FakeXMLHttpRequest = require('fake-xml-http-request');
window.RouteRecognizer = require('route-recognizer');
window.$ = require('jquery');

window.self = window;
// window.self = window; // NOTE: maybe removing this is dangerous
global.self = window.self;

require('pretender');

// register models
import MemServer from './lib/mem-server.js';

// register fixtures
export default MemServer;

// scenario starts

// configurations

// register routes

let PHOTOS = {
'10': {
id: 10,
src: 'http://media.giphy.com/media/UdqUo8xvEcvgA/giphy.gif'
},
'42': {
id: 42,
src: 'http://media0.giphy.com/media/Ko2pyD26RdYRi/giphy.gif'
}
};

let server = new window.Pretender(function(){
this.get('/photos', (request) => {
var all = JSON.stringify(Object.keys(PHOTOS).map((k) => PHOTOS[k]));
return [200, {'Content-Type': 'application/json'}, all];
});

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(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(chalk.cyan('MemServer'), chalk.red('[UNHANDLED REQUEST]', verb, path));
console.log('REQUEST:');
console.log(request);
}

function colorStatusCode(statusCode) {
if (statusCode === 200 || statusCode === 201) {
return chalk.green(statusCode);
}

return chalk.red(statusCode);
}


window.$.getJSON('/photos/10');
window.$.getJSON('/lol');
// let PHOTOS = {
// '10': {
// id: 10,
// src: 'http://media.giphy.com/media/UdqUo8xvEcvgA/giphy.gif'
// },
// '42': {
// id: 42,
// src: 'http://media0.giphy.com/media/Ko2pyD26RdYRi/giphy.gif'
// }
// };

// let server = new window.Pretender(function(){
// this.get('/photos', (request) => {
// var all = JSON.stringify(Object.keys(PHOTOS).map((k) => PHOTOS[k]));
// return [200, {'Content-Type': 'application/json'}, all];
// });
//
// 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(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(chalk.cyan('MemServer'), chalk.red('[UNHANDLED REQUEST]', verb, path));
// console.log('REQUEST:');
// console.log(request);
// }

// function colorStatusCode(statusCode) {
// if (statusCode === 200 || statusCode === 201) {
// return chalk.green(statusCode);
// }
//
// return chalk.red(statusCode);
// }
//
//
// window.$.getJSON('/photos/10');
// window.$.getJSON('/lol');

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

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

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

},
shutdown() {

}
import fs from 'fs';
import chalk from 'chalk';
import stringUtils from 'ember-cli-string-utils';

const inflect = require('i')(); // NOTE: make this ES6 import

if (!fs.existsSync('memserver')) {
throw new Error(chalk.red('/memserver folder doesn\'t exist for this directory!'));
}

const modelFileNames = fs.readdirSync('memserver/models');
const Models = modelFileNames.reduce((result, modelFileName) => {
const ModelName = stringUtils.classify(modelFileName.slice(0, -3));

result[ModelName] = require(`./memserver/models/${modelFileName}`).default; // NOTE: make this ES6 import
result[ModelName].modelName = ModelName;
return result;
}, {});

const DB = Object.keys(Models).reduce((result, modelName) => {
console.log('modelName is', modelName);
const fileName = stringUtils.dasherize(inflect.pluralize(modelName));
console.log('fileName is', fileName);
const path = `./memserver/fixtures/${fileName}.js`;

if (fs.existsSync(path)) {
result[modelName] = require(path).default; // NOTE: make this ES6 import
// TODO: maybe check ids must exist and all of them are integers
}

return result;
}, {});


return MemServer
}
export default {
DB: DB,
Models: Models,
Pretender: {},
Routes: [],
start(options) {
delete this.start;

return this;
},
shutdown() {

}
}


// BUILD A CLI
11 changes: 6 additions & 5 deletions lib/mem-server/model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// NOTE: make this an Ember.Object.extend
// import EmberObject from 'core-object';

export default {
modelName: '',
insert(options) {

},
Expand All @@ -8,7 +12,7 @@ export default {
find(id) {

},
where(options) {
findAll(options) {

},
findBy(options) {
Expand All @@ -20,10 +24,7 @@ export default {
destroy(record) {

},
all() {
serialize(objectOrArray) {

},
serialize() {

}
}
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"description": "in-memory database/ORM and http mock server you can run in-browser and node environments. Built for large frontend teams, fast tests and rapid prototyping",
"main": "index.js",
"license": "MIT",
"license": "ISC",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -12,19 +12,23 @@
"url": "https://github.com/izelnakri/memserver.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"@std/esm": "^0.12.1",
"chalk": "^2.2.0",
"ember": "^1.0.3",
"core-object": "^3.1.5",
"ember-cli-string-utils": "^1.1.0",
"ember-inflector": "^2.0.1",
"fake-xml-http-request": "^1.6.0",
"i": "^0.3.6",
"jsdom": "^11.3.0",
"pretender": "^1.6.0",
"route-recognizer": "^0.3.3"
},
"devDependencies": {
"jquery": "^3.2.1",
"pryjs": "^1.0.3"
},
"@std/esm": {
"esm": "all"
}
}
20 changes: 15 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ chalk@^1.1.3:
strip-ansi "^3.0.0"
supports-color "^2.0.0"

chalk@^2.2.0:
chalk@^2.0.0, chalk@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.2.0.tgz#477b3bf2f9b8fd5ca9e429747e37f724ee7af240"
dependencies:
Expand Down Expand Up @@ -825,6 +825,12 @@ core-js@^2.4.0, core-js@^2.5.0:
version "2.5.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.1.tgz#ae6874dc66937789b80754ff5428df66819ca50b"

core-object@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/core-object/-/core-object-3.1.5.tgz#fa627b87502adc98045e44678e9a8ec3b9c0d2a9"
dependencies:
chalk "^2.0.0"

core-util-is@1.0.2, core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
Expand Down Expand Up @@ -913,6 +919,10 @@ ember-cli-babel@^6.0.0:
clone "^2.0.0"
ember-cli-version-checker "^2.0.0"

ember-cli-string-utils@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/ember-cli-string-utils/-/ember-cli-string-utils-1.1.0.tgz#39b677fc2805f55173735376fcef278eaa4452a1"

ember-cli-version-checker@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/ember-cli-version-checker/-/ember-cli-version-checker-2.1.0.tgz#fc79a56032f3717cf844ada7cbdec1a06fedb604"
Expand All @@ -930,10 +940,6 @@ ember-rfc176-data@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/ember-rfc176-data/-/ember-rfc176-data-0.3.0.tgz#6aee728cb521c5f80710990965027b9c320f6f08"

ember@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/ember/-/ember-1.0.3.tgz#783670495ce15fc5f4a36534b2f50e7fec60c0b0"

ensure-posix-path@^1.0.0, ensure-posix-path@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/ensure-posix-path/-/ensure-posix-path-1.0.2.tgz#a65b3e42d0b71cfc585eb774f9943c8d9b91b0c2"
Expand Down Expand Up @@ -1134,6 +1140,10 @@ http-signature@~1.2.0:
jsprim "^1.2.2"
sshpk "^1.7.0"

i@^0.3.6:
version "0.3.6"
resolved "https://registry.yarnpkg.com/i/-/i-0.3.6.tgz#d96c92732076f072711b6b10fd7d4f65ad8ee23d"

iconv-lite@0.4.13:
version "0.4.13"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2"
Expand Down

0 comments on commit f4640c7

Please sign in to comment.