Files
bruno/packages/bruno-js/src/bruno-response.js
sanish-bruno 592679538b Fix: res.setBody fails for Object in Developer-mode
vm2 returns a recursive Proxy for accessing the return value which
cannot be serialized for IPC using `structuredClone`.

Co-authored-by: ramki-bruno <ramki@usebruno.com>
2025-04-02 13:18:58 +05:30

57 lines
1.2 KiB
JavaScript

const { get } = require('@usebruno/query');
const _ = require('lodash');
class BrunoResponse {
constructor(res) {
this.res = res;
this.status = res ? res.status : null;
this.statusText = res ? res.statusText : null;
this.headers = res ? res.headers : null;
this.body = res ? res.data : null;
this.responseTime = res ? res.responseTime : null;
// Make the instance callable
const callable = (...args) => get(this.body, ...args);
Object.setPrototypeOf(callable, this.constructor.prototype);
Object.assign(callable, this);
return callable;
}
getStatus() {
return this.res ? this.res.status : null;
}
getStatusText() {
return this.res ? this.res.statusText : null;
}
getHeader(name) {
return this.res && this.res.headers ? this.res.headers[name] : null;
}
getHeaders() {
return this.res ? this.res.headers : null;
}
getBody() {
return this.res ? this.res.data : null;
}
getResponseTime() {
return this.res ? this.res.responseTime : null;
}
setBody(data) {
if (!this.res) {
return;
}
const clonedData = _.cloneDeep(data);
this.res.data = clonedData;
this.body = clonedData;
}
}
module.exports = BrunoResponse;