lesca-fetcher
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -14,2 +14,3 @@ export declare enum contentType { | ||
}; | ||
export declare const mergePath: (api?: String) => string; | ||
declare const Fetcher: { | ||
@@ -16,0 +17,0 @@ install: (setting: Config) => void; |
@@ -7,21 +7,21 @@ "use strict"; | ||
}); | ||
exports.formatType = exports["default"] = exports.contentType = void 0; | ||
exports.mergePath = exports.formatType = exports["default"] = exports.contentType = void 0; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
var contentType; | ||
exports.contentType = contentType; | ||
(function (contentType) { | ||
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) { (0, _defineProperty2["default"])(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; } | ||
var contentType = /*#__PURE__*/function (contentType) { | ||
contentType["URL_ENCODED"] = "application/x-www-form-urlencoded; charset=UTF-8"; | ||
contentType["JSON"] = "application/json;charset=utf-8"; | ||
})(contentType || (exports.contentType = contentType = {})); | ||
var formatType; | ||
exports.formatType = formatType; | ||
(function (formatType) { | ||
return contentType; | ||
}({}); | ||
exports.contentType = contentType; | ||
var formatType = /*#__PURE__*/function (formatType) { | ||
formatType[formatType["string"] = 0] = "string"; | ||
formatType[formatType["JSON"] = 1] = "JSON"; | ||
})(formatType || (exports.formatType = formatType = {})); | ||
return formatType; | ||
}({}); | ||
exports.formatType = formatType; | ||
var defaultConfig = { | ||
hostUrl: 'https://jsonplaceholder.typicode.com/todos/1', | ||
contentType: contentType.URL_ENCODED, | ||
contentType: contentType.JSON, | ||
formatType: formatType.JSON | ||
@@ -46,2 +46,7 @@ }; | ||
}; | ||
var mergePath = function mergePath() { | ||
var api = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '/api'; | ||
return api.slice(0, 1) === '/' ? "".concat(host).concat(api) : "".concat(host, "/").concat(api); | ||
}; | ||
exports.mergePath = mergePath; | ||
var post = function post() { | ||
@@ -51,3 +56,2 @@ var api = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '/api'; | ||
var method = 'POST'; | ||
var url = api.slice(0, 1) === '/' ? "".concat(host).concat(api) : "".concat(host, "/").concat(api); | ||
var body = JSON.stringify(data); | ||
@@ -59,4 +63,4 @@ if (headers['Content-Type'] === contentType.URL_ENCODED) { | ||
} | ||
return new Promise(function (resovle, rejack) { | ||
fetch(url, { | ||
return new Promise(function (resolve, reject) { | ||
fetch(mergePath(api), { | ||
method: method, | ||
@@ -68,15 +72,15 @@ body: body, | ||
res.json().then(function (e) { | ||
return resovle(e); | ||
return resolve(e); | ||
})["catch"](function (e) { | ||
return rejack(e); | ||
return reject(e); | ||
}); | ||
} else { | ||
res.text().then(function (e) { | ||
return resovle(e); | ||
return resolve(e); | ||
})["catch"](function (e) { | ||
return rejack(e); | ||
return reject(e); | ||
}); | ||
} | ||
})["catch"](function (e) { | ||
return rejack(e); | ||
return reject(e); | ||
}); | ||
@@ -88,5 +92,4 @@ }); | ||
var method = 'GET'; | ||
var url = api.slice(0, 1) === '/' ? "".concat(host).concat(api) : "".concat(host, "/").concat(api); | ||
return new Promise(function (resovle, rejack) { | ||
fetch(url, { | ||
return new Promise(function (resolve, reject) { | ||
fetch(mergePath(api), { | ||
method: method, | ||
@@ -97,15 +100,15 @@ headers: headers | ||
res.json().then(function (e) { | ||
return resovle(e); | ||
return resolve(e); | ||
})["catch"](function (e) { | ||
return rejack(e); | ||
return reject(e); | ||
}); | ||
} else { | ||
res.text().then(function (e) { | ||
return resovle(e); | ||
return resolve(e); | ||
})["catch"](function (e) { | ||
return rejack(e); | ||
return reject(e); | ||
}); | ||
} | ||
})["catch"](function (e) { | ||
return rejack(e); | ||
return reject(e); | ||
}); | ||
@@ -112,0 +115,0 @@ }); |
{ | ||
"name": "lesca-fetcher", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Integrate fetch for easier use", | ||
@@ -22,40 +22,40 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.20.7", | ||
"@babel/core": "^7.20.12", | ||
"@babel/cli": "^7.22.15", | ||
"@babel/core": "^7.22.20", | ||
"@babel/plugin-proposal-class-properties": "^7.18.6", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.20.7", | ||
"@babel/plugin-transform-runtime": "^7.19.6", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-react": "^7.18.6", | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@emotion/react": "^11.10.5", | ||
"@emotion/styled": "^11.10.5", | ||
"@line/liff": "^2.21.4", | ||
"@mui/icons-material": "^5.11.0", | ||
"@mui/material": "^5.11.4", | ||
"autoprefixer": "^10.4.13", | ||
"babel-loader": "^9.1.2", | ||
"concurrently": "^7.6.0", | ||
"css-loader": "^6.7.3", | ||
"@babel/plugin-transform-runtime": "^7.22.15", | ||
"@babel/preset-env": "^7.22.20", | ||
"@babel/preset-react": "^7.22.15", | ||
"@babel/preset-typescript": "^7.22.15", | ||
"@emotion/react": "^11.11.1", | ||
"@emotion/styled": "^11.11.0", | ||
"@line/liff": "^2.22.3", | ||
"@mui/icons-material": "^5.14.9", | ||
"@mui/material": "^5.14.10", | ||
"autoprefixer": "^10.4.15", | ||
"babel-loader": "^9.1.3", | ||
"concurrently": "^8.2.1", | ||
"css-loader": "^6.8.1", | ||
"file-loader": "^6.2.0", | ||
"gh-pages": "^4.0.0", | ||
"html-react-parser": "^3.0.7", | ||
"html-webpack-plugin": "^5.5.0", | ||
"gh-pages": "^6.0.0", | ||
"html-react-parser": "^4.2.2", | ||
"html-webpack-plugin": "^5.5.3", | ||
"lesca-url-parameters": "^2.0.7", | ||
"less": "^4.1.3", | ||
"less-loader": "^11.1.0", | ||
"less": "^4.2.0", | ||
"less-loader": "^11.1.3", | ||
"less-vars-to-js": "^1.3.0", | ||
"postcss": "^8.4.21", | ||
"postcss-loader": "^7.0.2", | ||
"postcss": "^8.4.30", | ||
"postcss-loader": "^7.3.3", | ||
"prismjs": "^1.29.0", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"style-loader": "^3.3.1", | ||
"ts-loader": "^9.4.2", | ||
"typescript": "^4.9.4", | ||
"webpack": "^5.75.0", | ||
"webpack-cli": "^5.0.1", | ||
"webpack-dev-server": "^4.11.1" | ||
"style-loader": "^3.3.3", | ||
"ts-loader": "^9.4.4", | ||
"typescript": "^5.2.2", | ||
"webpack": "^5.88.2", | ||
"webpack-cli": "^5.1.4", | ||
"webpack-dev-server": "^4.15.1" | ||
}, | ||
"author": "jamehsu1125 <jameshsu1125@gmail.com>" | ||
} |
@@ -35,5 +35,4 @@ [![NPM](https://img.shields.io/badge/NPM-ba443f?style=for-the-badge&logo=npm&logoColor=white)](https://www.npmjs.com/) | ||
const data = { name: 'myName', age: '18' }; | ||
Fetch.post(path, data).then((respone) => { | ||
// ...script | ||
console.log(respone); | ||
Fetch.post(path, data).then((respond) => { | ||
console.log(respond); | ||
}); | ||
@@ -46,5 +45,4 @@ }} | ||
const path = '/get'; | ||
Fetch.get(path).then((respone) => { | ||
// ...script | ||
console.log(respone); | ||
Fetch.get(path).then((respond) => { | ||
console.log(respond); | ||
}); | ||
@@ -66,2 +64,3 @@ }} | ||
| .**postStringify**(**api**:_string_, **data**:_object_):_string_ | post and stringify | | | ||
| .**mergePath**(**api**:_string_):_string_ | get full path | | | ||
@@ -68,0 +67,0 @@ #### config |
@@ -47,5 +47,8 @@ export enum contentType { | ||
export const mergePath = (api: String = '/api') => { | ||
return api.slice(0, 1) === '/' ? `${host}${api}` : `${host}/${api}`; | ||
}; | ||
const post = (api: String = '/api', data: Object) => { | ||
const method = 'POST'; | ||
const url = api.slice(0, 1) === '/' ? `${host}${api}` : `${host}/${api}`; | ||
@@ -59,4 +62,4 @@ let body: any = JSON.stringify(data); | ||
return new Promise((resovle, rejack) => { | ||
fetch(url, { method, body, headers }) | ||
return new Promise((resolve, reject) => { | ||
fetch(mergePath(api), { method, body, headers }) | ||
.then((res) => { | ||
@@ -66,12 +69,12 @@ if (format === formatType.JSON) { | ||
.json() | ||
.then((e) => resovle(e)) | ||
.catch((e) => rejack(e)); | ||
.then((e) => resolve(e)) | ||
.catch((e) => reject(e)); | ||
} else { | ||
res | ||
.text() | ||
.then((e) => resovle(e)) | ||
.catch((e) => rejack(e)); | ||
.then((e) => resolve(e)) | ||
.catch((e) => reject(e)); | ||
} | ||
}) | ||
.catch((e) => rejack(e)); | ||
.catch((e) => reject(e)); | ||
}); | ||
@@ -82,6 +85,5 @@ }; | ||
const method = 'GET'; | ||
const url = api.slice(0, 1) === '/' ? `${host}${api}` : `${host}/${api}`; | ||
return new Promise((resovle, rejack) => { | ||
fetch(url, { method, headers }) | ||
return new Promise((resolve, reject) => { | ||
fetch(mergePath(api), { method, headers }) | ||
.then((res) => { | ||
@@ -91,12 +93,12 @@ if (format === formatType.JSON) { | ||
.json() | ||
.then((e) => resovle(e)) | ||
.catch((e) => rejack(e)); | ||
.then((e) => resolve(e)) | ||
.catch((e) => reject(e)); | ||
} else { | ||
res | ||
.text() | ||
.then((e) => resovle(e)) | ||
.catch((e) => rejack(e)); | ||
.then((e) => resolve(e)) | ||
.catch((e) => reject(e)); | ||
} | ||
}) | ||
.catch((e) => rejack(e)); | ||
.catch((e) => reject(e)); | ||
}); | ||
@@ -103,0 +105,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
310871
1624
77