-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresponse_meta.go
More file actions
49 lines (43 loc) · 1.4 KB
/
response_meta.go
File metadata and controls
49 lines (43 loc) · 1.4 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
package httpsuite
// Response represents the structure of an HTTP response, including an optional body and metadata.
type Response[T any] struct {
Data T `json:"data"`
Meta any `json:"meta,omitempty"`
}
// PageMeta provides page-based pagination details.
type PageMeta struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
TotalPages int `json:"total_pages,omitempty"`
TotalItems int `json:"total_items,omitempty"`
}
// Meta is kept as a compatibility alias for page-based pagination metadata.
type Meta = PageMeta
// CursorMeta provides cursor-based pagination details.
type CursorMeta struct {
NextCursor string `json:"next_cursor,omitempty"`
PrevCursor string `json:"prev_cursor,omitempty"`
HasNext bool `json:"has_next"`
HasPrev bool `json:"has_prev"`
}
// NewPageMeta builds page-based metadata and derives total pages when possible.
func NewPageMeta(page, pageSize, totalItems int) *PageMeta {
meta := &PageMeta{
Page: page,
PageSize: pageSize,
TotalItems: totalItems,
}
if pageSize > 0 && totalItems > 0 {
meta.TotalPages = (totalItems + pageSize - 1) / pageSize
}
return meta
}
// NewCursorMeta builds cursor-based metadata.
func NewCursorMeta(nextCursor, prevCursor string, hasNext, hasPrev bool) *CursorMeta {
return &CursorMeta{
NextCursor: nextCursor,
PrevCursor: prevCursor,
HasNext: hasNext,
HasPrev: hasPrev,
}
}