| 12345678910111213141516171819202122232425262728 |
- class HydraParser {
- static parseCollection<T>(data: any, normalize: ((s: object) => T)): HydraCollection<T> {
- const view = data['hydra:view'] as any
- return {
- iri: data['@id'] as string,
- totalItems: parseInt(data['hydra:totalItems']) ?? 0,
- page: this.getPageFromIri(view['@id'] ?? ''),
- previousPage: this.getPageFromIri(view['hydra:previous'] ?? ''),
- nextPage: this.getPageFromIri(view['hydra:next'] ?? ''),
- firstPage: this.getPageFromIri(view['hydra:first'] ?? ''),
- lastPage: this.getPageFromIri(view['hydra:last'] ?? ''),
- items: data['hydra:member'].map((s: any) => { return normalize(s) }),
- }
- }
- private static getPageFromIri(iri: string): number | null {
- const match = iri.match(/page=(\d+)/)
- return match ? parseInt(match[1]) : null;
- }
- }
- export default HydraParser
|