Skip to content

Commit

Permalink
feat(middleware): allow navigate to next/prev pages
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Aug 4, 2024
1 parent ee5ac0c commit b89cf5b
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 50 deletions.
12 changes: 11 additions & 1 deletion src/Middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OperationOptions } from '@azure/core-client';
import { OperationArguments, OperationOptions, OperationSpec } from '@azure/core-client';
import { userAgentPolicyName, setClientRequestIdPolicyName } from '@azure/core-rest-pipeline';
import {
genRequestQueuesPolicy, genCombineGetRequestsPolicy, genErrorFormatterPolicy,
Expand All @@ -7,6 +7,7 @@ import {
import { Middleware as MiddlewareApi, MiddlewareOptionalParams, ErrorResponse } from './apis/middleware';
import { operationSpecs } from './apis/middleware/middleware';
import { IllegalArgumentError, InternalError } from './utils/errors';
import { MiddlewarePage, isMiddlewareRawPage } from './utils/MiddlewarePage';

export default class Middleware extends MiddlewareApi {
/**
Expand Down Expand Up @@ -97,4 +98,13 @@ export default class Middleware extends MiddlewareApi {
})),
});
}

override async sendOperationRequest<T>(
operationArguments: OperationArguments,
operationSpec: OperationSpec,
): Promise<T> {
const response = await super.sendOperationRequest(operationArguments, operationSpec);
if (!isMiddlewareRawPage(response)) return response as T;
return new MiddlewarePage(response, this) as T;
}
}
1 change: 1 addition & 0 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export {
default as MiddlewareSubscriber, MiddlewareSubscriberError, MiddlewareSubscriberDisconnected,
} from './MiddlewareSubscriber';
export { default as Middleware } from './Middleware';
export { MiddlewarePageMissed } from './utils/MiddlewarePage';

export { default as connectionProxy } from './aepp-wallet-communication/connection-proxy';
export {
Expand Down
66 changes: 66 additions & 0 deletions src/utils/MiddlewarePage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable max-classes-per-file */
import type Middleware from '../Middleware';
import { BaseError } from './errors';

export interface MiddlewareRawPage {
data: unknown[];
next: string | null;
prev: string | null;
}

export function isMiddlewareRawPage(maybePage: unknown): maybePage is MiddlewareRawPage {
const testPage = maybePage as MiddlewareRawPage;
return testPage?.data != null && Array.isArray(testPage.data)
&& 'next' in testPage
&& 'prev' in testPage;
}

/**
* @category exception
*/
export class MiddlewarePageMissed extends BaseError {
constructor(isNext: boolean) {
super(`There is no ${isNext ? 'next' : 'previous'} page`);
this.name = 'MiddlewarePageMissed';
}
}

/**
* A wrapper around the middleware's page allowing to get the next/previous pages.
*/
export class MiddlewarePage<Item> {
readonly data: Item[];

readonly nextPath: null | string;

readonly prevPath: null | string;

readonly #middleware: Middleware;

constructor(rawPage: MiddlewareRawPage, middleware: Middleware) {
this.data = rawPage.data as Item[];
this.nextPath = rawPage.next;
this.prevPath = rawPage.prev;
this.#middleware = middleware;
}

/**
* Get the next page.
* Check the presence of `nextPath` to not fall outside existing pages.
* @throws MiddlewarePageMissed
*/
async next(): Promise<MiddlewarePage<Item>> {
if (this.nextPath == null) throw new MiddlewarePageMissed(true);
return this.#middleware.requestByPath(this.nextPath);
}

/**
* Get the previous page.
* Check the presence of `prevPath` to not fall outside existing pages.
* @throws MiddlewarePageMissed
*/
async prev(): Promise<MiddlewarePage<Item>> {
if (this.prevPath == null) throw new MiddlewarePageMissed(false);
return this.#middleware.requestByPath(this.prevPath);
}
}
Loading

0 comments on commit b89cf5b

Please sign in to comment.