Scripts
Programmatic request-script execution from the main entry via hc.scripts.createContext(). Requires appropriate script permissions.
PluginScriptContext
| Method | Description |
|---|---|
setVariable(name, value) | Injects a global variable visible to subsequent run() calls. |
setFunction(name, fn) | Injects a global function; overrides built-in globals such as console when names collide. |
run(script) | Evaluates script synchronously; returns {@link PluginScriptRunResult} with hc mutations and the last-expression value. |
run() returns the full structured result: mutated request, variableSets, collectionVariableSets, environmentVariableSets, globalVariableSets, collectionHeaders, tests, logs, data (the final hc.data bag), optional error, and value (the script's last expression).
The hc surface matches pre/post request scripts: hc.request, hc.collection, hc.environment, hc.globals, hc.data, hc.test, hc.expect, and hc.response (when init.response is provided). Pass optional init.data to seed hc.data when creating a context. See Request scripts for the full hc reference, including hc.data.
Tests, logs, and hc.data accumulate across multiple run() calls on the same context. Request and variable mutations persist until you create a new context.
hc.scripts
Available since v2.7.0
Signature:(init?: PluginScriptContextInit) => PluginScriptContext
Creates a script sandbox that exposes the same hc object as collection and request pre/post scripts. Use it to run tests, mutate a request snapshot, or read a response with hc.response.json() inside your main entry.
No extra permission is required. Contexts start with only the hc API plus globals you inject with setVariable and setFunction.
PluginScriptContext
| Method | Description |
|---|---|
setVariable(name, value) |
Injects a global variable visible to subsequent run() calls. |
setFunction(name, fn) |
Injects a global function; overrides built-in globals such as console when names collide. |
run(script) |
Evaluates script synchronously; returns {@link PluginScriptRunResult} with hc mutations and the last-expression value. |
run() returns the full structured result: mutated request, variableSets, collectionVariableSets, environmentVariableSets, globalVariableSets, collectionHeaders, tests, logs, data (the final hc.data bag), optional error, and value (the script's last expression).
The hc surface matches pre/post request scripts: hc.request, hc.collection, hc.environment, hc.globals, hc.data, hc.test, hc.expect, and hc.response (when init.response is provided). Pass optional init.data to seed hc.data when creating a context. See Request scripts for the full hc reference, including hc.data.
Tests, logs, and hc.data accumulate across multiple run() calls on the same context. Request and variable mutations persist until you create a new context.
import type { MainPluginContext } from '@harborclient/sdk';
export function activate(hc: MainPluginContext): void {
const context = hc.scripts.createContext({
phase: 'post',
request: {
method: 'GET',
url: 'https://api.example.com/users',
headers: [],
params: [],
body: '',
bodyType: 'none'
},
response: {
status: 200,
statusText: 'OK',
headers: { 'content-type': 'application/json' },
body: '{"ok":true}',
timeMs: 12,
sizeBytes: 11
},
variables: { token: 'abc' }
});
context.setFunction('console', console);
const result = context.run(`
const data = hc.response.json();
hc.test('is ok', () => hc.expect(data.ok).to.equal(true));
hc.request.variables.set('lastStatus', String(hc.response.code));
data.ok;
`);
// result.value === true
// result.tests, result.variableSets, result.logs, result.request, ...
}