Merge pull request #103 from movie-web/fix/revert-urlsearchparams
Fix URLSearchParams usage for react-native
This commit is contained in:
commit
9e4e06b4c2
|
@ -1,6 +1,7 @@
|
|||
import FormData from 'form-data';
|
||||
|
||||
import { FetcherOptions } from '@/fetchers/types';
|
||||
import { isReactNative } from '@/utils/native';
|
||||
|
||||
export interface SeralizedBody {
|
||||
headers: Record<string, string>;
|
||||
|
@ -8,11 +9,20 @@ 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: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: body.toString(),
|
||||
};
|
||||
}
|
||||
return {
|
||||
headers: {},
|
||||
body,
|
||||
};
|
||||
}
|
||||
|
||||
// serialize as JSON
|
||||
return {
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
};
|
|
@ -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('&');
|
||||
}
|
Loading…
Reference in New Issue