mirror of
https://github.com/CompeyDev/fxtwitter-docker.git
synced 2025-04-07 19:40:54 +01:00
🐛 Added logic for non-existant users
Fixes an internal server error when users do not exist
This commit is contained in:
parent
80207fc0d8
commit
c196207fc3
3 changed files with 25 additions and 1 deletions
|
@ -60,7 +60,12 @@ export const userAPI = async (
|
||||||
flags?: InputFlags
|
flags?: InputFlags
|
||||||
): Promise<UserAPIResponse> => {
|
): Promise<UserAPIResponse> => {
|
||||||
const userResponse = await fetchUser(username, event);
|
const userResponse = await fetchUser(username, event);
|
||||||
|
if (!userResponse || !Object.keys(userResponse).length) {
|
||||||
|
return {
|
||||||
|
code: 404,
|
||||||
|
message: 'User not found'
|
||||||
|
};
|
||||||
|
}
|
||||||
/* Creating the response objects */
|
/* Creating the response objects */
|
||||||
const response: UserAPIResponse = { code: 200, message: 'OK' } as UserAPIResponse;
|
const response: UserAPIResponse = { code: 200, message: 'OK' } as UserAPIResponse;
|
||||||
const apiUser: APIUser = (await populateUserProperties(userResponse)) as APIUser;
|
const apiUser: APIUser = (await populateUserProperties(userResponse)) as APIUser;
|
||||||
|
|
|
@ -231,6 +231,10 @@ export const fetchUser = async (
|
||||||
// Validator function
|
// Validator function
|
||||||
(_res: unknown) => {
|
(_res: unknown) => {
|
||||||
const response = _res as GraphQLUserResponse;
|
const response = _res as GraphQLUserResponse;
|
||||||
|
// If _res.data is an empty object, we have no user
|
||||||
|
if (!Object.keys(response?.data).length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return !(
|
return !(
|
||||||
response?.data?.user?.result?.__typename !== 'User' ||
|
response?.data?.user?.result?.__typename !== 'User' ||
|
||||||
typeof response.data.user.result.legacy === 'undefined'
|
typeof response.data.user.result.legacy === 'undefined'
|
||||||
|
|
|
@ -270,3 +270,18 @@ test('API fetch user', async () => {
|
||||||
expect(user.birthday.month).toEqual(3);
|
expect(user.birthday.month).toEqual(3);
|
||||||
expect(user.birthday.year).toBeUndefined();
|
expect(user.birthday.year).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.only('API fetch user that does not exist', async () => {
|
||||||
|
const result = await cacheWrapper(
|
||||||
|
new Request('https://api.fxtwitter.com/usesaahah123', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: botHeaders
|
||||||
|
})
|
||||||
|
);
|
||||||
|
expect(result.status).toEqual(404);
|
||||||
|
const response = (await result.json()) as UserAPIResponse;
|
||||||
|
expect(response).toBeTruthy();
|
||||||
|
expect(response.code).toEqual(404);
|
||||||
|
expect(response.message).toEqual('User not found');
|
||||||
|
expect(response.user).toBeUndefined();
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue