UNPKG

@statezero/core

Version:

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

81 lines (80 loc) 3.31 kB
/** * This file was auto-generated. Do not make direct changes to the file. * Action: Calculate Hash * App: django_app */ import axios from 'axios'; import { z } from 'zod'; import { configInstance, parseStateZeroError, serializeActionPayload } from '../../../../src'; import actionSchema from './calculate-hash.schema.json' assert { type: 'json' }; /** * Zod schema for the input of calculateHash. * NOTE: This is an object schema for validating the data payload. */ export const calculateHashInputSchema = z.object({ text: z.string(), algorithm: z.enum(["md5", "sha1", "sha256"]).optional().default("sha256") }); /** * Zod schema for the response of calculateHash. */ export const calculateHashResponseSchema = z.object({ original_text: z.string(), algorithm: z.string(), hash: z.string(), text_length: z.number().int(), processed_by: z.string(), processed_at: z.string().datetime({ message: "Invalid ISO 8601 datetime string" }) }); /** * Calculate hash of input text (demonstrates string processing) * * @param string text - Text to hash * @param 'md5' | 'sha1' | 'sha256' algorithm - Hash algorithm to use * @param {Object} [axiosOverrides] - Allows overriding Axios request parameters. * @returns {Promise<Object>} A promise that resolves with the action's result. */ export async function calculateHash(text, algorithm = "sha256", axiosOverrides = {}) { // Construct the data payload from the function arguments const rawPayload = { text, algorithm }; // 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/calculate_hash/`; const headers = backend.getAuthHeaders ? backend.getAuthHeaders() : {}; try { const response = await axios.post(actionUrl, payload, { headers: { 'Content-Type': 'application/json', ...headers }, ...axiosOverrides, }); return calculateHashResponseSchema.parse(response.data); } catch (error) { if (error instanceof z.ZodError) { throw new Error(`Calculate Hash 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, calculateHash); } throw parsedError; } else if (error.request) { throw new Error(`Calculate Hash failed: No response received from server.`); } else { throw new Error(`Calculate Hash failed: ${error.message}`); } } } export default calculateHash; calculateHash.actionName = 'calculate_hash'; calculateHash.title = 'Calculate Hash'; calculateHash.app = 'django_app'; calculateHash.permissions = ['IsAuthenticated']; calculateHash.configKey = 'default';