Removed: anti-pattern return new Promise if it is a Promise Already
This commit is contained in:
parent
492650a938
commit
e931c568b5
|
@ -11,7 +11,7 @@ module.exports = {
|
|||
"SharedArrayBuffer": "readonly",
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018,
|
||||
"ecmaVersion": 2020,
|
||||
"sourceType": "module",
|
||||
},
|
||||
"plugins": [],
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import initializeSession from './helpers/initializeSession.js';
|
||||
import getImage from './helpers/getImage.js';
|
||||
import verifyImage from './helpers/verifyImage.js';
|
||||
import {cE as createElement} from './util/documnet.js';
|
||||
|
||||
/**
|
||||
* Create a uCaptcha box
|
||||
|
@ -37,8 +38,12 @@ function uCaptchaBox(websiteKey) {
|
|||
document.head.appendChild(styleTag);
|
||||
|
||||
const checkbox = createElement('div', {
|
||||
style: 'cursor:pointer;border-radius:3px;border:2px solid #888;width:25px;height:25px;display:inline-block',
|
||||
style: 'height:25px;display:inline-block',
|
||||
});
|
||||
checkbox.style.cursor = 'pointer';
|
||||
checkbox.style.borderRadius = '3px';
|
||||
checkbox.style.border = '2px solid #888';
|
||||
checkbox.style.width = '25px';
|
||||
|
||||
const captchaBox = createElement('div');
|
||||
captchaBox.appendChild(checkbox);
|
||||
|
@ -99,16 +104,4 @@ export function create(websiteKey, selector) {
|
|||
document.querySelector(selector).appendChild(uCaptchaBox(websiteKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* Short hand for document.createElement
|
||||
* @param {string} tagName Tag name
|
||||
* @param {object} attributes A key-value pair of DOM attributes
|
||||
* @return {HTMLElement}
|
||||
*/
|
||||
function createElement(tagName, attributes={}) {
|
||||
const elem = document.createElement(tagName);
|
||||
for (const [name, value] of Object.entries(attributes)) {
|
||||
elem.setAttribute(name, value);
|
||||
}
|
||||
return elem;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,16 @@
|
|||
import request from './util/request.js';
|
||||
import request from '../util/request.js';
|
||||
import UserSession from '../../shared/models/UserSession.js';
|
||||
|
||||
/**
|
||||
* @param {string} websiteKey
|
||||
* @return {Promise<UserSession>}
|
||||
*/
|
||||
export default function initializeSession(websiteKey) {
|
||||
return new Promise((resolve, reject)=>{
|
||||
request(`/api/init?k=${websiteKey}`)
|
||||
.then((resp)=>{
|
||||
console.log(resp);
|
||||
const session = new UserSession();
|
||||
session.deserialize(resp);
|
||||
resolve(session);
|
||||
})
|
||||
.catch((e)=>{
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
export default async function initializeSession(websiteKey) {
|
||||
return request(`/api/init?k=${websiteKey}`)
|
||||
.then((resp) => {
|
||||
console.log(resp);
|
||||
const session = new UserSession();
|
||||
session.deserialize(resp);
|
||||
return session;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
/**
|
||||
* @typedef {Object} RequestOptions
|
||||
* @property {Object<string, any>} [body] Request body
|
||||
* @property {string} [method] Request method
|
||||
* @property {XMLHttpRequestResponseType} [responseType] Return raw response from server
|
||||
*/
|
||||
|
||||
/** @type {RequestOptions} */
|
||||
const defaultOptions = {
|
||||
method: 'GET',
|
||||
responseType: 'text', // Text because we will need to substr it.
|
||||
};
|
||||
|
||||
/**
|
||||
* Make an HTTP request
|
||||
* @param {string} url
|
||||
* @param {RequestOptions} options
|
||||
* @return {Promise}
|
||||
*/
|
||||
export default function(url, options) {
|
||||
console.log(options);
|
||||
options = Object.assign({}, defaultOptions, options);
|
||||
console.log(options);
|
||||
return new Promise((resolve, reject)=>{
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open(options.method, url);
|
||||
|
||||
xhr.responseType = options.responseType;
|
||||
|
||||
xhr.onload = function() {
|
||||
if (this.status < 400) {
|
||||
if (options.responseType !== 'text') {
|
||||
resolve(this.response);
|
||||
} else {
|
||||
resolve(JSON.parse(this.responseText.substr(2)));
|
||||
}
|
||||
} else {
|
||||
reject(JSON.parse(this.responseText));
|
||||
}
|
||||
};
|
||||
|
||||
if (options.method === 'POST') {
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.send(JSON.stringify(options.body));
|
||||
} else {
|
||||
xhr.send();
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue