Akash Sharma
Akash Sharma
TTCTheo's Typesafe Cult
Created by Akash Sharma on 6/24/2024 in #questions
Issue with Fetching PDF from Uploadthing in the server
I'm trying to fetch a PDF from Uploadthing in the server, but many time it fails. How can I fix this issue so it always fetches the PDF without problems?
let response;
let retries = 5;

while (retries > 0) {
try {
response = await fetch(`https://utfs.io/f/${fileKey}`);
if (response.ok) break;
} catch (error) {
console.log(`Fetch failed, retrying... (${3 - retries + 1}/${3})\n\n ${error}`);
}
retries--;
if (retries > 0) await new Promise(resolve => setTimeout(resolve, 1000));
}
if (!response || !response.ok) {
throw new Error('Failed to fetch PDF after multiple retries');
}

const blob = await response.blob();

const loader = new PDFLoader(blob);

const pageLevelDocs = await loader.load();
let response;
let retries = 5;

while (retries > 0) {
try {
response = await fetch(`https://utfs.io/f/${fileKey}`);
if (response.ok) break;
} catch (error) {
console.log(`Fetch failed, retrying... (${3 - retries + 1}/${3})\n\n ${error}`);
}
retries--;
if (retries > 0) await new Promise(resolve => setTimeout(resolve, 1000));
}
if (!response || !response.ok) {
throw new Error('Failed to fetch PDF after multiple retries');
}

const blob = await response.blob();

const loader = new PDFLoader(blob);

const pageLevelDocs = await loader.load();
Error
error: Error: fetch failed
error: Error: fetch failed
1 replies
TTCTheo's Typesafe Cult
Created by Akash Sharma on 5/20/2024 in #questions
TypeScript Type Mismatch in Microsoft Authentication with Passport
I've created a backend using Microsoft authentication in Express. I'm facing some type errors that are a bit confusing for me. passport.tsx
import { Strategy as MicrosoftStrategy } from "passport-microsoft";

passport.serializeUser(function(user: any, done) {
done(null, user._id);
});

passport.deserializeUser(async function(id: string, done) {

const user = await User.findById(id).lean().exec();
done(null, user);

});
import { Strategy as MicrosoftStrategy } from "passport-microsoft";

passport.serializeUser(function(user: any, done) {
done(null, user._id);
});

passport.deserializeUser(async function(id: string, done) {

const user = await User.findById(id).lean().exec();
done(null, user);

});
custom.d.ts
interface IUser {
_id: string;
username: string;
password?: string;
email: string;
firstName: string;
lastName?: string;
profilePicUrl?: string;
googleId?: string;
googleAccessToken?: string;
googleRefreshToken?: string;
microsoftId?: string
microsoftAccessToken?: string
microsoftRefreshToken?: string
meetings: string[];
bookings: string[];
}


declare global {
namespace Express {
interface Request {
user: IUser;
}
}
}
interface IUser {
_id: string;
username: string;
password?: string;
email: string;
firstName: string;
lastName?: string;
profilePicUrl?: string;
googleId?: string;
googleAccessToken?: string;
googleRefreshToken?: string;
microsoftId?: string
microsoftAccessToken?: string
microsoftRefreshToken?: string
meetings: string[];
bookings: string[];
}


declare global {
namespace Express {
interface Request {
user: IUser;
}
}
}
microsoftRoutes.tsx
router.get('/',
passport.authenticate('microsoft', {
}),
function(req, res) {
// The request will be redirected to Microsoft for authentication, so this
// function will not be called.
}
);

router.get('/callback',
passport.authenticate('microsoft', { failureRedirect: '/failure' }),
function(req, res) {
res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName}`);
}
);
router.get('/',
passport.authenticate('microsoft', {
}),
function(req, res) {
// The request will be redirected to Microsoft for authentication, so this
// function will not be called.
}
);

router.get('/callback',
passport.authenticate('microsoft', { failureRedirect: '/failure' }),
function(req, res) {
res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName}`);
}
);
req.user possibly 'undefined'
src/routes/microsoftRoutes.ts:30:70 - error TS18048: 'req.user' is possibly 'undefined'.
30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`);

src/routes/microsoftRoutes.ts:30:79 - error TS2339: Property 'firstName' does not exist on type 'User'.

30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`)
src/routes/microsoftRoutes.ts:30:70 - error TS18048: 'req.user' is possibly 'undefined'.
30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`);

src/routes/microsoftRoutes.ts:30:79 - error TS2339: Property 'firstName' does not exist on type 'User'.

30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`)
req.user:Express.User but i want req.user:Express.User && IUser to fix the type issue
2 replies
TTCTheo's Typesafe Cult
Created by Akash Sharma on 4/13/2024 in #questions
It works locally but doesn't save to the database when deployed on Vercel.
It's working fine on my local machine, but after deploying to Vercel, it's not saving data to the database.
18 replies
TTCTheo's Typesafe Cult
Created by Akash Sharma on 8/1/2023 in #questions
Can anyone suggest a good library for PostgreSQL similar to Mongoose?
I am learning PostgreSQL with Express. Can anyone suggest a good library for PostgreSQL similar to Mongoose?
7 replies