@statezero/core
Version:
The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate
82 lines (81 loc) • 3.43 kB
JavaScript
/**
* This file was auto-generated. Do not make direct changes to the file.
* Action: Send Notification
* App: django_app
*/
import axios from 'axios';
import { z } from 'zod';
import { configInstance, parseStateZeroError, serializeActionPayload } from '../../../../src';
import actionSchema from './send-notification.schema.json' assert { type: 'json' };
/**
* Zod schema for the input of sendNotification.
* NOTE: This is an object schema for validating the data payload.
*/
export const sendNotificationInputSchema = z.object({ message: z.string().max(500),
recipients: z.array(z.any()),
priority: z.enum(["low", "high"]).optional().default("low") });
/**
* Zod schema for the response of sendNotification.
*/
export const sendNotificationResponseSchema = z.object({ success: z.boolean(),
message_id: z.string(),
sent_to: z.number().int(),
queued_at: z.string().datetime({ message: "Invalid ISO 8601 datetime string" }) });
/**
* Send notifications to multiple recipients
*
* @param string message - Notification message to send
* @param any[] recipients - List of email addresses to notify
* @param 'low' | 'high' priority - Notification priority level
* @param {Object} [axiosOverrides] - Allows overriding Axios request parameters.
* @returns {Promise<Object>} A promise that resolves with the action's result.
*/
export async function sendNotification(message, recipients, priority = "low", axiosOverrides = {}) {
// Construct the data payload from the function arguments
const rawPayload = {
message,
recipients,
priority
};
// 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/send_notification/`;
const headers = backend.getAuthHeaders ? backend.getAuthHeaders() : {};
try {
const response = await axios.post(actionUrl, payload, {
headers: { 'Content-Type': 'application/json', ...headers },
...axiosOverrides,
});
return sendNotificationResponseSchema.parse(response.data);
}
catch (error) {
if (error instanceof z.ZodError) {
throw new Error(`Send Notification 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, sendNotification);
}
throw parsedError;
}
else if (error.request) {
throw new Error(`Send Notification failed: No response received from server.`);
}
else {
throw new Error(`Send Notification failed: ${error.message}`);
}
}
}
export default sendNotification;
sendNotification.actionName = 'send_notification';
sendNotification.title = 'Send Notification';
sendNotification.app = 'django_app';
sendNotification.permissions = ['CanSendNotifications'];
sendNotification.configKey = 'default';