@schmock/query 2.4.0 → 2.4.1

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/dist/index.js +1 -1
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{isStatusTuple as L,SchmockError as c}from"@schmock/core";var _="2.4.0";var F=new Set(["__proto__","constructor","prototype"]);function T(h){return typeof h==="object"&&h!==null}function W(h,j){try{return Object.hasOwn(h,j)?h[j]:void 0}catch{return}}function O(h){try{return typeof h==="string"?JSON.stringify(h):String(h)}catch{return"<unprintable>"}}function Z(h,j,M){return new c(`queryPlugin: ${h} (received ${O(M)})`,"QUERY_CONFIG_INVALID",{option:j,received:M})}function I(h,j){if(h===void 0)return;if(typeof h!=="number"||!Number.isSafeInteger(h)||h<1)throw Z(`${j} must be a positive integer`,j,h)}function Y(h,j){if(h===void 0)return;if(typeof h!=="string"||h.length===0)throw Z(`${j} must be a non-empty string`,j,h)}function K(h,j){if(!Array.isArray(h))throw Z(`${j} must be an array of field names`,j,h);for(let M of h){if(typeof M!=="string"||M.length===0)throw Z(`${j} must contain only non-empty field names`,j,M);if(F.has(M))throw Z(`${j} must not contain the reserved field name`,j,M)}}function V(h){let{pagination:j,sorting:M,filtering:z}=h;if(j)I(j.defaultLimit,"pagination.defaultLimit"),I(j.maxLimit,"pagination.maxLimit"),Y(j.pageParam,"pagination.pageParam"),Y(j.limitParam,"pagination.limitParam");if(M)K(M.allowed,"sorting.allowed"),Y(M.sortParam,"sorting.sortParam"),Y(M.orderParam,"sorting.orderParam");if(z)K(z.allowed,"filtering.allowed"),Y(z.filterPrefix,"filtering.filterPrefix")}function q(h){let{pagination:j,sorting:M,filtering:z}=h,D,H,Q;if(j){let{defaultLimit:J,maxLimit:G,pageParam:U,limitParam:X}=j;D={defaultLimit:J,maxLimit:G,pageParam:U,limitParam:X}}if(M){let{allowed:J,default:G,defaultOrder:U,sortParam:X,orderParam:$}=M;H={allowed:Array.isArray(J)?[...J]:J,default:G,defaultOrder:U,sortParam:X,orderParam:$}}if(z){let{allowed:J,filterPrefix:G}=z;Q={allowed:Array.isArray(J)?[...J]:J,filterPrefix:G}}return{pagination:D,sorting:H,filtering:Q}}function P(h){return T(h)&&!Array.isArray(h)&&Object.values(h).every((j)=>typeof j==="string")}function N(h){return T(h)&&!Array.isArray(h)&&typeof h.status==="number"&&"body"in h&&(h.headers===void 0||P(h.headers))}function E(h){if(L(h))return h[1];if(N(h))return h.body;return h}function k(h,j){if(L(h))return h.length===3?[h[0],j,h[2]]:[h[0],j];if(N(h))return h.headers===void 0?{status:h.status,body:j}:{status:h.status,body:j,headers:h.headers};return j}function u(h={}){let j=q(h);return V(j),{name:"query",version:_,process(M,z){let D=E(z);if(!Array.isArray(D))return{context:M,response:z};let H=[...D],Q=M.query||{};if(j.filtering)H=R(H,Q,j.filtering);if(j.sorting)H=b(H,Q,j.sorting);if(j.pagination){let J=d(H,Q,j.pagination);return{context:M,response:k(z,J)}}return{context:M,response:k(z,H)}}}}function R(h,j,M){let z=M.filterPrefix??"filter",D=h;for(let H of M.allowed){let Q=W(j,`${z}[${H}]`)??W(j,`${z}.${H}`);if(Q!==void 0)D=D.filter((J)=>{if(!T(J))return!1;let G=W(J,H);if(G===void 0)return!1;try{return String(G)===Q}catch{return!1}})}return D}function w(h,j){if(h<j)return-1;return h>j?1:0}function S(h){if(h===void 0)return{kind:"missing"};if(h===null)return{kind:"null"};if(typeof h==="number")return Number.isFinite(h)?{kind:"number",value:h}:{kind:"non-finite-number",value:h};if(typeof h==="string")return{kind:"string",value:h};if(typeof h==="boolean")return{kind:"boolean",value:h};if(typeof h==="bigint")return{kind:"bigint",value:h};return{kind:"unsupported"}}function A(h){switch(h.kind){case"number":return 0;case"non-finite-number":return 1;case"string":return 2;case"boolean":return 3;case"bigint":return 4;case"null":return 5;case"unsupported":return 6;case"missing":return 7}}function C(h){if(h===Number.NEGATIVE_INFINITY)return 0;if(h===Number.POSITIVE_INFINITY)return 1;return 2}function f(h,j,M){if(h.kind==="missing"||j.kind==="missing"){if(h.kind===j.kind)return 0;return h.kind==="missing"?1:-1}if(h.kind==="unsupported"||j.kind==="unsupported"){if(h.kind===j.kind)return 0;return h.kind==="unsupported"?1:-1}let z=A(h)-A(j),D=z;if(z===0){if(h.kind==="number"&&j.kind==="number")D=h.value<j.value?-1:h.value>j.value?1:0;else if(h.kind==="non-finite-number"&&j.kind==="non-finite-number")D=C(h.value)-C(j.value);else if(h.kind==="string"&&j.kind==="string")D=w(h.value,j.value);else if(h.kind==="boolean"&&j.kind==="boolean")D=Number(h.value)-Number(j.value);else if(h.kind==="bigint"&&j.kind==="bigint")D=h.value<j.value?-1:h.value>j.value?1:0}return M==="desc"?-D:D}function b(h,j,M){let z=M.sortParam??"sort",D=M.orderParam??"order",H=W(j,z)??M.default,J=(W(j,D)??M.defaultOrder??"asc")==="desc"?"desc":"asc";if(!H)return h;if(!M.allowed.includes(H))return h;return h.map((G)=>({item:G,key:S(T(G)?W(G,H):void 0)})).sort((G,U)=>f(G.key,U.key,J)).map(({item:G})=>G)}function B(h,j){if(typeof h!=="string"||!/^[0-9]+$/.test(h))return j;let M=Number(h);return Number.isSafeInteger(M)&&M>=1?M:j}function d(h,j,M){let z=M.pageParam??"page",D=M.limitParam??"limit",H=M.defaultLimit??10,Q=M.maxLimit??100,J=B(W(j,z),1),G=Math.min(Q,B(W(j,D),H)),U=h.length,X=Math.ceil(U/G),$=(J-1)*G;return{data:h.slice($,$+G),pagination:{page:J,limit:G,total:U,totalPages:X}}}export{u as queryPlugin};
1
+ import{isStatusTuple as L,SchmockError as c}from"@schmock/core";var _="2.4.1";var F=new Set(["__proto__","constructor","prototype"]);function T(h){return typeof h==="object"&&h!==null}function W(h,j){try{return Object.hasOwn(h,j)?h[j]:void 0}catch{return}}function O(h){try{return typeof h==="string"?JSON.stringify(h):String(h)}catch{return"<unprintable>"}}function Z(h,j,M){return new c(`queryPlugin: ${h} (received ${O(M)})`,"QUERY_CONFIG_INVALID",{option:j,received:M})}function I(h,j){if(h===void 0)return;if(typeof h!=="number"||!Number.isSafeInteger(h)||h<1)throw Z(`${j} must be a positive integer`,j,h)}function Y(h,j){if(h===void 0)return;if(typeof h!=="string"||h.length===0)throw Z(`${j} must be a non-empty string`,j,h)}function K(h,j){if(!Array.isArray(h))throw Z(`${j} must be an array of field names`,j,h);for(let M of h){if(typeof M!=="string"||M.length===0)throw Z(`${j} must contain only non-empty field names`,j,M);if(F.has(M))throw Z(`${j} must not contain the reserved field name`,j,M)}}function V(h){let{pagination:j,sorting:M,filtering:z}=h;if(j)I(j.defaultLimit,"pagination.defaultLimit"),I(j.maxLimit,"pagination.maxLimit"),Y(j.pageParam,"pagination.pageParam"),Y(j.limitParam,"pagination.limitParam");if(M)K(M.allowed,"sorting.allowed"),Y(M.sortParam,"sorting.sortParam"),Y(M.orderParam,"sorting.orderParam");if(z)K(z.allowed,"filtering.allowed"),Y(z.filterPrefix,"filtering.filterPrefix")}function q(h){let{pagination:j,sorting:M,filtering:z}=h,D,H,Q;if(j){let{defaultLimit:J,maxLimit:G,pageParam:U,limitParam:X}=j;D={defaultLimit:J,maxLimit:G,pageParam:U,limitParam:X}}if(M){let{allowed:J,default:G,defaultOrder:U,sortParam:X,orderParam:$}=M;H={allowed:Array.isArray(J)?[...J]:J,default:G,defaultOrder:U,sortParam:X,orderParam:$}}if(z){let{allowed:J,filterPrefix:G}=z;Q={allowed:Array.isArray(J)?[...J]:J,filterPrefix:G}}return{pagination:D,sorting:H,filtering:Q}}function P(h){return T(h)&&!Array.isArray(h)&&Object.values(h).every((j)=>typeof j==="string")}function N(h){return T(h)&&!Array.isArray(h)&&typeof h.status==="number"&&"body"in h&&(h.headers===void 0||P(h.headers))}function E(h){if(L(h))return h[1];if(N(h))return h.body;return h}function k(h,j){if(L(h))return h.length===3?[h[0],j,h[2]]:[h[0],j];if(N(h))return h.headers===void 0?{status:h.status,body:j}:{status:h.status,body:j,headers:h.headers};return j}function u(h={}){let j=q(h);return V(j),{name:"query",version:_,process(M,z){let D=E(z);if(!Array.isArray(D))return{context:M,response:z};let H=[...D],Q=M.query||{};if(j.filtering)H=R(H,Q,j.filtering);if(j.sorting)H=b(H,Q,j.sorting);if(j.pagination){let J=d(H,Q,j.pagination);return{context:M,response:k(z,J)}}return{context:M,response:k(z,H)}}}}function R(h,j,M){let z=M.filterPrefix??"filter",D=h;for(let H of M.allowed){let Q=W(j,`${z}[${H}]`)??W(j,`${z}.${H}`);if(Q!==void 0)D=D.filter((J)=>{if(!T(J))return!1;let G=W(J,H);if(G===void 0)return!1;try{return String(G)===Q}catch{return!1}})}return D}function w(h,j){if(h<j)return-1;return h>j?1:0}function S(h){if(h===void 0)return{kind:"missing"};if(h===null)return{kind:"null"};if(typeof h==="number")return Number.isFinite(h)?{kind:"number",value:h}:{kind:"non-finite-number",value:h};if(typeof h==="string")return{kind:"string",value:h};if(typeof h==="boolean")return{kind:"boolean",value:h};if(typeof h==="bigint")return{kind:"bigint",value:h};return{kind:"unsupported"}}function A(h){switch(h.kind){case"number":return 0;case"non-finite-number":return 1;case"string":return 2;case"boolean":return 3;case"bigint":return 4;case"null":return 5;case"unsupported":return 6;case"missing":return 7}}function C(h){if(h===Number.NEGATIVE_INFINITY)return 0;if(h===Number.POSITIVE_INFINITY)return 1;return 2}function f(h,j,M){if(h.kind==="missing"||j.kind==="missing"){if(h.kind===j.kind)return 0;return h.kind==="missing"?1:-1}if(h.kind==="unsupported"||j.kind==="unsupported"){if(h.kind===j.kind)return 0;return h.kind==="unsupported"?1:-1}let z=A(h)-A(j),D=z;if(z===0){if(h.kind==="number"&&j.kind==="number")D=h.value<j.value?-1:h.value>j.value?1:0;else if(h.kind==="non-finite-number"&&j.kind==="non-finite-number")D=C(h.value)-C(j.value);else if(h.kind==="string"&&j.kind==="string")D=w(h.value,j.value);else if(h.kind==="boolean"&&j.kind==="boolean")D=Number(h.value)-Number(j.value);else if(h.kind==="bigint"&&j.kind==="bigint")D=h.value<j.value?-1:h.value>j.value?1:0}return M==="desc"?-D:D}function b(h,j,M){let z=M.sortParam??"sort",D=M.orderParam??"order",H=W(j,z)??M.default,J=(W(j,D)??M.defaultOrder??"asc")==="desc"?"desc":"asc";if(!H)return h;if(!M.allowed.includes(H))return h;return h.map((G)=>({item:G,key:S(T(G)?W(G,H):void 0)})).sort((G,U)=>f(G.key,U.key,J)).map(({item:G})=>G)}function B(h,j){if(typeof h!=="string"||!/^[0-9]+$/.test(h))return j;let M=Number(h);return Number.isSafeInteger(M)&&M>=1?M:j}function d(h,j,M){let z=M.pageParam??"page",D=M.limitParam??"limit",H=M.defaultLimit??10,Q=M.maxLimit??100,J=B(W(j,z),1),G=Math.min(Q,B(W(j,D),H)),U=h.length,X=Math.ceil(U/G),$=(J-1)*G;return{data:h.slice($,$+G),pagination:{page:J,limit:G,total:U,totalPages:X}}}export{u as queryPlugin};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@schmock/query",
3
3
  "description": "Pagination, filtering, and sorting plugin for Schmock",
4
- "version": "2.4.0",
4
+ "version": "2.4.1",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "license": "MIT",
45
45
  "peerDependencies": {
46
- "@schmock/core": "^2.4.0"
46
+ "@schmock/core": "^2.4.1"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@amiceli/vitest-cucumber": "^7.0.0",