UNPKG

@statezero/core

Version:

The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate

71 lines (70 loc) 2.99 kB
/** * This file was auto-generated. Do not make direct changes to the file. * Action: Get User Info * App: django_app */ import axios from 'axios'; import { z } from 'zod'; import { configInstance, parseStateZeroError, serializeActionPayload } from '../../../../src'; import actionSchema from './get-user-info.schema.json' assert { type: 'json' }; /** * Zod schema for the response of getUserInfo. */ export const getUserInfoResponseSchema = z.object({ username: z.string(), email: z.string().email(), is_staff: z.boolean(), is_superuser: z.boolean(), date_joined: z.string().datetime({ message: "Invalid ISO 8601 datetime string" }), last_login: z.string().datetime({ message: "Invalid ISO 8601 datetime string" }).nullable(), server_time: z.string().datetime({ message: "Invalid ISO 8601 datetime string" }) }); /** * Get current user information (no serializers - simple action) * * @param {Object} [axiosOverrides] - Allows overriding Axios request parameters. * @returns {Promise<Object>} A promise that resolves with the action's result. */ export async function getUserInfo(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_user_info/`; const headers = backend.getAuthHeaders ? backend.getAuthHeaders() : {}; try { const response = await axios.post(actionUrl, payload, { headers: { 'Content-Type': 'application/json', ...headers }, ...axiosOverrides, }); return getUserInfoResponseSchema.parse(response.data); } catch (error) { if (error instanceof z.ZodError) { throw new Error(`Get User Info 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, getUserInfo); } throw parsedError; } else if (error.request) { throw new Error(`Get User Info failed: No response received from server.`); } else { throw new Error(`Get User Info failed: ${error.message}`); } } } export default getUserInfo; getUserInfo.actionName = 'get_user_info'; getUserInfo.title = 'Get User Info'; getUserInfo.app = 'django_app'; getUserInfo.permissions = ['IsAuthenticated']; getUserInfo.configKey = 'default';