Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface IExtraLifeUser {
numIncentives: number;
isCustomAvatarImage: boolean;
URL: string;
cacheTag: string;
lastModifiedUTC: string;
}

export interface IExtraLifeMilestone {
Expand Down
54 changes: 27 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IExtraLifeUser> => {
export const getUserInfo = async (id: string | number, cacheTag: string = ''): Promise<IExtraLifeUser> => {
return new Promise<IExtraLifeUser>((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');
});
});
};

Expand Down