mirror of
https://github.com/sky-systems/sky_phone.git
synced 2026-08-30 17:59:02 +00:00
c766cb5044
Introduce a browserPreview mode (isBrowserPreview) with adjusted viewport scaling and CSS (.phone-stage--browser-preview) and update related contract tests. Allow apiBase via URL params in nui utils so the dev build can call a local browser API base. Replace many SVG app icons with WebP (and update imports), swap crypto header PNG to WebP, and add a demo wallpaper image. Testserver now installs demo apps and configures a demo wallpaper; smoke tests updated to assert these browser-demo conditions.
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
const resourceName = globalThis.window?.GetParentResourceName?.() ?? 'sky_phone'
|
|
const requestTimeoutMs = 20_000
|
|
|
|
export type NuiResponse<T = unknown> = {
|
|
success: boolean
|
|
data?: T
|
|
error?: string
|
|
}
|
|
|
|
export async function nuiCall<T = unknown>(
|
|
endpoint: string,
|
|
data: Record<string, unknown> = {},
|
|
): Promise<NuiResponse<T>> {
|
|
const developmentParameters = new URLSearchParams(window.location.search)
|
|
const developmentPort = developmentParameters.get('apiPort')
|
|
const configuredApiBase = developmentParameters.get('apiBase')
|
|
const browserApiBase = configuredApiBase?.startsWith('/')
|
|
? configuredApiBase.replace(/\/$/, '')
|
|
: null
|
|
const baseUrl = browserApiBase
|
|
?? (import.meta.env.DEV
|
|
? `http://localhost:${developmentPort ?? '3002'}/api`
|
|
: `https://${resourceName}`)
|
|
const requestData = browserApiBase || import.meta.env.DEV
|
|
? {
|
|
...data,
|
|
_testScenario:
|
|
developmentParameters.get('testScenario') ?? undefined,
|
|
}
|
|
: data
|
|
const controller = new AbortController()
|
|
const timeoutId = window.setTimeout(() => controller.abort(), requestTimeoutMs)
|
|
|
|
try {
|
|
const response = await fetch(`${baseUrl}/${endpoint}`, {
|
|
body: JSON.stringify(requestData),
|
|
headers: { 'Content-Type': 'application/json' },
|
|
method: 'POST',
|
|
signal: controller.signal,
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const error = `${response.status} ${response.statusText}`
|
|
console.error(`[NUI] ${endpoint} failed: ${error}`)
|
|
return { error, success: false }
|
|
}
|
|
|
|
const body = await response.text()
|
|
return body ? (JSON.parse(body) as NuiResponse<T>) : { success: true }
|
|
} catch (error) {
|
|
const message = controller.signal.aborted
|
|
? 'request_timeout'
|
|
: error instanceof Error
|
|
? error.message
|
|
: 'Unknown error'
|
|
console.error(`[NUI] ${endpoint} failed:`, error)
|
|
return { error: message, success: false }
|
|
} finally {
|
|
window.clearTimeout(timeoutId)
|
|
}
|
|
}
|