-
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.
- Loading branch information
1 parent
500f2b3
commit de64cbd
Showing
8 changed files
with
202 additions
and
28 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Request } from 'express'; | ||
|
||
type FilterOperators = { | ||
equals?: any; | ||
in?: any[]; | ||
notIn?: any[]; | ||
contains?: string; | ||
startsWith?: string; | ||
endsWith?: string; | ||
mode?: 'insensitive'; | ||
lt?: number; | ||
lte?: number; | ||
gt?: number; | ||
gte?: number; | ||
}; | ||
|
||
export const hydrateQuery = (query: Request['query']) => { | ||
const filter: Record<string, FilterOperators> = {}; | ||
const orderBy: Record<string, 'asc' | 'desc'> = {}; | ||
|
||
// Process each query parameter | ||
Object.entries(query).forEach(([key, value]) => { | ||
// Normalize the value to string or string[] | ||
const normalizedValue = Array.isArray(value) | ||
? value.map(v => v.toString()) | ||
: value?.toString(); | ||
|
||
if (!normalizedValue) return; | ||
|
||
// Handle orderBy parameters | ||
if (key.startsWith('orderBy[')) { | ||
const field = key.slice(8, -1); | ||
orderBy[field] = normalizedValue as 'asc' | 'desc'; | ||
return; | ||
} | ||
|
||
// Rest of the function remains the same... | ||
const filterMatch = key.match(/^(\w+)\[(\w+)\]$/); | ||
if (filterMatch) { | ||
const [, field, operator] = filterMatch; | ||
if (!field || !operator) return; | ||
|
||
if (!filter[field]) { | ||
filter[field] = {}; | ||
} | ||
|
||
if (Array.isArray(normalizedValue)) { | ||
filter[field][operator] = normalizedValue.map(v => | ||
!isNaN(Number(v)) ? Number(v) : v | ||
); | ||
} else { | ||
filter[field][operator] = !isNaN(Number(normalizedValue)) | ||
? Number(normalizedValue) | ||
: normalizedValue; | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
...(Object.keys(filter).length > 0 && { where: filter }), | ||
...(Object.keys(orderBy).length > 0 && { orderBy }) | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Sort } from '../types/api'; | ||
|
||
type StringFilter = { | ||
equals?: string; | ||
in?: string[]; | ||
notIn?: string[]; | ||
contains?: string; | ||
startsWith?: string; | ||
endsWith?: string; | ||
mode?: 'insensitive'; | ||
}; | ||
|
||
type IntFilter = { | ||
equals?: number; | ||
in?: number[]; | ||
notIn?: number[]; | ||
lt?: number; | ||
lte?: number; | ||
gt?: number; | ||
gte?: number; | ||
}; | ||
|
||
type ProductFilter = { | ||
name?: StringFilter; | ||
description?: StringFilter; | ||
unitPrice?: IntFilter; | ||
quantity?: IntFilter; | ||
}; | ||
|
||
type ProductOrderBy = { | ||
createdAt?: Sort; | ||
updatedAt?: Sort; | ||
unitPrice?: Sort; | ||
}; | ||
|
||
type OrderFilter = { | ||
total?: IntFilter; | ||
}; | ||
|
||
type OrderOrderBy = { | ||
total?: Sort; | ||
createdAt?: Sort; | ||
updatedAt?: Sort; | ||
}; | ||
|
||
export const buildQuery = < | ||
T extends Record<string, any>, | ||
U extends Record<string, Sort> | ||
>({ | ||
filter, | ||
orderBy | ||
}: { | ||
filter?: T; | ||
orderBy?: U; | ||
}) => { | ||
const params = new URLSearchParams(); | ||
|
||
// Handle filters | ||
if (filter) { | ||
Object.entries(filter).forEach(([field, conditions]) => { | ||
Object.entries(conditions || {}).forEach(([operator, value]) => { | ||
if (value !== undefined) { | ||
if (Array.isArray(value)) { | ||
value.forEach(v => | ||
params.append(`${field}[${operator}]`, v.toString()) | ||
); | ||
} else { | ||
params.append(`${field}[${operator}]`, value.toString()); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Handle ordering | ||
if (orderBy) { | ||
Object.entries(orderBy).forEach(([field, direction]) => { | ||
if (direction) { | ||
params.append(`orderBy[${field}]`, direction); | ||
} | ||
}); | ||
} | ||
|
||
return params.toString(); | ||
}; | ||
|
||
export const buildProductQuery = (params: { | ||
filter?: ProductFilter; | ||
orderBy?: ProductOrderBy; | ||
}) => buildQuery(params); | ||
|
||
export const buildOrderQuery = (params: { | ||
filter?: OrderFilter; | ||
orderBy?: OrderOrderBy; | ||
}) => buildQuery(params); |
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