-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse.dto.ts
More file actions
112 lines (83 loc) · 2.83 KB
/
Copy pathresponse.dto.ts
File metadata and controls
112 lines (83 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type { BooruPostObject, BooruTagObject, BooruTypes } from '@alejandroakbal/universal-booru-wrapper'
import {
booruQueryValuesPostsDTO,
booruQueryValuesRandomPostsDTO,
booruQueryValuesTagsDTO
} from '../../booru/dto/booru-queries.dto'
import type { booruQueryValuesSinglePostDTO } from '../../booru/dto/booru-queries.dto'
import type { BooruHttpRequest } from '../../booru/interfaces/booru-http.interface'
import { createFirstPageUrl, createNextPageUrl, createPreviousPageUrl, createUrlFromRequest } from '../support/url'
type PaginatedResponseQuery = booruQueryValuesPostsDTO | booruQueryValuesRandomPostsDTO | booruQueryValuesTagsDTO
type ControllerResponseQuery = PaginatedResponseQuery | booruQueryValuesSinglePostDTO
function isPaginatedResponseQuery(queries: ControllerResponseQuery): queries is PaginatedResponseQuery {
return (
queries instanceof booruQueryValuesPostsDTO ||
queries instanceof booruQueryValuesRandomPostsDTO ||
queries instanceof booruQueryValuesTagsDTO
)
}
export class ResponseDto {
readonly data: unknown[]
readonly meta: {
items_count: number
total_items: number | null
current_page: number | null
total_pages: number | null
items_per_page: number | null
}
readonly links: {
self: string | null
first: string | null
last: string | null
prev: string | null
next: string | null
}
constructor(data: ResponseDto['data'], meta: ResponseDto['meta'], links: ResponseDto['links']) {
this.data = data
this.meta = meta
this.links = links
}
public static createFromController(
request: BooruHttpRequest,
queries: ControllerResponseQuery,
booruApi: BooruTypes,
posts: BooruPostObject[] | BooruTagObject[]
): ResponseDto {
const isPaginatedQuery = isPaginatedResponseQuery(queries)
const meta: ResponseDto['meta'] = isPaginatedQuery
? {
items_count: posts.length,
total_items: null,
current_page: queries.pageID ?? null,
total_pages: null,
items_per_page: queries.limit ?? null
}
: {
items_count: posts.length,
total_items: null,
current_page: null,
total_pages: null,
items_per_page: null
}
const links: ResponseDto['links'] = isPaginatedQuery
? {
self: createUrlFromRequest(request),
first: createFirstPageUrl(request, booruApi.booruType.initialPageID),
last: null,
prev: createPreviousPageUrl(request, queries.pageID, booruApi.booruType.initialPageID),
next: createNextPageUrl(request, queries.pageID)
}
: {
self: createUrlFromRequest(request),
first: null,
last: null,
prev: null,
next: null
}
return new ResponseDto(
posts,
meta,
links
)
}
}