@webframe/use-pagination-query-parameters 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +24 -2
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -0
- package/package.json +30 -4
- package/src/index.ts +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tw050x
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webframe/use-pagination-query-parameters
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Reads the page index and size from the URL's query into `context.pagination`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @webframe/use-pagination-query-parameters
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { defineMiddleware } from '@webframe/core'
|
|
11
|
+
import { usePaginationQueryParameters } from '@webframe/use-pagination-query-parameters'
|
|
12
|
+
|
|
13
|
+
defineMiddleware([
|
|
14
|
+
usePaginationQueryParameters({ defaultPageIndex: 0, defaultPageSize: 20, maxPageSize: 100 }),
|
|
15
|
+
async ({ pagination, serverResponse }) => {
|
|
16
|
+
// GET /users?pi=2&ps=20 → { pageIndex: 2, pageSize: 20 }
|
|
17
|
+
const users = await findUsers({ skip: pagination.pageIndex * pagination.pageSize, limit: pagination.pageSize })
|
|
18
|
+
serverResponse.sendOKJSONResponse(users)
|
|
19
|
+
},
|
|
20
|
+
])
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The full documentation is at [webframe.tw050x.net/packages/use-pagination-query-parameters](https://webframe.tw050x.net/packages/use-pagination-query-parameters).
|
|
24
|
+
|
|
25
|
+
Part of [webframe](https://webframe.tw050x.net), MIT licensed.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Middleware, type ServiceRequestContext } from "@webframe/core";
|
|
2
|
+
export type UsePaginationQueryParametersOptions = {
|
|
3
|
+
/** The page shown when the URL doesn't say, counting from 0. */
|
|
4
|
+
defaultPageIndex: number;
|
|
5
|
+
/** How many to a page when the URL doesn't say. */
|
|
6
|
+
defaultPageSize: number;
|
|
7
|
+
/** The largest page size a URL may ask for; no limit if left out. */
|
|
8
|
+
maxPageSize?: number;
|
|
9
|
+
/** The query's names for the page index and size; `pi` and `ps` by default. */
|
|
10
|
+
names?: {
|
|
11
|
+
pageIndex?: string;
|
|
12
|
+
pageSize?: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export type Pagination = {
|
|
16
|
+
pageIndex: number;
|
|
17
|
+
pageSize: number;
|
|
18
|
+
};
|
|
19
|
+
/** The context after `usePaginationQueryParameters`. */
|
|
20
|
+
export type UsePaginationQueryParametersContext = ServiceRequestContext & {
|
|
21
|
+
pagination: Pagination;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Reads the page index and size from the URL's query (`?pi=0&ps=20`) into `context.pagination`.
|
|
25
|
+
* A URL missing either is redirected (302) to the same URL with the default added, so every
|
|
26
|
+
* page has one address; a value that isn't a whole number in range is replaced by the default.
|
|
27
|
+
*/
|
|
28
|
+
export declare const usePaginationQueryParameters: (options: UsePaginationQueryParametersOptions) => Middleware<ServiceRequestContext, UsePaginationQueryParametersContext>;
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,UAAU,EAAE,KAAK,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAE9F,MAAM,MAAM,mCAAmC,GAAG;IAChD,gEAAgE;IAChE,gBAAgB,EAAE,MAAM,CAAC;IACzB,mDAAmD;IACnD,eAAe,EAAE,MAAM,CAAC;IACxB,qEAAqE;IACrE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,+EAA+E;IAC/E,KAAK,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACnD,CAAA;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAA;AAED,wDAAwD;AACxD,MAAM,MAAM,mCAAmC,GAAG,qBAAqB,GAAG;IACxE,UAAU,EAAE,UAAU,CAAC;CACxB,CAAA;AAKD;;;;GAIG;AACH,eAAO,MAAM,4BAA4B,GACvC,SAAS,mCAAmC,KAC3C,UAAU,CAAC,qBAAqB,EAAE,mCAAmC,CAkCvE,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { parseRequestUrl } from "@webframe/core";
|
|
2
|
+
const isIndex = (value) => Number.isInteger(value) && value >= 0;
|
|
3
|
+
const isSize = (value) => Number.isInteger(value) && value > 0;
|
|
4
|
+
/**
|
|
5
|
+
* Reads the page index and size from the URL's query (`?pi=0&ps=20`) into `context.pagination`.
|
|
6
|
+
* A URL missing either is redirected (302) to the same URL with the default added, so every
|
|
7
|
+
* page has one address; a value that isn't a whole number in range is replaced by the default.
|
|
8
|
+
*/
|
|
9
|
+
export const usePaginationQueryParameters = (options) => {
|
|
10
|
+
if (isIndex(options.defaultPageIndex) === false) {
|
|
11
|
+
throw new TypeError('The default page index must be a whole number, 0 or more');
|
|
12
|
+
}
|
|
13
|
+
if (isSize(options.defaultPageSize) === false) {
|
|
14
|
+
throw new TypeError('The default page size must be a whole number, 1 or more');
|
|
15
|
+
}
|
|
16
|
+
if (options.maxPageSize !== undefined && (isSize(options.maxPageSize) === false || options.maxPageSize < options.defaultPageSize)) {
|
|
17
|
+
throw new TypeError('The largest page size must be a whole number, at least the default page size');
|
|
18
|
+
}
|
|
19
|
+
const indexName = options.names?.pageIndex ?? 'pi';
|
|
20
|
+
const sizeName = options.names?.pageSize ?? 'ps';
|
|
21
|
+
return async (context) => {
|
|
22
|
+
const url = parseRequestUrl(context.incomingMessage.url);
|
|
23
|
+
const pageIndex = url.searchParams.get(indexName);
|
|
24
|
+
const pageSize = url.searchParams.get(sizeName);
|
|
25
|
+
if (pageIndex === null || pageSize === null) {
|
|
26
|
+
if (pageIndex === null)
|
|
27
|
+
url.searchParams.set(indexName, String(options.defaultPageIndex));
|
|
28
|
+
if (pageSize === null)
|
|
29
|
+
url.searchParams.set(sizeName, String(options.defaultPageSize));
|
|
30
|
+
return void context.serverResponse.sendFoundRedirect(`${url.pathname}${url.search}`);
|
|
31
|
+
}
|
|
32
|
+
const index = Number(pageIndex);
|
|
33
|
+
const size = Number(pageSize);
|
|
34
|
+
context.pagination = {
|
|
35
|
+
pageIndex: isIndex(index) ? index : options.defaultPageIndex,
|
|
36
|
+
pageSize: isSize(size)
|
|
37
|
+
? Math.min(size, options.maxPageSize ?? size)
|
|
38
|
+
: options.defaultPageSize,
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAA+C,MAAM,gBAAgB,CAAC;AAuB9F,MAAM,OAAO,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;AACzE,MAAM,MAAM,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC;AAEvE;;;;GAIG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,OAA4C,EAC4B,EAAE;IAC1E,IAAI,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,KAAK,KAAK,EAAE,CAAC;QAChD,MAAM,IAAI,SAAS,CAAC,0DAA0D,CAAC,CAAC;IAClF,CAAC;IACD,IAAI,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,KAAK,EAAE,CAAC;QAC9C,MAAM,IAAI,SAAS,CAAC,yDAAyD,CAAC,CAAC;IACjF,CAAC;IACD,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,KAAK,IAAI,OAAO,CAAC,WAAW,GAAG,OAAO,CA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BAAgB;YAC5D,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC;gBACpB,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,WAAW,IAAI,IAAI,CAAC;gBAC7C,CAAC,CAAC,OAAO,CAAC,eAAe;SAC5B,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webframe/use-pagination-query-parameters",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pagination query parameters middleware for webframe.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"src"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@webframe/core": "^0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/tw050x/webframe.git",
|
|
26
|
+
"directory": "packages/node/use-pagination-query-parameters"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://webframe.tw050x.net/packages/use-pagination-query-parameters",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { parseRequestUrl, type Middleware, type ServiceRequestContext } from "@webframe/core";
|
|
2
|
+
|
|
3
|
+
export type UsePaginationQueryParametersOptions = {
|
|
4
|
+
/** The page shown when the URL doesn't say, counting from 0. */
|
|
5
|
+
defaultPageIndex: number;
|
|
6
|
+
/** How many to a page when the URL doesn't say. */
|
|
7
|
+
defaultPageSize: number;
|
|
8
|
+
/** The largest page size a URL may ask for; no limit if left out. */
|
|
9
|
+
maxPageSize?: number;
|
|
10
|
+
/** The query's names for the page index and size; `pi` and `ps` by default. */
|
|
11
|
+
names?: { pageIndex?: string; pageSize?: string };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type Pagination = {
|
|
15
|
+
pageIndex: number;
|
|
16
|
+
pageSize: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The context after `usePaginationQueryParameters`. */
|
|
20
|
+
export type UsePaginationQueryParametersContext = ServiceRequestContext & {
|
|
21
|
+
pagination: Pagination;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const isIndex = (value: number) => Number.isInteger(value) && value >= 0;
|
|
25
|
+
const isSize = (value: number) => Number.isInteger(value) && value > 0;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Reads the page index and size from the URL's query (`?pi=0&ps=20`) into `context.pagination`.
|
|
29
|
+
* A URL missing either is redirected (302) to the same URL with the default added, so every
|
|
30
|
+
* page has one address; a value that isn't a whole number in range is replaced by the default.
|
|
31
|
+
*/
|
|
32
|
+
export const usePaginationQueryParameters = (
|
|
33
|
+
options: UsePaginationQueryParametersOptions,
|
|
34
|
+
): Middleware<ServiceRequestContext, UsePaginationQueryParametersContext> => {
|
|
35
|
+
if (isIndex(options.defaultPageIndex) === false) {
|
|
36
|
+
throw new TypeError('The default page index must be a whole number, 0 or more');
|
|
37
|
+
}
|
|
38
|
+
if (isSize(options.defaultPageSize) === false) {
|
|
39
|
+
throw new TypeError('The default page size must be a whole number, 1 or more');
|
|
40
|
+
}
|
|
41
|
+
if (options.maxPageSize !== undefined && (isSize(options.maxPageSize) === false || options.maxPageSize < options.defaultPageSize)) {
|
|
42
|
+
throw new TypeError('The largest page size must be a whole number, at least the default page size');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const indexName = options.names?.pageIndex ?? 'pi';
|
|
46
|
+
const sizeName = options.names?.pageSize ?? 'ps';
|
|
47
|
+
|
|
48
|
+
return async (context) => {
|
|
49
|
+
const url = parseRequestUrl(context.incomingMessage.url);
|
|
50
|
+
const pageIndex = url.searchParams.get(indexName);
|
|
51
|
+
const pageSize = url.searchParams.get(sizeName);
|
|
52
|
+
|
|
53
|
+
if (pageIndex === null || pageSize === null) {
|
|
54
|
+
if (pageIndex === null) url.searchParams.set(indexName, String(options.defaultPageIndex));
|
|
55
|
+
if (pageSize === null) url.searchParams.set(sizeName, String(options.defaultPageSize));
|
|
56
|
+
return void context.serverResponse.sendFoundRedirect(`${url.pathname}${url.search}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const index = Number(pageIndex);
|
|
60
|
+
const size = Number(pageSize);
|
|
61
|
+
context.pagination = {
|
|
62
|
+
pageIndex: isIndex(index) ? index : options.defaultPageIndex,
|
|
63
|
+
pageSize: isSize(size)
|
|
64
|
+
? Math.min(size, options.maxPageSize ?? size)
|
|
65
|
+
: options.defaultPageSize,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}
|