From 36d4b41baa65dbd7669a1f989ec6e6be6b859896 Mon Sep 17 00:00:00 2001 From: Jorrin <43169049+JorrinKievit@users.noreply.github.com> Date: Thu, 29 Feb 2024 22:44:38 +0100 Subject: [PATCH] Fix URLSearchParams usage for react-native --- src/fetchers/body.ts | 10 +++++++++- src/providers/sources/showbox/sendRequest.ts | 18 ++++++++---------- src/utils/native.ts | 9 +++++++++ src/utils/params.ts | 5 ----- 4 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 src/utils/native.ts delete mode 100644 src/utils/params.ts diff --git a/src/fetchers/body.ts b/src/fetchers/body.ts index 44438e5..2d9d219 100644 --- a/src/fetchers/body.ts +++ b/src/fetchers/body.ts @@ -1,6 +1,7 @@ import FormData from 'form-data'; import { FetcherOptions } from '@/fetchers/types'; +import { isReactNative } from '@/utils/native'; export interface SeralizedBody { headers: Record; @@ -8,11 +9,18 @@ export interface SeralizedBody { } export function serializeBody(body: FetcherOptions['body']): SeralizedBody { - if (body === undefined || typeof body === 'string' || body instanceof URLSearchParams || body instanceof FormData) + if (body === undefined || typeof body === 'string' || body instanceof URLSearchParams || body instanceof FormData) { + if (body instanceof URLSearchParams && isReactNative()) { + return { + headers: {}, + body: body.toString(), + }; + } return { headers: {}, body, }; + } // serialize as JSON return { diff --git a/src/providers/sources/showbox/sendRequest.ts b/src/providers/sources/showbox/sendRequest.ts index 248fddd..7ea9024 100644 --- a/src/providers/sources/showbox/sendRequest.ts +++ b/src/providers/sources/showbox/sendRequest.ts @@ -2,7 +2,6 @@ import CryptoJS from 'crypto-js'; import { customAlphabet } from 'nanoid'; import type { ScrapeContext } from '@/utils/context'; -import { createSearchParams } from '@/utils/params'; import { apiUrls, appId, appKey, key } from './common'; import { encrypt, getVerify } from './crypto'; @@ -35,14 +34,13 @@ export const sendRequest = async (ctx: ScrapeContext, data: object, altApi = fal }); const base64body = btoa(body); - const formatted = { - data: base64body, - appid: '27', - platform: 'android', - version: '129', - medium: 'Website', - token: randomId(32), - }; + const formatted = new URLSearchParams(); + formatted.append('data', base64body); + formatted.append('appid', '27'); + formatted.append('platform', 'android'); + formatted.append('version', '129'); + formatted.append('medium', 'Website'); + formatted.append('token', randomId(32)); const requestUrl = altApi ? apiUrls[1] : apiUrls[0]; @@ -53,7 +51,7 @@ export const sendRequest = async (ctx: ScrapeContext, data: object, altApi = fal 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'okhttp/3.2.0', }, - body: createSearchParams(formatted), + body: formatted, }); return JSON.parse(response); }; diff --git a/src/utils/native.ts b/src/utils/native.ts new file mode 100644 index 0000000..cc91cdb --- /dev/null +++ b/src/utils/native.ts @@ -0,0 +1,9 @@ +export const isReactNative = () => { + try { + // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires + require('react-native'); + return true; + } catch (e) { + return false; + } +}; diff --git a/src/utils/params.ts b/src/utils/params.ts deleted file mode 100644 index ea2149f..0000000 --- a/src/utils/params.ts +++ /dev/null @@ -1,5 +0,0 @@ -export function createSearchParams(params: { [key: string]: string | number }): string { - return Object.entries(params) - .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) - .join('&'); -}