From a90466b159b79ef77a38a7df0288123ded87c7f5 Mon Sep 17 00:00:00 2001 From: Kruimol <41127358+kruimol@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:16:39 +0100 Subject: [PATCH 1/2] add fetch request --- .../function-utilities/request-DELETE.md | 18 +++++++++++++++ .../function-utilities/request-GET.md | 15 +++++++++++++ .../function-utilities/request-POST.md | 22 +++++++++++++++++++ .../function-utilities/request-PUT.md | 22 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 snippets/javascript/function-utilities/request-DELETE.md create mode 100644 snippets/javascript/function-utilities/request-GET.md create mode 100644 snippets/javascript/function-utilities/request-POST.md create mode 100644 snippets/javascript/function-utilities/request-PUT.md diff --git a/snippets/javascript/function-utilities/request-DELETE.md b/snippets/javascript/function-utilities/request-DELETE.md new file mode 100644 index 00000000..6145b522 --- /dev/null +++ b/snippets/javascript/function-utilities/request-DELETE.md @@ -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)); +``` diff --git a/snippets/javascript/function-utilities/request-GET.md b/snippets/javascript/function-utilities/request-GET.md new file mode 100644 index 00000000..5edae682 --- /dev/null +++ b/snippets/javascript/function-utilities/request-GET.md @@ -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)); +``` diff --git a/snippets/javascript/function-utilities/request-POST.md b/snippets/javascript/function-utilities/request-POST.md new file mode 100644 index 00000000..ecac8bb3 --- /dev/null +++ b/snippets/javascript/function-utilities/request-POST.md @@ -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)); +``` diff --git a/snippets/javascript/function-utilities/request-PUT.md b/snippets/javascript/function-utilities/request-PUT.md new file mode 100644 index 00000000..aab2179c --- /dev/null +++ b/snippets/javascript/function-utilities/request-PUT.md @@ -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)); +``` From 58ded2389f75d5709d931b90065810ee871de725 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 2 Jan 2025 13:18:23 +0000 Subject: [PATCH 2/2] Update consolidated snippets --- public/consolidated/javascript.json | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index 7b1ceee5..193b08e4 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -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.",