@8ms/helpers 1.29.0 → 1.32.0

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.
@@ -1,4 +1,5 @@
1
1
  import { Param } from "../param";
2
+ import ApiResponse from "../../api/ApiResponse";
2
3
  type GetAsync = {
3
4
  auth: Param;
4
5
  requestId: string;
@@ -7,5 +8,5 @@ type GetAsync = {
7
8
  * Using the request ID from getAsyncRequestId fetch to see if the data is available.
8
9
  * https://docs.brightdata.com/scraping-automation/serp-api/asynchronous-requests
9
10
  */
10
- declare const getAsync: (props: GetAsync) => Promise<{}>;
11
- export default getAsync;
11
+ declare const getAsyncResults: (props: GetAsync) => Promise<ApiResponse>;
12
+ export default getAsyncResults;
@@ -6,12 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const get_1 = __importDefault(require("../../axios/get"));
7
7
  const getCustomerId_1 = __importDefault(require("../getCustomerId"));
8
8
  const getZone_1 = __importDefault(require("../getZone"));
9
+ const ApiResponse_1 = __importDefault(require("../../api/ApiResponse"));
9
10
  /**
10
11
  * Using the request ID from getAsyncRequestId fetch to see if the data is available.
11
12
  * https://docs.brightdata.com/scraping-automation/serp-api/asynchronous-requests
12
13
  */
13
- const getAsync = async (props) => {
14
- let response = {};
14
+ const getAsyncResults = async (props) => {
15
+ let response = (new ApiResponse_1.default());
15
16
  // Prevent auth requests getting blocked
16
17
  process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
17
18
  const customerId = (0, getCustomerId_1.default)({
@@ -29,8 +30,20 @@ const getAsync = async (props) => {
29
30
  url: `https://api.brightdata.com/serp/get_result?customer=${customerId}&zone=${zone}&response_id=${props.requestId}`,
30
31
  });
31
32
  if (requestResponse.isSuccess()) {
32
- response = requestResponse.getBody();
33
+ if ("Request is pending" === requestResponse.getBody()) {
34
+ response.setToPending();
35
+ }
36
+ else {
37
+ response.setToSuccess({
38
+ body: requestResponse.getBody(),
39
+ });
40
+ }
41
+ }
42
+ else {
43
+ response.setToError({
44
+ error: "Error in request",
45
+ });
33
46
  }
34
47
  return response;
35
48
  };
36
- exports.default = getAsync;
49
+ exports.default = getAsyncResults;
@@ -3,6 +3,7 @@ type GetBatch = {
3
3
  auth: Param;
4
4
  data?: object;
5
5
  scraperId: string;
6
+ isDev?: boolean;
6
7
  };
7
8
  /**
8
9
  * Depends on the Scraper setup!
@@ -11,7 +11,12 @@ const post_1 = __importDefault(require("../../axios/post"));
11
11
  */
12
12
  const getBatch = async (props) => {
13
13
  let response = false;
14
- const requestUrl = `https://api.brightdata.com/dca/trigger?collector=${props.scraperId}&queue_next=1`;
14
+ const requestUrl = new URL('https://api.brightdata.com/dca/trigger');
15
+ requestUrl.searchParams.append('collector', props.scraperId);
16
+ requestUrl.searchParams.append('queue_next', '1');
17
+ if (props.isDev) {
18
+ requestUrl.searchParams.append('version', 'dev');
19
+ }
15
20
  const requestResponse = await (0, post_1.default)({
16
21
  config: {
17
22
  headers: {
@@ -19,7 +24,7 @@ const getBatch = async (props) => {
19
24
  },
20
25
  },
21
26
  data: props?.data || {},
22
- url: requestUrl,
27
+ url: requestUrl.toString(),
23
28
  });
24
29
  if (requestResponse.isSuccess()) {
25
30
  response = true;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.29.0",
4
+ "version": "1.32.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"
@@ -4,7 +4,9 @@
4
4
  * https://next-auth.js.org/schemas/adapters
5
5
  * Library: @prisma/client, prisma-query-log
6
6
  */
7
- declare const initClient: ({ debug }?: {
7
+ type InitClient = {
8
8
  debug?: boolean;
9
- }) => void;
9
+ planetScale?: boolean;
10
+ };
11
+ declare const initClient: (props?: InitClient) => void;
10
12
  export default initClient;
@@ -7,7 +7,7 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  global.prisma = null;
10
- const initClient = ({ debug } = { debug: false }) => {
10
+ const initClient = (props = {}) => {
11
11
  if (!global.prisma) {
12
12
  const { Client } = require("@planetscale/database");
13
13
  const { PrismaClient } = require("@prisma/client");
@@ -15,9 +15,12 @@ const initClient = ({ debug } = { debug: false }) => {
15
15
  const client = new Client({
16
16
  url: process.env.DATABASE_URL
17
17
  });
18
- const adapter = new PrismaPlanetScale(client);
18
+ let adapter;
19
+ if (props?.planetScale) {
20
+ adapter = new PrismaPlanetScale(client);
21
+ }
19
22
  // Show the full query if debugging
20
- if (debug) {
23
+ if (props?.debug) {
21
24
  global.prisma = new PrismaClient({
22
25
  adapter,
23
26
  log: [