]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/rest/rest-data-source.ts
Better typescript typing for a better world
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / rest / rest-data-source.ts
CommitLineData
df98563e 1import { Http, RequestOptionsArgs, URLSearchParams, Response } from '@angular/http'
28798b5d 2
df98563e 3import { ServerDataSource } from 'ng2-smart-table'
28798b5d
C
4
5export class RestDataSource extends ServerDataSource {
df98563e
C
6 constructor (http: Http, endpoint: string) {
7 const options = {
8 endPoint: endpoint,
9 sortFieldKey: 'sort',
10 dataKey: 'data'
11 }
12
13 super(http, options)
14 }
15
16 protected extractTotalFromResponse (res: Response) {
17 const rawData = res.json()
18 return rawData ? parseInt(rawData.total, 10) : 0
28798b5d
C
19 }
20
df98563e 21 protected addSortRequestOptions (requestOptions: RequestOptionsArgs) {
42374cf5 22 const searchParams = requestOptions.params as URLSearchParams
28798b5d
C
23
24 if (this.sortConf) {
25 this.sortConf.forEach((fieldConf) => {
df98563e 26 const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''
28798b5d 27
df98563e
C
28 searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field)
29 })
28798b5d
C
30 }
31
df98563e 32 return requestOptions
28798b5d
C
33 }
34
df98563e 35 protected addPagerRequestOptions (requestOptions: RequestOptionsArgs) {
42374cf5 36 const searchParams = requestOptions.params as URLSearchParams
28798b5d
C
37
38 if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) {
df98563e
C
39 const perPage = this.pagingConf['perPage']
40 const page = this.pagingConf['page']
28798b5d 41
df98563e
C
42 const start = (page - 1) * perPage
43 const count = perPage
28798b5d 44
df98563e
C
45 searchParams.set('start', start.toString())
46 searchParams.set('count', count.toString())
28798b5d
C
47 }
48
df98563e 49 return requestOptions
28798b5d
C
50 }
51}