Request

Proxies an HTTP call through the app so block code never sends a bearer token.

Description

The request helper proxies an HTTP call through the native app. The block sends the method, path, query, and body. Native performs the call, attaches credentials, and chooses the host. Block code never sends an Authorization header or a full URL.

Call Superfans.helpers.request. Do not call Superfans.actions.request.

Function Signature

const res = await Superfans.helpers.request({
  method: "GET",
  path: "loyalty-program/customer-access-token",
  query: { customerId: "123" },
  body: null
});

const customerToken =
  res && res.data && (res.data.customerAccessToken || res.data.customer_access_token);
ParameterTypeRequiredDescription
methodStringYesGET, POST, PUT, or DELETE. Any letter case is accepted. The method is sent in uppercase.
pathStringYesRelative path. No host. A leading / is removed. A ?query or #hash on the path is removed and is not merged into query.
queryObject or nullNoPlain object of string, number, or boolean values. Numbers and booleans are sent as strings.
bodyJSON or nullNoJSON-serializable body for POST and PUT.

Structure of Response

On a 2xx response the promise resolves with status, statusCode, and data. Read data. data may be null.

StatusResponse
Success{ "status": "success", "statusCode": 200, "data": {} }
Invalid input{ "errorCode": 400, "errorType": "ERROR_INVALID_INPUT", "errorMessage": "..." }
Hermes error{ "errorCode": 400, "errorType": "ERROR_API", "errorMessage": "...", "details": {} }
Service unavailable{ "errorCode": 503, "errorType": "ERROR_SERVICE_UNAVAILABLE", "errorMessage": "..." }

statusCode is the HTTP status from the backend. A failed call rejects. details is optional.

Example Usage

try {
  const res = await Superfans.helpers.request({
    method: "GET",
    path: "loyalty-program/customer-access-token",
    query: { customerId: "123" }
  });

  const customerToken =
    res && res.data && (res.data.customerAccessToken || res.data.customer_access_token);
} catch (error) {
  await Superfans.actions.showToast({
    title: "Something went wrong",
    message: error.errorMessage || "Please try again."
  });
}

Best Practices

  1. Keep path relative. Put search parameters in query, not on the path string.
  2. Do not send a bearer token, an Authorization header, or a host. Native adds those.
  3. Always use try/catch. Show error.errorMessage with showToast or showAlert.
  4. Read the result from res.data.

Caveats

  1. Absolute URLs are rejected. http://, https://, and a path that starts with // are invalid.
  2. query must be a plain object or null. Each value must be a string, a finite number, or a boolean.
  3. body must be JSON-serializable when it is sent.
  4. The call has to run inside the app. Outside the webview the native bridge is missing and the promise rejects.

Did this page help you?