Skip to content

Commit 6a77206

Browse files
committed
clean: Use node-fetch instead of deprecated request
1 parent 7725898 commit 6a77206

File tree

4 files changed

+830
-624
lines changed

4 files changed

+830
-624
lines changed

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@
8181
]
8282
},
8383
"dependencies": {
84-
"clipboardy": "^2.2.0",
85-
"diff2html": "3.1.6",
86-
"open": "^7.0.3",
87-
"request": "^2.88.2",
88-
"yargs": "^15.3.0"
84+
"clipboardy": "^2.1.0",
85+
"diff2html": "3.0.0",
86+
"node-fetch": "^2.6.0",
87+
"open": "^7.0.2",
88+
"request": "^2.88.0",
89+
"yargs": "^15.0.2"
8990
},
9091
"devDependencies": {
9192
"@types/hogan.js": "^3.0.0",
92-
"@types/jest": "25.1.4",
93-
"@types/node": "13.9.1",
93+
"@types/jest": "25.1.1",
94+
"@types/node": "13.7.0",
95+
"@types/node-fetch": "^2.5.6",
9496
"@types/request": "2.48.4",
9597
"@typescript-eslint/eslint-plugin": "2.23.0",
9698
"@typescript-eslint/parser": "2.23.0",

src/cli.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,30 @@ export function preview(content: string, format: string): void {
102102
open(filePath, { wait: false });
103103
}
104104

105+
type CreateDiffResponse = { id: string };
106+
107+
type ApiError = { error: string };
108+
109+
function isCreateDiffResponse(obj: unknown): obj is CreateDiffResponse {
110+
return (obj as CreateDiffResponse).id !== undefined;
111+
}
112+
113+
function isApiError(obj: unknown): obj is ApiError {
114+
return (obj as ApiError).error !== undefined;
115+
}
116+
105117
export async function postToDiffy(diff: string, diffyOutput: DiffyType): Promise<string> {
106-
const response = await http.put<{ id: string }>('https://diffy.org/api/diff/', { diff: diff });
118+
const response = await http.put('https://diffy.org/api/diff/', { diff: diff });
119+
120+
if (!isCreateDiffResponse(response)) {
121+
if (isApiError(response)) {
122+
throw new Error(response.error);
123+
} else {
124+
throw new Error(
125+
`Could not find 'id' of created diff in the response json.\nBody:\n\n${JSON.stringify(response, null, 2)}`,
126+
);
127+
}
128+
}
107129

108130
const url = `https://diffy.org/diff/${response.id}`;
109131

src/http-utils.ts

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1-
import request from 'request';
1+
import fetch from 'node-fetch';
22

3-
export function put<T extends object>(url: string, payload: object): Promise<T> {
4-
return new Promise<T>((resolve, reject): void => {
5-
request({
6-
url: url,
7-
method: 'PUT',
8-
headers: {},
9-
body: payload,
10-
json: true,
11-
})
12-
.on('response', response => {
13-
response.on('data', body => {
14-
try {
15-
const jsonObj = JSON.parse(body.toString('utf8'));
16-
if (jsonObj as T) {
17-
return resolve(jsonObj);
18-
} else if (jsonObj.error !== undefined && typeof jsonObj.error === 'string') {
19-
return reject(new Error(jsonObj.error));
20-
} else {
21-
return reject(
22-
new Error(
23-
`Failed to read response.
24-
Body:
25-
${body.toString('utf8')}`,
26-
),
27-
);
28-
}
29-
} catch (err) {
30-
return reject(err);
31-
}
32-
});
33-
})
34-
.on('error', err => reject(err));
3+
export function put(url: string, payload: object): Promise<unknown> {
4+
return fetch(url, {
5+
method: 'PUT',
6+
headers: {
7+
Accept: 'application/json',
8+
'Content-Type': 'application/json',
9+
},
10+
body: JSON.stringify(payload),
11+
}).then(async response => {
12+
const bodyText = await response.text();
13+
try {
14+
return JSON.parse(bodyText);
15+
} catch (error) {
16+
if (error instanceof SyntaxError) {
17+
throw new Error(`Failed to parse response json.\nBody:\n\n${bodyText}`);
18+
} else {
19+
throw error;
20+
}
21+
}
3522
});
3623
}

0 commit comments

Comments
 (0)