Yuki Takei пре 2 година
родитељ
комит
6d1de1be20

+ 8 - 12
packages/remark-lsx/src/server/routes/list-pages/add-num-condition.spec.ts

@@ -6,20 +6,16 @@ import type { PageQuery } from './generate-base-query';
 
 
 describe('addNumCondition() throws 400 http-errors instance ', () => {
 describe('addNumCondition() throws 400 http-errors instance ', () => {
 
 
-  it.concurrent.each`
-    offset  | limit
-    ${-1}   | ${9}
-    ${1}    | ${-9}
-  `('when the specified condition is { offset: $offset, limit: $limit }', ({ offset, limit }) => {
+  it("when the param 'offset' is a negative value", () => {
 
 
     // setup
     // setup
     const queryMock = mock<PageQuery>();
     const queryMock = mock<PageQuery>();
 
 
     // when
     // when
-    const caller = () => addNumCondition(queryMock, offset, limit);
+    const caller = () => addNumCondition(queryMock, -1, 10);
 
 
     // then
     // then
-    expect(caller).toThrowError(createError(400, "Both specified 'offset' or 'limit must be larger or equal than 0"));
+    expect(caller).toThrowError(createError(400, "The param 'offset' must be larger or equal than 0"));
     expect(queryMock.skip).not.toHaveBeenCalledWith();
     expect(queryMock.skip).not.toHaveBeenCalledWith();
     expect(queryMock.limit).not.toHaveBeenCalledWith();
     expect(queryMock.limit).not.toHaveBeenCalledWith();
   });
   });
@@ -29,11 +25,11 @@ describe('addNumCondition() throws 400 http-errors instance ', () => {
 describe('addNumCondition() set skip and limit with', () => {
 describe('addNumCondition() set skip and limit with', () => {
 
 
   it.concurrent.each`
   it.concurrent.each`
-    offset  | limit                   | expectedSkip   | expectedLimit
-    ${1}    | ${Number.MAX_VALUE}     | ${1}           | ${null}
-    ${0}    | ${10}                   | ${null}        | ${10}
-    ${0}    | ${Number.MAX_VALUE}     | ${null}        | ${null}
-    ${NaN}  | ${NaN}                  | ${null}        | ${null}
+    offset  | limit     | expectedSkip   | expectedLimit
+    ${1}    | ${-1}     | ${1}           | ${null}
+    ${0}    | ${0}      | ${null}        | ${0}
+    ${0}    | ${10}     | ${null}        | ${10}
+    ${NaN}  | ${NaN}    | ${null}        | ${null}
   `("{ offset: $offset, limit: $limit }'", ({
   `("{ offset: $offset, limit: $limit }'", ({
     offset, limit, expectedSkip, expectedLimit,
     offset, limit, expectedSkip, expectedLimit,
   }) => {
   }) => {

+ 8 - 4
packages/remark-lsx/src/server/routes/list-pages/add-num-condition.ts

@@ -11,15 +11,19 @@ const DEFAULT_PAGES_NUM = 50;
 export const addNumCondition = (query: PageQuery, offset = 0, limit = DEFAULT_PAGES_NUM): PageQuery => {
 export const addNumCondition = (query: PageQuery, offset = 0, limit = DEFAULT_PAGES_NUM): PageQuery => {
 
 
   // check offset
   // check offset
-  if (offset < 0 || limit < 0) {
-    throw createError(400, "Both specified 'offset' or 'limit must be larger or equal than 0");
+  if (offset < 0) {
+    throw createError(400, "The param 'offset' must be larger or equal than 0");
+  }
+  // check offset
+  if (offset < 0) {
+    throw createError(400, "The param 'offset' must be larger or equal than 0");
   }
   }
 
 
   let q = query;
   let q = query;
-  if (offset !== 0) {
+  if (offset > 0) {
     q = q.skip(offset);
     q = q.skip(offset);
   }
   }
-  if (limit !== Number.MAX_VALUE) {
+  if (limit >= 0) {
     q = q.limit(limit);
     q = q.limit(limit);
   }
   }
 
 

+ 1 - 0
packages/remark-lsx/src/stores/lsx/lsx.tsx

@@ -20,6 +20,7 @@ export const useSWRxLsx = (pagePath: string, options?: Record<string, string|und
         const offsetAndLimit = options?.num != null
         const offsetAndLimit = options?.num != null
           ? parseNumOption(options.num)
           ? parseNumOption(options.num)
           : null;
           : null;
+        delete options?.num;
 
 
         const params: LsxApiParams = {
         const params: LsxApiParams = {
           pagePath,
           pagePath,

+ 15 - 2
packages/remark-lsx/src/stores/lsx/parse-num-option.spec.ts

@@ -36,10 +36,22 @@ describe('addNumCondition()', () => {
     const caller = () => parseNumOption('-1:10');
     const caller = () => parseNumOption('-1:10');
 
 
     // then
     // then
-    expect(caller).toThrowError();
+    expect(caller).toThrowError("The specified option 'num' is [-1:10] : the start must be larger or equal than 1");
     expect(parseRangeSpy).toHaveBeenCalledWith('-1:10');
     expect(parseRangeSpy).toHaveBeenCalledWith('-1:10');
   });
   });
 
 
+  it('throws an error when the end value is smaller than the start value', () => {
+    // setup
+    const parseRangeSpy = vi.spyOn(OptionParser, 'parseRange');
+
+    // when
+    const caller = () => parseNumOption('3:2');
+
+    // then
+    expect(caller).toThrowError("The specified option 'num' is [3:2] : the end must be larger or equal than the start");
+    expect(parseRangeSpy).toHaveBeenCalledWith('3:2');
+  });
+
 });
 });
 
 
 
 
@@ -48,7 +60,8 @@ describe('addNumCondition() set skip and limit with the range string', () => {
   it.concurrent.each`
   it.concurrent.each`
     optionsNum    | expected
     optionsNum    | expected
     ${'1:10'}     | ${{ offset: 0, limit: 10 }}
     ${'1:10'}     | ${{ offset: 0, limit: 10 }}
-    ${'3:'}       | ${{ offset: 2, limit: Number.MAX_VALUE }}
+    ${'2:2'}      | ${{ offset: 1, limit: 1 }}
+    ${'3:'}       | ${{ offset: 2, limit: -1 }}
   `("'$optionsNum", ({ optionsNum, expected }) => {
   `("'$optionsNum", ({ optionsNum, expected }) => {
     // setup
     // setup
     const parseRangeSpy = vi.spyOn(OptionParser, 'parseRange');
     const parseRangeSpy = vi.spyOn(OptionParser, 'parseRange');

+ 6 - 5
packages/remark-lsx/src/stores/lsx/parse-num-option.ts

@@ -24,12 +24,13 @@ export const parseNumOption = (optionsNum: string): ParseNumOptionResult | null
   if (start < 1) {
   if (start < 1) {
     throw new Error(`The specified option 'num' is [${start}:${end}] : the start must be larger or equal than 1`);
     throw new Error(`The specified option 'num' is [${start}:${end}] : the start must be larger or equal than 1`);
   }
   }
+  // check end
+  if (start > end && end > 0) {
+    throw new Error(`The specified option 'num' is [${start}:${end}] : the end must be larger or equal than the start`);
+  }
 
 
   const offset = start - 1;
   const offset = start - 1;
-  const limit = end - offset;
+  const limit = Math.max(-1, end - offset);
 
 
-  return {
-    offset,
-    limit: (limit > 0) ? limit : Number.MAX_VALUE,
-  };
+  return { offset, limit };
 };
 };