docs:
parent
22032e45e0
commit
22697c365b
@ -1,37 +1,83 @@
|
|||||||
|
|
||||||
|
|
||||||
# @haina/npm
|
# @haina/npm
|
||||||
|
|
||||||
可复用的 npm 模块
|
可复用的 npm 模块
|
||||||
支持 `ESM`(import) 和 `CommonJS`(require)
|
支持 `ESM`(import) 和 `CommonJS`(require)
|
||||||
|
|
||||||
## 📦 Installation
|
## Dev
|
||||||
|
|
||||||
```sh
|
### Package Structure
|
||||||
# 安装整个包
|
|
||||||
npm install http://xxxx/npm/haina-npm-0.1.0.tgz
|
|
||||||
|
|
||||||
# 安装单个模块
|
```
|
||||||
npm install http://xxxx/npm/utils-0.1.0.tgz
|
src/
|
||||||
|
├── utils/
|
||||||
|
│ ├── common.js
|
||||||
|
│ └── request.js
|
||||||
|
├── hooks/
|
||||||
|
│ ├── useXx.js
|
||||||
|
│ └── useYy.js
|
||||||
|
|
||||||
# 安装Utils的子模块
|
```
|
||||||
npm install http://xxxx/npm/utils-commons-0.1.0.tgz
|
|
||||||
|
### Build & Pack
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# 打包各模块
|
||||||
|
npm run pack
|
||||||
|
|
||||||
|
# 打包各模块和root
|
||||||
|
npm run pack:all
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🚀 Usage
|
1. 生成导出文件 `index.js`
|
||||||
|
2. 创建`ESM`和`CommonJS` 版本
|
||||||
|
3. 打包
|
||||||
|
1. 打包`src`下的子文件夹 --> `hooks-0.1.10.tgz` `utils-0.1.10.tgz` …
|
||||||
|
2. `src/utils`下的模块, 分别单独打包 --> `utils-request-0.1.10.tgz` `utils-commons-0.1.10.tgz` …
|
||||||
|
3. 打包整个项目(root) --> `haina-npm-0.1.10.tgz`
|
||||||
|
|
||||||
|
### Serve
|
||||||
|
|
||||||
|
#### FTP
|
||||||
|
|
||||||
|
#### Github Action
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### 安装整个包
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install http://xxxx/npm/haina-npm-0.1.0.tgz
|
||||||
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Import everything
|
//
|
||||||
import { commons, request } from '@haina/npm';
|
import { commons, request, utils, hooks } from '@haina/npm';
|
||||||
// Import only common utilities
|
// Import only common utilities
|
||||||
import { isEmpty } from '@haina/npm/esm/utils/commons.js';
|
import { isEmpty } from '@haina/npm/esm/utils/commons.js';
|
||||||
// Import only request utilities
|
// Import only request utilities
|
||||||
import { fetchJSON } from '@haina/npm/esm/utils/request.js';
|
import { fetchJSON } from '@haina/npm/esm/utils/request.js';
|
||||||
|
```
|
||||||
|
|
||||||
// Import only common utilities
|
### 安装单个模块
|
||||||
import { isEmpty } from '@haina/utils-commons';
|
|
||||||
|
|
||||||
// Import only common utilities
|
```sh
|
||||||
import { commons } from '@haina/utils';
|
npm install http://xxxx/npm/utils-0.1.0.tgz
|
||||||
|
npm install http://xxxx/npm/hooks-0.1.0.tgz
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { commons, request } from '@haina/utils';
|
||||||
|
```
|
||||||
|
|
||||||
|
### 安装Utils的子模块
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install http://xxxx/npm/utils-commons-0.1.0.tgz
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { isEmpty } from '@haina/utils-commons';
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,12 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
||||||
Object.defineProperty(exports, "__esModule", {
|
|
||||||
value: true
|
|
||||||
});
|
|
||||||
exports.request = exports.commons = void 0;
|
|
||||||
var _commons = _interopRequireWildcard(require("./utils/commons.js"));
|
|
||||||
exports.commons = _commons;
|
|
||||||
var _request = _interopRequireWildcard(require("./utils/request.js"));
|
|
||||||
exports.request = _request;
|
|
||||||
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|
||||||
@ -1,126 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
|
|
||||||
Object.defineProperty(exports, "__esModule", {
|
|
||||||
value: true
|
|
||||||
});
|
|
||||||
exports.appendRequestHeader = appendRequestHeader;
|
|
||||||
exports.fetchJSON = fetchJSON;
|
|
||||||
exports.fetchText = fetchText;
|
|
||||||
exports.postForm = postForm;
|
|
||||||
exports.postJSON = postJSON;
|
|
||||||
exports.postStream = postStream;
|
|
||||||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
||||||
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
||||||
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
||||||
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
||||||
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
||||||
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
||||||
// import { BUILD_VERSION } from '@/config'
|
|
||||||
var BUILD_VERSION = '';
|
|
||||||
|
|
||||||
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
|
||||||
|
|
||||||
var customHeaders = [];
|
|
||||||
|
|
||||||
// 添加 HTTP Reuqest 自定义头部
|
|
||||||
function appendRequestHeader(n, v) {
|
|
||||||
customHeaders.push({
|
|
||||||
name: n,
|
|
||||||
value: v
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function getRequestHeader() {
|
|
||||||
return customHeaders.reduce(function (acc, item) {
|
|
||||||
acc[item.name] = item.value;
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
function checkStatus(response) {
|
|
||||||
if (response.status >= 200 && response.status < 300) {
|
|
||||||
return response;
|
|
||||||
} else {
|
|
||||||
var message = 'Fetch error: ' + response.url + ' ' + response.status + ' (' + response.statusText + ')';
|
|
||||||
var error = new Error(message);
|
|
||||||
error.response = response;
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function checkBizCode(responseJson) {
|
|
||||||
if (responseJson.errcode === 0) {
|
|
||||||
return responseJson;
|
|
||||||
} else {
|
|
||||||
throw new Error(responseJson.errmsg + ': ' + responseJson.errcode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function fetchText(url) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: _objectSpread({
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.text();
|
|
||||||
})["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function fetchJSON(url, data) {
|
|
||||||
var params = data ? new URLSearchParams(data).toString() : '';
|
|
||||||
var ifp = url.includes('?') ? data ? '&' : '' : '?';
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch("".concat(url).concat(ifp).concat(params), {
|
|
||||||
method: 'GET',
|
|
||||||
headers: _objectSpread({
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function postForm(url, data) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
body: data,
|
|
||||||
headers: _objectSpread({
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function postJSON(url, obj) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(obj),
|
|
||||||
headers: _objectSpread({
|
|
||||||
'Content-type': 'application/json; charset=UTF-8',
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function postStream(url, obj) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(obj),
|
|
||||||
headers: _objectSpread({
|
|
||||||
'Content-type': 'application/octet-stream',
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@ -1,4 +0,0 @@
|
|||||||
import * as _commons from './utils/commons.js';
|
|
||||||
export { _commons as commons };
|
|
||||||
import * as _request from './utils/request.js';
|
|
||||||
export { _request as request };
|
|
||||||
@ -1,115 +0,0 @@
|
|||||||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
||||||
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
||||||
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
||||||
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
||||||
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
||||||
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
||||||
// import { BUILD_VERSION } from '@/config'
|
|
||||||
var BUILD_VERSION = '';
|
|
||||||
|
|
||||||
// Ref: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
|
||||||
|
|
||||||
var customHeaders = [];
|
|
||||||
|
|
||||||
// 添加 HTTP Reuqest 自定义头部
|
|
||||||
export function appendRequestHeader(n, v) {
|
|
||||||
customHeaders.push({
|
|
||||||
name: n,
|
|
||||||
value: v
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function getRequestHeader() {
|
|
||||||
return customHeaders.reduce(function (acc, item) {
|
|
||||||
acc[item.name] = item.value;
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
}
|
|
||||||
function checkStatus(response) {
|
|
||||||
if (response.status >= 200 && response.status < 300) {
|
|
||||||
return response;
|
|
||||||
} else {
|
|
||||||
var message = 'Fetch error: ' + response.url + ' ' + response.status + ' (' + response.statusText + ')';
|
|
||||||
var error = new Error(message);
|
|
||||||
error.response = response;
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function checkBizCode(responseJson) {
|
|
||||||
if (responseJson.errcode === 0) {
|
|
||||||
return responseJson;
|
|
||||||
} else {
|
|
||||||
throw new Error(responseJson.errmsg + ': ' + responseJson.errcode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export function fetchText(url) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: _objectSpread({
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.text();
|
|
||||||
})["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
export function fetchJSON(url, data) {
|
|
||||||
var params = data ? new URLSearchParams(data).toString() : '';
|
|
||||||
var ifp = url.includes('?') ? data ? '&' : '' : '?';
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch("".concat(url).concat(ifp).concat(params), {
|
|
||||||
method: 'GET',
|
|
||||||
headers: _objectSpread({
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
export function postForm(url, data) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
body: data,
|
|
||||||
headers: _objectSpread({
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
export function postJSON(url, obj) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(obj),
|
|
||||||
headers: _objectSpread({
|
|
||||||
'Content-type': 'application/json; charset=UTF-8',
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
export function postStream(url, obj) {
|
|
||||||
var headerObj = getRequestHeader();
|
|
||||||
return fetch(url, {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(obj),
|
|
||||||
headers: _objectSpread({
|
|
||||||
'Content-type': 'application/octet-stream',
|
|
||||||
'X-Web-Version': BUILD_VERSION
|
|
||||||
}, headerObj)
|
|
||||||
}).then(checkStatus).then(function (response) {
|
|
||||||
return response.json();
|
|
||||||
}).then(checkBizCode)["catch"](function (error) {
|
|
||||||
throw error;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue