Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Talagaev committed Sep 23, 2023
1 parent a8b3b24 commit 8332896
Show file tree
Hide file tree
Showing 28 changed files with 938 additions and 172 deletions.
12 changes: 11 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
"extends": "airbnb",
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts", ".scss", ".pug"]
}
}
},
"rules": {
"max-len": [2, 100],
"@typescript-eslint/no-unused-vars": 2,
"no-console": 0
"no-console": 0,
"import/extensions": 0,
"quotes": 0,
"semi": 0
}
}
76 changes: 76 additions & 0 deletions _http
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}))
}
68 changes: 68 additions & 0 deletions _proxyProps.js
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: Нет прав
*/
Loading

0 comments on commit 8332896

Please sign in to comment.