Skip to content

Commit

Permalink
fix(api): handle trailing / in MS transcript urls (freeCodeCamp#51766)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom <[email protected]>
  • Loading branch information
ojeytonwilliams and moT01 authored Oct 18, 2023
1 parent 1ad87a0 commit ec51cbb
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions api-server/src/server/boot/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ function deleteUserTokenResponse(req, res) {
return res.send({ userToken: null });
}

export const getMsTranscriptApiUrl = msTranscript => {
// example msTranscriptUrl: https://learn.microsoft.com/en-us/users/mot01/transcript/8u6awert43q1plo
const url = new URL(msTranscript);

const transcriptUrlRegex = /\/transcript\/([^/]+)\/?/;
const id = transcriptUrlRegex.exec(url.pathname)?.[1];
return `https://learn.microsoft.com/api/profiles/transcript/share/${
id ?? ''
}`;
};

function createPostMsUsername(app) {
const { MsUsername } = app.models;

Expand All @@ -129,8 +140,7 @@ function createPostMsUsername(app) {
});
}

const msTranscriptId = msTranscriptUrl.split('/').pop();
const msTranscriptApiUrl = `https://learn.microsoft.com/api/profiles/transcript/share/${msTranscriptId}`;
const msTranscriptApiUrl = getMsTranscriptApiUrl(msTranscriptUrl);

try {
const msApiRes = await fetch(msTranscriptApiUrl);
Expand Down

0 comments on commit ec51cbb

Please sign in to comment.