diff --git a/src/api/user.ts b/src/api/user.ts index 593c047..9230fd5 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -60,7 +60,12 @@ export const userAPI = async ( flags?: InputFlags ): Promise => { const userResponse = await fetchUser(username, event); - + if (!userResponse || !Object.keys(userResponse).length) { + return { + code: 404, + message: 'User not found' + }; + } /* Creating the response objects */ const response: UserAPIResponse = { code: 200, message: 'OK' } as UserAPIResponse; const apiUser: APIUser = (await populateUserProperties(userResponse)) as APIUser; diff --git a/src/fetch.ts b/src/fetch.ts index c78c579..6c6ef68 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -231,6 +231,10 @@ export const fetchUser = async ( // Validator function (_res: unknown) => { 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 !( response?.data?.user?.result?.__typename !== 'User' || typeof response.data.user.result.legacy === 'undefined' diff --git a/test/index.test.ts b/test/index.test.ts index f5b06f0..6a8af41 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -270,3 +270,18 @@ test('API fetch user', async () => { expect(user.birthday.month).toEqual(3); 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(); +}); \ No newline at end of file