Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/models/requestParserFactory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { CurlRequestParser } from '../utils/curlRequestParser';
import { FetchRequestParser } from '../utils/fetchRequestParser';
import { HttpRequestParser } from '../utils/httpRequestParser';
import { IRestClientSettings, SystemSettings } from './configurationSettings';
import { RequestParser } from './requestParser';

export class RequestParserFactory {

private static readonly curlRegex: RegExp = /^\s*curl/i;
private static readonly fetchRegex: RegExp = /^\s*fetch\s*\(/i;

public static createRequestParser(rawRequest: string): RequestParser;
public static createRequestParser(rawRequest: string, settings: IRestClientSettings): RequestParser;
public static createRequestParser(rawHttpRequest: string, settings?: IRestClientSettings): RequestParser {
settings = settings || SystemSettings.Instance;
if (RequestParserFactory.curlRegex.test(rawHttpRequest)) {
return new CurlRequestParser(rawHttpRequest, settings);
} else if (RequestParserFactory.fetchRegex.test(rawHttpRequest)) {
return new FetchRequestParser(rawHttpRequest, settings);
} else {
return new HttpRequestParser(rawHttpRequest, settings);
}
Expand Down
56 changes: 56 additions & 0 deletions src/utils/fetchRequestParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { RequestHeaders } from '../models/base';
import { IRestClientSettings } from '../models/configurationSettings';
import { HttpRequest } from '../models/httpRequest';
import { RequestParser } from '../models/requestParser';
import { hasHeader } from './misc';

const DefaultContentType: string = 'application/json';

export class FetchRequestParser implements RequestParser {

private readonly fetchRegex: RegExp = /^\s*fetch\s*\(\s*(?:'([^']+)'|"([^"]+)"|`([^`]+)`)\s*(?:,\s*(\{[\s\S]*\}))?\s*\)\s*;?\s*$/i;

public constructor(private readonly requestRawText: string, private readonly settings: IRestClientSettings) {
}

public async parseHttpRequest(name?: string): Promise<HttpRequest> {
const match = this.fetchRegex.exec(this.requestRawText);
if (!match) {
throw new Error('Invalid fetch request format.');
}

const url = match[1] || match[2] || match[3];
const optionsStr = match[4];

let options: any = {};
if (optionsStr) {
try {
options = new Function(`return ${optionsStr}`)();
} catch (error) {
// Ignore parse error and keep options as empty
}
}

const method = (options.method || 'GET').toUpperCase();

const headers: RequestHeaders = { ...this.settings.defaultHeaders };
if (options.headers) {
for (const [key, value] of Object.entries(options.headers)) {
headers[key] = value as string;
}
}

let body = options.body;
if (body === null || body === undefined) {
body = undefined;
} else if (typeof body !== 'string') {
body = JSON.stringify(body);
}

if (body && !hasHeader(headers, 'content-type')) {
headers['Content-Type'] = DefaultContentType;
}

return new HttpRequest(method, url, headers, body, body, name);
}
}