Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { nextLink } from './next-link'; | ||
import { Request } from 'express'; | ||
|
||
describe('nextLink', () => { | ||
it('should generate the correct next link with a cursor', () => { | ||
const req = { | ||
baseUrl: '/api/events', | ||
path: '/', | ||
query: { page: '2', limit: '10' }, | ||
} as Pick<Request, 'baseUrl' | 'path' | 'query'>; | ||
|
||
const cursor = 'abc123'; | ||
const result = nextLink(req, cursor); | ||
|
||
expect(result).toBe('/api/events/?page=2&limit=10&cursor=abc123'); | ||
}); | ||
|
||
it('should generate the correct next link without a cursor', () => { | ||
const req = { | ||
baseUrl: '/api/events', | ||
path: '/', | ||
query: { page: '2', limit: '10' }, | ||
} as Pick<Request, 'baseUrl' | 'path' | 'query'>; | ||
|
||
const result = nextLink(req); | ||
|
||
expect(result).toBe(''); | ||
}); | ||
|
||
it('should exclude existing cursor from query parameters', () => { | ||
const req = { | ||
baseUrl: '/api/events', | ||
path: '/', | ||
query: { page: '2', limit: '10', cursor: 'oldCursor' }, | ||
} as Pick<Request, 'baseUrl' | 'path' | 'query'>; | ||
|
||
const cursor = 'newCursor'; | ||
const result = nextLink(req, cursor); | ||
|
||
expect(result).toBe('/api/events/?page=2&limit=10&cursor=newCursor'); | ||
}); | ||
|
||
it('should handle empty query parameters correctly', () => { | ||
const req = { | ||
baseUrl: '/api/events', | ||
path: '/', | ||
query: {}, | ||
} as Pick<Request, 'baseUrl' | 'path' | 'query'>; | ||
|
||
const cursor = 'abc123'; | ||
const result = nextLink(req, cursor); | ||
|
||
expect(result).toBe('/api/events/?cursor=abc123'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Request } from 'express'; | ||
|
||
export function nextLink( | ||
req: Pick<Request, 'baseUrl' | 'path' | 'query'>, | ||
cursor?: string, | ||
): string { | ||
if (!cursor) { | ||
return ''; | ||
} | ||
|
||
const url = `${req.baseUrl}${req.path}?`; | ||
|
||
const params = new URLSearchParams(req.query as Record<string, string>); | ||
|
||
params.set('cursor', cursor); | ||
|
||
return `${url}${params.toString()}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters