🔥 initial commit 🔥
This commit is contained in:
201
assets/js/ash_rpc.ts
Normal file
201
assets/js/ash_rpc.ts
Normal file
@@ -0,0 +1,201 @@
|
||||
// Generated by AshTypescript - RPC Actions
|
||||
// Do not edit this file manually
|
||||
|
||||
|
||||
export type * from "./ash_types";
|
||||
|
||||
// Helper Functions
|
||||
|
||||
/**
|
||||
* Configuration options for action RPC requests
|
||||
*/
|
||||
export interface ActionConfig {
|
||||
// Request data
|
||||
input?: Record<string, any>;
|
||||
identity?: any;
|
||||
fields?: Array<string | Record<string, any>>; // Field selection
|
||||
filter?: Record<string, any>; // Filter options (for reads)
|
||||
sort?: string | string[]; // Sort options
|
||||
page?:
|
||||
| {
|
||||
// Offset-based pagination
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
count?: boolean;
|
||||
}
|
||||
| {
|
||||
// Keyset pagination
|
||||
limit?: number;
|
||||
after?: string;
|
||||
before?: string;
|
||||
};
|
||||
|
||||
// Metadata
|
||||
metadataFields?: ReadonlyArray<string>;
|
||||
|
||||
// HTTP customization
|
||||
headers?: Record<string, string>; // Custom headers
|
||||
fetchOptions?: RequestInit; // Fetch options (signal, cache, etc.)
|
||||
customFetch?: (
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit,
|
||||
) => Promise<Response>;
|
||||
|
||||
// Multitenancy
|
||||
tenant?: string; // Tenant parameter
|
||||
|
||||
// Hook context
|
||||
hookCtx?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration options for validation RPC requests
|
||||
*/
|
||||
export interface ValidationConfig {
|
||||
// Request data
|
||||
input?: Record<string, any>;
|
||||
|
||||
// HTTP customization
|
||||
headers?: Record<string, string>;
|
||||
fetchOptions?: RequestInit;
|
||||
customFetch?: (
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit,
|
||||
) => Promise<Response>;
|
||||
|
||||
// Hook context
|
||||
hookCtx?: Record<string, any>;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Gets the CSRF token from the page's meta tag
|
||||
* Returns null if no CSRF token is found
|
||||
*/
|
||||
export function getPhoenixCSRFToken(): string | null {
|
||||
return document
|
||||
?.querySelector("meta[name='csrf-token']")
|
||||
?.getAttribute("content") || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds headers object with CSRF token for Phoenix applications
|
||||
* Returns headers object with X-CSRF-Token (if available)
|
||||
*/
|
||||
export function buildCSRFHeaders(headers: Record<string, string> = {}): Record<string, string> {
|
||||
const csrfToken = getPhoenixCSRFToken();
|
||||
if (csrfToken) {
|
||||
headers["X-CSRF-Token"] = csrfToken;
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal helper function for making action RPC requests
|
||||
* Handles hooks, request configuration, fetch execution, and error handling
|
||||
* @param config Configuration matching ActionConfig
|
||||
*/
|
||||
export async function executeActionRpcRequest<T>(
|
||||
payload: Record<string, any>,
|
||||
config: ActionConfig
|
||||
): Promise<T> {
|
||||
const processedConfig = config;
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
...processedConfig.headers,
|
||||
...config.headers,
|
||||
};
|
||||
|
||||
const fetchFunction = config.customFetch || processedConfig.customFetch || fetch;
|
||||
const fetchOptions: RequestInit = {
|
||||
...processedConfig.fetchOptions,
|
||||
...config.fetchOptions,
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
const response = await fetchFunction("/rpc/run", fetchOptions);
|
||||
const result = response.ok ? await response.json() : null;
|
||||
|
||||
|
||||
if (!response.ok) {
|
||||
return {
|
||||
success: false,
|
||||
errors: [
|
||||
{
|
||||
type: "network_error",
|
||||
message: `Network request failed: ${response.statusText}`,
|
||||
shortMessage: "Network error",
|
||||
vars: { statusCode: response.status, statusText: response.statusText },
|
||||
fields: [],
|
||||
path: [],
|
||||
details: { statusCode: response.status }
|
||||
}
|
||||
],
|
||||
} as T;
|
||||
}
|
||||
|
||||
return result as T;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal helper function for making validation RPC requests
|
||||
* Handles hooks, request configuration, fetch execution, and error handling
|
||||
* @param config Configuration matching ValidationConfig
|
||||
*/
|
||||
export async function executeValidationRpcRequest<T>(
|
||||
payload: Record<string, any>,
|
||||
config: ValidationConfig
|
||||
): Promise<T> {
|
||||
const processedConfig = config;
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
...processedConfig.headers,
|
||||
...config.headers,
|
||||
};
|
||||
|
||||
const fetchFunction = config.customFetch || processedConfig.customFetch || fetch;
|
||||
const fetchOptions: RequestInit = {
|
||||
...processedConfig.fetchOptions,
|
||||
...config.fetchOptions,
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(payload),
|
||||
};
|
||||
|
||||
const response = await fetchFunction("/rpc/validate", fetchOptions);
|
||||
const result = response.ok ? await response.json() : null;
|
||||
|
||||
|
||||
if (!response.ok) {
|
||||
return {
|
||||
success: false,
|
||||
errors: [
|
||||
{
|
||||
type: "network_error",
|
||||
message: `Network request failed: ${response.statusText}`,
|
||||
shortMessage: "Network error",
|
||||
vars: { statusCode: response.status, statusText: response.statusText },
|
||||
fields: [],
|
||||
path: [],
|
||||
details: { statusCode: response.status }
|
||||
}
|
||||
],
|
||||
} as T;
|
||||
}
|
||||
|
||||
return result as T;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user