screenshotone-api-sdk 1.0.25 → 1.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/main.ts CHANGED
@@ -4,6 +4,7 @@ import fetch from 'cross-fetch';
4
4
 
5
5
  const API_BASE_URL = "https://api.screenshotone.com";
6
6
  const API_TAKE_PATH = "/take";
7
+ const API_ANIMATE_PATH = "/animate";
7
8
 
8
9
  /**
9
10
  * Represents an API client for the ScreenshotOne.com API.
@@ -17,6 +18,14 @@ export class Client {
17
18
  this.secretKey = secretKey;
18
19
  }
19
20
 
21
+ /**
22
+ * Generates an URL that can be used to send GET requests for taking screenshots.
23
+ *
24
+ * Warning! Don't share the link publicly generated by this method publicly.
25
+ *
26
+ * @param options Options for taking the screenshot.
27
+ * @returns An URL that will generate the screenshot if requested.
28
+ */
20
29
  generateTakeURL(options: TakeOptions): string {
21
30
  const query = options.toQuery();
22
31
  query.append("access_key", this.accessKey);
@@ -30,8 +39,72 @@ export class Client {
30
39
  return `${API_BASE_URL}${API_TAKE_PATH}?${queryString}`;
31
40
  }
32
41
 
42
+ /**
43
+ * Generates the link that can be publicly available, but make sure
44
+ * you read the documentation before at https://screenshotone.com/docs/signed-requests/.
45
+ *
46
+ * @param options
47
+ * @returns A signed URL that will generate the screenshot if requested.
48
+ */
49
+ generateSignedTakeURL(options: TakeOptions): string {
50
+ const query = options.toQuery();
51
+ query.append("access_key", this.accessKey);
52
+ let queryString = query.toString();
53
+
54
+ const signature = crypto.createHmac("sha256", this.secretKey)
55
+ .update(queryString, 'utf-8')
56
+ .digest('hex');
57
+ queryString += '&signature=' + signature;
58
+
59
+ return `${API_BASE_URL}${API_TAKE_PATH}?${queryString}`;
60
+ }
61
+
62
+ /**
63
+ * Generates the link that can be publicly available, but make sure
64
+ * you read the documentation before at https://screenshotone.com/docs/signed-requests/.
65
+ *
66
+ * @param options
67
+ * @returns A signed URL that will generate the animated screenshot if requested.
68
+ */
69
+ generateSignedAnimateURL(options: AnimateOptions): string {
70
+ const query = options.toQuery();
71
+ query.append("access_key", this.accessKey);
72
+ let queryString = query.toString();
73
+
74
+ const signature = crypto.createHmac("sha256", this.secretKey)
75
+ .update(queryString, 'utf-8')
76
+ .digest('hex');
77
+ queryString += '&signature=' + signature;
78
+
79
+ return `${API_BASE_URL}${API_ANIMATE_PATH}?${queryString}`;
80
+ }
81
+
82
+ /**
83
+ * Generates a screenshot and return the image blob.
84
+ *
85
+ * @param options
86
+ * @returns
87
+ */
33
88
  async take(options: TakeOptions): Promise<Blob> {
34
- const url = this.generateTakeURL(options);
89
+ const url = this.generateSignedTakeURL(options);
90
+ const response = await fetch(url);
91
+ if (response.ok) {
92
+ return await response.blob()
93
+ }
94
+
95
+ const data = await response.json();
96
+
97
+ throw new Error(`failed to take screenshot, response returned ${response.status} ${response.statusText}: ${data?.message}`);
98
+ }
99
+
100
+ /**
101
+ * Generates an animated screenshot and return the image blob.
102
+ *
103
+ * @param options
104
+ * @returns
105
+ */
106
+ async animate(options: AnimateOptions): Promise<Blob> {
107
+ const url = this.generateSignedAnimateURL(options);
35
108
  const response = await fetch(url);
36
109
  if (response.ok) {
37
110
  return await response.blob()
@@ -440,6 +513,28 @@ export class TakeOptions {
440
513
  return this;
441
514
  }
442
515
 
516
+ /**
517
+ * It scrolls the page if needed and ensures that the given
518
+ * selector is present in the view when taking a screenshot.
519
+ */
520
+ scrollIntoView(selector: string): TakeOptions {
521
+ this.put("scroll_into_view", selector);
522
+
523
+ return this;
524
+ }
525
+
526
+ /**
527
+ * You can use your custom proxy to take screenshots
528
+ * or render HTML with the proxy option.
529
+ *
530
+ * The https, http, socks4 and socks5 proxies are supported.
531
+ */
532
+ proxy(proxy: string): TakeOptions {
533
+ this.put("proxy", proxy);
534
+
535
+ return this;
536
+ }
537
+
443
538
  /**
444
539
  * Set cookies for the request.
445
540
  */
@@ -458,6 +553,7 @@ export class TakeOptions {
458
553
  return this;
459
554
  }
460
555
 
556
+
461
557
  /**
462
558
  * TimeZone sets time zone for the request.
463
559
  * Available time zones are: "America/Santiago", "Asia/Shanghai", "Europe/Berlin", "America/Guayaquil",
@@ -529,6 +625,498 @@ export class TakeOptions {
529
625
  return this;
530
626
  }
531
627
 
628
+ toQuery(): URLSearchParams {
629
+ return new URLSearchParams(this.query.toString());
630
+ }
631
+ }
632
+
633
+ /**
634
+ * Represents options for animating screenshots.
635
+ */
636
+ export class AnimateOptions {
637
+ private readonly query: URLSearchParams;
638
+
639
+ private constructor() {
640
+ const query = new URLSearchParams();
641
+
642
+ this.query = query;
643
+ }
644
+
645
+ /**
646
+ * Initializes take screenshot options with provided website URL.
647
+ */
648
+ static url(url: string): AnimateOptions {
649
+ const options = new AnimateOptions();
650
+ options.put("url", url);
651
+
652
+ return options;
653
+ }
654
+
655
+ /**
656
+ * Initializes take screenshot options with provided HTML.
657
+ */
658
+ static html(html: string): AnimateOptions {
659
+ const options = new AnimateOptions();
660
+ options.put("html", html);
661
+
662
+ return options;
663
+ }
664
+
665
+ /**
666
+ * Initializes take screenshot options with provided Markdown.
667
+ */
668
+ static markdown(markdown: string): AnimateOptions {
669
+ const options = new AnimateOptions();
670
+ options.put("markdown", markdown);
671
+
672
+ return options;
673
+ }
674
+
675
+ private put(key: string, ...values: string[]) {
676
+ for (const value of values) {
677
+ this.query.append(key, value);
678
+ }
679
+ };
680
+
681
+ /**
682
+ * Available response types:
683
+ * by_format — return exactly the response defined in the format option. If format=png, the response will be the binary representation of the png with the content type header image/png;
684
+ * empty — return only status or error. It is suitable when you want to upload the screenshot to storage and don’t care about the results. It also speeds up the response since there are no networking costs involved.
685
+ * The default value is by_format.
686
+ */
687
+ responseType(responseType: string) {
688
+ this.put("response_type", responseType);
689
+
690
+ return this;
691
+ }
692
+
693
+ /**
694
+ * When reduced_motion set to `true`,
695
+ * the API will request the site to minimize the amount of non-essential motion it uses.
696
+ * When the site supports it, it should use animations as least as possible.
697
+ */
698
+ reducedMotion(darkMode: boolean): AnimateOptions {
699
+ this.put("reduced_motion", darkMode ? "true" : "false");
700
+
701
+ return this;
702
+ }
703
+
704
+ /**
705
+ * If you want to request the page and it is supported to be rendered for printers, specify `print`.
706
+ * If the page is by default rendered for printers and you want it to be rendered for screens,
707
+ * use `screen`.
708
+ */
709
+ mediaType(mediaType: string): AnimateOptions {
710
+ this.put("media_type", mediaType);
711
+
712
+ return this;
713
+ }
714
+
715
+ /**
716
+ * Set `true` to request site rendering, if supported, in the dark mode.
717
+ * Set `false` to request site rendering in the light mode if supported.
718
+ * If you don’t set the parameter. The site is rendered in the default mode.
719
+ */
720
+ darkMode(darkMode: boolean): AnimateOptions {
721
+ this.put("dark_mode", darkMode ? "true" : "false");
722
+
723
+ return this;
724
+ }
725
+
726
+ /**
727
+ * The `hide_selectors` option allows hiding elements before taking a screenshot.
728
+ * You can specify as many selectors as you wish.
729
+ * All elements that match each selector will be hidden by
730
+ * setting the `display` style property to none `!important`.
731
+ */
732
+ hideSelectors(...selectors: string[]): AnimateOptions {
733
+ this.put("hide_selectors", ...selectors);
734
+
735
+ return this;
736
+ }
737
+
738
+ /**
739
+ * The scripts_wain_until option allows you to wait until a given
740
+ * set of events after the scripts were executed.
741
+ * It accepts the same values as wait_until and can have multiple values:
742
+ * - `load`: the navigation is successful when the load even is fired;
743
+ * - `domcontentloaded`: the navigation is finished when the DOMContentLoaded even is fired;
744
+ * - `networkidle0`: the navigation is finished when there are no more than 0 network connections for at least 500 ms;
745
+ * _ `networkidle2`: consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.
746
+ */
747
+ scriptsWaitUntil(...until: string[]): AnimateOptions {
748
+ this.put("scripts_wait_until", ...until);
749
+
750
+ return this;
751
+ }
752
+
753
+ /**
754
+ * Styles specifies custom CSS styles for the page.
755
+ */
756
+ styles(styles: string) {
757
+ this.put("styles", styles);
758
+
759
+ return this;
760
+ }
761
+
762
+ /**
763
+ * You can use your custom proxy to take screenshots
764
+ * or render HTML with the proxy option.
765
+ *
766
+ * The https, http, socks4 and socks5 proxies are supported.
767
+ */
768
+ proxy(proxy: string): AnimateOptions {
769
+ this.put("proxy", proxy);
770
+
771
+ return this;
772
+ }
773
+
774
+ /**
775
+ * Scripts specifies custom scripts for the page.
776
+ */
777
+ scripts(scripts: string) {
778
+ this.put("scripts", scripts);
779
+
780
+ return this;
781
+ }
782
+
783
+ /**
784
+ * Sets response format, one of: "mp4", "avi", "webm", "mov" or "gif".
785
+ */
786
+ format(format: string): AnimateOptions {
787
+ this.put("format", format);
788
+
789
+ return this;
790
+ }
791
+
792
+ /**
793
+ * Renders a transparent background for the image. Works only if the site has not defined background color.
794
+ * Available for the following response formats: "png", "webp".
795
+ */
796
+ omitBackground(omitBackground: boolean): AnimateOptions {
797
+ this.put("omit_background", omitBackground ? "true" : "false");
798
+
799
+ return this;
800
+ }
801
+
802
+ /**
803
+ * Instead of manually specifying viewport parameters like width and height,
804
+ * you can specify a device to use for emulation.
805
+ * In addition, other parameters of the viewport, including the user agent,
806
+ * will be set automatically.
807
+ */
808
+ viewportDevice(viewportDevice: string): AnimateOptions {
809
+ this.put("viewport_device", viewportDevice);
810
+
811
+ return this;
812
+ }
813
+
814
+ /**
815
+ * Sets the width of the browser viewport (pixels).
816
+ */
817
+ viewportWidth(viewportWidth: number): AnimateOptions {
818
+ this.put("viewport_width", viewportWidth.toString());
819
+
820
+ return this;
821
+ }
822
+
823
+ /**
824
+ * Sets the height of the browser viewport (pixels).
825
+ */
826
+ viewportHeight(viewportHeight: number): AnimateOptions {
827
+ this.put("viewport_height", viewportHeight.toString());
828
+
829
+ return this;
830
+ }
831
+
832
+ /**
833
+ * Sets the device scale factor. Acceptable value is one of: 1, 2 or 3.
834
+ */
835
+ deviceScaleFactor(deviceScaleFactor: number): AnimateOptions {
836
+ this.put("device_scale_factor", deviceScaleFactor.toString());
837
+
838
+ return this;
839
+ }
840
+
841
+ /**
842
+ * Whether the meta viewport tag is taken into account. Defaults to `false`.
843
+ */
844
+ viewportMobile(isMobile: boolean): AnimateOptions {
845
+ this.put("viewport_mobile", isMobile ? "true" : "false");
846
+
847
+ return this;
848
+ }
849
+
850
+ /**
851
+ * The default value is `false`. Set to `true` if the viewport supports touch events.
852
+ */
853
+ viewportHasTouch(hasTouch: boolean): AnimateOptions {
854
+ this.put("viewport_has_touch", hasTouch ? "true" : "false");
855
+
856
+ return this;
857
+ }
858
+
859
+ /**
860
+ * The default value is `false`. Set to `true` if the viewport is in landscape mode.
861
+ */
862
+ viewportLandscape(landscape: boolean): AnimateOptions {
863
+ this.put("viewport_landscape", landscape ? "true" : "false");
864
+
865
+ return this;
866
+ }
867
+
868
+ /**
869
+ * Sets geolocation latitude for the request.
870
+ * Both latitude and longitude are required if one of them is set.
871
+ */
872
+ geolocationLatitude(latitude: number): AnimateOptions {
873
+ this.put("geolocation_latitude", new Big(latitude).toFixed(20).replace(/0+$/, ""));
874
+
875
+ return this;
876
+ }
877
+
878
+ /**
879
+ * Sets geolocation longitude for the request. Both latitude and longitude are required if one of them is set.
880
+ */
881
+ geolocationLongitude(longitude: number): AnimateOptions {
882
+ this.put("geolocation_longitude", new Big(longitude).toFixed(20).replace(/0+$/, ""));
883
+
884
+ return this;
885
+ }
886
+
887
+ /**
888
+ * Sets the geolocation accuracy in meters.
889
+ */
890
+ geolocationAccuracy(accuracy: number): AnimateOptions {
891
+ this.put("geolocation_accuracy", accuracy.toString());
892
+
893
+ return this;
894
+ }
895
+
896
+ /**
897
+ * Blocks ads.
898
+ */
899
+ blockAds(blockAds: boolean): AnimateOptions {
900
+ this.put("block_ads", blockAds ? "true" : "false");
901
+
902
+ return this;
903
+ }
904
+
905
+ /**
906
+ * Blocks cookie banners.
907
+ */
908
+ blockCookieBanners(block: boolean): AnimateOptions {
909
+ this.put("block_cookie_banners", block ? "true" : "false");
910
+
911
+ return this;
912
+ }
913
+
914
+ /**
915
+ * Blocks chats.
916
+ */
917
+ blockChats(block: boolean): AnimateOptions {
918
+ this.put("block_chats", block ? "true" : "false");
919
+
920
+ return this;
921
+ }
922
+
923
+ /**
924
+ * Blocks trackers.
925
+ */
926
+ blockTrackers(blockTrackers: boolean): AnimateOptions {
927
+ this.put("block_trackers", blockTrackers ? "true" : "false");
928
+
929
+ return this;
930
+ }
931
+
932
+ /**
933
+ * Blocks requests by specifying URL, domain, or even a simple pattern.
934
+ */
935
+ blockRequests(...blockRequests: string[]): AnimateOptions {
936
+ this.put("block_requests", ...blockRequests);
937
+
938
+ return this;
939
+ }
940
+
941
+ /**
942
+ * Blocks loading resources by type. Available resource types are: "document", "stylesheet", "image", "media",
943
+ * "font", "script", "texttrack", "xhr", "fetch", "eventsource", "websocket", "manifest", "other".
944
+ */
945
+ blockResources(...blockResources: string[]): AnimateOptions {
946
+ this.put("block_resources", ...blockResources);
947
+
948
+ return this;
949
+ }
950
+
951
+ /**
952
+ * Enables caching.
953
+ */
954
+ cache(cache: boolean): AnimateOptions {
955
+ this.put("cache", cache ? "true" : "false");
956
+
957
+ return this;
958
+ }
959
+
960
+ /**
961
+ * Sets cache TTL.
962
+ */
963
+ cacheTtl(cacheTTL: number): AnimateOptions {
964
+ this.put("cache_ttl", cacheTTL.toString());
965
+
966
+ return this;
967
+ }
968
+
969
+ /**
970
+ * Sets cache key.
971
+ */
972
+ cacheKey(cacheKey: string): AnimateOptions {
973
+ this.put("cache_key", cacheKey);
974
+
975
+ return this;
976
+ }
977
+
978
+ /**
979
+ * Sets a user agent for the request.
980
+ */
981
+ userAgent(userAgent: string): AnimateOptions {
982
+ this.put("user_agent", userAgent);
983
+
984
+ return this;
985
+ }
986
+
987
+ /**
988
+ * Sets an authorization header for the request.
989
+ */
990
+ authorization(authorization: string): AnimateOptions {
991
+ this.put("authorization", authorization);
992
+
993
+ return this;
994
+ }
995
+
996
+ /**
997
+ * Set cookies for the request.
998
+ */
999
+ cookies(...cookies: string[]): AnimateOptions {
1000
+ this.put("cookies", ...cookies);
1001
+
1002
+ return this;
1003
+ }
1004
+
1005
+ /**
1006
+ * Sets extra headers for the request.
1007
+ */
1008
+ headers(...headers: string[]) {
1009
+ this.put("headers", ...headers);
1010
+
1011
+ return this;
1012
+ }
1013
+
1014
+ /**
1015
+ * TimeZone sets time zone for the request.
1016
+ * Available time zones are: "America/Santiago", "Asia/Shanghai", "Europe/Berlin", "America/Guayaquil",
1017
+ * "Europe/Madrid", "Pacific/Majuro", "Asia/Kuala_Lumpur", "Pacific/Auckland", "Europe/Lisbon", "Europe/Kiev",
1018
+ * "Asia/Tashkent", "Europe/London".
1019
+ */
1020
+ timeZone(timeZone: string): AnimateOptions {
1021
+ this.put("time_zone", timeZone);
1022
+
1023
+ return this;
1024
+ }
1025
+
1026
+ /**
1027
+ * Sets delay.
1028
+ */
1029
+ delay(delay: number): AnimateOptions {
1030
+ this.put("delay", delay.toString());
1031
+
1032
+ return this;
1033
+ }
1034
+
1035
+ /**
1036
+ * Sets timeout.
1037
+ */
1038
+ timeout(timeout: number): AnimateOptions {
1039
+ this.put("timeout", timeout.toString());
1040
+
1041
+ return this;
1042
+ }
1043
+
1044
+ /**
1045
+ * Default value is false. Use store=true to trigger upload of the taken screenshot,
1046
+ * rendered HTML or PDF to the configured S3 bucket.
1047
+ * Make sure you configured access to S3.
1048
+ */
1049
+ store(store: boolean): AnimateOptions {
1050
+ this.put("store", store ? "true" : "false");
1051
+
1052
+ return this;
1053
+ }
1054
+
1055
+ /**
1056
+ * The parameter is required if you set store=true.
1057
+ * You must specify the key for the file, but don’t specify an extension,
1058
+ * it will be added automatically based on the format you specified.
1059
+ * You can also specify “subdirectories” in the path part.
1060
+ */
1061
+ storagePath(path: string): AnimateOptions {
1062
+ this.put("storage_path", path);
1063
+
1064
+ return this;
1065
+ }
1066
+
1067
+ /**
1068
+ * You can override the default bucket you configured with storage_bucket=<bucket name>.
1069
+ */
1070
+ storageBucket(bucket: string): AnimateOptions {
1071
+ this.put("storage_bucket", bucket);
1072
+
1073
+ return this;
1074
+ }
1075
+
1076
+ /**
1077
+ * Storage class allows you to specify the object storage class.
1078
+ */
1079
+ storageClass(storageClass: string): AnimateOptions {
1080
+ this.put("storage_class", storageClass);
1081
+
1082
+ return this;
1083
+ }
1084
+
1085
+ /**
1086
+ * Sets animation duration.
1087
+ */
1088
+ duration(duration: number): AnimateOptions {
1089
+ this.put("duration", duration.toString());
1090
+
1091
+ return this;
1092
+ }
1093
+
1094
+ /**
1095
+ * Sets animation duration.
1096
+ */
1097
+ scenario(scenario: string): AnimateOptions {
1098
+ this.put("scenario", scenario);
1099
+
1100
+ return this;
1101
+ }
1102
+
1103
+ scrollDelay(delay: number): AnimateOptions {
1104
+ this.put("scroll_delay", delay.toString());
1105
+
1106
+ return this;
1107
+ }
1108
+
1109
+ scrollDuration(duration: number): AnimateOptions {
1110
+ this.put("scroll_duration", duration.toString());
1111
+
1112
+ return this;
1113
+ }
1114
+
1115
+ scrollStartImmediately(startImmediately: boolean): AnimateOptions {
1116
+ this.put("scroll_start_immediately", startImmediately ? "true" : "false");
1117
+ return this;
1118
+ }
1119
+
532
1120
  toQuery(): URLSearchParams {
533
1121
  return new URLSearchParams(this.query.toString());
534
1122
  }