Repository navigation
Expand file tree
/
Copy pathhistoryController.ts
More file actions
88 lines (78 loc) · 2.97 KB
/
Copy pathhistoryController.ts
File metadata and controls
88 lines (78 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import * as fs from 'fs-extra';
import { EOL, tmpdir } from 'os';
import * as path from 'path';
import { QuickPickItem, window, workspace } from 'vscode';
import { HistoricalHttpRequest } from '../models/httpRequest';
import { trace } from "../utils/decorator";
import { UserDataManager } from '../utils/userDataManager';
dayjs.extend(relativeTime);
const uuidv4 = require('uuid/v4');
export class HistoryController {
public constructor() {
}
@trace('History')
public async save() {
const requests = await UserDataManager.getRequestHistory();
if (requests.length === 0) {
window.showInformationMessage("No request history items are found!");
return;
}
const itemPickList = requests.map(request => {
const item: QuickPickItem & { rawRequest: HistoricalHttpRequest } = {
label: `${request.method.toUpperCase()} ${request.url}`,
rawRequest: request
};
if (typeof request.body === 'string' && request.body.length > 0) {
item.description = `${request.body.length} body bytes`;
}
if (request.startTime) {
item.detail = `${dayjs().to(request.startTime)}`;
}
return item;
});
const item = await window.showQuickPick(itemPickList, { placeHolder: "" });
if (!item) {
return;
}
const path = await this.createRequestInTempFile(item.rawRequest);
const document = await workspace.openTextDocument(path);
await window.showTextDocument(document);
}
@trace('Clear History')
public async clear() {
const btn = await window.showInformationMessage(
'Do you really want to clear request history?',
{ title: 'Yes' },
{ title: 'No' });
if (btn?.title === 'Yes') {
await UserDataManager.clearRequestHistory();
window.showInformationMessage('Request history has been cleared');
}
}
private async createRequestInTempFile(request: HistoricalHttpRequest): Promise<string> {
const file = await this.createTempFile();
let output = `${request.method.toUpperCase()} ${request.url}${EOL}`;
if (request.headers) {
for (const header in request.headers) {
if (request.headers.hasOwnProperty(header)) {
const value = request.headers[header];
output += `${header}: ${value}${EOL}`;
}
}
}
if (request.body) {
output += `${EOL}${request.body}`;
}
await fs.writeFile(file, output);
return file;
}
private async createTempFile(): Promise<string> {
const file = path.join(tmpdir(), `vscode-restclient-${uuidv4()}.http`);
await fs.ensureFile(file);
return file;
}
public dispose() {
}
}