api-paginate 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/README.md +36 -21
  2. package/package.json +4 -3
package/README.md CHANGED
@@ -4,6 +4,16 @@ Paginate arrays in **Node** (and browsers). In-memory pagination for arrays. Use
4
4
 
5
5
  Returns JSON with `data`, `meta`, and `links`—ready to send. Configure once, pass only `route`.
6
6
 
7
+ ### Why api-paginate?
8
+
9
+ - **One place to configure** — Set `baseUrl` in your entry file; in every route you only pass `route` and `per_page`. No repeating yourself.
10
+ - **Any array, any stack** — Mongoose, Prisma, Sequelize, raw SQL, or a plain `[]`. Pagination happens in memory after you fetch; no ORM magic or query hooks.
11
+ - **Familiar JSON shape** — `data`, `meta`, and `links` (first/prev/next/last) match what many APIs and frontends expect. No custom envelope.
12
+ - **Small and predictable** — No database layer, no heavy deps. Just slicing arrays and building links. Easy to reason about and safe to upgrade.
13
+ - **Works in Node and browsers** — Use it in API routes or in the client; in the browser it uses `window.location.origin` when you don’t call `configure`.
14
+
15
+ If you’ve ever copy-pasted pagination logic or built `meta`/`links` by hand, this package replaces that with a single call in your return.
16
+
7
17
  ### Features
8
18
 
9
19
  - **Framework-agnostic** — Use with Express, Next.js, Fastify, or plain Node
@@ -24,47 +34,53 @@ The package supports both **CommonJS** (`require`) and **ESM** (`import`). Use `
24
34
 
25
35
  ## Usage
26
36
 
27
- ### 1. Configure once at startup
37
+ ### 1. Configure once in your entry point
28
38
 
29
- ```javascript
30
- // CommonJS
31
- const { configure } = require('api-paginate');
39
+ Call `configure` **once** in your app entry file (`app.ts`, `server.ts`, `index.js`, or `main.js`)—not in every route file.
32
40
 
33
- // ESM / TypeScript
41
+ ```javascript
42
+ // app.ts or server.ts or index.js (your entry point)
43
+ // CommonJS: const { configure } = require('api-paginate');
34
44
  import { configure } from 'api-paginate';
35
45
 
36
46
  configure({ baseUrl: process.env.API_BASE_URL || 'https://myserver.com' });
37
47
  ```
38
48
 
39
- ### 2. Use in your return—only route, never baseUrl
49
+ ### 2. Use in route handlers—only route and per_page
40
50
 
41
- ```javascript
42
- // CommonJS
43
- const { paginate, paginateFromRequest } = require('api-paginate');
51
+ 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
52
 
45
- // ESM / TypeScript
53
+ ```javascript
54
+ // routes/users.js or similar — no configure here
55
+ // CommonJS: const { paginate, paginateFromRequest } = require('api-paginate');
46
56
  import { paginate, paginateFromRequest } from 'api-paginate';
47
57
 
48
- // paginate:
58
+ // paginate (when you have the page number):
49
59
  return res.json(paginate(docs, { route: '/users', current_page: 1, per_page: 20 }));
50
60
 
51
- // paginateFromRequest (page from req.query):
61
+ // paginateFromRequest (reads page from req.query):
52
62
  return res.json(paginateFromRequest(req, users, { route: '/users', per_page: 15 }));
53
63
  ```
54
64
 
55
- ### Express example
65
+ ### Express example (two places)
56
66
 
57
- Both CommonJS and ESM are supported:
67
+ **Entry point** (e.g. `app.ts` or `server.ts`):
58
68
 
59
69
  ```javascript
60
- // CommonJS
61
- const { paginateFromRequest, configure } = require('api-paginate');
62
-
63
- // ESM / TypeScript
64
- import { paginateFromRequest, configure } from 'api-paginate';
70
+ import express from 'express';
71
+ import { configure } from 'api-paginate';
65
72
 
66
73
  configure({ baseUrl: process.env.API_BASE_URL });
67
74
 
75
+ const app = express();
76
+ // ... middleware, then mount routes
77
+ ```
78
+
79
+ **Route handler** (e.g. `routes/users.ts`)—no `configure`, only `paginateFromRequest`:
80
+
81
+ ```javascript
82
+ import { paginateFromRequest } from 'api-paginate';
83
+
68
84
  app.get('/users', async (req, res) => {
69
85
  try {
70
86
  const users = await User.find().lean();
@@ -75,8 +91,7 @@ app.get('/users', async (req, res) => {
75
91
  });
76
92
  ```
77
93
 
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`.
94
+ 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
95
 
81
96
  ### When to use `paginate` vs `paginateFromRequest`
82
97
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "api-paginate",
3
- "version": "1.0.1",
4
- "description": "Paginate arrays for Node and browsers. Returns JSON with data, meta, and links—ready for API responses.",
3
+ "version": "1.0.3",
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": {
@@ -21,6 +21,7 @@
21
21
  "url": "git+https://github.com/Michael-Builds/api-paginate.git"
22
22
  },
23
23
  "homepage": "https://api-paginate.vercel.app",
24
+ "docs": "https://api-paginate.vercel.app/docs",
24
25
  "bugs": {
25
26
  "url": "https://github.com/Michael-Builds/api-paginate/issues"
26
27
  },