refactor(books): frontend book query service and pagination basics (#2094)
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import {
|
||||
BrowseFacetGroup,
|
||||
BrowseFacetValue,
|
||||
BrowseLink,
|
||||
BrowsePage,
|
||||
BrowsePageMetadata,
|
||||
} from './browse.models';
|
||||
|
||||
interface RawLink {
|
||||
rel: string | string[];
|
||||
href: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
interface RawBrowsePage<T> {
|
||||
content: T[];
|
||||
page: BrowsePageMetadata;
|
||||
links: RawLink[];
|
||||
}
|
||||
|
||||
interface RawFacetLink extends RawLink {
|
||||
title: string;
|
||||
value: string;
|
||||
properties?: {numberOfItems?: number};
|
||||
}
|
||||
|
||||
interface RawFacetGroup {
|
||||
metadata: {rel: string; key: string; title: string};
|
||||
links: RawFacetLink[];
|
||||
}
|
||||
|
||||
interface RawFacetResponse {
|
||||
facets: RawFacetGroup[];
|
||||
}
|
||||
|
||||
export function mapBrowsePage<T>(response: RawBrowsePage<T>): BrowsePage<T> {
|
||||
return {
|
||||
content: response.content,
|
||||
page: response.page,
|
||||
links: response.links.map(mapBrowseLink),
|
||||
};
|
||||
}
|
||||
|
||||
export function mapBrowseFacetGroups(response: RawFacetResponse): BrowseFacetGroup[] {
|
||||
return response.facets.map(group => ({
|
||||
rel: group.metadata.rel,
|
||||
key: group.metadata.key,
|
||||
title: group.metadata.title,
|
||||
values: group.links.map(mapBrowseFacetValue),
|
||||
}));
|
||||
}
|
||||
|
||||
function mapBrowseLink(raw: RawLink): BrowseLink {
|
||||
return {
|
||||
rel: normalizeRel(raw.rel),
|
||||
href: raw.href,
|
||||
type: raw.type,
|
||||
};
|
||||
}
|
||||
|
||||
function mapBrowseFacetValue(raw: RawFacetLink): BrowseFacetValue {
|
||||
const count = raw.properties?.numberOfItems;
|
||||
return {
|
||||
value: raw.value,
|
||||
title: raw.title,
|
||||
selected: normalizeRel(raw.rel).includes('self'),
|
||||
...(count === undefined ? {} : {count}),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeRel(rel: string | string[]): string[] {
|
||||
return Array.isArray(rel) ? rel : [rel];
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import {describe, expect, it} from 'vitest';
|
||||
|
||||
import {BrowsePage, findBrowsePageLink} from './browse.models';
|
||||
|
||||
describe('browse models', () => {
|
||||
it('finds page links by an array relation', () => {
|
||||
const page: BrowsePage<string> = {
|
||||
content: [],
|
||||
page: {
|
||||
number: 0,
|
||||
size: 20,
|
||||
totalElements: 0,
|
||||
totalPages: 0,
|
||||
cursor: 'opaque-cursor',
|
||||
},
|
||||
links: [
|
||||
{rel: ['self'], href: '/api/v1/items/page', type: 'application/json'},
|
||||
{rel: ['next', 'collection'], href: '/api/v1/items/page?cursor=next', type: 'application/json'},
|
||||
],
|
||||
};
|
||||
|
||||
expect(findBrowsePageLink(page, 'next')?.href).toBe('/api/v1/items/page?cursor=next');
|
||||
expect(findBrowsePageLink(page, 'previous')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
export interface BrowseLink {
|
||||
rel: string[];
|
||||
href: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface BrowsePageMetadata {
|
||||
number: number;
|
||||
size: number;
|
||||
totalElements: number;
|
||||
totalPages: number;
|
||||
cursor: string;
|
||||
}
|
||||
|
||||
export interface BrowsePage<T> {
|
||||
content: T[];
|
||||
page: BrowsePageMetadata;
|
||||
links: BrowseLink[];
|
||||
}
|
||||
|
||||
export interface BrowseFacetValue {
|
||||
value: string;
|
||||
title: string;
|
||||
count?: number;
|
||||
selected: boolean;
|
||||
}
|
||||
|
||||
export interface BrowseFacetGroup {
|
||||
rel: string;
|
||||
key: string;
|
||||
title: string;
|
||||
values: BrowseFacetValue[];
|
||||
}
|
||||
|
||||
export type BrowseFacetLogic = 'and' | 'or' | 'not';
|
||||
export type BrowseSortDirection = 'asc' | 'desc';
|
||||
|
||||
export interface BrowseSortTerm<Key extends string = string> {
|
||||
key: Key;
|
||||
direction: BrowseSortDirection;
|
||||
}
|
||||
|
||||
export function findBrowsePageLink(
|
||||
page: BrowsePage<unknown>,
|
||||
rel: string,
|
||||
): BrowseLink | undefined {
|
||||
return page.links.find(link => link.rel.includes(rel));
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import {HttpErrorResponse} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
|
||||
export function retryTransientQueryError(failureCount: number, error: unknown): boolean {
|
||||
if (failureCount >= 2) {
|
||||
return false;
|
||||
}
|
||||
return error instanceof HttpErrorResponse && (error.status === 0 || error.status >= 500);
|
||||
}
|
||||
|
||||
export const QUERY_DEFAULTS = {
|
||||
staleTime: 30_000,
|
||||
retry: retryTransientQueryError,
|
||||
} as const;
|
||||
|
||||
export function abortSignal(signal: AbortSignal): Observable<void> {
|
||||
return new Observable(subscriber => {
|
||||
if (signal.aborted) {
|
||||
subscriber.next();
|
||||
subscriber.complete();
|
||||
return;
|
||||
}
|
||||
const onAbort = () => {
|
||||
subscriber.next();
|
||||
subscriber.complete();
|
||||
};
|
||||
signal.addEventListener('abort', onAbort, {once: true});
|
||||
return () => signal.removeEventListener('abort', onAbort);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user