Skip to content

Commit

Permalink
docs: update pagination code example
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Dec 23, 2023
1 parent c027629 commit bca66d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 166 deletions.
10 changes: 8 additions & 2 deletions docs/docs/snippets/model/pagination-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {Pagination} from "../models/Pagination";
class PaginationFilter implements ResponseFilterMethods {
transform(data: unknown, ctx: PlatformContext): any {
if (ctx.data instanceof Pagination) {
// get the unserialized data
if (ctx.data.isPaginated) {
// /!\ don't modify the ctx.data. at this step, the serializer has already been called.

if (ctx.data.totalCount > (ctx.data.pageable.page + 1) * ctx.data.pageable.size) {
ctx.response.status(206);
data.links.next = `${ctx.request.url}?page=${ctx.data.pageable.page + 1}&size=${ctx.data.pageable.size}`;
}

if (ctx.data.pageable.page > 0) {
data.links.prev = `${ctx.request.url}?page=${ctx.data.pageable.page - 1}&size=${ctx.data.pageable.size}`;
}
}

Expand Down
13 changes: 12 additions & 1 deletion docs/docs/snippets/model/pagination-model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {CollectionOf, Default, Generics, Integer, MinLength} from "@tsed/schema";
import {Pageable} from "./Pageable";

export class PaginationLink {
@Property()
next: string;

@Property()
prev: string;
}

@Generics("T")
class Pagination<T> extends Pageable {
export class Pagination<T> extends Pageable {
@CollectionOf("T")
data: T[];

Expand All @@ -11,6 +19,9 @@ class Pagination<T> extends Pageable {
@Default(0)
totalCount: number = 0;

@Property()
links: PaginationLink = new PaginationLink();

constructor({data, totalCount, pageable}: Partial<Pagination<T>> & {pageable: Pageable}) {
super(pageable);
data && (this.data = data);
Expand Down
163 changes: 0 additions & 163 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5614,70 +5614,6 @@
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.2.tgz#423c77877d0569db20e1fc80885ac4118314010e"
integrity sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/common/-/common-7.52.0.tgz#6ab3b2427289b809999f21ba976ec010666cb12e"
integrity sha512-BGwBOCb236FJGvQq1v/HSVit8HnUwpC62Hn+lVfgqjsfUo6VlSpEjZ+/O1cGJa3NN9y0guAmRLSL90HCsD8+rg==
dependencies:
"@tsed/core" "7.52.0"
"@tsed/di" "7.52.0"
"@tsed/exceptions" "7.52.0"
"@tsed/json-mapper" "7.52.0"
"@tsed/logger" ">=6.2.2"
"@tsed/logger-file" ">=6.2.2"
"@tsed/platform-exceptions" "7.52.0"
"@tsed/platform-log-middleware" "7.52.0"
"@tsed/platform-middlewares" "7.52.0"
"@tsed/platform-params" "7.52.0"
"@tsed/platform-response-filter" "7.52.0"
"@tsed/platform-router" "7.52.0"
"@tsed/platform-views" "7.52.0"
"@tsed/schema" "7.52.0"
"@types/json-schema" "7.0.11"
accepts "^1.3.8"
tslib "2.5.0"
uuid "8.3.2"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/core/-/core-7.52.0.tgz#e9f618e3b7a6be7410a87f681b9f887e1588797e"
integrity sha512-0gAanMBXTvOiQRkVfuNbj5LjcvyA9W3pT2h3fYYLoI3HrMPWW25P9LJ8kleLXOfKXPVZ4WXD4Ka/3Qvaj1XBgQ==
dependencies:
reflect-metadata "^0.1.13"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/di/-/di-7.52.0.tgz#a14f41be9c5768afb17ab4f42ecb23abd1d0bd20"
integrity sha512-XKGStgl0uBVv47lHJFR5BcMY5AIXxCstkGhAmUiHviQwHXMeWX4HTVbLDJmhaSVHF1lWuZXXkLG82syoif14Og==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/engines/-/engines-7.52.0.tgz#e59baf0a8f2996664a504885173504e4a94039a2"
integrity sha512-oeNyLHeWB9y13Xx0igiqMLO71B/saQLi1an5R3mOnO7ARQq0M7vb7tHSwitaQ199jQ3AyUZGobrPw2estjxFWw==
dependencies:
filedirname "^2.7.0"
fs-extra "11.1.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/exceptions/-/exceptions-7.52.0.tgz#024052fa74181aa92136bf080dbc79c41027247e"
integrity sha512-EfoTIfEvq6+sU8KGoDpF5qzgkbDJwwoag0QQHXRMhBWbJ2nM38AmBA9hdGWARhY6CqQJStNdNWjd7kHEtqSCEw==
dependencies:
change-case "4.1.2"
statuses ">=2.0.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/json-mapper/-/json-mapper-7.52.0.tgz#d60fac5b53a6fc8eb1b23eab402d3377bbe3192b"
integrity sha512-bQzopGzCGTdXorI0puw432T18okADKmJBrtEDlOtUkMRogtCQer/Az6Tj28lZeo2VboGExMoh8bo8U43jmKGZg==
dependencies:
tslib "2.5.0"

"@tsed/logger-file@>=6.2.2":
version "6.2.2"
resolved "https://registry.yarnpkg.com/@tsed/logger-file/-/logger-file-6.2.2.tgz#ffd1f9ab1a0e09160433676976c370a0800ba674"
Expand Down Expand Up @@ -5717,105 +5653,6 @@
normalize-path "3.0.0"
semver "7.3.7"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/normalize-path/-/normalize-path-7.52.0.tgz#0d37c8c3cc21646f36b34f389daaa470ee07257d"
integrity sha512-u6hFcl8QgPFFZQqcHgFUUnztyPwf4g5okb2w6l3AXkMc3RWp+3VAkdWkI/nzJsblK9z4pYbuI+4PC8tXoYqrmw==
dependencies:
normalize-path "3.0.0"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/openspec/-/openspec-7.52.0.tgz#20a8416fb16a579a3c146db772a785652762c0fb"
integrity sha512-rxJ1WKiRmgl9hylv2/Q4ku23ioqqeIb2ZcoVdB7Kj3kbI8LbL5HWruxD4sYpQYpbVJZzFUYBOjDcQAEU3Bb1Fg==

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-exceptions/-/platform-exceptions-7.52.0.tgz#e7136ce24e2987da03d222d6c15536c64e069149"
integrity sha512-3/UDFMxmBKjOEjQI9nUtBXYPfgoD0WQEv77udZfZNwZ0U1ScJx21nR5yPM5u/X0dDEnLnMQ5NODnl+zckkGGpw==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-express/-/platform-express-7.52.0.tgz#66bc1fc8d7f995d51e772d97ca312a9d8f2a85ba"
integrity sha512-bmCHAPXeU8SO+T2+mZ2Qt0c6rpkk+aYUttBC8s6tDJv2ifXmMvse5eHxMmxqezeO4Hybg0ALKTwWwQWiZNpnTQ==
dependencies:
express "^4.18.2"
multer "^1.4.5-lts.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-log-middleware/-/platform-log-middleware-7.52.0.tgz#17748c8b2015cdbf7c3d081acd7c07fd2e49ba43"
integrity sha512-1qUztBiKFcB7lpSTMFEIOOi1oJT+iWmEjB9tG4kA4/g0ze9jNWE1VbH6Kif1TZVXbDbw82VFJ/hR/NLq9T4+sQ==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-middlewares/-/platform-middlewares-7.52.0.tgz#0b52fc55d10bda4d6c9bc282ce1ee51031772fcd"
integrity sha512-xmXC3JMdh3LVDG9y3/11mPLvnjbkOBqwYs29nbvmBNbj5kK6ltJnSYsyyRKHqLTa1dNAaWC55+M/Z6oyfRAD/g==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-params/-/platform-params-7.52.0.tgz#fe7c737322b007660020f0c2e1ce8529fcf7a3da"
integrity sha512-eK4xchh7TEoTX6VyZ6O+mHpgosVAdwYWFXouaqAl9ziGy+WB/fOMFMy7+Hzqd/mCINqRLM1APKk4bfD36uWaCg==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-response-filter/-/platform-response-filter-7.52.0.tgz#ba45734c776ef730e23968759c54c124dcab245e"
integrity sha512-D+Iq5SHc1dTSxQmbiGCTlCzlfOii9jeulPELoLa5GOY8S3d5V9/eQlbegIf89Oo3ZtohiwZ22gphNxd22/ke0Q==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-router/-/platform-router-7.52.0.tgz#6b3a17b31d9fa541e62d08026a5c1e9b57004700"
integrity sha512-FrJw+nHbA1fzvoFhmq7hMAUi9qE+g4oUMysAPx4DiZ29Iu9CyaGvWHVURkYm5fg+zO/faTmYinHG1YrIG+4G9g==
dependencies:
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/platform-views/-/platform-views-7.52.0.tgz#15d2681e8c5e468a966c6a15f4dae2bc47a2d382"
integrity sha512-PXqAKbHBn4QfQReLCZ6Wk/hSj06ss0Z0rvP7csSvhDOqHIjI3RIIrHXsLAm5tR6n/HaPgJvf5HvalwL9/tBZHQ==
dependencies:
"@tsed/engines" "7.52.0"
ejs "^3.1.5"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/schema/-/schema-7.52.0.tgz#838f98abef64975657ff980f6674106f2723c1bf"
integrity sha512-Ww8PyOt+ZNV+5Om2W5Ip7IKm+2k0KWtW6zDEAaPtV9mr8Unc6lbbVawnM/RUTp0ia951FIzgs2i5qQL9pIH9qw==
dependencies:
"@tsed/openspec" "7.52.0"
change-case "^4.1.2"
fs-extra "^11.1.1"
json-schema "0.4.0"
picomatch "2.3.1"
statuses ">=2.0.1"
tslib "2.5.0"

"@tsed/[email protected]":
version "7.52.0"
resolved "https://registry.yarnpkg.com/@tsed/swagger/-/swagger-7.52.0.tgz#bb66b208af810b42be9072073aee15964c9d1bee"
integrity sha512-m91hdztJVbGratnt9to2BMP0h4mWyl6uxaLLUbiZsRtE96eyYUfirLbaBiDXVHqnIpflHPg5t4Hm4MTAl/dnMQ==
dependencies:
"@tsed/normalize-path" "7.52.0"
"@tsed/openspec" "7.52.0"
filedirname "^2.7.0"
fs-extra "11.1.1"
micromatch "4.0.5"
swagger-ui-dist "^4.5.2"
tslib "2.5.0"

"@tsed/[email protected]":
version "4.0.14"
resolved "https://registry.yarnpkg.com/@tsed/ts-doc/-/ts-doc-4.0.14.tgz#6f09a1b09962354c0a1e7847f475a3e35a191eed"
Expand Down

0 comments on commit bca66d2

Please sign in to comment.