-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Evgeny Talagaev
committed
Sep 23, 2023
1 parent
a8b3b24
commit 8332896
Showing
28 changed files
with
938 additions
and
172 deletions.
There are no files selected for viewing
This file contains 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 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,76 @@ | ||
/* eslint-disable */ | ||
|
||
const METHODS = { | ||
GET: 'GET', | ||
POST: 'POST', | ||
PUT: 'PUT', | ||
DELETE: 'DELETE', | ||
}; | ||
|
||
function queryStringify (data) { | ||
let result = '?'; | ||
|
||
for (const [key, value] of Object.entries(data)) { | ||
result += `${key}=${value.toString()}&`; | ||
} | ||
|
||
return result.slice(0, result.length - 1); | ||
} | ||
|
||
class HTTPTransport { | ||
get = (url, options = {}) => { | ||
const { data } = options; | ||
return this.request((data ? `${url}${queryStringify(data)}` : url), {...options, method: METHODS.GET}); | ||
}; | ||
|
||
post = () => { | ||
return this.request(url, {...options, method: METHODS.POST}); | ||
} | ||
|
||
put = () => { | ||
return this.request(url, {...options, method: METHODS.PUT}); | ||
} | ||
|
||
delete = () => { | ||
return this.request(url, {...options, method: METHODS.DELETE}); | ||
} | ||
|
||
request = (url, options) => { | ||
const {method, headers, data, timeout = 5000} = options; | ||
|
||
return new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open(method, url); | ||
if (headers) xhr.setRequestHeader(...headers); | ||
|
||
xhr.onload = () => { | ||
resolve(xhr); | ||
}; | ||
|
||
xhr.onabort = reject; | ||
xhr.onerror = reject; | ||
xhr.ontimeout = reject; | ||
|
||
if (method === METHODS.GET) { | ||
xhr.send(); | ||
} else { | ||
xhr.send(JSON.stringify(data)); | ||
} | ||
|
||
setTimeout(() => { | ||
xhr.abort(); | ||
}, timeout) | ||
}) | ||
}; | ||
} | ||
|
||
function fetchWithRetry (url, options = {}) { | ||
let { retries = 2 } = options; | ||
|
||
if (retries === 0) { | ||
throw new Error('The number of attempts has been exhausted'); | ||
} | ||
|
||
return new HTTPTransport().get(url, options) | ||
.catch(err => fetchWithRetry(url, {...options, retries: retries - 1})) | ||
} |
This file contains 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,68 @@ | ||
const props = { | ||
name: 'Abby', | ||
chat: 'the last of us. Part II', | ||
getChat() { | ||
this._privateMethod(); | ||
}, | ||
_privateMethod() { | ||
console.log(this._privateProp); | ||
}, | ||
__privateMethodToo() {}, | ||
_privateProp: 'Нельзя получить просто так', | ||
}; | ||
|
||
const proxyProps = new Proxy(props, { | ||
get(o, name) { | ||
if (name.search('_') !== -1) { | ||
// console.log('Нет прав'); | ||
// return false; | ||
} | ||
|
||
return o[name]; | ||
// return o[name]; | ||
}, | ||
set(o, name, newValue) { | ||
if (name.search('_') !== -1) { | ||
throw new Error('Нет прав'); | ||
} | ||
|
||
o[name] = newValue; | ||
|
||
return true; | ||
}, | ||
deleteProperty(o, name) { | ||
if (name.search('_') !== -1) { | ||
throw new Error('Нет прав'); | ||
} | ||
|
||
return true; | ||
}, | ||
}); | ||
|
||
// proxyProps.getChat(); | ||
// delete proxyProps.chat; | ||
|
||
// proxyProps.newProp = 2; | ||
// console.log(proxyProps.newProp); | ||
|
||
try { | ||
proxyProps._newPrivateProp = 'Super game'; | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
|
||
console.log(props); | ||
|
||
// try { | ||
// delete proxyProps._privateProp; | ||
// } catch (error) { | ||
// console.log(error); // Error: Нет прав | ||
// } | ||
|
||
/* | ||
* Вывод в консоль следующий: | ||
Нельзя получить просто так | ||
2 | ||
Error: Нет прав | ||
Error: Нет прав | ||
*/ |
Oops, something went wrong.