Skip to content

Commit 4ac5adb

Browse files
avoid use of node modules
1 parent f394c02 commit 4ac5adb

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/convert.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { CurlExporter } from "./exporters/curl";
66
import { JavaScriptExporter } from "./exporters/javascript";
77
import util from "util";
88

9-
const execAsync = util.promisify(childProcess.exec);
9+
const execAsync =
10+
typeof window === "undefined" ? util.promisify(childProcess.exec) : undefined;
1011

1112
export type ConvertOptions = {
1213
/** When `true`, the converter will only check if the conversion can be carried
@@ -188,6 +189,9 @@ export class SubprocessExporter implements FormatExporter {
188189
const input = base64url.encode(
189190
JSON.stringify({ requests: getSlimRequests(requests) }),
190191
);
192+
if (execAsync === undefined) {
193+
throw new Error("Cannot use exec()");
194+
}
191195
const { stdout, stderr } = await execAsync(
192196
`${this.baseCmd} check ${input}`,
193197
);
@@ -208,6 +212,9 @@ export class SubprocessExporter implements FormatExporter {
208212
const input = base64url.encode(
209213
JSON.stringify({ requests: getSlimRequests(requests), options }),
210214
);
215+
if (execAsync === undefined) {
216+
throw new Error("Cannot use exec()");
217+
}
211218
const { stdout } = await execAsync(`${this.baseCmd} convert ${input}`);
212219
if (stdout) {
213220
const json = JSON.parse(base64url.decode(stdout));

src/parse.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { METHODS } from "http";
21
import { URL } from "url";
32
import { readFile } from "fs/promises";
43
import path from "path";
54
import * as Router from "find-my-way-ts";
65
import { Model, Request } from "./metamodel";
76

7+
const httpMethods = ["HEAD", "GET", "POST", "PUT", "PATCH", "DELETE"];
88
export type JSONValue = string | number | boolean | JSONArray | JSONObject;
99
interface JSONArray extends Array<JSONValue> {}
1010
interface JSONObject {
@@ -62,7 +62,7 @@ export function splitSource(source: string): string[] {
6262
let prev = 0;
6363
while (index < len) {
6464
// Beginning of a new command, we should find the method and proceede to the url.
65-
for (const method of METHODS) {
65+
for (const method of httpMethods) {
6666
if (source.slice(index, len).startsWith(method)) {
6767
index += method.length;
6868
break;
@@ -80,7 +80,7 @@ export function splitSource(source: string): string[] {
8080
if (index == len) return;
8181
let brackets = 0;
8282
// If we found an http method, then we have found a new command.
83-
for (const method of METHODS) {
83+
for (const method of httpMethods) {
8484
if (source.slice(index, len).startsWith(method)) {
8585
return;
8686
}
@@ -131,7 +131,7 @@ function parseCommand(source: string, options: ParseOptions) {
131131
const len = source.length;
132132
let index = 0;
133133
// identify the method
134-
for (const method of METHODS) {
134+
for (const method of httpMethods) {
135135
if (source.slice(index, len).startsWith(method)) {
136136
data.method = method;
137137
index += method.length;

0 commit comments

Comments
 (0)