-
Notifications
You must be signed in to change notification settings - Fork 42
feat: ajax #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat: ajax #44
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
04361bc
chore: babel, webpack to transpile es6
heenakwag fe5c994
feat: initial ajax module
heenakwag 202ed66
feat: event handler - beforeRequest, success, error, complete
heenakwag 1b4a292
feat: return the Promise object if the browser supports
heenakwag fcc0e5d
feat: response and error wrapper
heenakwag 864bea8
feat: set default options using static property
heenakwag ac8e3ac
feat: support aliases for request methods
heenakwag b8fc963
chore: merge into one file
heenakwag 8edccb3
feat: defaults.headers.[method]
heenakwag 35a4e85
chore: apply code review
heenakwag da23137
chore: apply code review
heenakwag cf82789
docs: jsdoc for the ajax module
heenakwag bcb4de6
chore: use babel-plugin-add-module-exports and @babel/plugin-transfor…
heenakwag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "presets": [["@babel/preset-env", { "loose": true }]], | ||
| "plugins": ["add-module-exports"], | ||
| "comments": false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ajax/index.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module.exports = { | ||
| 'extends': ['tui/es6', 'plugin:prettier/recommended'], | ||
| 'parserOptions': { | ||
| 'sourceType': 'module' | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,332 @@ | ||
| "use strict"; | ||
|
|
||
| exports.__esModule = true; | ||
| exports["default"] = void 0; | ||
|
|
||
| var _forEachArray = _interopRequireDefault(require("../collection/forEachArray")); | ||
|
|
||
| var _forEachOwnProperties = _interopRequireDefault(require("../collection/forEachOwnProperties")); | ||
|
|
||
| var _extend = _interopRequireDefault(require("../object/extend")); | ||
|
|
||
| var _isArray = _interopRequireDefault(require("../type/isArray")); | ||
|
|
||
| var _isEmpty = _interopRequireDefault(require("../type/isEmpty")); | ||
|
|
||
| var _isFunction = _interopRequireDefault(require("../type/isFunction")); | ||
|
|
||
| var _isNull = _interopRequireDefault(require("../type/isNull")); | ||
|
|
||
| var _isObject = _interopRequireDefault(require("../type/isObject")); | ||
|
|
||
| var _isUndefined = _interopRequireDefault(require("../type/isUndefined")); | ||
|
|
||
| function _interopRequireDefault(obj) { | ||
| return obj && obj.__esModule ? obj : { | ||
| "default": obj | ||
| }; | ||
| } | ||
|
|
||
| function encodePairs(key, value) { | ||
| return encodeURIComponent(key) + "=" + encodeURIComponent((0, _isNull["default"])(value) || (0, _isUndefined["default"])(value) ? '' : value); | ||
| } | ||
|
|
||
| function serializeParams(key, value, serializedList) { | ||
| if ((0, _isArray["default"])(value)) { | ||
| (0, _forEachArray["default"])(value, function (arrVal, index) { | ||
| serializeParams(key + "[" + ((0, _isObject["default"])(arrVal) ? index : '') + "]", arrVal, serializedList); | ||
| }); | ||
| } else if ((0, _isObject["default"])(value)) { | ||
| (0, _forEachOwnProperties["default"])(value, function (objValue, objKey) { | ||
| serializeParams(key + "[" + objKey + "]", objValue, serializedList); | ||
| }); | ||
| } else { | ||
| serializedList.push(encodePairs(key, value)); | ||
| } | ||
| } | ||
|
|
||
| function serialize(params) { | ||
| if (!params || (0, _isEmpty["default"])(params)) { | ||
| return ''; | ||
| } | ||
|
|
||
| var serializedList = []; | ||
| (0, _forEachOwnProperties["default"])(params, function (value, key) { | ||
| serializeParams(key, value, serializedList); | ||
| }); | ||
| return serializedList.join('&'); | ||
| } | ||
|
|
||
| var getDefaultOptions = function getDefaultOptions() { | ||
| return { | ||
| baseURL: '', | ||
| headers: { | ||
| common: {}, | ||
| get: {}, | ||
| post: {}, | ||
| put: {}, | ||
| "delete": {}, | ||
| patch: {}, | ||
| options: {}, | ||
| head: {} | ||
| }, | ||
| serializer: serialize | ||
| }; | ||
| }; | ||
|
|
||
| var HTTP_PROTOCOL_REGEXP = /^(http|https):\/\//i; | ||
|
|
||
| function combineURL(baseURL, url) { | ||
| if (HTTP_PROTOCOL_REGEXP.test(url)) { | ||
| return url; | ||
| } | ||
|
|
||
| if (baseURL.slice(-1) === '/' && url.slice(0, 1) === '/') { | ||
| url = url.slice(1); | ||
| } | ||
|
|
||
| return baseURL + url; | ||
| } | ||
|
|
||
| function getComputedOptions(defaultOptions, customOptions) { | ||
| var baseURL = defaultOptions.baseURL, | ||
| defaultHeaders = defaultOptions.headers, | ||
| defaultSerializer = defaultOptions.serializer, | ||
| defaultBeforeRequest = defaultOptions.beforeRequest, | ||
| defaultSuccess = defaultOptions.success, | ||
| defaultError = defaultOptions.error, | ||
| defaultComplete = defaultOptions.complete; | ||
| var url = customOptions.url, | ||
| contentType = customOptions.contentType, | ||
| method = customOptions.method, | ||
| params = customOptions.params, | ||
| headers = customOptions.headers, | ||
| serializer = customOptions.serializer, | ||
| beforeRequest = customOptions.beforeRequest, | ||
| success = customOptions.success, | ||
| error = customOptions.error, | ||
| complete = customOptions.complete, | ||
| withCredentials = customOptions.withCredentials, | ||
| mimeType = customOptions.mimeType; | ||
heenakwag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var options = { | ||
| url: combineURL(baseURL, url), | ||
| method: method, | ||
| params: params, | ||
| headers: (0, _extend["default"])(defaultHeaders.common, defaultHeaders[method.toLowerCase()], headers), | ||
| serializer: serializer || defaultSerializer || serialize, | ||
| beforeRequest: [defaultBeforeRequest, beforeRequest], | ||
| success: [defaultSuccess, success], | ||
| error: [defaultError, error], | ||
| complete: [defaultComplete, complete], | ||
| withCredentials: withCredentials, | ||
| mimeType: mimeType | ||
| }; | ||
| options.contentType = contentType || options.headers['Content-Type']; | ||
| delete options.headers['Content-Type']; | ||
| return options; | ||
| } | ||
|
|
||
| var ENCODED_SPACE_REGEXP = /%20/g; | ||
| var QS_DELIM_REGEXP = /\?/; | ||
|
|
||
| function validateStatus(status) { | ||
| return status >= 200 && status < 300; | ||
| } | ||
|
|
||
| function hasRequestBody(method) { | ||
| return /^(?:POST|PUT|PATCH)$/.test(method.toUpperCase()); | ||
| } | ||
|
|
||
| function executeCallback(callback, param) { | ||
| if ((0, _isArray["default"])(callback)) { | ||
| (0, _forEachArray["default"])(callback, function (fn) { | ||
| return executeCallback(fn, param); | ||
| }); | ||
| } else if ((0, _isFunction["default"])(callback)) { | ||
| callback(param); | ||
| } | ||
| } | ||
|
|
||
| function parseHeaders(text) { | ||
| var headers = {}; | ||
| (0, _forEachArray["default"])(text.split('\r\n'), function (header) { | ||
| var _header$split = header.split(': '), | ||
| key = _header$split[0], | ||
| value = _header$split[1]; | ||
|
|
||
| if (key !== '' && !(0, _isUndefined["default"])(value)) { | ||
| headers[key] = value; | ||
| } | ||
| }); | ||
| return headers; | ||
| } | ||
|
|
||
| function parseJSONData(data) { | ||
| var result = ''; | ||
|
|
||
| try { | ||
| result = JSON.parse(data); | ||
| } catch (_) { | ||
| result = data; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function handleReadyStateChange(xhr, options) { | ||
| var readyState = xhr.readyState, | ||
| status = xhr.status, | ||
| statusText = xhr.statusText, | ||
| responseText = xhr.responseText; | ||
| var success = options.success, | ||
| resolve = options.resolve, | ||
| error = options.error, | ||
| reject = options.reject, | ||
| complete = options.complete; | ||
|
|
||
| if (readyState != XMLHttpRequest.DONE) { | ||
| return; | ||
| } | ||
|
|
||
| if (validateStatus(status)) { | ||
| var contentType = xhr.getResponseHeader('Content-Type'); | ||
| var data = responseText; | ||
|
|
||
| if (contentType && contentType.indexOf('application/json') > -1) { | ||
| data = parseJSONData(data); | ||
| } | ||
|
|
||
| executeCallback([success, resolve], { | ||
| status: status, | ||
| statusText: statusText, | ||
| data: data, | ||
| headers: parseHeaders(xhr.getAllResponseHeaders()) | ||
| }); | ||
| } else { | ||
| executeCallback([error, reject], { | ||
| status: status, | ||
| statusText: statusText | ||
| }); | ||
| } | ||
|
|
||
| executeCallback(complete, { | ||
| status: status, | ||
| statusText: statusText | ||
| }); | ||
| } | ||
|
|
||
| function open(xhr, options) { | ||
| var url = options.url, | ||
| method = options.method, | ||
| serializer = options.serializer, | ||
| params = options.params; | ||
| var requestUrl = url; | ||
|
|
||
| if (!hasRequestBody(method) && params) { | ||
| var qs = (QS_DELIM_REGEXP.test(url) ? '&' : '?') + serializer(params); | ||
| requestUrl = "" + url + qs; | ||
| } | ||
|
|
||
| xhr.open(method, requestUrl); | ||
| } | ||
|
|
||
| function applyConfig(xhr, options) { | ||
| var method = options.method, | ||
| contentType = options.contentType, | ||
| mimeType = options.mimeType, | ||
| headers = options.headers, | ||
| _options$withCredenti = options.withCredentials, | ||
| withCredentials = _options$withCredenti === void 0 ? false : _options$withCredenti; | ||
|
|
||
| if (withCredentials) { | ||
| xhr.withCredentials = withCredentials; | ||
| } | ||
|
|
||
| if (mimeType) { | ||
| xhr.overrideMimeType(mimeType); | ||
| } | ||
|
|
||
| (0, _forEachOwnProperties["default"])(headers, function (value, header) { | ||
| if (!(0, _isObject["default"])(value)) { | ||
| xhr.setRequestHeader(header, value); | ||
| } | ||
| }); | ||
|
|
||
| if (hasRequestBody(method)) { | ||
| xhr.setRequestHeader('Content-Type', contentType + "; charset=UTF-8"); | ||
| } | ||
|
|
||
| xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest'); | ||
| } | ||
|
|
||
| function send(xhr, options) { | ||
| var method = options.method, | ||
| serializer = options.serializer, | ||
| beforeRequest = options.beforeRequest, | ||
| _options$params = options.params, | ||
| params = _options$params === void 0 ? {} : _options$params, | ||
| _options$contentType = options.contentType, | ||
| contentType = _options$contentType === void 0 ? 'application/x-www-form-urlencoded' : _options$contentType; | ||
| var body = null; | ||
|
|
||
| if (hasRequestBody(method)) { | ||
| body = contentType.indexOf('application/x-www-form-urlencoded') > -1 ? serializer(params).replace(ENCODED_SPACE_REGEXP, '+') : JSON.stringify(params); | ||
| } | ||
|
|
||
| xhr.onreadystatechange = function () { | ||
| return handleReadyStateChange(xhr, options); | ||
| }; | ||
|
|
||
| executeCallback(beforeRequest, xhr); | ||
| xhr.send(body); | ||
| } | ||
|
|
||
| function ajax(options) { | ||
| var xhr = new XMLHttpRequest(); | ||
|
|
||
| var request = function request(opts) { | ||
| return (0, _forEachArray["default"])([open, applyConfig, send], function (fn) { | ||
| return fn(xhr, opts); | ||
| }); | ||
| }; | ||
|
|
||
| options = getComputedOptions(ajax.defaults, options); | ||
|
|
||
| if (typeof Promise !== 'undefined') { | ||
| return new Promise(function (resolve, reject) { | ||
| request((0, _extend["default"])(options, { | ||
| resolve: resolve, | ||
| reject: reject | ||
| })); | ||
| }); | ||
| } | ||
|
|
||
| request(options); | ||
| return null; | ||
| } | ||
|
|
||
| ajax.defaults = getDefaultOptions(); | ||
|
|
||
| ajax._reset = function () { | ||
| ajax.defaults = getDefaultOptions(); | ||
| }; | ||
|
|
||
| ajax._request = function (url, method, options) { | ||
| if (options === void 0) { | ||
| options = {}; | ||
| } | ||
|
|
||
| return ajax((0, _extend["default"])(options, { | ||
| url: url, | ||
| method: method | ||
| })); | ||
| }; | ||
|
|
||
| (0, _forEachArray["default"])(['get', 'post', 'put', 'delete', 'patch', 'options', 'head'], function (type) { | ||
| ajax[type] = function (url, options) { | ||
| return ajax._request(url, type.toUpperCase(), options); | ||
| }; | ||
| }); | ||
| var _default = ajax; | ||
| exports["default"] = _default; | ||
| module.exports = exports["default"]; | ||
heenakwag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.