api-paginate 1.0.1 → 1.0.2
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/README.md +26 -21
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -24,47 +24,53 @@ The package supports both **CommonJS** (`require`) and **ESM** (`import`). Use `
|
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
|
-
### 1. Configure once
|
|
27
|
+
### 1. Configure once in your entry point
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
// CommonJS
|
|
31
|
-
const { configure } = require('api-paginate');
|
|
29
|
+
Call `configure` **once** in your app entry file (`app.ts`, `server.ts`, `index.js`, or `main.js`)—not in every route file.
|
|
32
30
|
|
|
33
|
-
|
|
31
|
+
```javascript
|
|
32
|
+
// app.ts or server.ts or index.js (your entry point)
|
|
33
|
+
// CommonJS: const { configure } = require('api-paginate');
|
|
34
34
|
import { configure } from 'api-paginate';
|
|
35
35
|
|
|
36
36
|
configure({ baseUrl: process.env.API_BASE_URL || 'https://myserver.com' });
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
### 2. Use in
|
|
39
|
+
### 2. Use in route handlers—only route and per_page
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
// CommonJS
|
|
43
|
-
const { paginate, paginateFromRequest } = require('api-paginate');
|
|
41
|
+
In your route files, **don’t call configure**. Just import and use `paginate` or `paginateFromRequest` with `route` and `per_page`. The package uses the `baseUrl` you set at startup.
|
|
44
42
|
|
|
45
|
-
|
|
43
|
+
```javascript
|
|
44
|
+
// routes/users.js or similar — no configure here
|
|
45
|
+
// CommonJS: const { paginate, paginateFromRequest } = require('api-paginate');
|
|
46
46
|
import { paginate, paginateFromRequest } from 'api-paginate';
|
|
47
47
|
|
|
48
|
-
// paginate:
|
|
48
|
+
// paginate (when you have the page number):
|
|
49
49
|
return res.json(paginate(docs, { route: '/users', current_page: 1, per_page: 20 }));
|
|
50
50
|
|
|
51
|
-
// paginateFromRequest (page from req.query):
|
|
51
|
+
// paginateFromRequest (reads page from req.query):
|
|
52
52
|
return res.json(paginateFromRequest(req, users, { route: '/users', per_page: 15 }));
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
-
### Express example
|
|
55
|
+
### Express example (two places)
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
**Entry point** (e.g. `app.ts` or `server.ts`):
|
|
58
58
|
|
|
59
59
|
```javascript
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
// ESM / TypeScript
|
|
64
|
-
import { paginateFromRequest, configure } from 'api-paginate';
|
|
60
|
+
import express from 'express';
|
|
61
|
+
import { configure } from 'api-paginate';
|
|
65
62
|
|
|
66
63
|
configure({ baseUrl: process.env.API_BASE_URL });
|
|
67
64
|
|
|
65
|
+
const app = express();
|
|
66
|
+
// ... middleware, then mount routes
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Route handler** (e.g. `routes/users.ts`)—no `configure`, only `paginateFromRequest`:
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
import { paginateFromRequest } from 'api-paginate';
|
|
73
|
+
|
|
68
74
|
app.get('/users', async (req, res) => {
|
|
69
75
|
try {
|
|
70
76
|
const users = await User.find().lean();
|
|
@@ -75,8 +81,7 @@ app.get('/users', async (req, res) => {
|
|
|
75
81
|
});
|
|
76
82
|
```
|
|
77
83
|
|
|
78
|
-
|
|
79
|
-
Base URL is read from config—never pass it in return statements. In the browser, `route` uses `window.location.origin` if you skip `configure`.
|
|
84
|
+
Base URL is read from the config you set at startup—don’t pass it in route handlers. In the browser, `route` uses `window.location.origin` if you never call `configure`.
|
|
80
85
|
|
|
81
86
|
### When to use `paginate` vs `paginateFromRequest`
|
|
82
87
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-paginate",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Paginate arrays for Node and browsers. Returns JSON with data, meta, and links
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Paginate arrays for Node and browsers. Configure once at app entry (app.ts/server.ts); use paginate/paginateFromRequest in routes. Returns JSON with data, meta, and links.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"files": ["dist", "README.md"],
|
|
16
|
-
"keywords": ["pagination", "array", "node", "nodejs", "api", "express", "nextjs", "react", "angular", "json", "res.json", "meta", "links"],
|
|
16
|
+
"keywords": ["pagination", "array", "node", "nodejs", "api", "express", "nextjs", "react", "angular", "json", "res.json", "meta", "links", "configure", "baseUrl"],
|
|
17
17
|
"license": "MIT",
|
|
18
18
|
"author": "Kabanda Kpanti Michael <michaelkpantiramp@gmail.com> (https://github.com/Michael-Builds)",
|
|
19
19
|
"repository": {
|