-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.lua
163 lines (151 loc) · 8.71 KB
/
http.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---@meta
---An API for making [HTTP
---requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). Allows
---sending/receiving data to/from a web server.
---
---[Making requests to local IPs](https://tweaked.cc/guide/local_ips.html) is
---blocked by default but can be enabled in the configuration file.
------
---[Official Documentation](https://tweaked.cc/module/http.html)
http = {}
---Asynchronously make a
---[GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) or
---[POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request
---
---When the request is completed, the `http_success` or `http_failure` event will be fired.
---@async
---@param url string The URL to make the request to
---@param body? string The body of the request. If provided, a [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request will be made
---@param headers? table<ccTweaked.http.HTTP_REQUEST_HEADERS, string> The [request headers](https://developer.mozilla.org/en-US/docs/Glossary/Request_header)
---@param binary? boolean If the request should be a binary request. If true, the body will not be UTF-8 encoded and the response will not be decoded
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:request)
function http.request(url, body, headers, binary) end
---Asynchronously make a [HTTP request](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
---
---When the request is completed, the `http_success` or `http_failure` event will be fired.
---@async
---@param request ccTweaked.http.HTTP_REQUEST
---## Request Parameter Shape
---```
---request = {
--- url: string, -- The URL to make the request to
--- body?: string, -- The body of the request, if it has one
--- headers?: {[string] = string}, -- The request headers
--- binary?: boolean, -- If the request should be a binary request. If true, the body will not be UTF-8 encoded and the response will not be decoded
--- method?: string, -- The HTTP method to use
--- redirect?: boolean -- Whether HTTP redirects should be followed. Defaults to true
---}
---```
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:request)
function http.request(request) end
---Make a [GET](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) request
---@param url string The URL to make the request to
---@param headers? table<ccTweaked.http.HTTP_REQUEST_HEADERS, string> The [request headers](https://developer.mozilla.org/en-US/docs/Glossary/Request_header)
---@param binary? boolean If the request should be a binary request. If true, the body will not be UTF-8 encoded and the response will not be decoded
---@return ccTweaked.http.Response|ccTweaked.http.BinaryResponse|nil response The HTTP response object. `nil` when the request fails
---@return string message Why the request failed
---@return nil|ccTweaked.http.Response|ccTweaked.http.BinaryResponse failedResponse The response object for the failed request, if available
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:get)
function http.get(url, headers, binary) end
---Make a HTTP request
---@param request ccTweaked.http.HTTP_REQUEST
---@return ccTweaked.http.Response|ccTweaked.http.BinaryResponse|nil response The HTTP response object. `nil` when the request fails
---@return string message Why the request failed
---@return nil|ccTweaked.http.Response|ccTweaked.http.BinaryResponse failedResponse The response object for the failed request, if available
---## Request Parameter Shape
---```
---request = {
--- url: string, -- The URL to make the request to
--- body?: string, -- The body of the request, if it has one
--- headers?: {[string] = string}, -- The request headers
--- binary?: boolean, -- If the request should be a binary request. If true, the body will not be UTF-8 encoded and the response will not be decoded
--- method?: string, -- The HTTP method to use
--- redirect?: boolean -- Whether HTTP redirects should be followed. Defaults to true
---}
---```
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:get)
function http.get(request) end
---Make a [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request
---@param url string The URL to make the request to
---@param body string The body of the [POST](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) request
---@param headers? table<ccTweaked.http.HTTP_REQUEST_HEADERS, string> The [request headers](https://developer.mozilla.org/en-US/docs/Glossary/Request_header)
---@param binary? boolean If the request should be a binary request. If true, the body will not be UTF-8 encoded and the response will not be decoded
---@return ccTweaked.http.Response|ccTweaked.http.BinaryResponse|nil response The HTTP response object. `nil` when the request fails
---@return string message Why the request failed
---@return nil|ccTweaked.http.Response|ccTweaked.http.BinaryResponse failedResponse The response object for the failed request, if available
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:post)
function http.post(url, body, headers, binary) end
---Make a HTTP request
---@param request ccTweaked.http.HTTP_REQUEST
---@return ccTweaked.http.Response|ccTweaked.http.BinaryResponse|nil response The HTTP response object. `nil` when the request fails
---@return string message Why the request failed
---@return nil|ccTweaked.http.Response|ccTweaked.http.BinaryResponse failedResponse The response object for the failed request, if available
---## Request Parameter Shape
---```
---request = {
--- url: string, -- The URL to make the request to
--- body?: string, -- The body of the request, if it has one
--- headers?: {[string] = string}, -- The request headers
--- binary?: boolean, -- If the request should be a binary request. If true, the body will not be UTF-8 encoded and the response will not be decoded
--- method?: string, -- The HTTP method to use
--- redirect?: boolean -- Whether HTTP requests should be followed. Defaults to true
---}
---```
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:post)
function http.post(request) end
---Asynchronously determine if a URL can be requested
---
---This will immediately return if the URL entered is itself invalid. You will
---then want to listen to the `http_check` event which will contain more info on
---if the URL can be requested.
---@async
---@param url string The URL to check
---@return boolean valid If the URL is valid. It could still 404, this is just confirming the URL is valid (read above)
---@return nil|string message Why the URL is invalid (e.g. blocked, malformed, etc.)
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:checkURLAsync)
function http.checkURLAsync(url) end
---Determine if a URL can be requested
---@param url string The URL to check
---@return boolean valid If the URL can be requested
---@return nil|string message Why the URL cannot be requested (e.g. blocked, malformed, etc.)
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:checkURL)
function http.checkURL(url) end
---Open a websocket
---@param url string The URL of the websocket to connect to. Should use the `ws://` or `wss://` protocol
---@param headers? table<string, string> Headers to send for the handshake
---@return ccTweaked.http.Websocket|false websocket The websocket connection object or false if it failed to connect
---@return string message Why the websocket failed to connect
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:websocket)
function http.websocket(url, headers) end
---Open a websocket
---@param request ccTweaked.http.WEBSOCKET_OPTIONS Options for the websocket connection
---@return ccTweaked.http.Websocket|false websocket The websocket connection object or false if it failed to connect
---@return string message Why the websocket failed to connect
---## Request Parameter Shape
---```
---request = {
--- url: string, -- The URL of the websocket to connect to. Should use the `ws://` or `wss://` protocol
--- headers?: {[string] = string}, -- Headers to send for the handshake
--- timeout?: number -- The amount of seconds to wait, before potentionally timing out
---}
---```
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:websocket)
function http.websocket(request) end
---Asynchronously open a websocket
---
---Returns immediately and a `websocket_success` or `websocket_failure` event will be fired when the request is completed
---@param url string The URL of the websocket to connect to. Should use the `ws://` or `wss://` protocol
---@param headers? table<string, string> Headers to send for the handshake
------
---[Official Documentation](https://tweaked.cc/module/http.html#v:websocketAsync)
function http.websocketAsync(url, headers) end