From 2d981e4e22b00a0c8dbc067620356753f79b8ffa Mon Sep 17 00:00:00 2001 From: Izel Nakri Date: Sun, 22 Oct 2017 16:38:10 +0200 Subject: [PATCH] more progress on the API sketch --- README.md | 2 +- TODO | 4 ++ lib/model.js => examples/photo-album/index.js | 0 .../photo-album/memserver/fixtures/photos.js | 2 + .../photo-album/memserver/models/photo.js | 5 +++ examples/photo-album/memserver/server.js | 10 +++++ examples/photo-album/package.json | 11 +++++ index.js | 42 ++++++++++++++----- lib/mem-server.js | 24 +++++++++++ lib/mem-server/model.js | 29 +++++++++++++ test/index.js | 0 11 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 TODO rename lib/model.js => examples/photo-album/index.js (100%) create mode 100644 examples/photo-album/memserver/fixtures/photos.js create mode 100644 examples/photo-album/memserver/models/photo.js create mode 100644 examples/photo-album/memserver/server.js create mode 100644 examples/photo-album/package.json create mode 100644 lib/mem-server.js create mode 100644 lib/mem-server/model.js create mode 100644 test/index.js diff --git a/README.md b/README.md index 0d5d20d..27c462a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/TODO b/TODO new file mode 100644 index 0000000..1d4c61d --- /dev/null +++ b/TODO @@ -0,0 +1,4 @@ +- create a cli +- fixtures +- serialization embedding +- factories diff --git a/lib/model.js b/examples/photo-album/index.js similarity index 100% rename from lib/model.js rename to examples/photo-album/index.js diff --git a/examples/photo-album/memserver/fixtures/photos.js b/examples/photo-album/memserver/fixtures/photos.js new file mode 100644 index 0000000..9f25cb1 --- /dev/null +++ b/examples/photo-album/memserver/fixtures/photos.js @@ -0,0 +1,2 @@ +export default [ +]; diff --git a/examples/photo-album/memserver/models/photo.js b/examples/photo-album/memserver/models/photo.js new file mode 100644 index 0000000..aef72a0 --- /dev/null +++ b/examples/photo-album/memserver/models/photo.js @@ -0,0 +1,5 @@ +import { Model } from 'memserver'; + +export default Model.extend({ + +}); diff --git a/examples/photo-album/memserver/server.js b/examples/photo-album/memserver/server.js new file mode 100644 index 0000000..2d63480 --- /dev/null +++ b/examples/photo-album/memserver/server.js @@ -0,0 +1,10 @@ +export default function() { + // this.namespace = ''; + + this.get('/photos', () => { + return { + authors: [ + ] + }; + }); +} diff --git a/examples/photo-album/package.json b/examples/photo-album/package.json new file mode 100644 index 0000000..281cdac --- /dev/null +++ b/examples/photo-album/package.json @@ -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" +} diff --git a/index.js b/index.js index ea11dfe..c10d185 100644 --- a/index.js +++ b/index.js @@ -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(`

Hello

`, { url: 'http://localhost' }); @@ -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, @@ -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); diff --git a/lib/mem-server.js b/lib/mem-server.js new file mode 100644 index 0000000..5f60700 --- /dev/null +++ b/lib/mem-server.js @@ -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 diff --git a/lib/mem-server/model.js b/lib/mem-server/model.js new file mode 100644 index 0000000..0df9248 --- /dev/null +++ b/lib/mem-server/model.js @@ -0,0 +1,29 @@ +export default { + insert(options) { + + }, + bulkInsert(count, options) { + + }, + find(id) { + + }, + where(options) { + + }, + findBy(options) { + + }, + update(record) { + + }, + destroy(record) { + + }, + all() { + + }, + serialize() { + + } +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..e69de29