-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from hearchco/as/fix/timing
fix: switch to universal page load and use only scraped timing
- Loading branch information
Showing
6 changed files
with
88 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,88 @@ | ||
import { browser } from '$app/environment'; | ||
import { error } from '@sveltejs/kit'; | ||
|
||
import { getCategoryFromQuery, getQueryWithoutCategory } from '$lib/functions/query/category'; | ||
import { concatSearchParams } from '$lib/functions/api/concatparams'; | ||
import { fetchResults } from '$lib/functions/api/fetchresults'; | ||
|
||
import { CategoryEnum, toCategoryType } from '$lib/types/search/category'; | ||
|
||
/** | ||
* Get category from URL or query | ||
* @param {string} query - Query from URL | ||
* @param {URLSearchParams} params - Parameters | ||
* @returns {string} - Category | ||
* @throws {Error} - If category is invalid | ||
*/ | ||
function getCategory(query, params) { | ||
const categoryFromQuery = getCategoryFromQuery(query); | ||
const categoryParam = params.get('category') ?? ''; | ||
const category = | ||
categoryFromQuery !== '' | ||
? toCategoryType(categoryFromQuery) | ||
: categoryParam !== '' | ||
? toCategoryType(categoryParam) | ||
: CategoryEnum.GENERAL; | ||
|
||
// Check if category is valid | ||
if (!category) { | ||
// Bad Request | ||
throw error(400, "Invalid 'category' parameter in URL or query"); | ||
} | ||
|
||
return category; | ||
} | ||
|
||
/** @type {import('./$types').PageLoad} */ | ||
export async function load({ data }) { | ||
export async function load({ url, fetch }) { | ||
// Get query, current page, and max pages from URL | ||
const query = url.searchParams.get('q') ?? ''; | ||
const currentPage = parseInt(url.searchParams.get('start') ?? '1', 10); | ||
const maxPages = parseInt(url.searchParams.get('pages') ?? '1', 10); | ||
|
||
// Validate query, current page, and max pages | ||
if (query === '') { | ||
// Bad Request | ||
throw error(400, "Missing 'q' parameter"); | ||
} | ||
if (isNaN(currentPage) || currentPage <= 0) { | ||
// Bad Request | ||
throw error(400, "Invalid 'start' parameter"); | ||
} | ||
if (isNaN(maxPages) || maxPages <= 0) { | ||
// Bad Request | ||
throw error(400, "Invalid 'pages' parameter"); | ||
} | ||
|
||
// Get category from URL or query (query takes precedence) | ||
const category = getCategory(query, url.searchParams); | ||
|
||
// Remove category from query if it exists | ||
const queryWithoutCategory = getQueryWithoutCategory(query); | ||
if (queryWithoutCategory === '') { | ||
// Bad Request | ||
throw error(400, "Only category specified in 'q' parameter"); | ||
} | ||
|
||
// Concatenate search params | ||
const newSearchParams = concatSearchParams({ | ||
q: queryWithoutCategory, | ||
category: category !== CategoryEnum.GENERAL ? category : '', | ||
start: currentPage !== 1 ? currentPage.toString() : '', | ||
pages: maxPages !== 1 ? maxPages.toString() : '' | ||
}); | ||
|
||
// Fetch results | ||
const resp = await fetchResults(newSearchParams, fetch); | ||
|
||
return { | ||
browser: browser, | ||
query: data.query, | ||
currentPage: data.currentPage, | ||
maxPages: data.maxPages, | ||
category: data.category, | ||
results: data.results, | ||
timing: data.timing | ||
apiVersion: resp.version, | ||
query: queryWithoutCategory, | ||
currentPage: currentPage, | ||
maxPages: maxPages, | ||
category: category, | ||
results: resp.results, | ||
duration: resp.duration | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters