feat: add JavaScript scripting foundation

Introduces a JavaScript setup script that defines the `ad` helper object.
This `ad` object exposes APIs for manipulating request data, accessing response details, and managing environment variables within scripts.

Adds an `evaluate` function to execute JS code using the `flutter_js` runtime and log results or errors.
This establishes the core infrastructure for pre-request and post-response scripting.
This commit is contained in:
Udhay-Adithya
2025-04-25 22:34:02 +05:30
parent f97b39a103
commit 839d8b5c00
2 changed files with 519 additions and 0 deletions

View File

@@ -485,3 +485,510 @@ const kMsgClearHistory =
const kMsgClearHistorySuccess = 'History cleared successfully';
const kMsgClearHistoryError = 'Error clearing history';
const kMsgShareError = "Unable to share";
const String setupScript = r"""
// === APIDash Setup Script ===
// --- 1. Parse Injected Data ---
// These variables are expected to be populated by Dart before this script runs.
// Example: const injectedRequestJson = '{"method":"get", "url":"...", ...}';
let request = {}; // Will hold the RequestModel data
let response = {}; // Will hold the ResponseModel data (only for post-request)
let environment = {}; // Will hold the active environment variables
// Note: Using 'let' because environment might be completely cleared/reassigned.
try {
// 'injectedRequestJson' should always be provided (even if empty for some edge case)
if (typeof injectedRequestJson !== 'undefined' && injectedRequestJson) {
request = JSON.parse(injectedRequestJson);
// Ensure essential arrays exist if they are null/undefined after parsing
request.headers = request.headers || [];
request.params = request.params || [];
request.formData = request.formData || [];
} else {
// Should not happen based on the plan, but good to log
sendMessage('consoleError', JSON.stringify(['Setup Error: injectedRequestJson is missing or empty.']));
}
// 'injectedResponseJson' is only for post-response scripts
if (typeof injectedResponseJson !== 'undefined' && injectedResponseJson) {
response = JSON.parse(injectedResponseJson);
// Ensure response headers map exists
response.headers = response.headers || {};
response.requestHeaders = response.requestHeaders || {};
}
// 'injectedEnvironmentJson' should always be provided
if (typeof injectedEnvironmentJson !== 'undefined' && injectedEnvironmentJson) {
environment = JSON.parse(injectedEnvironmentJson);
} else {
// Should not happen based on the plan, but good to log
sendMessage('consoleError', JSON.stringify(['Setup Error: injectedEnvironmentJson is missing or empty.']));
environment = {}; // Initialize to empty object to avoid errors later
}
} catch (e) {
// Send error back to Dart if parsing fails catastrophically
sendMessage('fatalError', JSON.stringify({
message: 'Failed to parse injected JSON data.',
error: e.toString(),
stack: e.stack
}));
// Optionally, re-throw to halt script execution immediately
// throw new Error('Failed to parse injected JSON data: ' + e.toString());
}
// --- 2. Define APIDash Helper (`ad`) Object ---
// This object provides functions to interact with the request, response,
// environment, and the Dart host application.
const ad = {
/**
* Functions to modify the request object *before* it is sent.
* Only available in pre-request scripts.
* Changes are made directly to the 'request' JS object.
*/
request: {
/**
* Access and modify request headers. Remember header names are case-insensitive in HTTP,
* but comparisons here might be case-sensitive unless handled carefully.
* Headers are represented as an array of objects: [{name: string, value: string}, ...]
*/
headers: {
/**
* Adds or updates a header. If a header with the same name (case-sensitive)
* already exists, it updates its value. Otherwise, adds a new header.
* @param {string} key The header name.
* @param {string} value The header value.
*/
set: (key, value) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.headers)) return; // Safety check
if (typeof key !== 'string') return; // Safety check
const stringValue = String(value); // Ensure value is a string
const existingHeaderIndex = request.headers.findIndex(h => typeof h === 'object' && h.name === key);
if (existingHeaderIndex > -1) {
request.headers[existingHeaderIndex].value = stringValue;
} else {
request.headers.push({ name: key, value: stringValue });
}
},
/**
* Gets the value of the first header matching the key (case-sensitive).
* @param {string} key The header name.
* @returns {string|undefined} The header value or undefined if not found.
*/
get: (key) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.headers)) return undefined; // Safety check
const header = request.headers.find(h => typeof h === 'object' && h.name === key);
return header ? header.value : undefined;
},
/**
* Removes all headers with the given name (case-sensitive).
* @param {string} key The header name to remove.
*/
remove: (key) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.headers)) return; // Safety check
request.headers = request.headers.filter(h => !(typeof h === 'object' && h.name === key));
},
/**
* Checks if a header with the given name exists (case-sensitive).
* @param {string} key The header name.
* @returns {boolean} True if the header exists, false otherwise.
*/
has: (key) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.headers)) return false; // Safety check
return request.headers.some(h => typeof h === 'object' && h.name === key);
},
/**
* Clears all request headers.
*/
clear: () => {
if (!request || typeof request !== 'object') return; // Safety check
request.headers = [];
}
},
/**
* Access and modify URL query parameters.
* Params are represented as an array of objects: [{name: string, value: string}, ...]
*/
params: {
/**
* Adds or updates a query parameter. If a param with the same name (case-sensitive)
* already exists, it updates its value. Use multiple times for duplicate keys if needed by server.
* Consider URL encoding implications - values should likely be pre-encoded if necessary.
* @param {string} key The parameter name.
* @param {string} value The parameter value.
*/
set: (key, value) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.params)) return; // Safety check
if (typeof key !== 'string') return; // Safety check
const stringValue = String(value); // Ensure value is a string
// Note: Unlike headers, duplicate param keys are sometimes meaningful.
// This simple 'set' replaces the *first* occurrence or adds if not found.
// A different method like 'add' could be used to allow duplicates.
const existingParamIndex = request.params.findIndex(p => typeof p === 'object' && p.name === key);
if (existingParamIndex > -1) {
request.params[existingParamIndex].value = stringValue;
} else {
request.params.push({ name: key, value: stringValue });
}
},
/**
* Gets the value of the first query parameter matching the key (case-sensitive).
* @param {string} key The parameter name.
* @returns {string|undefined} The parameter value or undefined if not found.
*/
get: (key) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.params)) return undefined; // Safety check
const param = request.params.find(p => typeof p === 'object' && p.name === key);
return param ? param.value : undefined;
},
/**
* Removes all query parameters with the given name (case-sensitive).
* @param {string} key The parameter name to remove.
*/
remove: (key) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.params)) return; // Safety check
request.params = request.params.filter(p => !(typeof p === 'object' && p.name === key));
},
/**
* Checks if a query parameter with the given name exists (case-sensitive).
* @param {string} key The parameter name.
* @returns {boolean} True if the parameter exists, false otherwise.
*/
has: (key) => {
if (!request || typeof request !== 'object' || !Array.isArray(request.params)) return false; // Safety check
return request.params.some(p => typeof p === 'object' && p.name === key);
},
/**
* Clears all query parameters.
*/
clear: () => {
if (!request || typeof request !== 'object') return; // Safety check
request.params = [];
}
},
/**
* Access or modify the request URL.
*/
url: {
/**
* Gets the current request URL string.
* @returns {string} The URL.
*/
get: () => {
return (request && typeof request === 'object') ? request.url : '';
},
/**
* Sets the request URL string.
* @param {string} newUrl The new URL.
*/
set: (newUrl) => {
if (request && typeof request === 'object' && typeof newUrl === 'string') {
request.url = newUrl;
}
}
// Future: Could add methods to manipulate parts (host, path, query) if needed
},
/**
* Access or modify the request body.
*/
body: {
/**
* Gets the current request body content (string).
* Note: For form-data, this returns the raw string body (if any), not the structured data. Use `ad.request.formData` for that.
* @returns {string|null|undefined} The request body string.
*/
get: () => {
return (request && typeof request === 'object') ? request.body : undefined;
},
/**
* Sets the request body content (string).
* Important: Also updates the Content-Type if setting JSON/Text, unless a Content-Type header is already explicitly set.
* Setting the body will clear form-data if the content type changes away from form-data.
* @param {string|object} newBody The new body content. If an object is provided, it's stringified as JSON.
* @param {string} [contentType] Optionally specify the Content-Type (e.g., 'application/json', 'text/plain'). If not set, defaults to 'text/plain' or 'application/json' if newBody is an object.
*/
set: (newBody, contentType) => {
if (!request || typeof request === 'object') return; // Safety check
let finalBody = '';
let finalContentType = contentType;
if (typeof newBody === 'object' && newBody !== null) {
try {
finalBody = JSON.stringify(newBody);
finalContentType = contentType || 'application/json'; // Default to JSON if object
request.bodyContentType = 'json'; // Update internal model type
} catch (e) {
sendMessage('consoleError', JSON.stringify(['Error stringifying object for request body:', e.toString()]));
return; // Don't proceed if stringify fails
}
} else {
finalBody = String(newBody); // Ensure it's a string
finalContentType = contentType || 'text/plain'; // Default to text
request.bodyContentType = 'text'; // Update internal model type
}
request.body = finalBody;
// Clear form data if we are setting a string/json body
request.formData = [];
// Set Content-Type header if not already set by user explicitly in headers
// Use case-insensitive check for existing Content-Type
const hasContentTypeHeader = request.headers.some(h => typeof h === 'object' && h.name.toLowerCase() === 'content-type');
if (!hasContentTypeHeader && finalContentType) {
ad.request.headers.set('Content-Type', finalContentType);
}
}
// TODO: Add helpers for request.formData if needed (similar to headers/params)
},
/**
* Access or modify the request method (e.g., 'GET', 'POST').
*/
method: {
/**
* Gets the current request method.
* @returns {string} The HTTP method (e.g., "get", "post").
*/
get: () => {
return (request && typeof request === 'object') ? request.method : '';
},
/**
* Sets the request method.
* @param {string} newMethod The new HTTP method (e.g., "POST", "put"). Case might matter for the Dart model enum.
*/
set: (newMethod) => {
if (request && typeof request === 'object' && typeof newMethod === 'string') {
// Consider converting to lowercase to match HTTPVerb enum likely usage
request.method = newMethod.toLowerCase();
}
}
}
},
/**
* Read-only access to the response data.
* Only available in post-response scripts.
*/
response: {
/**
* The HTTP status code of the response.
* @type {number|undefined}
*/
get status() { return (response && typeof response === 'object') ? response.statusCode : undefined; },
/**
* The response body as a string. If the response was binary, this might be decoded text
* based on Content-Type or potentially garbled. Use `bodyBytes` for raw binary data access (if provided).
* @type {string|undefined}
*/
get body() { return (response && typeof response === 'object') ? response.body : undefined; },
/**
* The response body automatically formatted (e.g., pretty-printed JSON). Provided by Dart.
* @type {string|undefined}
*/
get formattedBody() { return (response && typeof response === 'object') ? response.formattedBody : undefined; },
/**
* The raw response body as an array of bytes (numbers).
* Note: This relies on the Dart side serializing Uint8List correctly (e.g., as List<int>).
* Accessing large byte arrays in JS might be memory-intensive.
* @type {number[]|undefined}
*/
get bodyBytes() { return (response && typeof response === 'object') ? response.bodyBytes : undefined; },
/**
* The approximate time taken for the request-response cycle. Provided by Dart.
* Assumes Dart sends it as microseconds and converts it to milliseconds here.
* @type {number|undefined} Time in milliseconds.
*/
get time() {
// Assuming response.time is in microseconds from Dart's DurationConverter
return (response && typeof response === 'object' && typeof response.time === 'number') ? response.time / 1000 : undefined;
},
/**
* An object containing the response headers (keys are header names, values are header values).
* Header names are likely lowercase from the http package.
* @type {object|undefined} e.g., {'content-type': 'application/json', ...}
*/
get headers() { return (response && typeof response === 'object') ? response.headers : undefined; },
/**
* An object containing the request headers that were actually sent (useful for verification).
* Header names are likely lowercase.
* @type {object|undefined} e.g., {'user-agent': '...', ...}
*/
get requestHeaders() { return (response && typeof response === 'object') ? response.requestHeaders : undefined; },
/**
* Attempts to parse the response body as JSON.
* @returns {object|undefined} The parsed JSON object, or undefined if parsing fails or body is empty.
*/
json: () => {
if (!ad.response.body) {
return undefined;
}
try {
return JSON.parse(ad.response.body);
} catch (e) {
ad.console.error("Failed to parse response body as JSON:", e.toString());
return undefined;
}
},
/**
* Gets a specific response header value (case-insensitive key lookup).
* @param {string} key The header name.
* @returns {string|undefined} The header value or undefined if not found.
*/
getHeader: (key) => {
const headers = ad.response.headers;
if (!headers || typeof key !== 'string') return undefined;
const lowerKey = key.toLowerCase();
const headerKey = Object.keys(headers).find(k => k.toLowerCase() === lowerKey);
return headerKey ? headers[headerKey] : undefined;
}
},
/**
* Access and modify environment variables for the active environment.
* Changes are made to the 'environment' JS object and sent back to Dart.
*/
environment: {
/**
* Gets the value of an environment variable.
* @param {string} key The variable name.
* @returns {any} The variable value or undefined if not found.
*/
get: (key) => {
return (environment && typeof environment === 'object') ? environment[key] : undefined;
},
/**
* Sets the value of an environment variable.
* @param {string} key The variable name.
* @param {any} value The variable value. Should be JSON-serializable (string, number, boolean, object, array).
*/
set: (key, value) => {
if (environment && typeof environment === 'object' && typeof key === 'string') {
environment[key] = value;
}
},
/**
* Removes an environment variable.
* @param {string} key The variable name to remove.
*/
unset: (key) => {
if (environment && typeof environment === 'object') {
delete environment[key];
}
},
/**
* Checks if an environment variable exists.
* @param {string} key The variable name.
* @returns {boolean} True if the variable exists, false otherwise.
*/
has: (key) => {
return (environment && typeof environment === 'object') ? environment.hasOwnProperty(key) : false;
},
/**
* Removes all variables from the current environment scope.
*/
clear: () => {
if (environment && typeof environment === 'object') {
for (const key in environment) {
if (environment.hasOwnProperty(key)) {
delete environment[key];
}
}
}
}
// Note: A separate 'globals' object could be added here if global variables are implemented distinctly.
},
/**
* Provides logging capabilities. Messages are sent back to Dart via the bridge.
*/
console: {
/**
* Logs informational messages.
* @param {...any} args Values to log. They will be JSON-stringified.
*/
log: (...args) => {
try {
sendMessage('consoleLog', JSON.stringify(args));
} catch(e) { /* Ignore stringify errors for console? Or maybe log the error itself? */ }
},
/**
* Logs warning messages.
* @param {...any} args Values to log.
*/
warn: (...args) => {
try {
sendMessage('consoleWarn', JSON.stringify(args));
} catch(e) { /* Ignore */ }
},
/**
* Logs error messages.
* @param {...any} args Values to log.
*/
error: (...args) => {
try {
sendMessage('consoleError', JSON.stringify(args));
} catch(e) { /* Ignore */ }
}
},
/**
* Basic testing functions (example structure).
* Results might need to be collected and sent back via the bridge or at the end.
*/
// test: (testName, callback) => {
// try {
// callback();
// ad.console.log(`Test Passed: ${testName}`);
// // TODO: Potentially collect results: sendMessage('testResult', JSON.stringify({ name: testName, status: 'passed' }));
// } catch (e) {
// ad.console.error(`Test Failed: ${testName}`, e.toString(), e.stack);
// // TODO: Potentially collect results: sendMessage('testResult', JSON.stringify({ name: testName, status: 'failed', error: e.toString() }));
// }
// },
// expect: (value) => {
// // Very basic assertion example (can be expanded or use a tiny library)
// return {
// toBe: (expected) => {
// if (value !== expected) {
// throw new Error(`Assertion Failed: Expected ${JSON.stringify(value)} to be ${JSON.stringify(expected)}`);
// }
// },
// Add more assertions: toBeTruthy, toEqual (deep compare), etc.
// };
// }
// TODO: Add other utilities if needed: crypto, base64 (atob/btoa - might need polyfills)
// E.g.,
// crypto: { /* methods */ },
// btoa: (str) => btoa(str), // Needs btoa to be available in the JS context
// atob: (encodedStr) => atob(encodedStr) // Needs atob
};
// --- End of APIDash Setup Script ---
// User's script will be appended below this line by Dart.
// Dart will also append the final JSON.stringify() call to return results.
""";

View File

@@ -1,3 +1,6 @@
import 'dart:developer';
import 'package:flutter/services.dart';
import 'package:flutter_js/flutter_js.dart';
late JavascriptRuntime jsRuntime;
@@ -9,3 +12,12 @@ void initializeJsRuntime() {
void disposeJsRuntime() {
jsRuntime.dispose();
}
void evaluate(String code) {
try {
JsEvalResult jsResult = jsRuntime.evaluate(code);
log(jsResult.stringResult);
} on PlatformException catch (e) {
log('ERROR: ${e.details}');
}
}