Skip to content

Commit 2b2425e

Browse files
committed
update
1 parent 24cc83e commit 2b2425e

3 files changed

Lines changed: 357 additions & 71 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- :page_with_curl: [Задачи](./assets/tasks.md)
1616
- :page_with_curl: [Паттерны](./assets/patterns/README.md)
1717
- :page_with_curl: [Алгоритмы и структуры данных](./assets/algorithms.md)
18+
- :page_with_curl: [Примеры использования Web API](./assets/webapi.md)
1819
- :page_with_curl: [Что за черт, JavaScript?](./assets/wtfjs.md)
1920

2021
## Другие проекты

assets/snippets.md

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3473,77 +3473,6 @@ const httpPost = async (url, data, cb, err = console.error) => {
34733473
}
34743474

34753475

3476-
// --- / --- / --- / --- //
3477-
/**
3478-
* REST-обертка для `fetch`
3479-
*/
3480-
const SERVER_URL = 'http://localhost:5000/todos'
3481-
3482-
const POST = 'POST'
3483-
const PUT = 'PUT'
3484-
const DELETE = 'DELETE'
3485-
3486-
const client = async (method, payload, endpoint = SERVER_URL) => {
3487-
let config = {}
3488-
3489-
if (method) {
3490-
config = {
3491-
method,
3492-
headers: {
3493-
'Content-Type': 'application/json'
3494-
}
3495-
}
3496-
3497-
if (method === POST || method === PUT) {
3498-
config.body = JSON.stringify(payload.body)
3499-
}
3500-
3501-
if (method === DELETE || method === PUT) {
3502-
endpoint = `${endpoint}/${payload.id}`
3503-
}
3504-
}
3505-
3506-
try {
3507-
const response = await fetch(endpoint, config)
3508-
if (response.ok) {
3509-
let message
3510-
3511-
switch (method) {
3512-
case POST: {
3513-
message = 'Data has been added'
3514-
break
3515-
}
3516-
case DELETE: {
3517-
message = 'Data has been removed'
3518-
break
3519-
}
3520-
case PUT: {
3521-
message = 'Data has been updated'
3522-
break
3523-
}
3524-
default: {
3525-
message = 'Data has been received'
3526-
}
3527-
}
3528-
3529-
console.log(message)
3530-
3531-
const result = await response.json()
3532-
return result
3533-
}
3534-
throw new Error(response.statusText)
3535-
} catch (err) {
3536-
console.error(err.message || err)
3537-
}
3538-
}
3539-
3540-
client.post = (payload, endpoint) => client(POST, payload, endpoint)
3541-
3542-
client.put = (payload, endpoint) => client(PUT, payload, endpoint)
3543-
3544-
client.delete = (payload, endpoint) => client(DELETE, payload, endpoint)
3545-
3546-
35473476
// --- / --- / --- / --- //
35483477
/**
35493478
* Работа с цветом

0 commit comments

Comments
 (0)