leanweb 1.3.6 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -29
- package/commands/build.js +93 -121
- package/commands/clean.js +3 -3
- package/commands/destroy.js +19 -20
- package/commands/dist.js +47 -52
- package/commands/generate.js +40 -41
- package/commands/help.js +23 -23
- package/commands/init.js +71 -80
- package/commands/serve.js +42 -63
- package/commands/upgrade.js +14 -43
- package/commands/utils.js +56 -124
- package/commands/version.js +1 -0
- package/leanweb.js +50 -51
- package/lib/lw-html-parser.js +82 -82
- package/package.json +35 -43
- package/templates/component.js +43 -43
- package/templates/env/prod.js +1 -1
- package/templates/env/test.js +1 -1
- package/templates/env.js +1 -1
- package/templates/index.html +6 -6
- package/templates/lib/api-client.js +50 -50
- package/templates/lib/lw-element.js +437 -438
- package/templates/lib/lw-event-bus.js +34 -34
- package/templates/lib/lw-expr-parser.js +163 -163
- package/commands/electron.js +0 -71
- package/templates/electron.js +0 -43
package/leanweb.js
CHANGED
|
@@ -1,64 +1,63 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import fs from 'fs';
|
|
4
|
-
import semver from 'semver';
|
|
5
|
-
import * as utils from './commands/utils.js';
|
|
6
|
-
|
|
7
|
-
import { createRequire } from "module";
|
|
8
|
-
const require = createRequire(import.meta.url);
|
|
9
|
-
|
|
10
3
|
import path from 'path';
|
|
11
4
|
import { fileURLToPath } from 'url';
|
|
12
5
|
const __filename = fileURLToPath(import.meta.url);
|
|
13
6
|
const __dirname = path.dirname(__filename);
|
|
7
|
+
import { createRequire } from "module";
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
14
9
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (args.length < 3) {
|
|
20
|
-
utils.exec('npx leanweb help');
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
let target = args[2];
|
|
25
|
-
|
|
26
|
-
const targetCandidates = Object.keys(utils.targets).filter(t => t.startsWith(target));
|
|
27
|
-
if (targetCandidates.length === 0) {
|
|
28
|
-
console.error(`Error: target ${target} not found.`);
|
|
29
|
-
return;
|
|
30
|
-
} else if (targetCandidates.length > 1) {
|
|
31
|
-
targetCandidates.forEach(t => {
|
|
32
|
-
console.log(t);
|
|
33
|
-
});
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
target = targetCandidates[0];
|
|
38
|
-
|
|
39
|
-
const leanwebJSONExisted = fs.existsSync(`${process.cwd()}/${utils.dirs.src}/leanweb.json`);
|
|
10
|
+
import fs from 'fs';
|
|
11
|
+
import semver from 'semver';
|
|
12
|
+
import * as utils from './commands/utils.js';
|
|
40
13
|
|
|
41
|
-
|
|
42
|
-
console.error('Error: leanweb.json not found.');
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
14
|
+
(async () => {
|
|
45
15
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
16
|
+
const args = process.argv;
|
|
17
|
+
|
|
18
|
+
if (args.length < 3) {
|
|
19
|
+
utils.exec('npx leanweb help');
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let target = args[2];
|
|
24
|
+
|
|
25
|
+
const targetCandidates = Object.keys(utils.targets).filter(t => t.startsWith(target));
|
|
26
|
+
if (targetCandidates.length === 0) {
|
|
27
|
+
console.error(`Error: target ${target} not found.`);
|
|
28
|
+
return;
|
|
29
|
+
} else if (targetCandidates.length > 1) {
|
|
30
|
+
targetCandidates.forEach(t => {
|
|
31
|
+
console.log(t);
|
|
32
|
+
});
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
target = targetCandidates[0];
|
|
37
|
+
|
|
38
|
+
const leanwebJSONExisted = fs.existsSync(`${process.cwd()}/${utils.dirs.src}/leanweb.json`);
|
|
39
|
+
|
|
40
|
+
if (!leanwebJSONExisted && target !== 'init' && target !== 'help' && target !== 'version') {
|
|
41
|
+
console.error('Error: leanweb.json not found.');
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (leanwebJSONExisted && target === 'version' || target === 'serve' || target === 'dist') {
|
|
46
|
+
const leanwebPackageJSON = require(`${__dirname}/package.json`);
|
|
47
|
+
const projectLeanwebJSON = require(`${process.cwd()}/${utils.dirs.src}/leanweb.json`);
|
|
48
|
+
const upgradeAvailable = semver.gt(leanwebPackageJSON.version, projectLeanwebJSON.version);
|
|
49
|
+
if (upgradeAvailable) {
|
|
50
|
+
console.log(`New version of leanweb lib (${projectLeanwebJSON.version} => ${leanwebPackageJSON.version}) is available. Please consider
|
|
52
51
|
running 'lw upgrade' to upgrade your project leanweb lib.`);
|
|
53
|
-
|
|
52
|
+
}
|
|
54
53
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
const projectTooNew = semver.gt(projectLeanwebJSON.version, leanwebPackageJSON.version);
|
|
55
|
+
if (projectTooNew) {
|
|
56
|
+
console.log(`Poject version of leanweb (${projectLeanwebJSON.version} > ${leanwebPackageJSON.version}) is newer than local leanweb tools version.
|
|
58
57
|
Please consider running 'npm i leanweb -g' to upgrade your local leanweb tools.`);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const targetData = utils.targets[target];
|
|
61
|
+
const command = `node --trace-deprecation ${__dirname}/commands/${targetData.file} ${args.slice(3).join(' ')}`;
|
|
62
|
+
utils.exec(command);
|
|
64
63
|
})();
|
package/lib/lw-html-parser.js
CHANGED
|
@@ -4,100 +4,100 @@ import * as parser from '@babel/parser';
|
|
|
4
4
|
let astKey = 0;
|
|
5
5
|
|
|
6
6
|
const removeASTLocation = ast => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
if (Array.isArray(ast)) {
|
|
8
|
+
ast.forEach(a => removeASTLocation(a));
|
|
9
|
+
} else if (typeof ast === 'object') {
|
|
10
|
+
delete ast['loc'];
|
|
11
|
+
delete ast['start'];
|
|
12
|
+
delete ast['end'];
|
|
13
|
+
const values = Object.values(ast).filter(v => Array.isArray(v) || typeof v === 'object');
|
|
14
|
+
removeASTLocation(values);
|
|
15
|
+
}
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
const getAST = expr => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
const parsedProgram = parser.parse(expr).program;
|
|
20
|
+
if (parsedProgram.directives.length > 0 && parsedProgram.body.length === 0) {
|
|
21
|
+
return parsedProgram.directives;
|
|
22
|
+
}
|
|
23
|
+
return parsedProgram.body;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
const walkNode = (node, interpolation) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
27
|
+
node.attrs && node.attrs.forEach(attr => {
|
|
28
|
+
const { startLine, endLine } = node.sourceCodeLocation;
|
|
29
|
+
const loc = { startLine, endLine };
|
|
30
|
+
const key = `${++astKey}`;
|
|
31
|
+
if (attr.name === 'lw-false' || attr.name === 'lw-context' || attr.name === 'lw-for-parent') {
|
|
32
|
+
// this should never happen
|
|
33
|
+
console.assert(false, attr.name);
|
|
34
|
+
// no op
|
|
35
|
+
} else if (attr.name === 'lw') {
|
|
36
|
+
node.attrs.push({ name: 'lw-elem', value: '' });
|
|
37
|
+
let expr = '';
|
|
38
|
+
if (node.childNodes) {
|
|
39
|
+
const exprNode = node.childNodes.find(childNode => childNode.nodeName === '#text');
|
|
40
|
+
expr = exprNode ? exprNode.value : '';
|
|
41
|
+
}
|
|
42
|
+
const ast = getAST(expr);
|
|
43
|
+
removeASTLocation(ast);
|
|
44
|
+
interpolation[key] = { ast, loc };
|
|
45
|
+
node.childNodes.length = 0;
|
|
46
|
+
attr.value = key;
|
|
47
|
+
} else if (attr.name === ('lw-for')) {
|
|
48
|
+
node.attrs.push({ name: 'lw-elem', value: '' });
|
|
49
|
+
const matched = attr.value.match(/(.+)\s+in\s+(.+)/);
|
|
50
|
+
const itemIndex = matched[1].split(',');
|
|
51
|
+
const itemExpr = itemIndex[0].trim();
|
|
52
|
+
let indexExpr;
|
|
53
|
+
if (itemIndex.length > 1) {
|
|
54
|
+
indexExpr = itemIndex[1].trim();
|
|
55
|
+
}
|
|
56
|
+
const itemsExpr = matched[2];
|
|
57
|
+
const astItems = getAST(itemsExpr);
|
|
58
|
+
removeASTLocation(astItems);
|
|
59
|
+
interpolation[key] = { astItems, loc, itemExpr, indexExpr, itemsExpr };
|
|
60
|
+
attr.value = key;
|
|
61
|
+
} else if (attr.name === ('lw-model')) {
|
|
62
|
+
node.attrs.push({ name: 'lw-elem-bind', value: '' });
|
|
63
|
+
node.attrs.push({ name: 'lw-elem', value: '' });
|
|
64
|
+
const ast = getAST(attr.value);
|
|
65
|
+
removeASTLocation(ast);
|
|
66
|
+
interpolation[key] = { ast, loc };
|
|
67
|
+
attr.value = key;
|
|
68
|
+
} else if (attr.name.startsWith('lw-on:') || attr.name.startsWith('lw-class:') || attr.name.startsWith('lw-bind:') || attr.name.startsWith('lw-input:')) {
|
|
69
|
+
if (attr.name.startsWith('lw-on:') || attr.name.startsWith('lw-input:')) {
|
|
70
|
+
node.attrs.push({ name: 'lw-elem-bind', value: '' });
|
|
71
|
+
}
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
node.attrs.push({ name: 'lw-elem', value: '' });
|
|
74
|
+
const lw = attr.name.split(':');
|
|
75
|
+
const lwType = lw[0];
|
|
76
|
+
const lwValue = lw[1];
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
const ast = getAST(attr.value);
|
|
79
|
+
removeASTLocation(ast);
|
|
80
|
+
interpolation[key] = { ast, loc, lwType, lwValue };
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
82
|
+
attr.value = key;
|
|
83
|
+
} else if (attr.name.startsWith('lw-')) {
|
|
84
|
+
node.attrs.push({ name: 'lw-elem', value: '' });
|
|
85
|
+
const ast = getAST(attr.value);
|
|
86
|
+
removeASTLocation(ast);
|
|
87
|
+
interpolation[key] = { ast, loc };
|
|
88
|
+
attr.value = key;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
node.childNodes && node.childNodes.forEach(childNode => walkNode(childNode, interpolation));
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
export const parse = html => {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
const ast = {};
|
|
96
|
+
const doc = parse5.parseFragment(html, { sourceCodeLocationInfo: true });
|
|
97
|
+
walkNode(doc, ast);
|
|
98
|
+
html = parse5.serialize(doc);
|
|
99
|
+
ast.html = html;
|
|
100
|
+
return ast;
|
|
101
101
|
};
|
|
102
102
|
|
|
103
103
|
|
package/package.json
CHANGED
|
@@ -1,44 +1,36 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"raw-loader": "^4.0.2",
|
|
38
|
-
"sass": "^1.56.1",
|
|
39
|
-
"sass-loader": "^13.2.0",
|
|
40
|
-
"semver": "^7.3.8",
|
|
41
|
-
"webpack": "^5.75.0",
|
|
42
|
-
"webpack-dev-server": "^4.11.1"
|
|
43
|
-
}
|
|
44
|
-
}
|
|
2
|
+
"name": "leanweb",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "Builds framework agnostic web components.",
|
|
5
|
+
"bin": {
|
|
6
|
+
"leanweb": "leanweb.js",
|
|
7
|
+
"lw": "leanweb.js"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/elgs/leanweb.git"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://leanweb.app",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"leanweb",
|
|
17
|
+
"lw",
|
|
18
|
+
"web components"
|
|
19
|
+
],
|
|
20
|
+
"author": "Qian Chen",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@babel/parser": "^7.20.5",
|
|
24
|
+
"clean-css": "^5.3.1",
|
|
25
|
+
"esbuild": "^0.15.16",
|
|
26
|
+
"fs-extra": "^11.1.0",
|
|
27
|
+
"globby": "^13.1.2",
|
|
28
|
+
"html-minifier": "^4.0.0",
|
|
29
|
+
"isomorphic-git": "^1.21.0",
|
|
30
|
+
"live-server": "^1.2.2",
|
|
31
|
+
"node-watch": "^0.7.3",
|
|
32
|
+
"parse5": "^7.1.2",
|
|
33
|
+
"sass": "^1.56.1",
|
|
34
|
+
"semver": "^7.3.8"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/templates/component.js
CHANGED
|
@@ -2,47 +2,47 @@ import LWElement from './../../${pathLevels}lib/lw-element.js';
|
|
|
2
2
|
import ast from './ast.js';
|
|
3
3
|
|
|
4
4
|
customElements.define('${projectName}-${component}',
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
5
|
+
class extends LWElement { // LWElement extends HTMLElement
|
|
6
|
+
constructor() {
|
|
7
|
+
super(ast);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// derived from LWElement
|
|
11
|
+
// domReady() {
|
|
12
|
+
// console.log('Dom is ready');
|
|
13
|
+
// }
|
|
14
|
+
|
|
15
|
+
// inputReady() {
|
|
16
|
+
// console.log('input is ready');
|
|
17
|
+
// }
|
|
18
|
+
|
|
19
|
+
// Called when the urlHash changes. This could be useful to update the
|
|
20
|
+
// DOM on component routing.
|
|
21
|
+
// urlHashChanged() {
|
|
22
|
+
// // update component DOM
|
|
23
|
+
// this.update();
|
|
24
|
+
// }
|
|
25
|
+
|
|
26
|
+
// derived from HTMLElement
|
|
27
|
+
// connectedCallback() {
|
|
28
|
+
// console.log(this.isConnected);
|
|
29
|
+
// console.log('Element added to page.');
|
|
30
|
+
// }
|
|
31
|
+
|
|
32
|
+
// disconnectedCallback() {
|
|
33
|
+
// console.log('Element removed from page.');
|
|
34
|
+
// }
|
|
35
|
+
|
|
36
|
+
// adoptedCallback() {
|
|
37
|
+
// console.log('Element moved to new page.');
|
|
38
|
+
// }
|
|
39
|
+
|
|
40
|
+
// static get observedAttributes() {
|
|
41
|
+
// return [];
|
|
42
|
+
// }
|
|
43
|
+
|
|
44
|
+
// attributeChangedCallback(name, oldValue, newValue) {
|
|
45
|
+
// console.log(name, oldValue, newValue);
|
|
46
|
+
// }
|
|
47
|
+
}
|
|
48
48
|
);
|
package/templates/env/prod.js
CHANGED
package/templates/env/test.js
CHANGED
package/templates/env.js
CHANGED
package/templates/index.html
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
<!DOCTYPE html>
|
|
2
2
|
<html>
|
|
3
3
|
<head>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<title>${project.name}</title>
|
|
6
|
+
<script type="module" src="${project.name}.js"></script>
|
|
7
|
+
<link rel="stylesheet" href="${project.name}.css">
|
|
8
|
+
<link rel="icon" type="image/svg+xml" href="favicon.svg">
|
|
9
9
|
</head>
|
|
10
10
|
<body style="opacity: 0;" onload="document.body.style.opacity=1">
|
|
11
|
-
|
|
11
|
+
<${project.name}-root></${project.name}-root>
|
|
12
12
|
</body>
|
|
13
13
|
</html>
|
|
@@ -3,65 +3,65 @@
|
|
|
3
3
|
// to overwrite on Leanweb upgrade.
|
|
4
4
|
|
|
5
5
|
class APIClient {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
constructor(baesUrl, sendToken = false, defaultHeaders = {}) {
|
|
7
|
+
this.baesUrl = baesUrl;
|
|
8
|
+
this.sendToken = sendToken;
|
|
9
|
+
this.defaultHeaders = defaultHeaders;
|
|
10
|
+
}
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
async _fetch(method, url = '', data = {}, headers = {}) {
|
|
13
|
+
if (!url.toLowerCase().startsWith('https://') && !url.toLowerCase().startsWith('http://')) {
|
|
14
|
+
url = this.baesUrl + url;
|
|
15
|
+
}
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
17
|
+
if (method === 'GET' && data && typeof data === 'object') {
|
|
18
|
+
// encode data and append to url
|
|
19
|
+
const queryString = paramsToQueryString(data);
|
|
20
|
+
data = null;
|
|
21
|
+
if (url.endsWith('?')) {
|
|
22
|
+
url += queryString;
|
|
23
|
+
} else if (url.indexOf('?') >= 0) {
|
|
24
|
+
url += ('&' + queryString);
|
|
25
|
+
} else {
|
|
26
|
+
url += ('?' + queryString);
|
|
28
27
|
}
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
30
|
+
if (this.sendToken) {
|
|
31
|
+
const token = localStorage.getItem('access_token');
|
|
32
|
+
if (token) {
|
|
33
|
+
headers['access_token'] = token;
|
|
34
|
+
} else {
|
|
35
|
+
return null;
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
37
|
+
}
|
|
38
|
+
const response = await fetch(url, {
|
|
39
|
+
method,
|
|
40
|
+
headers: { ...this.defaultHeaders, ...headers },
|
|
41
|
+
body: data ? JSON.stringify(data) : null,
|
|
42
|
+
});
|
|
43
|
+
return response.json();
|
|
44
|
+
}
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
post(url, data, headers) { return this._fetch('POST', url, data, headers); }
|
|
47
|
+
get(url, data, headers) { return this._fetch('GET', url, data, headers); }
|
|
48
|
+
patch(url, data, headers) { return this._fetch('PATCH', url, data, headers); }
|
|
49
|
+
delete(url, data, headers) { return this._fetch('DELETE', url, data, headers); }
|
|
50
|
+
put(url, data, headers) { return this._fetch('PUT', url, data, headers); }
|
|
51
|
+
options(url, data, headers) { return this._fetch('OPTIONS', url, data, headers); }
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
const paramsToQueryString = (params) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
55
|
+
return Object.keys(params).map(k => {
|
|
56
|
+
const v = params[k];
|
|
57
|
+
if (Array.isArray(v)) {
|
|
58
|
+
return v.reduce((vacc, vcurr) => {
|
|
59
|
+
return `${vacc}${k}=${encodeURIComponent(vcurr)}&`;
|
|
60
|
+
}, '');
|
|
61
|
+
} else {
|
|
62
|
+
return `${k}=${encodeURIComponent(v)}&`;
|
|
63
|
+
}
|
|
64
|
+
}).reduce((acc, curr) => acc + curr, '').slice(0, -1);
|
|
65
65
|
};
|
|
66
66
|
|
|
67
67
|
// const apiUrl = 'http://localhost:1234';
|