UNPKG

@hhoangphuoc/escape-room-cli

Version:

A CLI for playing AI-generated escape room games. Install globally with: npm install -g @hhoangphuoc/escape-room-cli

44 lines (43 loc) 1.32 kB
import { getApiUrl } from './apiConfig.js'; const API_URL = getApiUrl(); /** * Handles the login API call. */ export async function loginUser(userId) { const response = await fetch(`${API_URL}/api/users/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId }), }); return response; } /** * Handles the user registration API call. */ export async function registerUser(payload) { const response = await fetch(`${API_URL}/api/users/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); return response; } /** * A type guard to check if a value is an object with an error property. */ function isErrorResponse(data) { return typeof data === 'object' && data !== null && 'error' in data; } /** * A generic function to handle API responses, parse JSON, and format errors. */ export async function handleApiResponse(response) { const data = await response.json(); if (response.ok) { return { ok: true, data: data }; } else { const errorMessage = isErrorResponse(data) ? data.error : `Request failed with status ${response.status}`; return { ok: false, data: { error: errorMessage } }; } }