diff --git a/src/demo.ts b/src/demo.ts index af8cebd..db3dee3 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -4,6 +4,10 @@ const demoUser = 484672; const demoTeam = 60878; getUserInfo(demoUser) + .then((data) => { + console.log(data); + return getUserInfo(demoUser, data.cacheTag); + }) .then((data) => { console.log(data); }) diff --git a/src/helpers/interfaces.ts b/src/helpers/interfaces.ts index cba204a..28ed855 100644 --- a/src/helpers/interfaces.ts +++ b/src/helpers/interfaces.ts @@ -22,6 +22,8 @@ export interface IExtraLifeUser { numIncentives: number; isCustomAvatarImage: boolean; URL: string; + cacheTag: string; + lastModifiedUTC: string; } export interface IExtraLifeMilestone { diff --git a/src/index.ts b/src/index.ts index f44c944..ccc2009 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,40 +8,40 @@ export { IDonationsList, IExtraLifeTeam, IExtraLifeUser, IRosterList } from './h /** * Gets the extra life info of a user * @param id - the user participant ID - * @param team - whether to return team info or not + * @param cacheTag - an optional cache identifier to fail if not changed * @return result - the promise for completion of function (async) */ -export const getUserInfo = async (id: string | number): Promise => { +export const getUserInfo = async (id: string | number, cacheTag: string = ''): Promise => { return new Promise((resolve, reject) => { const url = apiPaths.profileUrl(id as number); let userInfoJson: any = {}; - fetch(url) - .then((res) => { - try { - userInfoJson = res.json(); - userInfoJson.avatarImageURL = 'https:' + userInfoJson.avatarImageURL; - userInfoJson.donateURL = `https://www.extra-life.org/index.cfm?fuseaction=donate.participant&participantID=${id}`; + fetch(url, { + headers: { + 'If-None-Match': `"${cacheTag}"`, + }, + redirect: 'error', + }).then(async (res) => { + // Reject calls on non-ok messages (like 304, cache hit) + if (!res.ok) { + return reject(res.statusText); + } - if (userInfoJson.teamID) { - getTeamInfo(userInfoJson.teamID, false) - .then((data: any) => { - userInfoJson.teamURL = data.teamURL; - resolve(userInfoJson); - }).catch((reason) => { - reject(reason); - }); - } else { - resolve(userInfoJson); - } - } catch (e) { - reject(e); - } - }) - .catch(() => { - console.log('Error parsing userInfo URL'); - reject('There was an error trying to make your request'); - }); + try { + userInfoJson = await res.json(); + // Removing the "weak" flag and quotation marks so the identifier is clear + userInfoJson.cacheTag = res.headers.get('etag').replace('W/"', '').replace('"', ''); + userInfoJson.lastModifiedUTC = (new Date(res.headers.get('last-modified'))).toISOString(); + + return resolve(userInfoJson); + } catch (e) { + reject(e); + } + }) + .catch(() => { + console.log('Error parsing userInfo URL'); + reject('There was an error trying to make your request'); + }); }); };