Skip to content

Commit

Permalink
Adding docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Aug 10, 2020
1 parent afd29e6 commit 97a3afe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { eventSequence } from '../../../../../common/endpoint/models/event';
import { JsonObject } from '../../../../../../../../src/plugins/kibana_utils/common';
import { urlEncodeCursor, SortFields, urlDecodeCursor } from './pagination';

/**
* Pagination information for the children class.
*/
export interface ChildrenPaginationCursor {
timestamp: number;
sequence: number;
Expand Down Expand Up @@ -43,6 +46,11 @@ export class ChildrenPaginationBuilder {
private readonly sequence?: number
) {}

/**
* This function validates that the parsed cursor is a ChildrenPaginationCursor.
*
* @param parsed an object parsed from an encoded cursor.
*/
static decode(
parsed: ChildrenPaginationCursor | undefined
): ChildrenPaginationCursor | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ import { eventId } from '../../../../../common/endpoint/models/event';
import { JsonObject } from '../../../../../../../../src/plugins/kibana_utils/common';
import { ChildrenPaginationCursor } from './children_pagination';

type SearchAfterFields = [number, string];

interface PaginationCursor {
timestamp: number;
eventID: string;
}

/**
* Defines the sorting fields for queries that leverage pagination
*/
export type SortFields = [
{
'@timestamp': string;
},
{ [x: string]: string }
];

/**
* Defines a type of a function used to convert the parsed json data into a typescript type.
* If the function fails to transform the data it should return undefined.
*/
export type Decoder<T> = (parsed: T | undefined) => T | undefined;

type SearchAfterFields = [number, string];

interface PaginationCursor {
timestamp: number;
eventID: string;
}

/**
* Interface for defining the returned pagination information.
*/
Expand All @@ -34,6 +41,11 @@ export interface PaginationFields {
searchAfter?: SearchAfterFields;
}

/**
* A function to encode a cursor from a pagination object.
*
* @param data Transforms a pagination cursor into a base64 encoded string
*/
export function urlEncodeCursor(data: PaginationCursor | ChildrenPaginationCursor): string {
const value = JSON.stringify(data);
return Buffer.from(value, 'utf8')
Expand All @@ -43,6 +55,12 @@ export function urlEncodeCursor(data: PaginationCursor | ChildrenPaginationCurso
.replace(/=+$/g, '');
}

/**
* A function to decode a cursor.
*
* @param cursor a cursor encoded by the `urlEncodeCursor` function
* @param decode a function to transform the parsed data into an actual type
*/
export function urlDecodeCursor<T>(cursor: string, decode: Decoder<T>): T | undefined {
const fixedCursor = cursor.replace(/\-/g, '+').replace(/_/g, '/');
const data = Buffer.from(fixedCursor, 'base64').toString('utf8');
Expand Down Expand Up @@ -76,6 +94,11 @@ export class PaginationBuilder {
private readonly eventID?: string
) {}

/**
* Validates that the parsed object is actually a PaginationCursor.
*
* @param parsed an object parsed from an encoded cursor.
*/
static decode(parsed: PaginationCursor | undefined): PaginationCursor | undefined {
if (parsed && parsed.timestamp && parsed.eventID) {
const { timestamp, eventID } = parsed;
Expand Down

0 comments on commit 97a3afe

Please sign in to comment.