Skip to content

Instantly share code, notes, and snippets.

@padupuy
Last active May 9, 2017 10:02
Show Gist options
  • Save padupuy/753789324c934538cb7177f785dddabc to your computer and use it in GitHub Desktop.
Save padupuy/753789324c934538cb7177f785dddabc to your computer and use it in GitHub Desktop.
use kakapo to mock data
export default {
"access_token": "NTZjM2MxYzZlZjBiMDhjY2Y4OWIxODBjYTAwMjRhYTU3M2JjMTczYjgyNDg2ZDQ1YTBlMGQ0OWMxMzU1MzI4ZA",
"expires_in": 3600,
"token_type": "bearer",
"scope": null,
"refresh_token": "ZDUxZGJjZjUxYmY3NTY0OTgzMjdlZTBjMWM3MWZiNmJjYjU5NTFlNzZmZjBhNTBkZWM5ZmZhZWIzMjZiODFjYw"
};
export const OAUTH = '/oauth/v2/token';
export const ARTICLES = '/articles';
export const PROFILE = '/profile';
export function isDevMode(){
//React Native environment
return global.__DEV__;
}
import {Server, Router, Database} from 'kakapo';
import {isDevMode} from './env';
import * as URL from './constants';
import authFixture from './auth.fixture.js';
import profileFixture from './profile.fixture.js';
const singleton = Symbol();
const singletonEnforcer = Symbol();
const TOTAL_ARTICLES = 100;
const NEWS_NUMBER_PER_PAGE = 20;
class MockRequest {
constructor(enforcer) {
if (enforcer !== singletonEnforcer) {
throw "Cannot construct MockRequest";
}
if (isDevMode()) {
this.init();
}
}
init() {
this.router = new Router({
host: 'https://yourAPIhost.com',
// requestDelay: 2000 // simulate network delay
});
this.database = new Database();
this.server = new Server();
this.generateFakeData();
this.mockRoutes();
this.server.use(this.database);
this.server.use(this.router);
}
generateFakeData() {
this.generateFakeArticles();
}
mockRoutes() {
this.mockOAuthRoute();
this.mockProfileRoute();
this.mockArticleRoute();
this.mockArticleDetailsRoute();
}
mockOAuthRoute() {
this.router.post(URL.OAUTH, (request, db) => {
return authFixture;
});
}
mockProfileRoute() {
this.router.get(URL.PROFILE, (request, db) => {
return profileFixture;
});
}
generateFakeArticles() {
this.database.register('article', faker => {
const id = faker.random.uuid();
return {
"id": id,
"title": faker.lorem.sentence,
"createdAt": faker.date.past(1),
"image": faker.image.business,
"content": faker.lorem.paragraph
}
});
this.database.create('article', TOTAL_ARTICLES);
}
mockArticleRoute() {
this.router.get(URL.ARTICLES, (request, db) => {
let allArticles = db.all('article');
const currentPage = request.query.page;
const start = (currentPage - 1) * NEWS_NUMBER_PER_PAGE;
const end = currentPage * NEWS_NUMBER_PER_PAGE;
const articles = allArticles.slice(start, end);
return articles;
});
}
mockArticleDetailsRoute() {
this.router.get(`${URL.ARTICLES}/:newsId`, (request, db) => {
let allArticles = db.all('article');
const newsId = parseInt(request.params.newsId, 10);
const fullArticle = allArticles.find(article => article.id === newsId);
return fullArticle;
});
}
/**
* Singleton
* @returns {Object}
*/
static get instance() {
if (!this[singleton]) {
this[singleton] = new MockRequest(singletonEnforcer)
}
return this[singleton]
}
}
export default MockRequest.instance;
export default {
"id": 1,
"firstName": "Pierre-Alexandre",
"lastName": "Dupuy",
"avatar": "https://avatars1.githubusercontent.com/u/715255?v=3&s=460",
"phone": "0612345678",
"email": "dummy@mail.com"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment