Skip to content

add fetch methodes request to javascript #126

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions public/consolidated/javascript.json
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,58 @@
"contributors": [],
"code": "const times = (func, n) => {\n Array.from(Array(n)).forEach(() => {\n func();\n });\n};\n\n// Usage:\nconst randomFunction = () => console.log('Function called!');\ntimes(randomFunction, 3); // Logs 'Function called!' three times\n"
},
{
"title": "DELETE request",
"description": "Simple request a DELETE api endpoint with a body",
"author": "kruimol",
"tags": [
"javascript",
"function",
"fetch",
"delete"
],
"contributors": [],
"code": "// then\nfetch(\"/api/delete\" /* api endpoint */, {\n method: \"DELETE\",\n})\n .then((response) => response.text())\n .then((response) => {\n console.log(response);\n })\n .catch((error) => console.error(\"Error:\", error));\n"
},
{
"title": "GET request",
"description": "Simple request a GET api endpoint",
"author": "kruimol",
"tags": [
"javascript",
"function",
"fetch",
"get"
],
"contributors": [],
"code": "// then\nfetch(\"/api/get\" /* api endpoint */)\n .then((response) => {\n console.log(response);\n })\n .catch((error) => console.error(\"Error:\", error));\n"
},
{
"title": "POST request",
"description": "Simple request a POST api endpoint with a body",
"author": "kruimol",
"tags": [
"javascript",
"function",
"fetch",
"post"
],
"contributors": [],
"code": "// then\nfetch(\"/api/post\" /* api endpoint */, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({ a: 1, b: \"Textual content\" })\n})\n .then((response) => response.json())\n .then((response) => {\n console.log(response);\n })\n .catch((error) => console.error(\"Error:\", error));\n"
},
{
"title": "PUT request",
"description": "Simple request a PUT api endpoint with a body",
"author": "kruimol",
"tags": [
"javascript",
"function",
"fetch",
"put"
],
"contributors": [],
"code": "// then\nfetch(\"/api/PUT\" /* api endpoint */, {\n method: \"PUT\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({ a: 1, b: \"Textual content\" })\n})\n .then((response) => response.json())\n .then((response) => {\n console.log(response);\n })\n .catch((error) => console.error(\"Error:\", error));\n"
},
{
"title": "Sleep Function",
"description": "Waits for a specified amount of milliseconds before resolving.",
Expand Down
18 changes: 18 additions & 0 deletions snippets/javascript/function-utilities/request-DELETE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: DELETE request
description: Simple request a DELETE api endpoint with a body
author: kruimol
tags: javascript,function,fetch,delete
---

```js
// then
fetch("/api/delete" /* api endpoint */, {
method: "DELETE",
})
.then((response) => response.text())
.then((response) => {
console.log(response);
})
.catch((error) => console.error("Error:", error));
```
15 changes: 15 additions & 0 deletions snippets/javascript/function-utilities/request-GET.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: GET request
description: Simple request a GET api endpoint
author: kruimol
tags: javascript,function,fetch,get
---

```js
// then
fetch("/api/get" /* api endpoint */)
.then((response) => {
console.log(response);
})
.catch((error) => console.error("Error:", error));
```
22 changes: 22 additions & 0 deletions snippets/javascript/function-utilities/request-POST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: POST request
description: Simple request a POST api endpoint with a body
author: kruimol
tags: javascript,function,fetch,post
---

```js
// then
fetch("/api/post" /* api endpoint */, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ a: 1, b: "Textual content" })
})
.then((response) => response.json())
.then((response) => {
console.log(response);
})
.catch((error) => console.error("Error:", error));
```
22 changes: 22 additions & 0 deletions snippets/javascript/function-utilities/request-PUT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: PUT request
description: Simple request a PUT api endpoint with a body
author: kruimol
tags: javascript,function,fetch,put
---

```js
// then
fetch("/api/PUT" /* api endpoint */, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ a: 1, b: "Textual content" })
})
.then((response) => response.json())
.then((response) => {
console.log(response);
})
.catch((error) => console.error("Error:", error));
```
Loading