catalog-client: make paginated response serializable
Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
@@ -128,7 +128,6 @@ export class CatalogClient implements CatalogApi {
|
||||
locationRef: string,
|
||||
options?: CatalogRequestOptions,
|
||||
): Promise<Location_2 | undefined>;
|
||||
// @alpha
|
||||
getPaginatedEntities?(
|
||||
request?: GetPaginatedEntitiesRequest,
|
||||
options?: CatalogRequestOptions,
|
||||
@@ -267,12 +266,8 @@ export type GetPaginatedEntitiesRequest =
|
||||
export type GetPaginatedEntitiesResponse = {
|
||||
entities: Entity[];
|
||||
totalItems: number;
|
||||
next?(
|
||||
request?: Omit<GetPaginatedEntitiesCursorRequest, 'cursor'>,
|
||||
): Promise<GetPaginatedEntitiesResponse>;
|
||||
prev?(
|
||||
request?: Omit<GetPaginatedEntitiesCursorRequest, 'cursor'>,
|
||||
): Promise<GetPaginatedEntitiesResponse>;
|
||||
nextCursor?: string;
|
||||
prevCursor?: string;
|
||||
};
|
||||
|
||||
// @public
|
||||
|
||||
@@ -310,8 +310,8 @@ describe('CatalogClient', () => {
|
||||
const response = await client.getPaginatedEntities?.({}, { token });
|
||||
expect(response?.entities).toEqual(defaultClientResponse.entities);
|
||||
expect(response?.totalItems).toEqual(defaultClientResponse.totalItems);
|
||||
expect(response?.next).toBeDefined();
|
||||
expect(response?.prev).toBeDefined();
|
||||
expect(response?.nextCursor).toBeDefined();
|
||||
expect(response?.prevCursor).toBeDefined();
|
||||
});
|
||||
|
||||
it('builds multiple entity search filters properly', async () => {
|
||||
@@ -429,15 +429,15 @@ describe('CatalogClient', () => {
|
||||
});
|
||||
expect(mockedEndpoint.mock.calls[0][0].url.search).toBe('?limit=2');
|
||||
|
||||
expect(response?.next).toBeDefined();
|
||||
expect(response?.prev).toBeDefined();
|
||||
expect(response?.nextCursor).toBeDefined();
|
||||
expect(response?.prevCursor).toBeDefined();
|
||||
|
||||
await response?.next?.();
|
||||
await client.getPaginatedEntities?.({ cursor: response!.nextCursor! });
|
||||
expect(mockedEndpoint.mock.calls[1][0].url.search).toBe(
|
||||
'?cursor=nextcursor',
|
||||
);
|
||||
|
||||
await response?.prev?.();
|
||||
await client.getPaginatedEntities?.({ cursor: response!.prevCursor! });
|
||||
expect(mockedEndpoint.mock.calls[2][0].url.search).toBe(
|
||||
'?cursor=prevcursor',
|
||||
);
|
||||
|
||||
@@ -42,7 +42,6 @@ import {
|
||||
GetPaginatedEntitiesRequest,
|
||||
GetPaginatedEntitiesResponse,
|
||||
EntityFilterQuery,
|
||||
GetPaginatedEntitiesCursorRequest,
|
||||
} from './types/api';
|
||||
import { DiscoveryApi } from './types/discovery';
|
||||
import { FetchApi } from './types/fetch';
|
||||
@@ -247,18 +246,12 @@ export class CatalogClient implements CatalogApi {
|
||||
}
|
||||
|
||||
const query = params.length ? `?${params.join('&')}` : '';
|
||||
const { entities, totalItems, nextCursor, prevCursor } =
|
||||
await this.requestRequired<{
|
||||
entities: Entity[];
|
||||
totalItems: number;
|
||||
nextCursor?: string;
|
||||
prevCursor?: string;
|
||||
}>('GET', `/v2beta1/entities${query}`, options);
|
||||
|
||||
const next = this.getEntitiesFromCursor(nextCursor, options);
|
||||
const prev = this.getEntitiesFromCursor(prevCursor, options);
|
||||
|
||||
return { entities, totalItems, next, prev };
|
||||
return this.requestRequired<{
|
||||
entities: Entity[];
|
||||
totalItems: number;
|
||||
nextCursor?: string;
|
||||
prevCursor?: string;
|
||||
}>('GET', `/v2beta1/entities${query}`, options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -546,15 +539,4 @@ export class CatalogClient implements CatalogApi {
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
private getEntitiesFromCursor(
|
||||
cursor: string | undefined,
|
||||
options?: CatalogRequestOptions,
|
||||
) {
|
||||
if (!cursor) {
|
||||
return undefined;
|
||||
}
|
||||
return (request: Omit<GetPaginatedEntitiesCursorRequest, 'cursor'> = {}) =>
|
||||
this.getPaginatedEntities!({ ...request, cursor }, options);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -427,14 +427,10 @@ export type GetPaginatedEntitiesResponse = {
|
||||
entities: Entity[];
|
||||
/* The number of entities among all the requests */
|
||||
totalItems: number;
|
||||
/* A method returning a promise containing the next batch of entities. */
|
||||
next?(
|
||||
request?: Omit<GetPaginatedEntitiesCursorRequest, 'cursor'>,
|
||||
): Promise<GetPaginatedEntitiesResponse>;
|
||||
/* A method returning a promise containing the previous batch of entities. */
|
||||
prev?(
|
||||
request?: Omit<GetPaginatedEntitiesCursorRequest, 'cursor'>,
|
||||
): Promise<GetPaginatedEntitiesResponse>;
|
||||
/* The cursor for the next batch of entities */
|
||||
nextCursor?: string;
|
||||
/* The cursor for the previous batch of entities */
|
||||
prevCursor?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user