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);| Parameter | Type | Required | Description |
|---|---|---|---|
method | String | Yes | GET, POST, PUT, or DELETE. Any letter case is accepted. The method is sent in uppercase. |
path | String | Yes | Relative path. No host. A leading / is removed. A ?query or #hash on the path is removed and is not merged into query. |
query | Object or null | No | Plain object of string, number, or boolean values. Numbers and booleans are sent as strings. |
body | JSON or null | No | JSON-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.
| Status | Response |
|---|---|
| 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
- Keep
pathrelative. Put search parameters inquery, not on the path string. - Do not send a bearer token, an
Authorizationheader, or a host. Native adds those. - Always use
try/catch. Showerror.errorMessagewithshowToastorshowAlert. - Read the result from
res.data.
Caveats
- Absolute URLs are rejected.
http://,https://, and a path that starts with//are invalid. querymust be a plain object ornull. Each value must be a string, a finite number, or a boolean.bodymust be JSON-serializable when it is sent.- The call has to run inside the app. Outside the webview the native bridge is missing and the promise rejects.
Updated about 2 hours ago
Did this page help you?