@statezero/core
Version:
The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate
66 lines (65 loc) • 2.84 kB
JavaScript
/**
* This file was auto-generated. Do not make direct changes to the file.
* Action: Get Current Username
* App: django_app
*/
import axios from 'axios';
import { z } from 'zod';
import { configInstance, parseStateZeroError, serializeActionPayload } from '../../../../src';
import actionSchema from './get-current-username.schema.json' assert { type: 'json' };
/**
* Zod schema for the response of getCurrentUsername.
*/
export const getCurrentUsernameResponseSchema = z.object({ username: z.string(),
retrieved_at: z.string().datetime({ message: "Invalid ISO 8601 datetime string" }) });
/**
* Get current user's username - simple focused action
*
* @param {Object} [axiosOverrides] - Allows overriding Axios request parameters.
* @returns {Promise<Object>} A promise that resolves with the action's result.
*/
export async function getCurrentUsername(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_current_username/`;
const headers = backend.getAuthHeaders ? backend.getAuthHeaders() : {};
try {
const response = await axios.post(actionUrl, payload, {
headers: { 'Content-Type': 'application/json', ...headers },
...axiosOverrides,
});
return getCurrentUsernameResponseSchema.parse(response.data);
}
catch (error) {
if (error instanceof z.ZodError) {
throw new Error(`Get Current Username 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, getCurrentUsername);
}
throw parsedError;
}
else if (error.request) {
throw new Error(`Get Current Username failed: No response received from server.`);
}
else {
throw new Error(`Get Current Username failed: ${error.message}`);
}
}
}
export default getCurrentUsername;
getCurrentUsername.actionName = 'get_current_username';
getCurrentUsername.title = 'Get Current Username';
getCurrentUsername.app = 'django_app';
getCurrentUsername.permissions = ['IsAuthenticated'];
getCurrentUsername.configKey = 'default';