widgrensit/asobi-defold/asobi.http
The asobi REST transport. Each verb sends a JSON request to the client's server with its
access token, refreshes the token pair once and retries on a 401 outside /auth/, and
calls back with the decoded body or an error. A project reaches it as client.http, and
every asobi.api.* module sends through it.
- GitHub: widgrensit/asobi-defold — pinned to
v1.18.1- Pick a release from widgrensit/asobi-defold releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as http from "asobi.http"
- Pick a release from widgrensit/asobi-defold releases and add its Source code (zip) URL (or a packaged
Functions
get(client: Client, path: string, query?:HttpQuery, callback?:HttpCallback)post(client: Client, path: string, body?: Payload, callback?:HttpCallback)put(client: Client, path: string, body?: Payload, callback?:HttpCallback)delete(2 overloads)_request(client: unknown, method: unknown, path: unknown, url: unknown, data: unknown, callback: unknown, retried: unknown)_refresh_and_retry(client: unknown, method: unknown, path: unknown, url: unknown, data: unknown, callback: unknown)_error(body: unknown, status: unknown):HttpError_is_auth_path(path: unknown): boolean_headers(client: unknown): Record<string, string>_urlencode(str: unknown): string_build_url(client: unknown, path: unknown, query: unknown): string_handle_response(response: unknown, callback: unknown)
get(client: Client, path: string, query?: HttpQuery, callback?: HttpCallback)
Sends a GET to the client's base_url plus path.
Parameters
client:Client— The client to send as.path:string— The path, from its leading/.query?:HttpQuery— The query string.callback?:HttpCallback— Receives the decoded body, or the error.
post(client: Client, path: string, body?: Payload, callback?: HttpCallback)
Sends a POST with a JSON body to the client's base_url plus path.
Parameters
client:Client— The client to send as.path:string— The path, from its leading/.body?:Payload— The body. Defaults to{}.callback?:HttpCallback— Receives the decoded body, or the error.
put(client: Client, path: string, body?: Payload, callback?: HttpCallback)
Sends a PUT with a JSON body to the client's base_url plus path.
Parameters
client:Client— The client to send as.path:string— The path, from its leading/.body?:Payload— The body. Defaults to{}.callback?:HttpCallback— Receives the decoded body, or the error.
delete(2 overloads)
(client: Client, path: string, callback?: HttpCallback)Sends a
DELETEwith no body to the client'sbase_urlpluspath.Parameters
client:Client— The client to send as.path:string— The path, from its leading/.callback?:HttpCallback— Receives the decoded body, or the error.
(client: Client, path: string, body: Payload | undefined, callback?: HttpCallback)Sends a
DELETEto the client'sbase_urlpluspath, with a JSON body when one is given.Parameters
client:Client— The client to send as.path:string— The path, from its leading/.body:Payload | undefined— The body.callback?:HttpCallback— Receives the decoded body, or the error.
_request(client: unknown, method: unknown, path: unknown, url: unknown, data: unknown, callback: unknown, retried: unknown)
Internal. Sends one request and, on a 401 outside /auth/ while the client holds a
refresh token, refreshes once and replays it: every verb calls it.
Parameters
client:unknownmethod:unknownpath:unknownurl:unknowndata:unknowncallback:unknownretried:unknown
_refresh_and_retry(client: unknown, method: unknown, path: unknown, url: unknown, data: unknown, callback: unknown)
Internal. Refreshes the token pair through client.auth.refresh and replays the request,
or calls back with auth_expired: _request calls it.
Parameters
client:unknownmethod:unknownpath:unknownurl:unknowndata:unknowncallback:unknown
_error(body: unknown, status: unknown): HttpError
Internal. Shapes a failed response's body into an HttpError: _handle_response calls it.
Parameters
body:unknownstatus:unknown
Returns
HttpError
_is_auth_path(path: unknown): boolean
Internal. Whether a path is an /auth/ route, which never refreshes: _request calls it.
Parameters
path:unknown
Returns
boolean
_headers(client: unknown): Record<string, string>
Internal. The JSON content type and, while the client holds an access token, its bearer
header: _request calls it.
Parameters
client:unknown
Returns
Record<string, string>
_urlencode(str: unknown): string
Internal. Url-encodes a value after tostring: _build_url and asobi.api.auth's
unlink_provider call it.
Parameters
str:unknown
Returns
string
_build_url(client: unknown, path: unknown, query: unknown): string
Internal. Joins the client's base_url, the path and the query string: every verb calls it.
Parameters
client:unknownpath:unknownquery:unknown
Returns
string
_handle_response(response: unknown, callback: unknown)
Internal. Decodes a response's body and calls back with it, or with an error for a status
of 400 or more or a body that is not JSON: _request calls it.
Parameters
response:unknowncallback:unknown
Types
type HttpCallback = (data: Payload | undefined, err: HttpError | undefined) => void
type HttpQuery = Record<string, string | number | boolean>
HttpError
HttpError.status_code: number
The HTTP status code, 401 when the token refresh after a 401 failed.
HttpError.error: string
The server's message, HTTP <status> when it sent none, auth_expired when the token
refresh after a 401 failed, or invalid_response when the body was not JSON.
HttpError.code?: string
The server's machine-readable error code, empty when it sent none. Absent when the token refresh after a 401 failed, and when the body was not JSON.
HttpError.details?: unknown
The details the server sent with the error.
HttpError.fields?: unknown
The per-field errors a form-validation route sends.
HttpError.raw?: string
The first 200 characters of a body that was not JSON.