@statezero/core
Version:
The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate
69 lines (68 loc) • 2.88 kB
JavaScript
/**
* This file was auto-generated. Do not make direct changes to the file.
* Action: Get Server Status
* App: django_app
*/
import axios from 'axios';
import { z } from 'zod';
import { configInstance, parseStateZeroError, serializeActionPayload } from '../../../../src';
import actionSchema from './get-server-status.schema.json' assert { type: 'json' };
/**
* Zod schema for the response of getServerStatus.
*/
export const getServerStatusResponseSchema = z.object({ status: z.string(),
timestamp: z.string().datetime({ message: "Invalid ISO 8601 datetime string" }),
random_number: z.number().int(),
server_info: z.record(z.any()),
demo_data: z.record(z.any()) });
/**
* Get server status and random data (no authentication required)
*
* @param {Object} [axiosOverrides] - Allows overriding Axios request parameters.
* @returns {Promise<Object>} A promise that resolves with the action's result.
*/
export async function getServerStatus(axiosOverrides = {}) {
// Construct the data payload from the function arguments
const rawPayload = {};
// Serialize payload - handles model instances (extracts PK), files, dates, etc.
const payload = serializeActionPayload(rawPayload, actionSchema.input_properties);
const config = configInstance.getConfig();
const backend = config.backendConfigs['default'];
if (!backend) {
throw new Error(`No backend configuration found for key: default`);
}
const baseUrl = backend.API_URL.replace(/\/+$/, '');
const actionUrl = `${baseUrl}/actions/get_server_status/`;
const headers = backend.getAuthHeaders ? backend.getAuthHeaders() : {};
try {
const response = await axios.post(actionUrl, payload, {
headers: { 'Content-Type': 'application/json', ...headers },
...axiosOverrides,
});
return getServerStatusResponseSchema.parse(response.data);
}
catch (error) {
if (error instanceof z.ZodError) {
throw new Error(`Get Server Status failed: Invalid response from server. Details: ${error.message}`);
}
if (error.response && error.response.data) {
const parsedError = parseStateZeroError(error.response.data);
if (Error.captureStackTrace) {
Error.captureStackTrace(parsedError, getServerStatus);
}
throw parsedError;
}
else if (error.request) {
throw new Error(`Get Server Status failed: No response received from server.`);
}
else {
throw new Error(`Get Server Status failed: ${error.message}`);
}
}
}
export default getServerStatus;
getServerStatus.actionName = 'get_server_status';
getServerStatus.title = 'Get Server Status';
getServerStatus.app = 'django_app';
getServerStatus.permissions = [];
getServerStatus.configKey = 'default';