HydraParser.ts 852 B

12345678910111213141516171819202122232425262728
  1. class HydraParser {
  2. static parseCollection<T>(data: any, normalize: ((s: object) => T)): HydraCollection<T> {
  3. const view = data['hydra:view'] as any
  4. return {
  5. iri: data['@id'] as string,
  6. totalItems: parseInt(data['hydra:totalItems']) ?? 0,
  7. page: this.getPageFromIri(view['@id'] ?? ''),
  8. previousPage: this.getPageFromIri(view['hydra:previous'] ?? ''),
  9. nextPage: this.getPageFromIri(view['hydra:next'] ?? ''),
  10. firstPage: this.getPageFromIri(view['hydra:first'] ?? ''),
  11. lastPage: this.getPageFromIri(view['hydra:last'] ?? ''),
  12. items: data['hydra:member'].map((s: any) => { return normalize(s) }),
  13. }
  14. }
  15. private static getPageFromIri(iri: string): number | null {
  16. const match = iri.match(/page=(\d+)/)
  17. return match ? parseInt(match[1]) : null;
  18. }
  19. }
  20. export default HydraParser