HTTP transport & response envelope
GStableHttp, DEFAULT_BASE_URL, DEFAULT_PUBLIC_BASE_URL, HttpMethod, GStableHttpOptions, and type ApiEnvelope are exported from gstable-js (src/http.ts, src/types/common.ts; see src/index.ts).
GStableHttp constructor
Builds a low-level client that knows private and public base URLs and how to sign private requests. Prefer GStableClient unless you need AiPaymentResource or custom paths.
new GStableHttp(options)
Parameters
Options object GStableHttpOptions (same fields as GStableClient uses):
| Field | Type | Required | Description |
|---|---|---|---|
apiKey | string | Yes | Trimmed; prefixed with Bearer unless already present. |
baseUrl | string | No | Private API root; default DEFAULT_BASE_URL. |
publicBaseUrl | string | No | Public API root; default DEFAULT_PUBLIC_BASE_URL. |
fetchImpl | typeof fetch | No | Override fetch (tests / custom runtimes). |
Example
import { GStableHttp } from 'gstable-js';
const http = new GStableHttp({
apiKey: process.env.GSTABLE_API_KEY!,
});
requestJson
Performs an authenticated GET or POST against the configured private base URL.
http.requestJson(method, path, body?)
Signature (conceptual)
requestJson<T>(method: 'GET' | 'POST', path: string, body?: unknown): Promise<T>
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
method | HttpMethod ('GET' | 'POST') | Yes | Only verbs used by the GStable API. |
path | string | Yes | Path segment(s) relative to the private base URL; may include or omit a leading /. |
body | unknown | No | For POST, JSON-serialized when undefined is not passed; GET ignores body. |
Headers (automatic)
Accept: application/jsonContent-Type: application/jsonAuthorization:Bearer+ your API key
Returns
Promise<T> — The API envelope’s data field after code === 0 and HTTP success.
Example
import { GStableHttp } from 'gstable-js';
const http = new GStableHttp({ apiKey: process.env.GSTABLE_API_KEY! });
type MyDto = { ok: boolean };
const path: string = '...'; // advanced use only — prefer Resource methods
const data = await http.requestJson<MyDto>('GET', path);
requestPublicJson
Performs GET or POST against the public base URL without Authorization.
http.requestPublicJson(method, path, body?)
Signature (conceptual)
requestPublicJson<T>(method: 'GET' | 'POST', path: string, body?: unknown): Promise<T>
Parameters
Same as requestJson for method, path, body.
Headers
Accept,Content-Typeonly — no bearer token.
Returns
Promise<T> — Unwrapped data on success.
Example
Prefer client.capability.all() in application code instead of calling requestPublicJson directly.
Response handling
The API responds with an envelope compatible with ApiEnvelope (generic data payload; see TypeScript in src/types/common.ts) with code, message, and data. The SDK:
- Reads response text and parses JSON.
- Validates a numeric
codefield exists. - On HTTP error or invalid JSON / non-envelope →
GStableError(often with hints for 401/403/non-JSON bodies). - On
code !== 0→createTypedApiError→GStableProductError|GStableCommonError|GStablePlatformError|GStableError. - On success → returns
dataonly.
See the API introduction for the envelope shape.
Network errors
Failures before any HTTP response use code: -1 and Network error: ….