Skip to content

Commit 041b177

Browse files
committed
move format checking to outer statement
1 parent 7114606 commit 041b177

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/client.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,34 @@ export class ScrapflyClient {
9595
/**
9696
* Handle clob and blob large objects
9797
*/
98-
async handleLargeObjects(result: any): Promise<ScrapeResult> {
99-
const format = result.format
100-
if (format === 'clob' || format === 'blob') {
101-
let response: Response;
102-
try {
103-
const url = new URL(result.content);
104-
const params = { key: this.key };
105-
url.search = new URLSearchParams(params).toString();
106-
response = await this.fetch({
107-
url: url.toString(),
108-
method: 'GET',
109-
headers: {
110-
'user-agent': this.ua,
111-
'accept-encoding': 'gzip, deflate, br',
112-
accept: 'application/json',
113-
},
114-
});
115-
} catch (e) {
116-
log.error('error', e);
117-
throw e;
118-
}
119-
const content: string = await response.text();
120-
result.content = content
121-
if (format === 'clob') {
122-
result.format = 'text'
123-
}
124-
if (format === 'blob') {
125-
result.format = 'binary'
126-
}
98+
async handleLargeObjects(result: any, format: "clob" | "blob"): Promise<ScrapeResult> {
99+
let response: Response;
100+
101+
try {
102+
const url = new URL(result.content);
103+
const params = { key: this.key };
104+
url.search = new URLSearchParams(params).toString();
105+
response = await this.fetch({
106+
url: url.toString(),
107+
method: 'GET',
108+
headers: {
109+
'user-agent': this.ua,
110+
'accept-encoding': 'gzip, deflate, br',
111+
accept: 'application/json',
112+
},
113+
});
114+
} catch (e) {
115+
log.error('error', e);
116+
throw e;
117+
}
118+
119+
const content: string = await response.text();
120+
result.content = content
121+
if (format === 'clob') {
122+
result.format = 'text'
123+
}
124+
if (format === 'blob') {
125+
result.format = 'binary'
127126
}
128127
return result
129128
}
@@ -210,8 +209,11 @@ export class ScrapflyClient {
210209
throw new errors.ApiHttpClientError(JSON.stringify(data));
211210
}
212211

213-
const content = await this.handleLargeObjects(data.result)
214-
data.result = content
212+
const content_format = data.result.format
213+
if (content_format === 'clob' || content_format === 'blob') {
214+
const content = await this.handleLargeObjects(data.result, content_format)
215+
data.result = content
216+
}
215217

216218
const result = this.handleResponse(
217219
response,

0 commit comments

Comments
 (0)