From dece4a8f6b60f49e20faae9f60a09bf177e63d6d Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:13:25 +0700 Subject: [PATCH 1/4] Create index.js --- scripts/fetch-api/index.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 scripts/fetch-api/index.js diff --git a/scripts/fetch-api/index.js b/scripts/fetch-api/index.js new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/scripts/fetch-api/index.js @@ -0,0 +1 @@ + From d4a0319e6d240897f913e489058af330bef48919 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:19:33 +0700 Subject: [PATCH 2/4] Core File --- scripts/fetch-api/index.js | 118 +++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/scripts/fetch-api/index.js b/scripts/fetch-api/index.js index 8b137891..e4e98278 100644 --- a/scripts/fetch-api/index.js +++ b/scripts/fetch-api/index.js @@ -1 +1,119 @@ +import { + http, + HttpHeader, + HttpRequest, + HttpRequestMethod, +} from "@minecraft/server-net"; + +/** + * Class Fetch - Abstraction for HTTP Requests + */ +class Fetch { + /** + * Constructor to initialize the base URL. + * @param {string} baseURL - The base URL for API requests. + */ + constructor(baseURL) { + this.baseURL = baseURL.trim(); + } + + /** + * Performs an HTTP GET request. + * @param {string} path - The API endpoint path. + * @param {Object} [params={}] - Query parameters. + * @returns {Promise} - The response body as JSON. + */ + async get(path, params = {}) { + const queryString = this._buildQueryString(params); + const uri = `${this.baseURL}${path}${queryString}`; + const request = new HttpRequest(uri); + request.method = HttpRequestMethod.Get; + request.headers = [new HttpHeader("Content-Type", "application/json")]; + + const response = await http.request(request); + return this._handleResponse(response); + } + + /** + * Performs an HTTP POST request. + * @param {string} path - The API endpoint path. + * @param {Object} data - The data to send in the request body. + * @returns {Promise} - The response body as JSON. + */ + async post(path, data) { + const uri = `${this.baseURL}${path}`; + const request = new HttpRequest(uri); + request.method = HttpRequestMethod.Post; + request.body = JSON.stringify(data); + request.headers = [new HttpHeader("Content-Type", "application/json")]; + + const response = await http.request(request); + return this._handleResponse(response); + } + + /** + * Performs an HTTP PUT request. + * @param {string} path - The API endpoint path. + * @param {Object} data - The data to send in the request body. + * @returns {Promise} - The response body as JSON. + */ + async put(path, data) { + const uri = `${this.baseURL}${path}`; + const request = new HttpRequest(uri); + request.method = HttpRequestMethod.Put; + request.body = JSON.stringify(data); + request.headers = [new HttpHeader("Content-Type", "application/json")]; + + const response = await http.request(request); + return this._handleResponse(response); + } + + /** + * Performs an HTTP DELETE request. + * @param {string} path - The API endpoint path. + * @returns {Promise} - The response body as JSON. + */ + async delete(path) { + const uri = `${this.baseURL}${path}`; + const request = new HttpRequest(uri); + request.method = HttpRequestMethod.Delete; + request.headers = [new HttpHeader("Content-Type", "application/json")]; + + const response = await http.request(request); + return this._handleResponse(response); + } + + /** + * Handles the response from the server. + * @param {Object} response - The HTTP response object. + * @returns {Promise} - The parsed JSON body. + * @throws {Error} - If the response status is not 200. + */ + async _handleResponse(response) { + if (response.status !== 200) { + throw new Error( + `HTTP Error: ${response.status} - ${response.body}` + ); + } + return JSON.parse(response.body); + } + + /** + * Builds a query string from an object of parameters. + * @param {Object} params - The query parameters. + * @returns {string} - The query string. + */ + _buildQueryString(params) { + const entries = Object.entries(params); + if (entries.length === 0) return ""; + return ( + "?" + + entries + .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) + .join("&") + ); + } +} + +export { Fetch }; From 0f75dbcba689247603f315975f6162df73c0703b Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:25:38 +0700 Subject: [PATCH 3/4] Docs Readme for Know --- scripts/fetch-api/README.md | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/fetch-api/README.md diff --git a/scripts/fetch-api/README.md b/scripts/fetch-api/README.md new file mode 100644 index 00000000..c3cf73b8 --- /dev/null +++ b/scripts/fetch-api/README.md @@ -0,0 +1,54 @@ +# Fetch + +## Description + +The `Fetch` class simplifies HTTP requests in Minecraft Bedrock Edition. It supports common HTTP methods like `GET`, `POST`, `PUT`, and `DELETE`, and automatically handles JSON data. + +### Methods + +#### `constructor(baseURL: string)` +- Initializes a new `Fetch` instance with the specified base URL. + +#### `get(path: string, params?: Object): Promise` +- Sends an HTTP `GET` request to the specified path with optional query parameters. + +#### `post(path: string, data: Object): Promise` +- Sends an HTTP `POST` request to the specified path with the provided data. + +#### `put(path: string, data: Object): Promise` +- Sends an HTTP `PUT` request to the specified path with the provided data. + +#### `delete(path: string): Promise` +- Sends an HTTP `DELETE` request to the specified path. + +### Example +```js +import { Fetch } from './fetch.js'; + +// Initialize Fetch instance +const api = new Fetch("https://jsonplaceholder.typicode.com"); + +// GET example +api.get("/posts", { userId: 1 }).then((data) => console.log(data)); + +// POST example +api.post("/posts", { + title: "foo", + body: "bar", + userId: 1 +}).then((data) => console.log(data)); + +// PUT example +api.put("/posts/1", { + title: "updated title", + body: "updated body", + userId: 1 +}).then((data) => console.log(data)); + +// DELETE example +api.delete("/posts/1").then((data) => console.log(data)); +``` + +## Credits + +These scripts were written by [nperma](https://github.com/nperma) From 1b3c096a284a402e77ee6c29711912529bbfbab3 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Wed, 25 Dec 2024 10:32:08 +0700 Subject: [PATCH 4/4] Core FILE --- scripts/fetch-api/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/fetch-api/index.js b/scripts/fetch-api/index.js index e4e98278..1936a0bf 100644 --- a/scripts/fetch-api/index.js +++ b/scripts/fetch-api/index.js @@ -1,3 +1,7 @@ +// Script example for ScriptAPI +// Author: nperma +// Project: https://github.com/JaylyDev/ScriptAPI + import { http, HttpHeader,