website refactor
This commit is contained in:
@@ -42,11 +42,22 @@ export interface ParsedWizardParams {
|
||||
}
|
||||
|
||||
export class SearchParamParser {
|
||||
private static getParam(params: URLSearchParams | Record<string, string | string[] | undefined>, key: string): string | null {
|
||||
if (params instanceof URLSearchParams) {
|
||||
return params.get(key);
|
||||
}
|
||||
const value = params[key];
|
||||
if (Array.isArray(value)) {
|
||||
return value[0] ?? null;
|
||||
}
|
||||
return value ?? null;
|
||||
}
|
||||
|
||||
// Parse auth parameters
|
||||
static parseAuth(params: URLSearchParams): Result<ParsedAuthParams, string> {
|
||||
static parseAuth(params: URLSearchParams | Record<string, string | string[] | undefined>): Result<ParsedAuthParams, string> {
|
||||
const errors: string[] = [];
|
||||
|
||||
const returnTo = params.get('returnTo');
|
||||
const returnTo = this.getParam(params, 'returnTo');
|
||||
if (returnTo !== null) {
|
||||
const validation = SearchParamValidators.validateReturnTo(returnTo);
|
||||
if (!validation.isValid) {
|
||||
@@ -54,7 +65,7 @@ export class SearchParamParser {
|
||||
}
|
||||
}
|
||||
|
||||
const token = params.get('token');
|
||||
const token = this.getParam(params, 'token');
|
||||
if (token !== null) {
|
||||
const validation = SearchParamValidators.validateToken(token);
|
||||
if (!validation.isValid) {
|
||||
@@ -62,7 +73,7 @@ export class SearchParamParser {
|
||||
}
|
||||
}
|
||||
|
||||
const email = params.get('email');
|
||||
const email = this.getParam(params, 'email');
|
||||
if (email !== null) {
|
||||
const validation = SearchParamValidators.validateEmail(email);
|
||||
if (!validation.isValid) {
|
||||
@@ -75,19 +86,19 @@ export class SearchParamParser {
|
||||
}
|
||||
|
||||
return Result.ok({
|
||||
returnTo: params.get('returnTo'),
|
||||
token: params.get('token'),
|
||||
email: params.get('email'),
|
||||
error: params.get('error'),
|
||||
message: params.get('message'),
|
||||
returnTo: this.getParam(params, 'returnTo'),
|
||||
token: this.getParam(params, 'token'),
|
||||
email: this.getParam(params, 'email'),
|
||||
error: this.getParam(params, 'error'),
|
||||
message: this.getParam(params, 'message'),
|
||||
});
|
||||
}
|
||||
|
||||
// Parse sponsor parameters
|
||||
static parseSponsor(params: URLSearchParams): Result<ParsedSponsorParams, string> {
|
||||
static parseSponsor(params: URLSearchParams | Record<string, string | string[] | undefined>): Result<ParsedSponsorParams, string> {
|
||||
const errors: string[] = [];
|
||||
|
||||
const type = params.get('type');
|
||||
const type = this.getParam(params, 'type');
|
||||
if (type !== null) {
|
||||
const validation = SearchParamValidators.validateCampaignType(type);
|
||||
if (!validation.isValid) {
|
||||
@@ -100,17 +111,17 @@ export class SearchParamParser {
|
||||
}
|
||||
|
||||
return Result.ok({
|
||||
type: params.get('type'),
|
||||
campaignId: params.get('campaignId'),
|
||||
type: this.getParam(params, 'type'),
|
||||
campaignId: this.getParam(params, 'campaignId'),
|
||||
});
|
||||
}
|
||||
|
||||
// Parse pagination parameters
|
||||
static parsePagination(params: URLSearchParams): Result<ParsedPaginationParams, string> {
|
||||
static parsePagination(params: URLSearchParams | Record<string, string | string[] | undefined>): Result<ParsedPaginationParams, string> {
|
||||
const result: ParsedPaginationParams = {};
|
||||
const errors: string[] = [];
|
||||
|
||||
const page = params.get('page');
|
||||
const page = this.getParam(params, 'page');
|
||||
if (page !== null) {
|
||||
const validation = SearchParamValidators.validatePage(page);
|
||||
if (!validation.isValid) {
|
||||
@@ -120,7 +131,7 @@ export class SearchParamParser {
|
||||
}
|
||||
}
|
||||
|
||||
const limit = params.get('limit');
|
||||
const limit = this.getParam(params, 'limit');
|
||||
if (limit !== null) {
|
||||
const validation = SearchParamValidators.validateLimit(limit);
|
||||
if (!validation.isValid) {
|
||||
@@ -130,7 +141,7 @@ export class SearchParamParser {
|
||||
}
|
||||
}
|
||||
|
||||
const offset = params.get('offset');
|
||||
const offset = this.getParam(params, 'offset');
|
||||
if (offset !== null) {
|
||||
const num = parseInt(offset);
|
||||
if (!isNaN(num)) {
|
||||
@@ -146,10 +157,10 @@ export class SearchParamParser {
|
||||
}
|
||||
|
||||
// Parse sorting parameters
|
||||
static parseSorting(params: URLSearchParams): Result<ParsedSortingParams, string> {
|
||||
static parseSorting(params: URLSearchParams | Record<string, string | string[] | undefined>): Result<ParsedSortingParams, string> {
|
||||
const errors: string[] = [];
|
||||
|
||||
const order = params.get('order');
|
||||
const order = this.getParam(params, 'order');
|
||||
if (order !== null) {
|
||||
const validation = SearchParamValidators.validateOrder(order);
|
||||
if (!validation.isValid) {
|
||||
@@ -162,29 +173,29 @@ export class SearchParamParser {
|
||||
}
|
||||
|
||||
return Result.ok({
|
||||
sortBy: params.get('sortBy'),
|
||||
order: (params.get('order') as 'asc' | 'desc') || undefined,
|
||||
sortBy: this.getParam(params, 'sortBy'),
|
||||
order: (this.getParam(params, 'order') as 'asc' | 'desc') || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
// Parse filter parameters
|
||||
static parseFilters(params: URLSearchParams): Result<ParsedFilterParams, string> {
|
||||
static parseFilters(params: URLSearchParams | Record<string, string | string[] | undefined>): Result<ParsedFilterParams, string> {
|
||||
return Result.ok({
|
||||
status: params.get('status'),
|
||||
role: params.get('role'),
|
||||
tier: params.get('tier'),
|
||||
status: this.getParam(params, 'status'),
|
||||
role: this.getParam(params, 'role'),
|
||||
tier: this.getParam(params, 'tier'),
|
||||
});
|
||||
}
|
||||
|
||||
// Parse wizard parameters
|
||||
static parseWizard(params: URLSearchParams): Result<ParsedWizardParams, string> {
|
||||
static parseWizard(params: URLSearchParams | Record<string, string | string[] | undefined>): Result<ParsedWizardParams, string> {
|
||||
return Result.ok({
|
||||
step: params.get('step'),
|
||||
step: this.getParam(params, 'step'),
|
||||
});
|
||||
}
|
||||
|
||||
// Parse all parameters at once
|
||||
static parseAll(params: URLSearchParams): Result<
|
||||
static parseAll(params: URLSearchParams | Record<string, string | string[] | undefined>): Result<
|
||||
{
|
||||
auth: ParsedAuthParams;
|
||||
sponsor: ParsedSponsorParams;
|
||||
|
||||
Reference in New Issue
Block a user