|
| 1 | +import Joi from 'joi'; |
| 2 | +import { ConnectionArgs, PageInfo } from '../type'; |
| 3 | +import { Cursor } from './Cursor'; |
| 4 | +import { ConnectionArgsValidationError, CursorValidationError } from '../error'; |
| 5 | + |
| 6 | +export class OffsetCursor extends Cursor { |
| 7 | + public parameters: { |
| 8 | + offset: number; |
| 9 | + }; |
| 10 | + |
| 11 | + public static create(encodedString: string): OffsetCursor { |
| 12 | + const parameters = Cursor.decode(encodedString); |
| 13 | + |
| 14 | + // validate the cursor parameters match the schema we expect, this also converts data types |
| 15 | + const { error, value: validatedParameters } = Joi.validate( |
| 16 | + parameters, |
| 17 | + Joi.object({ |
| 18 | + offset: Joi.number() |
| 19 | + .integer() |
| 20 | + .min(0) |
| 21 | + .empty('') |
| 22 | + .required(), |
| 23 | + }).unknown(false), |
| 24 | + ); |
| 25 | + |
| 26 | + if (error != null) { |
| 27 | + const errorMessages = |
| 28 | + error.details != null ? error.details.map(detail => `- ${detail.message}`).join('\n') : `- ${error.message}`; |
| 29 | + |
| 30 | + throw new CursorValidationError( |
| 31 | + `A provided cursor value is not valid. The following problems were found:\n\n${errorMessages}`, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + return new OffsetCursor(validatedParameters); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export class OffsetCursorPaginator { |
| 40 | + public take: number = 20; |
| 41 | + public skip: number = 0; |
| 42 | + public totalEdges: number = 0; |
| 43 | + |
| 44 | + constructor({ take, skip, totalEdges }: Pick<OffsetCursorPaginator, 'take' | 'skip' | 'totalEdges'>) { |
| 45 | + this.take = take; |
| 46 | + this.skip = skip; |
| 47 | + this.totalEdges = totalEdges; |
| 48 | + } |
| 49 | + |
| 50 | + public createPageInfo(edgesInPage: number): PageInfo { |
| 51 | + return { |
| 52 | + startCursor: edgesInPage > 0 ? this.createCursor(0).encode() : null, |
| 53 | + endCursor: edgesInPage > 0 ? this.createCursor(edgesInPage - 1).encode() : null, |
| 54 | + hasNextPage: this.skip + edgesInPage < this.totalEdges, |
| 55 | + hasPreviousPage: this.skip > 0, |
| 56 | + totalEdges: this.totalEdges, |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + public createCursor(index: number): OffsetCursor { |
| 61 | + return new OffsetCursor({ offset: this.skip + index }); |
| 62 | + } |
| 63 | + |
| 64 | + public static createFromConnectionArgs( |
| 65 | + { first, last, before, after }: ConnectionArgs, |
| 66 | + totalEdges: number, |
| 67 | + ): OffsetCursorPaginator { |
| 68 | + let take: number = 20; |
| 69 | + let skip: number = 0; |
| 70 | + |
| 71 | + if (first != null) { |
| 72 | + if (first > 100 || first < 1) { |
| 73 | + throw new ConnectionArgsValidationError('The "first" argument accepts a value between 1 and 100, inclusive.'); |
| 74 | + } |
| 75 | + |
| 76 | + take = first; |
| 77 | + skip = 0; |
| 78 | + } |
| 79 | + |
| 80 | + if (last != null) { |
| 81 | + if (first != null) { |
| 82 | + throw new ConnectionArgsValidationError( |
| 83 | + 'It is not permitted to specify both "first" and "last" arguments simultaneously.', |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + if (last > 100 || last < 1) { |
| 88 | + throw new ConnectionArgsValidationError('The "last" argument accepts a value between 1 and 100, inclusive.'); |
| 89 | + } |
| 90 | + |
| 91 | + take = last; |
| 92 | + skip = totalEdges > last ? totalEdges - last : 0; |
| 93 | + } |
| 94 | + |
| 95 | + if (after != null) { |
| 96 | + if (last != null) { |
| 97 | + throw new ConnectionArgsValidationError( |
| 98 | + 'It is not permitted to specify both "last" and "after" arguments simultaneously.', |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + skip = OffsetCursor.create(after).parameters.offset + 1; |
| 103 | + } |
| 104 | + |
| 105 | + if (before != null) { |
| 106 | + throw new ConnectionArgsValidationError('This connection does not support the "before" argument for pagination.'); |
| 107 | + } |
| 108 | + |
| 109 | + return new OffsetCursorPaginator({ |
| 110 | + take, |
| 111 | + skip, |
| 112 | + totalEdges, |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
0 commit comments