UNPKG

@wirequery/wirequery-js-core

Version:
77 lines (76 loc) 2.46 kB
"use strict"; // Copyright 2023 Wouter Nederhof // // Use of this source code is governed by the MIT // license that can be found in the `licenses` folder. // // SPDX-License-Identifier: MIT Object.defineProperty(exports, "__esModule", { value: true }); exports.Recorder = void 0; const rrweb_1 = require("rrweb"); const recordNetworkPlugin_1 = require("./recordNetworkPlugin"); class Recorder { wirequeryUrl; templateId; apiKey; recording; events = []; constructor(wirequeryUrl, templateId, apiKey) { this.wirequeryUrl = wirequeryUrl; this.templateId = templateId; this.apiKey = apiKey; } get correlationId() { return this.recording?.correlationId; } startRecording(args) { if (this.recording) { return Promise.reject('Recording already started.'); } return fetch(`${this.wirequeryUrl}/api/v1/recordings`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ templateId: this.templateId, apiKey: this.apiKey, args, }), }).then(data => { data.json().then(json => { this.recording = json; window.recordingCorrelationId = json.correlationId; }); }).then(() => { return (0, rrweb_1.record)({ emit: event => { this.events.push(event); }, plugins: [(0, rrweb_1.getRecordConsolePlugin)(), (0, recordNetworkPlugin_1.getRecordNetworkPlugin)({ recordHeaders: false })], }); }); } stopRecording() { if (!this.recording) { return Promise.reject('No recording started.'); } const recording = this.recording; const events = this.events; window.recordingCorrelationId = undefined; this.recording = undefined; this.events = []; return fetch(`${this.wirequeryUrl}/api/v1/recordings/${recording.id}/finish`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ secret: recording.secret, recording: JSON.stringify(events), context: {}, }), }); } } exports.Recorder = Recorder;