thyris-client.mjs
Shared API client helper used by the other examples.
const DEFAULT_BASE_URL = "https://merchant.thyris.cloud"
export function readConfig() {
const baseUrl = (process.env.THYRIS_BASE_URL || DEFAULT_BASE_URL).replace(/\/$/, "")
const apiKey = process.env.THYRIS_API_KEY
const storeId = process.env.THYRIS_STORE_ID
if (!apiKey) {
throw new Error("Missing THYRIS_API_KEY environment variable")
}
return { baseUrl, apiKey, storeId }
}
export function requireStoreId(storeId) {
if (!storeId) {
throw new Error("Missing THYRIS_STORE_ID environment variable")
}
return storeId
}
export async function thyrisRequest(config, path, options = {}) {
const response = await fetch(`${config.baseUrl}${path}`, {
...options,
headers: {
Accept: "application/json",
Authorization: `Bearer ${config.apiKey}`,
...(options.body ? { "Content-Type": "application/json" } : {}),
...options.headers,
},
})
const text = await response.text()
const body = text ? parseJson(text) : null
if (!response.ok) {
const detail = body ? JSON.stringify(body) : text
throw new Error(`Thyris API ${response.status} ${response.statusText}: ${detail}`)
}
return body
}
export function parseJson(text) {
try {
return JSON.parse(text)
} catch {
return text
}
}
export function printJson(value) {
console.log(JSON.stringify(value, null, 2))
}