Skip to content

Instantly share code, notes, and snippets.

@codfish codfish/Dockerfile
Last active Feb 20, 2019

Embed
What would you like to do?
Example json-server module for marketing campaigns. `json-server api.js`
/* eslint-disable import/no-extraneous-dependencies */
const faker = require('faker');
const times = require('lodash/times');
module.exports = () => ({
campaigns: times(100, () => {
const id = faker.random.uuid();
return {
id,
name: faker.name.findName(),
description: faker.lorem.sentences(),
target_deployment_date: faker.date.future(),
channel: faker.random.arrayElement(['postal', 'email']),
recipient_selection: faker.random.arrayElement(['household']),
service_line: faker.random.words(2),
refinements: [
{
id: faker.random.number(1000),
type: faker.random.arrayElement(['suppressed', 'included']),
},
],
current_count: faker.random.number(100000),
created_by: faker.random.uuid(),
segments_url: `https://api.reachmarketing.com/v1/campaigns/${id}/segments`,
};
}),
segments: times(100, () => {
const id = faker.random.uuid();
return {
id,
name: faker.name.findName(),
description: faker.lorem.sentences(),
channel: faker.random.arrayElement([['postal'], ['email'], ['postal', 'email']]),
race: faker.random.arrayElement([
['white'],
['black'],
['asian'],
['hispanic'],
['asian', 'hispanic'],
['white', 'black'],
]),
gender: faker.random.arrayElement([['male'], ['female'], ['male', 'female']]),
age_range: faker.random.arrayElement(['household']),
target_individual: faker.random.arrayElement([
['patients'],
['customers'],
['prospects'],
['patients', 'customers'],
['customers', 'prospects'],
['patients', 'customers', 'prospects'],
]),
geography: times(4, () => ({
zip: faker.address.zipCode(),
county: faker.address.county(),
state: faker.address.stateAbbr(),
})),
service_line: faker.random.words(2),
refinements: [
{
id: faker.random.number(1000),
type: faker.random.arrayElement(['suppressed', 'included']),
},
],
current_count: faker.random.number(20000),
};
}),
});
FROM mhart/alpine-node:11.10
RUN mkdir /app
WORKDIR /app
COPY ./package.json ./package-lock.json ./
RUN npm install
EXPOSE 4000
CMD npm run dev
{
"name": "api",
"version": "0.0.0",
"dependencies": {
"faker": "^4.1.0",
"json-server": "^0.12.1",
"lodash": "^4.17.5"
},
"scripts": {
"start": "json-server -w --fks _id --routes routes.json --middlewares ./middleware.js -p 4000 index.js",
"dev": "nodemon --exec npm start"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-underscore-dangle */
const faker = require('faker');
module.exports = (req, res, next) => {
if (req.query.sort) {
req.query._order = req.query.sort.indexOf('-') === 0 ? 'desc' : 'asc';
req.query._sort = req.query.sort.replace(/^-/, '');
delete req.query.sort;
}
if (req.query.per_page) {
req.query._limit = req.query.per_page;
delete req.query.per_page;
}
if (req.query.page) {
req.query._page = parseInt(req.query.page, 10) + 1;
delete req.query.page;
}
if (req.query.count) {
delete req.query.count;
}
if (req.method === 'POST') {
req.body.created_date = new Date().toISOString();
req.body.modified_date = new Date().toISOString();
req.body.created_by = faker.name.findName();
}
if (req.method === 'PATCH' || req.method === 'PUT') {
req.body.modified_date = new Date().toISOString();
}
next();
};
{
"/campaigns/:id/segments/:segmentId": "/segments/:segmentId",
"/campaigns/:id/segments/:segmentId/duplicate": "/segments",
"/campaigns/:id/duplicate": "/campaigns"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.