-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): allow navigate to next/prev pages
- Loading branch information
Showing
5 changed files
with
189 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.