Last active
February 29, 2024 23:18
-
-
Save Exponential-Workload/15efd9c864e4b60ac897392325bd7650 to your computer and use it in GitHub Desktop.
fetch.lua: A Roblox fetch() library for ts/js enjoyers - luadoc types included
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
@license @legal @copyright | |
jfetch.lua - A JS-fetch-in-Lua library. | |
This library is a wrapper around the Roblox HttpService API to provide a fetch-like interface. | |
It's source code, in it's entirety, can be found at https://gist.github.com/Exponential-Workload/15efd9c864e4b60ac897392325bd7650 | |
The MIT License (MIT) | |
Copyright © 2024 Expo. | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
--]] | |
--- The JSON library | |
--- @class JSONLib | |
--- @field public stringify fun(obj: any): string | |
--- @field public parse fun(str: string): any | |
--- The roblox http response type | |
--- @class RbxHttpResponse | |
--- @field public Success boolean Whether the request was successful. | |
--- @field public StatusCode number The status code of the response. | |
--- @field public StatusMessage string The status message of the response. | |
--- @field public Headers table The headers of the response. | |
--- @field public Body string The body of the response. | |
--- JS-alike http response type | |
--- @class HttpResponse | |
--- @field public success boolean Whether the request was successful. | |
--- @field public statusCode number The status code of the response. | |
--- @field public statusMessage string The status message of the response. | |
--- @field public headers table The headers of the response. | |
--- @field public body string The body of the response. | |
--- @field public json fun(): table? Parses the body as JSON if the content type is 'application/json'. | |
--- @field public text fun(): string Returns the body as text. | |
--- JS-alike Request Info type | |
--- @class RequestInfo | |
--- @field public body string|table? The body of the request. Sets the Content-Type to 'application/json' if a table is passed. | |
--- @field public headers table? The headers of the request. | |
--- @field public method string? The HTTP method to use (e.g., "GET", "POST"). | |
--- ## jfetch.lua<br/> | |
--- A JS-fetch-in-Lua library.<br/> | |
--- This library is a wrapper around the Roblox HttpService API to provide a fetch-like interface.<br/> | |
--- It is designed to be used in Roblox games and can be used to make HTTP requests to external servers.<br/> | |
--- It is a near-identical replacement for the `fetch` function in JavaScript.<br/><hr/><br/> | |
--- Copyright © 2024 Expo.<br/> | |
--- The MIT License (MIT).<br/> | |
--- [Source Code](https://gist.github.com/Exponential-Workload/15efd9c864e4b60ac897392325bd7650) | |
--- @async | |
--- @alias JFetch fun(url: string, options: RequestInfo): HttpResponse|nil | |
--- The jfetch loader | |
--- @param jsonLib JSONLib A JSON library like https://gist.githubusercontent.com/tylerneylon/59f4bcf316be525b30ab/raw/7f69cc2cea38bf68298ed3dbfc39d197d53c80de/json.lua | |
--- @return JFetch | |
local jFetchLoader = function(jsonLib) | |
local http = game:GetService 'HttpService' | |
--- The fetch function | |
--- @type JFetch | |
local jfetch = function(url, options) | |
-- Create the request object | |
local request = { | |
Url = url, | |
Method = options.method or 'GET', | |
Headers = options.headers or {}, | |
Body = nil, | |
} | |
-- Handle the body | |
if options.body then | |
if type(options.body) == 'string' then | |
request.Body = options.body | |
elseif options.body then | |
request.Body = jsonLib.stringify(options.body) | |
request.Headers['Content-Type'] = request.Headers['Content-Type'] or 'application/json' | |
end | |
end | |
---@type RbxHttpResponse | |
local rbxResponse = http:RequestAsync(request) | |
---@type HttpResponse | |
local res = { | |
success = rbxResponse.Success, | |
statusCode = rbxResponse.StatusCode, | |
statusMessage = rbxResponse.StatusMessage, | |
headers = rbxResponse.Headers, | |
body = rbxResponse.Body, | |
json = function() | |
if rbxResponse.Headers['Content-Type'] == 'application/json' then | |
return jsonLib.parse(rbxResponse.Body) | |
else | |
return nil | |
end | |
end, | |
text = function() | |
return rbxResponse.Body | |
end, | |
} | |
return res | |
end | |
return jfetch | |
end | |
return jFetchLoader |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment