Skip to content

Commit

Permalink
Merge pull request #34 from marmelab/modernize
Browse files Browse the repository at this point in the history
Modernize
  • Loading branch information
fzaninotto authored Feb 4, 2021
2 parents 3db14bc + 4a36bcf commit e3c5ad0
Show file tree
Hide file tree
Showing 15 changed files with 6,495 additions and 73 deletions.
3 changes: 1 addition & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"presets": ["es2015", "stage-0"],
"plugins": ["transform-runtime", "add-module-exports"]
"presets": [["@babel/preset-env"]]
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '7.0'
- 'lts/*'
cache:
directories:
- node_modules
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-dev:
@${CURDIR}/node_modules/.bin/webpack

build:
@${CURDIR}/node_modules/.bin/webpack --optimize-minimize --output-file=FakeRest.min.js
@${CURDIR}/node_modules/.bin/webpack --mode=production

watch:
@${CURDIR}/node_modules/.bin/webpack --watch
Expand Down
1 change: 1 addition & 0 deletions dist/FakeRest.js.map

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions dist/FakeRest.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/FakeRest.min.js.map

Large diffs are not rendered by default.

6,431 changes: 6,431 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

32 changes: 15 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,35 @@
"name": "fakerest",
"version": "2.2.0",
"repository": "https://github.com/marmelab/FakeRest",
"description":
"Patch XMLHttpRequest to fake a REST server based on JSON data. ",
"description": "Patch XMLHttpRequest to fake a REST server based on JSON data. ",
"scripts": {
"test": "make test"
},
"main": "dist/FakeRest.min.js",
"author": "François Zaninotto <[email protected]>",
"license": "MIT",
"devDependencies": {
"array.prototype.findindex": "~2.0.0",
"babel-core": "~6.22.1",
"babel-loader": "~6.2.10",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-polyfill": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-stage-0": "^6.22.0",
"babel-runtime": "~6.22.0",
"fetch-mock": "~5.9.3",
"@babel/preset-env": "^7.12.13",
"babel-core": "^6.26.3",
"babel-loader": "^8.2.2",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-runtime": "^6.26.0",
"fetch-mock": "^9.11.0",
"jasmine-core": "^2.5.2",
"karma": "1.4.1",
"karma-chrome-launcher": "2.0.0",
"karma-jasmine": "1.1.0",
"karma-phantomjs-launcher": "1.0.2",
"karma-spec-reporter": "0.0.26",
"object.assign": "~4.0.1",
"sinon": "~1.14.1",
"string.prototype.endswith": "~0.2.0",
"webpack": "~1.7.1"
"terser-webpack-plugin": "^5.1.1",
"webpack": "^5.20.0",
"webpack-cli": "^4.5.0"
},
"dependencies": {
"babel-runtime": "^6.22.0"
}
"babel-runtime": "^6.26.0"
},
"browserslist": "> 0.25%, not dead"
}
14 changes: 5 additions & 9 deletions src/Collection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import objectAssign from 'object.assign';
import 'array.prototype.findindex';
import 'string.prototype.endswith';

const every = (array, predicate) =>
array.reduce((acc, value) => acc && predicate(value), true);

Expand Down Expand Up @@ -118,7 +114,7 @@ function rangeItems(items, range) {
throw new Error('Unsupported range type');
}

export default class Collection {
export class Collection {

constructor(items=[], identifierName='id') {
if (!Array.isArray(items)) {
Expand Down Expand Up @@ -226,7 +222,7 @@ export default class Collection {
if (query.range) {
items = rangeItems(items, query.range);
}
items = items.map(item => objectAssign({}, item)) // clone item to avoid returning the original
items = items.map(item => Object.assign({}, item)) // clone item to avoid returning the original
if (query.embed && this.server) {
items = items.map(this._itemEmbedder(query.embed)); // embed reference
}
Expand All @@ -244,7 +240,7 @@ export default class Collection {
throw new Error(`No item with identifier ${ identifier }`);
}
let item = this.items[index];
item = objectAssign({}, item); // clone item to avoid returning the original
item = Object.assign({}, item); // clone item to avoid returning the original
if (query && query.embed && this.server) {
item = this._itemEmbedder(query.embed)(item); // embed reference
}
Expand All @@ -263,7 +259,7 @@ export default class Collection {
item[this.identifierName] = this.sequence++;
}
this.items.push(item);
return objectAssign({}, item); // clone item to avoid returning the original;
return Object.assign({}, item); // clone item to avoid returning the original;
}

updateOne(identifier, item) {
Expand All @@ -274,7 +270,7 @@ export default class Collection {
for (let key in item) {
this.items[index][key] = item[key];
}
return objectAssign({}, this.items[index]); // clone item to avoid returning the original
return Object.assign({}, this.items[index]); // clone item to avoid returning the original
}

removeOne(identifier) {
Expand Down
16 changes: 6 additions & 10 deletions src/FakeRest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import Server from 'Server';
import FetchServer from 'FetchServer';
import Collection from 'Collection';
import Single from 'Single';
import { Server } from './Server';
import { FetchServer } from './FetchServer';
import { Collection } from './Collection';
import { Single } from './Single';

export default {
Server: Server,
FetchServer: FetchServer,
Collection: Collection,
Single: Single
}
export { Server, FetchServer, Collection, Single };
export default { Server, FetchServer, Collection, Single };
13 changes: 4 additions & 9 deletions src/FetchServer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import objectAssign from 'object.assign';
import Server from 'Server';
import Collection from 'Collection';
import Single from 'Single';
import parseQueryString from 'parseQueryString';
import { Server } from './Server';
import { parseQueryString } from './parseQueryString';

const assign = objectAssign.getPolyfill();

export default class FetchServer extends Server {
export class FetchServer extends Server {
decode(request, opts) {
const req = (typeof request === 'string') ? new Request(request, opts) : request;
req.queryString = decodeURIComponent(req.url.slice(req.url.indexOf('?') + 1));
Expand Down Expand Up @@ -111,7 +106,7 @@ export default class FetchServer extends Server {
for (let name of this.getCollectionNames()) {
let matches = request.url.match(new RegExp('^' + this.baseUrl + '\\/(' + name + ')(\\/(\\d+))?(\\?.*)?$' ));
if (!matches) continue;
let params = assign({}, this.defaultQuery(name), request.params);
let params = Object.assign({}, this.defaultQuery(name), request.params);
if (!matches[2]) {
if (request.method == 'GET') {
let count = this.getCount(name, params.filter ? { filter: params.filter } : {});
Expand Down
13 changes: 5 additions & 8 deletions src/Server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import objectAssign from 'object.assign';
import Collection from 'Collection';
import Single from 'Single';
import parseQueryString from 'parseQueryString';
import { Collection } from './Collection';
import { Single } from './Single';
import { parseQueryString } from './parseQueryString';

const assign = objectAssign.getPolyfill();

export default class Server {
export class Server {
constructor(baseUrl='') {
this.baseUrl = baseUrl;
this.loggingEnabled = false;
Expand Down Expand Up @@ -279,7 +276,7 @@ export default class Server {
let matches = request.url.match(new RegExp('^' + this.baseUrl + '\\/([^\\/?]+)(\\/(\\d+))?(\\?.*)?$' ));
if (!matches) return;
let name = matches[1];
let params = assign({}, this.defaultQuery(name), request.params);
let params = Object.assign({}, this.defaultQuery(name), request.params);
if (!matches[2]) {
if (request.method == 'GET') {
if (!this.getCollection(name)) {
Expand Down
7 changes: 2 additions & 5 deletions src/Single.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import objectAssign from 'object.assign';
import 'string.prototype.endswith';

export default class Single {
export class Single {
constructor(obj) {
if (!(obj instanceof Object)) {
throw new Error('Can\'t initialize a Single with anything except an object');
Expand Down Expand Up @@ -65,7 +62,7 @@ export default class Single {
getOnly(query) {
let item = this.obj;
if (query && query.embed && this.server) {
item = objectAssign({}, item); // Clone
item = Object.assign({}, item); // Clone
item = this._itemEmbedder(query.embed)(item);
}
return item;
Expand Down
2 changes: 1 addition & 1 deletion src/parseQueryString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function parseQueryString(queryString) {
export function parseQueryString(queryString) {
if (!queryString) {
return {};
}
Expand Down
24 changes: 18 additions & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
const path = require('path');
const TerserPlugin = require("terser-webpack-plugin");

module.exports = {
mode: 'development',
entry: {
FakeRest: './src/FakeRest.js'
FakeRest: './src/FakeRest.js',
"FakeRest.min": './src/FakeRest.js'
},
devtool: "source-map",
resolve:{
modulesDirectories: [
modules: [
'node_modules',
'src'
path.join(__dirname, "src")
]
},
module: {
loaders: [{
rules: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
loader: 'babel-loader'
}]
},
optimization: {
minimize: true,
minimizer: [new TerserPlugin({
include: /\.min\.js$/
})]
},
output: {
path: './dist',
path: path.resolve(__dirname, './dist'),
filename: '[name].js',
library: 'FakeRest',
libraryTarget: 'umd'
Expand Down

0 comments on commit e3c5ad0

Please sign in to comment.