Skip to content

Commit 7c59a1b

Browse files
committed
support plain text on enum valdiated params
1 parent b5dd654 commit 7c59a1b

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

__tests__/result.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Deno.test('cheerio selector lazy loads and caches itself', async () => {
1414

1515

1616
Deno.test('cheerio selector loads with case sensitive headers', async () => {
17-
const response = JSON.parse(await Deno.readTextFile('__tests__/data/response_html_case_sensitive_headers.json'));
17+
const response = JSON.parse(await Deno.readTextFile('__tests__/data/response_html_success.json'));
1818
const result = new ScrapeResult(response);
1919
assertEquals(result.selector('h1').text(), 'Herman Melville - Moby-Dick');
2020
});

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
},
55
"name": "@scrapfly/scrapfly-sdk",
66
"exports": "./src/main.ts",
7-
"version": "0.6.6",
7+
"version": "0.6.7",
88
"description": "SDK for Scrapfly.io API for web scraping, screenshotting and data extraction",
99
"tasks": {
1010
"start": "deno run --allow-net --allow-read src/main.ts",

src/extractionconfig.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ExtractionConfigOptions = {
2626
extraction_prompt?: string;
2727
extraction_model?: string;
2828
is_document_compressed?: boolean;
29-
document_compression_format?: CompressionFormat;
29+
document_compression_format?: string | CompressionFormat;
3030
webhook?: string;
3131
};
3232

@@ -40,11 +40,14 @@ export class ExtractionConfig {
4040
extraction_prompt?: string;
4141
extraction_model?: string;
4242
is_document_compressed?: boolean;
43-
document_compression_format?: CompressionFormat;
43+
document_compression_format?: string | CompressionFormat;
4444
webhook?: string;
4545

4646
constructor(options: ExtractionConfigOptions) {
4747
this.validateOptions(options);
48+
if (options.document_compression_format && !Object.values(CompressionFormat).includes(options.document_compression_format as CompressionFormat)) {
49+
throw new errors.ExtractionConfigError(`Invalid CompressionFormat param value: ${options.document_compression_format}`);
50+
}
4851
this.body = options.body;
4952
this.content_type = options.content_type;
5053
this.url = options.url ?? this.url;

src/scrapeconfig.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export enum Format {
3838
export enum FormatOption {
3939
NO_LINKS = 'no_links',
4040
NO_IMAGES = 'no_images',
41+
NO_CONTENT = 'only_content'
4142
}
4243

4344
type ScrapeConfigOptions = {
@@ -58,8 +59,8 @@ type ScrapeConfigOptions = {
5859
proxy_pool?: string;
5960
session?: string;
6061
tags?: string[];
61-
format?: Format;
62-
format_options?: FormatOption[];
62+
format?: string | Format;
63+
format_options?: string[] | FormatOption[];
6364
correlation_id?: string;
6465
cookies?: Rec<string>;
6566
body?: string;
@@ -69,7 +70,7 @@ type ScrapeConfigOptions = {
6970
rendering_wait?: number;
7071
wait_for_selector?: string;
7172
screenshots?: Rec<any>;
72-
screenshot_flags?: ScreenshotFlags[];
73+
screenshot_flags?: string[] | ScreenshotFlags[] ;
7374
session_sticky_proxy?: boolean;
7475
webhook?: string;
7576
timeout?: number;
@@ -100,8 +101,8 @@ export class ScrapeConfig {
100101
proxy_pool?: string;
101102
session?: string;
102103
tags: Set<string> = new Set<string>();
103-
format?: Format; // raw(unchanged)
104-
format_options?: FormatOption[];
104+
format?: Format| string; // raw(unchanged)
105+
format_options?: string[] | FormatOption[];
105106
correlation_id?: string;
106107
cookies?: Rec<string>;
107108
body?: string;
@@ -112,7 +113,7 @@ export class ScrapeConfig {
112113
wait_for_selector?: string;
113114
session_sticky_proxy = false;
114115
screenshots?: Rec<any>;
115-
screenshot_flags?: ScreenshotFlags[];
116+
screenshot_flags?: ScreenshotFlags[] | string[];
116117
webhook?: string;
117118
timeout?: number; // in milliseconds
118119
js_scenario?: Rec<any>;
@@ -122,14 +123,21 @@ export class ScrapeConfig {
122123

123124
constructor(options: ScrapeConfigOptions) {
124125
this.validateOptions(options);
125-
if (options.format && !Object.values(Format).includes(options.format)) {
126-
throw new ScrapeConfigError(`Invalid format param value: ${options.format}`);
126+
if (options.format && !Object.values(Format).includes(options.format as Format)) {
127+
throw new ScrapeConfigError(`Invalid Format param value: ${options.format}`);
127128
}
128129
this.format = options.format ?? this.format;
130+
if (options.format_options) {
131+
options.format_options.forEach((flag) => {
132+
if (!Object.values(FormatOption).includes(flag as FormatOption)) {
133+
throw new ScrapeConfigError(`Invalid FormatOption param value: ${flag}`);
134+
}
135+
});
136+
}
129137
if (options.screenshot_flags) {
130138
options.screenshot_flags.forEach((flag) => {
131-
if (!Object.values(ScreenshotFlags).includes(flag)) {
132-
throw new ScrapeConfigError(`Invalid screenshot_flags param value: ${flag}`);
139+
if (!Object.values(ScreenshotFlags).includes(flag as ScreenshotFlags)) {
140+
throw new ScrapeConfigError(`Invalid ScreenshotFlags param value: ${flag}`);
133141
}
134142
});
135143
}

src/screenshotconfig.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ export enum Format {
3434

3535
type ScreenshotConfigOptions = {
3636
url: string;
37-
format?: Format;
37+
format?: string | Format;
3838
capture?: string;
3939
resolution?: string;
4040
country?: string;
4141
timeout?: number;
4242
rendering_wait?: number;
4343
wait_for_selector?: string;
44-
options?: Options[];
44+
options?: string[] | Options[];
4545
auto_scroll?: boolean;
4646
js?: string;
4747
cache?: boolean;
@@ -52,14 +52,14 @@ type ScreenshotConfigOptions = {
5252

5353
export class ScreenshotConfig {
5454
url: string;
55-
format?: Format;
55+
format?: string | Format;
5656
capture?: string;
5757
resolution?: string;
5858
country?: string = undefined;
5959
timeout?: number;
6060
rendering_wait?: number;
6161
wait_for_selector?: string;
62-
options?: Options[];
62+
options?: string[] | Options[];
6363
auto_scroll?: boolean;
6464
js?: string;
6565
cache?: boolean;
@@ -69,18 +69,17 @@ export class ScreenshotConfig {
6969

7070
constructor(options: ScreenshotConfigOptions) {
7171
this.validateOptions(options);
72-
if (options.format && !Object.values(Format).includes(options.format)) {
73-
throw new ScreenshotConfigError(`Invalid format param value: ${options.format}`);
72+
if (options.format && !Object.values(Format).includes(options.format as Format)) {
73+
throw new ScreenshotConfigError(`Invalid Format param value: ${options.format}`);
7474
}
7575
this.format = options.format ?? this.format;
76-
// Validate options against the enum
7776
if (options.options) {
7877
options.options.forEach((opt) => {
79-
if (!Object.values(Options).includes(opt)) {
80-
throw new ScreenshotConfigError(`Invalid options param value: ${opt}`);
78+
if (!Object.values(Options).includes(opt as Options)) {
79+
throw new ScreenshotConfigError(`Invalid Options param value: ${opt}`);
8180
}
8281
});
83-
}
82+
}
8483
this.url = options.url;
8584
this.format = options.format ?? this.format;
8685
this.capture = options.capture ?? this.capture;

0 commit comments

Comments
 (0)