@mr-aftab-ahmad-khan/apiblocks 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/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +34 -0
- package/dist/index.cjs +96 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/package.json +77 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aftab Ahmad Khan
|
|
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
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# apiblocks
|
|
2
|
+
|
|
3
|
+
**Topics:** `api` · `apiblocks` · `composition` · `express` · `mern-packages` · `merndev` · `middleware` · `nodejs` · `npm-pm` · `observability` · `rest` · `typescript`
|
|
4
|
+
|
|
5
|
+
**Smart API composition** — plug pagination, search, and your own **blocks** into Express as one ordered middleware chain plus optional `setup` hooks.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @mr-aftab-ahmad-khan/apiblocks express
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Example
|
|
14
|
+
|
|
15
|
+
Register blocks **before** `router.get` / `post` so composed middleware runs first.
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import express from "express";
|
|
19
|
+
import { applyApiBlocks, paginationBlock, searchBlock, getPagination, getSearchRegex } from "@mr-aftab-ahmad-khan/apiblocks";
|
|
20
|
+
|
|
21
|
+
const app = express();
|
|
22
|
+
const api = express.Router();
|
|
23
|
+
applyApiBlocks(api, [paginationBlock(), searchBlock(["title", "body"])]);
|
|
24
|
+
api.get("/posts", (req, res) => {
|
|
25
|
+
const p = getPagination(req);
|
|
26
|
+
const rx = getSearchRegex(req);
|
|
27
|
+
res.json({ p, hasSearch: !!rx });
|
|
28
|
+
});
|
|
29
|
+
app.use("/v1", api);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
applyApiBlocks: () => applyApiBlocks,
|
|
24
|
+
block: () => block,
|
|
25
|
+
getPagination: () => getPagination,
|
|
26
|
+
getSearchRegex: () => getSearchRegex,
|
|
27
|
+
paginationBlock: () => paginationBlock,
|
|
28
|
+
searchBlock: () => searchBlock
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
var kPagination = /* @__PURE__ */ Symbol("apiblocks.pagination");
|
|
32
|
+
var kSearch = /* @__PURE__ */ Symbol("apiblocks.search");
|
|
33
|
+
function asBlocksReq(req) {
|
|
34
|
+
return req;
|
|
35
|
+
}
|
|
36
|
+
function block(def) {
|
|
37
|
+
return def;
|
|
38
|
+
}
|
|
39
|
+
function getPagination(req) {
|
|
40
|
+
return asBlocksReq(req)[kPagination];
|
|
41
|
+
}
|
|
42
|
+
function getSearchRegex(req) {
|
|
43
|
+
return asBlocksReq(req)[kSearch];
|
|
44
|
+
}
|
|
45
|
+
function paginationBlock(opts) {
|
|
46
|
+
const defaultLimit = opts?.defaultLimit ?? 20;
|
|
47
|
+
const maxLimit = opts?.maxLimit ?? 100;
|
|
48
|
+
return {
|
|
49
|
+
name: "pagination",
|
|
50
|
+
middleware: [
|
|
51
|
+
(req, _res, next) => {
|
|
52
|
+
const q = req.query;
|
|
53
|
+
const page = Math.max(1, Number.parseInt(String(q.page ?? "1"), 10) || 1);
|
|
54
|
+
let limit = Number.parseInt(String(q.limit ?? String(defaultLimit)), 10) || defaultLimit;
|
|
55
|
+
limit = Math.min(maxLimit, Math.max(1, limit));
|
|
56
|
+
asBlocksReq(req)[kPagination] = { page, limit, skip: (page - 1) * limit };
|
|
57
|
+
next();
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function searchBlock(fields) {
|
|
63
|
+
return {
|
|
64
|
+
name: "search",
|
|
65
|
+
middleware: [
|
|
66
|
+
(req, _res, next) => {
|
|
67
|
+
const q = String(req.query.q ?? "").trim();
|
|
68
|
+
if (!q || fields.length === 0) {
|
|
69
|
+
asBlocksReq(req)[kSearch] = void 0;
|
|
70
|
+
next();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const esc = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
74
|
+
asBlocksReq(req)[kSearch] = new RegExp(esc, "i");
|
|
75
|
+
void fields;
|
|
76
|
+
next();
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function applyApiBlocks(router, blocks) {
|
|
82
|
+
for (const b of blocks) {
|
|
83
|
+
for (const m of b.middleware ?? []) router.use(m);
|
|
84
|
+
}
|
|
85
|
+
for (const b of blocks) b.setup?.(router);
|
|
86
|
+
}
|
|
87
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
88
|
+
0 && (module.exports = {
|
|
89
|
+
applyApiBlocks,
|
|
90
|
+
block,
|
|
91
|
+
getPagination,
|
|
92
|
+
getSearchRegex,
|
|
93
|
+
paginationBlock,
|
|
94
|
+
searchBlock
|
|
95
|
+
});
|
|
96
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { RequestHandler, Router } from \"express\";\n\nconst kPagination = Symbol(\"apiblocks.pagination\");\nconst kSearch = Symbol(\"apiblocks.search\");\n\nexport interface PaginationState {\n page: number;\n limit: number;\n skip: number;\n}\n\nexport interface ApiBlocksRequest {\n [kPagination]?: PaginationState;\n /** Regex from `q` + search fields, or `undefined` */\n [kSearch]?: RegExp | undefined;\n}\n\nfunction asBlocksReq(req: object): ApiBlocksRequest {\n return req as ApiBlocksRequest;\n}\n\nexport interface ApiBlock {\n readonly name: string;\n readonly middleware?: RequestHandler[];\n /** Extend the mounted router after global middleware runs */\n readonly setup?: (router: Router) => void;\n}\n\nexport function block(def: ApiBlock): ApiBlock {\n return def;\n}\n\nexport function getPagination(req: object): PaginationState | undefined {\n return asBlocksReq(req)[kPagination];\n}\n\nexport function getSearchRegex(req: object): RegExp | undefined {\n return asBlocksReq(req)[kSearch];\n}\n\nexport function paginationBlock(opts?: { defaultLimit?: number; maxLimit?: number }): ApiBlock {\n const defaultLimit = opts?.defaultLimit ?? 20;\n const maxLimit = opts?.maxLimit ?? 100;\n return {\n name: \"pagination\",\n middleware: [\n (req, _res, next) => {\n const q = req.query as Record<string, string | string[] | undefined>;\n const page = Math.max(1, Number.parseInt(String(q.page ?? \"1\"), 10) || 1);\n let limit = Number.parseInt(String(q.limit ?? String(defaultLimit)), 10) || defaultLimit;\n limit = Math.min(maxLimit, Math.max(1, limit));\n asBlocksReq(req)[kPagination] = { page, limit, skip: (page - 1) * limit };\n next();\n },\n ],\n };\n}\n\n/** Reads `q` and exposes `getSearchRegex(req)` for Mongo `$regex` or SQL ILIKE builders. */\nexport function searchBlock(fields: string[]): ApiBlock {\n return {\n name: \"search\",\n middleware: [\n (req, _res, next) => {\n const q = String((req.query as { q?: string }).q ?? \"\").trim();\n if (!q || fields.length === 0) {\n asBlocksReq(req)[kSearch] = undefined;\n next();\n return;\n }\n const esc = q.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n asBlocksReq(req)[kSearch] = new RegExp(esc, \"i\");\n void fields;\n next();\n },\n ],\n };\n}\n\n/** Flatten middleware, then run each `setup` in order. */\nexport function applyApiBlocks(router: Router, blocks: ApiBlock[]): void {\n for (const b of blocks) {\n for (const m of b.middleware ?? []) router.use(m);\n }\n for (const b of blocks) b.setup?.(router);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAM,cAAc,uBAAO,sBAAsB;AACjD,IAAM,UAAU,uBAAO,kBAAkB;AAczC,SAAS,YAAY,KAA+B;AAClD,SAAO;AACT;AASO,SAAS,MAAM,KAAyB;AAC7C,SAAO;AACT;AAEO,SAAS,cAAc,KAA0C;AACtE,SAAO,YAAY,GAAG,EAAE,WAAW;AACrC;AAEO,SAAS,eAAe,KAAiC;AAC9D,SAAO,YAAY,GAAG,EAAE,OAAO;AACjC;AAEO,SAAS,gBAAgB,MAA+D;AAC7F,QAAM,eAAe,MAAM,gBAAgB;AAC3C,QAAM,WAAW,MAAM,YAAY;AACnC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY;AAAA,MACV,CAAC,KAAK,MAAM,SAAS;AACnB,cAAM,IAAI,IAAI;AACd,cAAM,OAAO,KAAK,IAAI,GAAG,OAAO,SAAS,OAAO,EAAE,QAAQ,GAAG,GAAG,EAAE,KAAK,CAAC;AACxE,YAAI,QAAQ,OAAO,SAAS,OAAO,EAAE,SAAS,OAAO,YAAY,CAAC,GAAG,EAAE,KAAK;AAC5E,gBAAQ,KAAK,IAAI,UAAU,KAAK,IAAI,GAAG,KAAK,CAAC;AAC7C,oBAAY,GAAG,EAAE,WAAW,IAAI,EAAE,MAAM,OAAO,OAAO,OAAO,KAAK,MAAM;AACxE,aAAK;AAAA,MACP;AAAA,IACF;AAAA,EACF;AACF;AAGO,SAAS,YAAY,QAA4B;AACtD,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY;AAAA,MACV,CAAC,KAAK,MAAM,SAAS;AACnB,cAAM,IAAI,OAAQ,IAAI,MAAyB,KAAK,EAAE,EAAE,KAAK;AAC7D,YAAI,CAAC,KAAK,OAAO,WAAW,GAAG;AAC7B,sBAAY,GAAG,EAAE,OAAO,IAAI;AAC5B,eAAK;AACL;AAAA,QACF;AACA,cAAM,MAAM,EAAE,QAAQ,uBAAuB,MAAM;AACnD,oBAAY,GAAG,EAAE,OAAO,IAAI,IAAI,OAAO,KAAK,GAAG;AAC/C,aAAK;AACL,aAAK;AAAA,MACP;AAAA,IACF;AAAA,EACF;AACF;AAGO,SAAS,eAAe,QAAgB,QAA0B;AACvE,aAAW,KAAK,QAAQ;AACtB,eAAW,KAAK,EAAE,cAAc,CAAC,EAAG,QAAO,IAAI,CAAC;AAAA,EAClD;AACA,aAAW,KAAK,OAAQ,GAAE,QAAQ,MAAM;AAC1C;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RequestHandler, Router } from 'express';
|
|
2
|
+
|
|
3
|
+
declare const kPagination: unique symbol;
|
|
4
|
+
declare const kSearch: unique symbol;
|
|
5
|
+
interface PaginationState {
|
|
6
|
+
page: number;
|
|
7
|
+
limit: number;
|
|
8
|
+
skip: number;
|
|
9
|
+
}
|
|
10
|
+
interface ApiBlocksRequest {
|
|
11
|
+
[kPagination]?: PaginationState;
|
|
12
|
+
/** Regex from `q` + search fields, or `undefined` */
|
|
13
|
+
[kSearch]?: RegExp | undefined;
|
|
14
|
+
}
|
|
15
|
+
interface ApiBlock {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly middleware?: RequestHandler[];
|
|
18
|
+
/** Extend the mounted router after global middleware runs */
|
|
19
|
+
readonly setup?: (router: Router) => void;
|
|
20
|
+
}
|
|
21
|
+
declare function block(def: ApiBlock): ApiBlock;
|
|
22
|
+
declare function getPagination(req: object): PaginationState | undefined;
|
|
23
|
+
declare function getSearchRegex(req: object): RegExp | undefined;
|
|
24
|
+
declare function paginationBlock(opts?: {
|
|
25
|
+
defaultLimit?: number;
|
|
26
|
+
maxLimit?: number;
|
|
27
|
+
}): ApiBlock;
|
|
28
|
+
/** Reads `q` and exposes `getSearchRegex(req)` for Mongo `$regex` or SQL ILIKE builders. */
|
|
29
|
+
declare function searchBlock(fields: string[]): ApiBlock;
|
|
30
|
+
/** Flatten middleware, then run each `setup` in order. */
|
|
31
|
+
declare function applyApiBlocks(router: Router, blocks: ApiBlock[]): void;
|
|
32
|
+
|
|
33
|
+
export { type ApiBlock, type ApiBlocksRequest, type PaginationState, applyApiBlocks, block, getPagination, getSearchRegex, paginationBlock, searchBlock };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RequestHandler, Router } from 'express';
|
|
2
|
+
|
|
3
|
+
declare const kPagination: unique symbol;
|
|
4
|
+
declare const kSearch: unique symbol;
|
|
5
|
+
interface PaginationState {
|
|
6
|
+
page: number;
|
|
7
|
+
limit: number;
|
|
8
|
+
skip: number;
|
|
9
|
+
}
|
|
10
|
+
interface ApiBlocksRequest {
|
|
11
|
+
[kPagination]?: PaginationState;
|
|
12
|
+
/** Regex from `q` + search fields, or `undefined` */
|
|
13
|
+
[kSearch]?: RegExp | undefined;
|
|
14
|
+
}
|
|
15
|
+
interface ApiBlock {
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly middleware?: RequestHandler[];
|
|
18
|
+
/** Extend the mounted router after global middleware runs */
|
|
19
|
+
readonly setup?: (router: Router) => void;
|
|
20
|
+
}
|
|
21
|
+
declare function block(def: ApiBlock): ApiBlock;
|
|
22
|
+
declare function getPagination(req: object): PaginationState | undefined;
|
|
23
|
+
declare function getSearchRegex(req: object): RegExp | undefined;
|
|
24
|
+
declare function paginationBlock(opts?: {
|
|
25
|
+
defaultLimit?: number;
|
|
26
|
+
maxLimit?: number;
|
|
27
|
+
}): ApiBlock;
|
|
28
|
+
/** Reads `q` and exposes `getSearchRegex(req)` for Mongo `$regex` or SQL ILIKE builders. */
|
|
29
|
+
declare function searchBlock(fields: string[]): ApiBlock;
|
|
30
|
+
/** Flatten middleware, then run each `setup` in order. */
|
|
31
|
+
declare function applyApiBlocks(router: Router, blocks: ApiBlock[]): void;
|
|
32
|
+
|
|
33
|
+
export { type ApiBlock, type ApiBlocksRequest, type PaginationState, applyApiBlocks, block, getPagination, getSearchRegex, paginationBlock, searchBlock };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var kPagination = /* @__PURE__ */ Symbol("apiblocks.pagination");
|
|
3
|
+
var kSearch = /* @__PURE__ */ Symbol("apiblocks.search");
|
|
4
|
+
function asBlocksReq(req) {
|
|
5
|
+
return req;
|
|
6
|
+
}
|
|
7
|
+
function block(def) {
|
|
8
|
+
return def;
|
|
9
|
+
}
|
|
10
|
+
function getPagination(req) {
|
|
11
|
+
return asBlocksReq(req)[kPagination];
|
|
12
|
+
}
|
|
13
|
+
function getSearchRegex(req) {
|
|
14
|
+
return asBlocksReq(req)[kSearch];
|
|
15
|
+
}
|
|
16
|
+
function paginationBlock(opts) {
|
|
17
|
+
const defaultLimit = opts?.defaultLimit ?? 20;
|
|
18
|
+
const maxLimit = opts?.maxLimit ?? 100;
|
|
19
|
+
return {
|
|
20
|
+
name: "pagination",
|
|
21
|
+
middleware: [
|
|
22
|
+
(req, _res, next) => {
|
|
23
|
+
const q = req.query;
|
|
24
|
+
const page = Math.max(1, Number.parseInt(String(q.page ?? "1"), 10) || 1);
|
|
25
|
+
let limit = Number.parseInt(String(q.limit ?? String(defaultLimit)), 10) || defaultLimit;
|
|
26
|
+
limit = Math.min(maxLimit, Math.max(1, limit));
|
|
27
|
+
asBlocksReq(req)[kPagination] = { page, limit, skip: (page - 1) * limit };
|
|
28
|
+
next();
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function searchBlock(fields) {
|
|
34
|
+
return {
|
|
35
|
+
name: "search",
|
|
36
|
+
middleware: [
|
|
37
|
+
(req, _res, next) => {
|
|
38
|
+
const q = String(req.query.q ?? "").trim();
|
|
39
|
+
if (!q || fields.length === 0) {
|
|
40
|
+
asBlocksReq(req)[kSearch] = void 0;
|
|
41
|
+
next();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const esc = q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
45
|
+
asBlocksReq(req)[kSearch] = new RegExp(esc, "i");
|
|
46
|
+
void fields;
|
|
47
|
+
next();
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function applyApiBlocks(router, blocks) {
|
|
53
|
+
for (const b of blocks) {
|
|
54
|
+
for (const m of b.middleware ?? []) router.use(m);
|
|
55
|
+
}
|
|
56
|
+
for (const b of blocks) b.setup?.(router);
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
applyApiBlocks,
|
|
60
|
+
block,
|
|
61
|
+
getPagination,
|
|
62
|
+
getSearchRegex,
|
|
63
|
+
paginationBlock,
|
|
64
|
+
searchBlock
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { RequestHandler, Router } from \"express\";\n\nconst kPagination = Symbol(\"apiblocks.pagination\");\nconst kSearch = Symbol(\"apiblocks.search\");\n\nexport interface PaginationState {\n page: number;\n limit: number;\n skip: number;\n}\n\nexport interface ApiBlocksRequest {\n [kPagination]?: PaginationState;\n /** Regex from `q` + search fields, or `undefined` */\n [kSearch]?: RegExp | undefined;\n}\n\nfunction asBlocksReq(req: object): ApiBlocksRequest {\n return req as ApiBlocksRequest;\n}\n\nexport interface ApiBlock {\n readonly name: string;\n readonly middleware?: RequestHandler[];\n /** Extend the mounted router after global middleware runs */\n readonly setup?: (router: Router) => void;\n}\n\nexport function block(def: ApiBlock): ApiBlock {\n return def;\n}\n\nexport function getPagination(req: object): PaginationState | undefined {\n return asBlocksReq(req)[kPagination];\n}\n\nexport function getSearchRegex(req: object): RegExp | undefined {\n return asBlocksReq(req)[kSearch];\n}\n\nexport function paginationBlock(opts?: { defaultLimit?: number; maxLimit?: number }): ApiBlock {\n const defaultLimit = opts?.defaultLimit ?? 20;\n const maxLimit = opts?.maxLimit ?? 100;\n return {\n name: \"pagination\",\n middleware: [\n (req, _res, next) => {\n const q = req.query as Record<string, string | string[] | undefined>;\n const page = Math.max(1, Number.parseInt(String(q.page ?? \"1\"), 10) || 1);\n let limit = Number.parseInt(String(q.limit ?? String(defaultLimit)), 10) || defaultLimit;\n limit = Math.min(maxLimit, Math.max(1, limit));\n asBlocksReq(req)[kPagination] = { page, limit, skip: (page - 1) * limit };\n next();\n },\n ],\n };\n}\n\n/** Reads `q` and exposes `getSearchRegex(req)` for Mongo `$regex` or SQL ILIKE builders. */\nexport function searchBlock(fields: string[]): ApiBlock {\n return {\n name: \"search\",\n middleware: [\n (req, _res, next) => {\n const q = String((req.query as { q?: string }).q ?? \"\").trim();\n if (!q || fields.length === 0) {\n asBlocksReq(req)[kSearch] = undefined;\n next();\n return;\n }\n const esc = q.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n asBlocksReq(req)[kSearch] = new RegExp(esc, \"i\");\n void fields;\n next();\n },\n ],\n };\n}\n\n/** Flatten middleware, then run each `setup` in order. */\nexport function applyApiBlocks(router: Router, blocks: ApiBlock[]): void {\n for (const b of blocks) {\n for (const m of b.middleware ?? []) router.use(m);\n }\n for (const b of blocks) b.setup?.(router);\n}\n"],"mappings":";AAEA,IAAM,cAAc,uBAAO,sBAAsB;AACjD,IAAM,UAAU,uBAAO,kBAAkB;AAczC,SAAS,YAAY,KAA+B;AAClD,SAAO;AACT;AASO,SAAS,MAAM,KAAyB;AAC7C,SAAO;AACT;AAEO,SAAS,cAAc,KAA0C;AACtE,SAAO,YAAY,GAAG,EAAE,WAAW;AACrC;AAEO,SAAS,eAAe,KAAiC;AAC9D,SAAO,YAAY,GAAG,EAAE,OAAO;AACjC;AAEO,SAAS,gBAAgB,MAA+D;AAC7F,QAAM,eAAe,MAAM,gBAAgB;AAC3C,QAAM,WAAW,MAAM,YAAY;AACnC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY;AAAA,MACV,CAAC,KAAK,MAAM,SAAS;AACnB,cAAM,IAAI,IAAI;AACd,cAAM,OAAO,KAAK,IAAI,GAAG,OAAO,SAAS,OAAO,EAAE,QAAQ,GAAG,GAAG,EAAE,KAAK,CAAC;AACxE,YAAI,QAAQ,OAAO,SAAS,OAAO,EAAE,SAAS,OAAO,YAAY,CAAC,GAAG,EAAE,KAAK;AAC5E,gBAAQ,KAAK,IAAI,UAAU,KAAK,IAAI,GAAG,KAAK,CAAC;AAC7C,oBAAY,GAAG,EAAE,WAAW,IAAI,EAAE,MAAM,OAAO,OAAO,OAAO,KAAK,MAAM;AACxE,aAAK;AAAA,MACP;AAAA,IACF;AAAA,EACF;AACF;AAGO,SAAS,YAAY,QAA4B;AACtD,SAAO;AAAA,IACL,MAAM;AAAA,IACN,YAAY;AAAA,MACV,CAAC,KAAK,MAAM,SAAS;AACnB,cAAM,IAAI,OAAQ,IAAI,MAAyB,KAAK,EAAE,EAAE,KAAK;AAC7D,YAAI,CAAC,KAAK,OAAO,WAAW,GAAG;AAC7B,sBAAY,GAAG,EAAE,OAAO,IAAI;AAC5B,eAAK;AACL;AAAA,QACF;AACA,cAAM,MAAM,EAAE,QAAQ,uBAAuB,MAAM;AACnD,oBAAY,GAAG,EAAE,OAAO,IAAI,IAAI,OAAO,KAAK,GAAG;AAC/C,aAAK;AACL,aAAK;AAAA,MACP;AAAA,IACF;AAAA,EACF;AACF;AAGO,SAAS,eAAe,QAAgB,QAA0B;AACvE,aAAW,KAAK,QAAQ;AACtB,eAAW,KAAK,EAAE,cAAc,CAAC,EAAG,QAAO,IAAI,CAAC;AAAA,EAClD;AACA,aAAW,KAAK,OAAQ,GAAE,QAAQ,MAAM;AAC1C;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mr-aftab-ahmad-khan/apiblocks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "apiblocks — Smart API composition: reusable Express blocks (pagination, search, etc.) merge into one middleware chain and router setup.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE",
|
|
21
|
+
"CHANGELOG.md"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsup",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"prepublishOnly": "npm run build"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"api",
|
|
31
|
+
"apiblocks",
|
|
32
|
+
"composition",
|
|
33
|
+
"express",
|
|
34
|
+
"mern-packages",
|
|
35
|
+
"merndev",
|
|
36
|
+
"middleware",
|
|
37
|
+
"nodejs",
|
|
38
|
+
"npm-pm",
|
|
39
|
+
"observability",
|
|
40
|
+
"rest",
|
|
41
|
+
"typescript"
|
|
42
|
+
],
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"express": "^4.0.0 || ^5.0.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"express": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/express": "^4.17.21",
|
|
53
|
+
"@types/node": "^20.11.0",
|
|
54
|
+
"@types/supertest": "^6.0.2",
|
|
55
|
+
"express": "^4.19.2",
|
|
56
|
+
"supertest": "^7.0.0",
|
|
57
|
+
"tsup": "^8.0.0",
|
|
58
|
+
"typescript": "^5.4.0",
|
|
59
|
+
"vitest": "^1.4.0"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=18"
|
|
63
|
+
},
|
|
64
|
+
"author": "Aftab Ahmad Khan (https://github.com/aftab-ahmad-khan-dev)",
|
|
65
|
+
"repository": {
|
|
66
|
+
"type": "git",
|
|
67
|
+
"url": "git+https://github.com/NPM-Packages-Modules/mern.git",
|
|
68
|
+
"directory": "apiblocks"
|
|
69
|
+
},
|
|
70
|
+
"bugs": {
|
|
71
|
+
"url": "https://github.com/NPM-Packages-Modules/mern/issues"
|
|
72
|
+
},
|
|
73
|
+
"homepage": "https://github.com/NPM-Packages-Modules/mern/tree/main/apiblocks",
|
|
74
|
+
"publishConfig": {
|
|
75
|
+
"access": "public"
|
|
76
|
+
}
|
|
77
|
+
}
|