import { IQueryParameter } from '@kentico/kontent-core' ; import { SortOrder } from './sort-order' ; export namespace Parameters { const defaultValue = '' ; export class QueryParameter implements IQueryParameter { constructor param { string } name param { string } value constructor ( public name , public value ) { if ( ! name ) { throw Error ( `Name of the parameter is not specified` ) ; } } getParam ( ) { return `${ this . name }=${ this . value }` ; } } export class ElementsParameter implements IQueryParameter { constructor param { string [ ] } elementCodenames constructor ( public elementCodenames ) { } getParam ( ) { return `elements=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { if ( ! this . elementCodenames ) { return defaultValue ; } return this . elementCodenames . map ( ( m ) => { if ( ! m ) { throw Error ( `Codename of 'ElementsParameter' cannot be null or empty` ) ; } return m . trim ( ) ; } ) . join ( ',' ) ; } } export class LimitParameter implements IQueryParameter { constructor param { number } limit constructor ( public limit ) { if ( limit <= 0 ) { throw Error ( `'LimitParameter' must specify a positive integer` ) ; } } getParam ( ) { return `limit=${ this . limit }` ; } } export class IncludeTotalCountParameter implements IQueryParameter { constructor constructor ( ) { } getParam ( ) { return `includeTotalCount=true` ; } } export class SkipParameter implements IQueryParameter { constructor param { number } skip constructor ( public skip ) { if ( skip < 0 ) { throw Error ( `'SkipParameter' must specify a positive integer number or zero."` ) ; } } getParam ( ) { return `skip=${ this . skip }` ; } } export class OrderParameter implements IQueryParameter { constructor param { string } element param { SortOrder } sortOrder constructor ( public element , public sortOrder ) { if ( ! element ) { throw Error ( `Element specified in 'OrderParameter' is null or empty` ) ; } } getParam ( ) { return `order=${ this . getParamValue ( ) }` ; } private getParamValue ( ) { return `${ this . element . trim ( ) }[${ this . sortOrder }]` ; } } export class CustomParameter implements IQueryParameter { constructor ( public param ) { } getParam ( ) { return this . param ; } } export class DepthParameter implements IQueryParameter { constructor param { number } depth constructor ( public depth ) { if ( depth < 0 ) { throw Error ( `'DepthParameter' must specify a positive integer or zero` ) ; } } getParam ( ) { return `depth=${ this . depth }` ; } } export class LanguageParameter implements IQueryParameter { constructor param { string } languageCodename constructor ( public languageCodename ) { if ( ! languageCodename ) { throw Error ( `'LanguageParameter' must specify codename of the language` ) ; } } getParam ( ) { return `language=${ this . languageCodename }` ; } } }