Multiple fixes for testing

This commit is contained in:
dangered wolf 2023-11-10 05:02:39 -05:00
parent d509965a3d
commit 91cadb2fca
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
6 changed files with 24 additions and 18 deletions

View file

@ -17,7 +17,17 @@ export const cacheMiddleware = (): MiddlewareHandler => async (c, next) => {
console.log('cacheUrl', cacheUrl);
const cacheKey = new Request(cacheUrl.toString(), request);
let cacheKey: Request;
try {
cacheKey = new Request(cacheUrl.toString(), request);
} catch(e) {
/* In Miniflare, you can't really create requests like this, so we ignore caching in the test environment */
await next();
return c.res.clone();
}
const cache = caches.default;
switch (request.method) {

View file

@ -24,7 +24,7 @@ export const twitterFetch = async (
url: string,
useElongator = experimentCheck(
Experiment.ELONGATOR_BY_DEFAULT,
typeof c.env.TwitterProxy !== 'undefined'
typeof c.env?.TwitterProxy !== 'undefined'
),
validateFunction: (response: unknown) => boolean,
elongatorRequired = false
@ -141,10 +141,10 @@ export const twitterFetch = async (
let apiRequest;
try {
if (useElongator && typeof c.env.TwitterProxy !== 'undefined') {
if (useElongator && typeof c.env?.TwitterProxy !== 'undefined') {
console.log('Fetching using elongator');
const performanceStart = performance.now();
apiRequest = await c.env.TwitterProxy.fetch(url, {
apiRequest = await c.env?.TwitterProxy.fetch(url, {
method: 'GET',
headers: headers
});
@ -189,7 +189,7 @@ export const twitterFetch = async (
if (
!wasElongatorDisabled &&
!useElongator &&
typeof c.env.TwitterProxy !== 'undefined' &&
typeof c.env?.TwitterProxy !== 'undefined' &&
(response as TweetResultsByRestIdResult)?.data?.tweetResult?.result?.reason ===
'NsfwLoggedOut'
) {
@ -251,7 +251,7 @@ export const fetchUser = async (
c: Context,
useElongator = experimentCheck(
Experiment.ELONGATOR_PROFILE_API,
typeof c.env.TwitterProxy !== 'undefined'
typeof c.env?.TwitterProxy !== 'undefined'
)
): Promise<GraphQLUserResponse> => {
return (await twitterFetch(

View file

@ -46,14 +46,14 @@ export const translateTweet = async (
headers['x-twitter-client-language'] = language;
/* As of August 2023, you can no longer fetch translations with guest token */
if (typeof c.env.TwitterProxy === 'undefined') {
if (typeof c.env?.TwitterProxy === 'undefined') {
return null;
}
try {
const url = `${Constants.TWITTER_ROOT}/i/api/1.1/strato/column/None/tweetId=${tweet.rest_id},destinationLanguage=None,translationSource=Some(Google),feature=None,timeout=None,onlyCached=None/translation/service/translateTweet`;
console.log(url, headers);
translationApiResponse = await c.env.TwitterProxy.fetch(url, {
translationApiResponse = await c.env?.TwitterProxy.fetch(url, {
method: 'GET',
headers: headers
});

View file

@ -8,7 +8,7 @@ import { Context } from 'hono';
export const fetchTweetDetail = async (
c: Context,
status: string,
useElongator = typeof c.env.TwitterProxy !== 'undefined',
useElongator = typeof c.env?.TwitterProxy !== 'undefined',
cursor: string | null = null
): Promise<TweetDetailResult> => {
return (await twitterFetch(
@ -93,7 +93,7 @@ export const fetchByRestId = async (
c: Context,
useElongator = experimentCheck(
Experiment.ELONGATOR_BY_DEFAULT,
typeof c.env.TwitterProxy !== 'undefined'
typeof c.env?.TwitterProxy !== 'undefined'
)
): Promise<TweetResultsByRestIdResult> => {
return (await twitterFetch(
@ -287,7 +287,7 @@ export const constructTwitterThread = async (
Also - dirty hack. Right now, TweetDetail requests aren't working with language and I haven't figured out why.
I'll figure out why eventually, but for now just don't use TweetDetail for this. */
if (
typeof c.env.TwitterProxy !== 'undefined' &&
typeof c.env?.TwitterProxy !== 'undefined' &&
!language &&
(experimentCheck(Experiment.TWEET_DETAIL_API) || processThread)
) {

5
src/types/env.d.ts vendored
View file

@ -13,8 +13,3 @@ declare const API_HOST_LIST: string;
declare const SENTRY_DSN: string;
declare const RELEASE_NAME: string;
declare const TEST: boolean | undefined;
declare const TwitterProxy: Fetcher;
declare const AnalyticsEngine: AnalyticsEngineDataset;

View file

@ -52,7 +52,8 @@ if (SENTRY_DSN) {
allowedSearchParams: /(.*)/
},
// integrations: [new RewriteFrames({ root: '/' }) as any],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
integrations: [new RewriteFrames({ root: '/' }) as any],
release: RELEASE_NAME
})
);