Skip to content
This repository was archived by the owner on Feb 12, 2026. It is now read-only.

Commit c03b53f

Browse files
committed
Merge branch 'feature/webpack'
2 parents 1b4bebd + 52bdfe7 commit c03b53f

17 files changed

+243
-184
lines changed

.eslintignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
tests/*
2-
api/*
3-
examples/*
1+
tests
2+
api
3+
examples
4+
dist

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
.nyc_output
33
coverage
44
npm-debug.log
5+
dist

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ tests
33
assets
44
api
55
docs
6+
src

package.json

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,54 @@
1515
"url": "https://github.com/raphaelpor/fitch.js/issues"
1616
},
1717
"homepage": "https://github.com/raphaelpor/fitch.js#readme",
18-
"main": "src/index.js",
18+
"main": "dist/index.js",
1919
"dependencies": {
2020
"es6-promise": "^4.0.5",
2121
"fetch-everywhere": "^1.0.5"
2222
},
2323
"devDependencies": {
2424
"ava": "^0.16.0",
25+
"babel-cli": "^6.18.0",
26+
"babel-eslint": "^7.1.0",
27+
"babel-loader": "^6.2.7",
28+
"babel-preset-es2015": "^6.18.0",
29+
"babel-preset-stage-2": "^6.18.0",
2530
"eslint": "^3.9.1",
2631
"eslint-config-airbnb": "^13.0.0",
2732
"eslint-plugin-import": "^2.2.0",
2833
"eslint-plugin-jsx-a11y": "^2.2.3",
2934
"eslint-plugin-react": "^6.6.0",
3035
"hapi": "^15.2.0",
31-
"nyc": "^8.4.0"
36+
"npm-run-all": "^3.1.1",
37+
"nyc": "^8.4.0",
38+
"rimraf": "^2.5.4",
39+
"webpack": "^1.13.3"
3240
},
3341
"scripts": {
34-
"start": "node ./api/index.js",
42+
"build": "npm-run-all --parallel build:*",
43+
"build:main": "babel --copy-files --out-dir dist src",
44+
"build:umd": "webpack --output-filename index.umd.js --colors",
45+
"build:umd.min": "webpack --output-filename index.umd.min.js -p --colors",
46+
"check": "npm run lint && npm run test",
47+
"clean": "rimraf dist coverage",
48+
"coverage": "nyc ava ./tests/*",
49+
"examples": "node ./examples/index.js",
50+
"prebuild": "npm run check && npm run clean",
3551
"lint": "eslint src",
52+
"start": "node ./api/index.js",
3653
"test": "ava ./tests/*",
37-
"watch:test": "npm test -- -w",
38-
"coverage": "nyc ava ./tests/*",
39-
"examples": "node ./examples/index.js"
54+
"watch:test": "npm test -- -w"
55+
},
56+
"babel": {
57+
"presets": [
58+
"es2015",
59+
"stage-2"
60+
]
61+
},
62+
"ava": {
63+
"require": [
64+
"babel-register"
65+
],
66+
"babel": "inherit"
4067
}
4168
}

src/config.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/index.js

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,36 @@
1-
require('es6-promise').polyfill();
2-
require('fetch-everywhere');
1+
import 'fetch-everywhere';
32

4-
const config = require('./config');
5-
const params = require('./params');
3+
import request from './utils/request';
64

7-
module.exports = {
8-
get(url, req) {
9-
return this.request('GET', url, req);
10-
},
5+
function get(url, req) {
6+
return request('GET', url, req);
7+
}
118

12-
post(url, req) {
13-
return this.request('POST', url, req);
14-
},
9+
function post(url, req) {
10+
return request('POST', url, req);
11+
}
1512

16-
put(url, req) {
17-
return this.request('PUT', url, req);
18-
},
13+
function put(url, req) {
14+
return request('PUT', url, req);
15+
}
1916

20-
patch(url, req) {
21-
return this.request('PATCH', url, req);
22-
},
17+
function patch(url, req) {
18+
return request('PATCH', url, req);
19+
}
2320

24-
delete(url, req) {
25-
return this.request('DELETE', url, req);
26-
},
21+
function del(url, req) {
22+
return request('DELETE', url, req);
23+
}
2724

28-
request(method, url, req = {}) {
29-
let paramsEncoded = '';
30-
if (req.params) {
31-
paramsEncoded = params.transform(req.params);
32-
}
25+
function error(err = '') {
26+
console.log('Error >', err);
27+
}
3328

34-
const configObj = config.create(method, req);
35-
const call = fetch(url + paramsEncoded, configObj);
36-
37-
return req.raw ? call : call.then(resp => this.check(resp, req.dataType));
38-
},
39-
40-
check(resp, dataType = 'json') {
41-
const typeList = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];
42-
const included = typeList.indexOf(dataType);
43-
44-
if (resp.ok && included !== -1) {
45-
return resp[dataType]();
46-
}
47-
48-
throw new Error(`${resp.status} - ${resp.statusText}.`);
49-
},
50-
51-
error(err = '') {
52-
console.log('Error >', err);
53-
},
29+
export {
30+
del,
31+
error,
32+
get,
33+
patch,
34+
post,
35+
put,
5436
};

src/params.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/utils/check.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'fetch-everywhere';
2+
3+
function check(resp, dataType = 'json') {
4+
const typeList = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];
5+
const included = typeList.indexOf(dataType);
6+
7+
if (resp.ok && included !== -1) {
8+
return resp[dataType]();
9+
}
10+
11+
throw new Error(`${resp.status} - ${resp.statusText}.`);
12+
}
13+
14+
export default check;

src/utils/config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export default function createConfig(method, {
2+
body,
3+
cache = 'default',
4+
credentials,
5+
headers = { 'Content-Type': 'application/json' },
6+
integrity,
7+
mode = 'cors',
8+
redirect,
9+
referrer,
10+
referrerPolicy,
11+
} = {}) {
12+
let data;
13+
14+
if (body) {
15+
data = JSON.stringify(body);
16+
}
17+
18+
const result = {
19+
body: data,
20+
cache,
21+
credentials,
22+
headers,
23+
method,
24+
mode,
25+
redirect,
26+
referrer,
27+
};
28+
29+
if (integrity) {
30+
result.integrity = integrity;
31+
}
32+
33+
if (referrerPolicy) {
34+
result.referrerPolicy = referrerPolicy;
35+
}
36+
37+
return result;
38+
}

src/utils/params.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function transformParams(source) {
2+
const list = [];
3+
4+
for (const key in source) {
5+
if ({}.hasOwnProperty.call(source, key)) {
6+
list.push(`${encodeURIComponent(key)}=${encodeURIComponent(source[key])}`);
7+
}
8+
}
9+
10+
return list.length ? `?${list.join('&')}` : '';
11+
}

0 commit comments

Comments
 (0)