Skip to content

WEB SDK Request Layer

SlaunchxFetch is the high-level HTTP client in @slaunchx/web-sdk. It wraps fetch, builds standard headers, retries recoverable failures, refreshes tokens, and can route payloads through Secure Channel for encrypted POST requests.

SlaunchxFetch

ConstructorDescription
new SlaunchxFetch(config: FetchConfig)Requires baseUrl, portalAccessCode, and a shared TokenManager. Optional workspaceId, locale, onTokenExpired, and onError customize behavior.

Standard Requests

get, post, put, and delete all return Promise<ApiResponse<T>>.

typescript
import {
  SlaunchxFetch,
  TokenManager,
} from '@slaunchx/web-sdk';

const tokenManager = new TokenManager();

const client = new SlaunchxFetch({
  baseUrl: 'https://api.example.com',
  portalAccessCode: 'portal-web',
  tokenManager,
  locale: 'en',
  workspaceId: 'workspace_123',
  onError: (error) => {
    console.error(error.code, error.httpStatus, error.message);
  },
});

const profile = await client.get<{ id: string; email: string }>('/me');

if (!profile.success) {
  console.error(profile.error.code);
}

Secure Requests

securePost() encrypts the request body through SecureChannel, injects SC headers, and decrypts the response envelope before returning ApiResponse<T>.

typescript
const quote = await client.securePost<{ approved: boolean }>(
  '/payments/quote',
  { amount: 100, currency: 'USD' },
);

if (quote.success) {
  console.log(quote.data.approved);
}

HeaderBuilder

HeaderBuilder centralizes the default request headers and can be reused directly when custom transport code still needs SDK-compatible headers.

HeaderAdded ByCondition
Content-Type: application/jsonbuild()Always
X-PORTAL-ACCESS-CODEbuild()Always
X-Request-Idbuild()Always, generated as UUID v4
X-LOCALEbuild()Always, defaults to en
X-Workspace-Idbuild()When workspaceId is set
Authorization: Bearer <token>build()When a bearer token is supplied
X-SC-Version: 2buildSecure()Secure Channel requests only
X-SC-Session-IdbuildSecure()Secure Channel requests only
typescript
import { HeaderBuilder } from '@slaunchx/web-sdk';

const headers = new HeaderBuilder({
  portalAccessCode: 'portal-web',
  locale: 'en',
  workspaceId: 'workspace_123',
}).build('access-token');

RetryPolicy

RetryPolicy is the transport policy behind SlaunchxFetch.

BehaviorDetails
First 401Tries token refresh once, then retries the original request with fresh headers.
Concurrent 401sShare one in-flight refresh promise.
5xx retryRetries with exponential backoff: baseDelayMs * 2^attempt, capped by maxDelayMs.
DefaultsmaxRetries = 2, baseDelayMs = 500, maxDelayMs = 5000.
No refresh tokenReturns the original 401 without refresh.
typescript
import { RetryPolicy, TokenManager } from '@slaunchx/web-sdk';

const retryPolicy = new RetryPolicy(
  new TokenManager(),
  async () => {
    console.log('refresh access token');
  },
  { maxRetries: 3, baseDelayMs: 250, maxDelayMs: 2000 },
);

const response = await retryPolicy.execute(() => fetch('https://api.example.com/health'));
console.log(response.status);

ErrorHandler

mapResponseToError(response) converts a failed Response into SdkError.

InputOutput
JSON body matching BackendErrorBodynew SdkError(body.code, body.message, response.status)
Non-JSON or unreadable body`new SdkError('UNKNOWN_ERROR', response.statusText

PublicErrorCode is the exported enum of public backend error categories: UNAUTHORIZED, FORBIDDEN, NOT_FOUND, VALIDATION_ERROR, RATE_LIMITED, INTERNAL_ERROR, SERVICE_UNAVAILABLE, SESSION_EXPIRED, MFA_REQUIRED, and MFA_INVALID.

typescript
import { mapResponseToError } from '@slaunchx/web-sdk';

const response = await fetch('https://api.example.com/me');

if (!response.ok) {
  const error = await mapResponseToError(response);
  console.error(error.code, error.message);
}

Response Shape

All high-level request helpers return ApiResponse<T>.

FieldTypeMeaning
successbooleantrue on success, false on failure
dataTPresent when success is true
errorSdkErrorPresent when success is false

SlaunchX Internal Documentation