- Created by Obinna Anaenugwu , last modified on Sept 23, 2024
You are viewing an old version of this page. View the current version.
Compare with Current View Page History
« Previous Version 4 Current »
The code then makes a GET request to the
/api/course
API endpoint using thefetchWithCache
function. This function appears to cache requests to reduce unnecessary API calls. It sends a query with the course'sprefix
andnumber
as URL parameters (after encoding them for safety), for example,/api/course?prefix=CS&number=101
.The
fetchWithCache
function takes in three main arguments:The URL with query parameters.
cacheIndexNebula
: Likely an identifier for the cache.expireTime
: Defines the time the cache expires.A configuration object specifying the request method (
GET
) and the accepted response type (application/json
).
The first
.then()
block checks the response to ensure the API call was successful. If themessage
in the response is not'success'
, an error is thrown.If the response is successful, the next
.then()
block processes the actual data (CourseData[]
), which is an array of course data.The array of course data is sorted by the
catalog_year
property in descending order. This ensures that the most recent course information is placed at the beginning of the array (index0
).It then sets the
courseData
state to{ state: 'done', data: response[0] }
, whereresponse[0]
is the most recent course data.
The code then makes a GET request to the
/api/professor
API endpoint using thefetchWithCache
function. This function appears to cache requests to reduce unnecessary API calls. It sends a query with the ProfessorFirst
andLast
name as URL parameters (after encoding them for safety), for example,/api/professor?profFirst=John &profLast=Cole
.The
fetchWithCache
function takes in three main arguments:The URL with query parameters.
cacheIndexNebula
: Likely an identifier for the cache.expireTime
: Defines the time the cache expires.A configuration object specifying the request method (
GET
) and the accepted response type (application/json
).
The first
.then()
block checks the response to ensure the API call was successful. If themessage
in the response is not'success'
, an error is thrown.The state
profData
is then updated to:{ state: 'done', data: response.data }
ifresponse.data
is defined (indicating that professor data was successfully fetched).data
: The actual professor data fetched from the API, which is cast to aProfessorInterface
type.This part assigns the fetched professor data (
response.data
) to thedata
property.The
as ProfessorInterface
part is a TypeScript type assertion, which explicitly tells TypeScript to treatresponse.data
as if it conforms to theProfessorInterface
type. This ensures the data structure aligns with the expected format for a professor.
OutStanding API’s
The
combosSearchResultsFetch
function is responsible for fetching search results related to course-professor combinations based on a search term, and it returns a list ofSearchQuery
objects.searchTerm: SearchQuery
: This is the input provided by the user or system, containing search terms like a professor's name or course details.controller: AbortController
: This is used to cancel the request if necessary, such as when a user navigates away before the data is fetched.
The function returns a
Promise<SearchQuery[]>
, meaning it will eventually return an array ofSearchQuery
objects once the API request completes.The function calls
fetchWithCache
, a custom function to handle API requests with caching. The API endpoint/api/combo
is queried with a single query parameter,input
, derived fromsearchQueryLabel(searchTerm)
. This likely formats or encodes the search term for use in the URL.The caching details (
cacheIndexNebula
andexpireTime
) are passed to help manage cached responses.The configuration object for the API request includes:
signal: controller.signal
: To handle the request cancellation.method: 'GET'
: A GET request to retrieve data from the API.headers: { Accept: 'application/json' }
: The server is informed that the client expects a JSON response.
After the API request completes, the
.then()
block processes the response:It first checks if
response.message
is'success'
. If it's anything else, the function throws an error with the message, which is handled by the calling code.If the response is successful, it processes the
response.data
(which is likely an array of course-professor combinations).
The result is an array of
SearchQuery
objects, which is constructed as follows:The original
searchTerm
object is added as the first element of the array.The rest of the array is built by mapping over the
response.data
, combining eachobj
(each course-professor combination) with thesearchTerm
. Each resulting object is created using the spread syntax (...
) to merge the properties ofsearchTerm
andobj
into a newSearchQuery
object.
course: SearchQuery
: This is an object representing a course with various properties (such ascourse.prefix
,course.number
, etc.).controller: AbortController
: This allows the request to be aborted if necessary (e.g., when the user navigates away from the page before the request completes).The function returns a
Promise<GradesType>
, meaning it returns a promise that resolves to an object of typeGradesType
after fetching and processing the data.The function makes an API request using
fetchWithCache
, which is likely a custom function to handle fetching data with caching support.The URL
/api/grades
is built dynamically by constructing query parameters from thecourse
object:Object.keys(course)
retrieves all keys from thecourse
object..map()
iterates over each key, creating a query string parameter in the form ofkey=value
. TheencodeURIComponent()
function ensures that special characters are properly encoded in the URL.The final URL string contains all key-value pairs joined by
&
, which is how query parameters are structured in URLs.
The
cacheIndexNebula
andexpireTime
are likely cache management variables that help control caching behavior for the request.The configuration object includes:
signal: controller.signal
: Allows the request to be aborted if needed.method: 'GET'
: Specifies that this is a GET request.headers: { Accept: 'application/json' }
: Tells the server that the client expects a JSON response.
The
.then()
block processes the response:First, it checks if the
response.message
is'success'
. If not, it throws an error with the message, which will be caught by the calling code.If
response.data
isnull
, it throws an error, indicating that no data was returned from the API.
If the response is valid, it returns an object that combines:
The result of
calculateGrades(response.data)
, which likely computes various statistics or calculations based on the fetched grade data. This could involve calculating averages, GPAs, or other metrics.The original
response.data
under thegrades
property (this is of typeGradesData
, likely containing raw grade data).
professor: SearchQuery
: This is an object representing the professor, containing fields such asprofFirst
(first name) andprofLast
(last name).controller: AbortController
: This enables cancellation of the request if needed. This is useful in situations where the user navigates away or the request needs to be aborted for any other reason.The function returns a
Promise<RateMyProfessorData>
, meaning the function returns a promise that resolves with the fetched RateMyProfessor data for the professor. If the request fails, the promise is rejected.The API request is made using
fetchWithCache
, which is a custom method that handles fetching and caching data.The request is sent to the
/api/ratemyprofessorScraper
endpoint with two query parameters:profFirst
: The professor's first name, which is passed throughencodeURIComponent
to ensure the name is safe to include in the URL.profLast
: The professor's last name, also URL-encoded.
The request is cached using
cacheIndexRmp
andexpireTime
, which likely manage how long the data remains in the cache.The request configuration includes:
signal: controller.signal
: This allows the request to be aborted using the controller.method: 'GET'
: Specifies a GET request to fetch data.headers: { Accept: 'application/json' }
: Tells the server that the client expects a JSON response.
Once the API request is completed, the
.then()
block processes the response:It checks if the
response.message
is'success'
. If the response message is anything else, it throws an error containing the message, which will be handled by the calling code.If the response is successful, the function returns
response.data
, which is the RateMyProfessor data for the professor.
- No labels