Skip to Content
defold-typescript libraries
widgrensitasobi-defold

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
    1. Pick a release from widgrensit/asobi-defold releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as http from "asobi.http"

Functions

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)

  1. (client: Client, path: string, callback?: HttpCallback)

    Sends a DELETE with no body to the client's base_url plus path.

    Parameters

    • client: Client — The client to send as.
    • path: string — The path, from its leading /.
    • callback?: HttpCallback — Receives the decoded body, or the error.
  2. (client: Client, path: string, body: Payload | undefined, callback?: HttpCallback)

    Sends a DELETE to the client's base_url plus path, 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: 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)

Internal. Refreshes the token pair through client.auth.refresh and replays the request, or calls back with auth_expired: _request calls it.

Parameters

  • client: unknown
  • method: unknown
  • path: unknown
  • url: unknown
  • data: unknown
  • callback: unknown

_error(body: unknown, status: unknown): HttpError

Internal. Shapes a failed response's body into an HttpError: _handle_response calls it.

Parameters

  • body: unknown
  • status: 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: unknown
  • path: unknown
  • query: 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: unknown
  • callback: 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.

On this page