-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- fix: added safe url join to avoid double slash in api calls (#104)
- Loading branch information
1 parent
0e7ad83
commit 389ec47
Showing
4 changed files
with
42 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { urlJoin } from './url-join'; | ||
|
||
describe('url join', () => { | ||
const sharedExpectedValue = 'localhost:5555/api/v1/send'; | ||
const cases = [ | ||
{ data: ['localhost:5555', 'api', 'v1/send'], expectedValue: sharedExpectedValue }, | ||
{ data: ['localhost:5555/', '/api/', 'v1/send/'], expectedValue: sharedExpectedValue }, | ||
{ data: ['localhost:5555///', 'api//', '/v1/send'], expectedValue: sharedExpectedValue }, | ||
{ data: ['localhost:5555', 'api//', '//v1/send/'], expectedValue: sharedExpectedValue }, | ||
{ data: ['localhost:5555', 'api//', '//v1/send?foo=bar/bar2'], expectedValue: `${sharedExpectedValue}?foo=bar/bar2` }, | ||
{ data: ['localhost:5555', '/', ''], expectedValue: 'localhost:5555' }, | ||
{ data: ['localhost:5555'], expectedValue: 'localhost:5555' }, | ||
{ data: ['https://google.com/', 'auth/', 'foo'], expectedValue: 'https://google.com/auth/foo' }, | ||
]; | ||
test.each(cases)('should generate a valid url', async ({ data, expectedValue }) => { | ||
const url = urlJoin(...data); | ||
expect(url).toBe(expectedValue); | ||
}); | ||
}); |
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,5 @@ | ||
// It safe join url chunks avoiding double '/' between paths | ||
// Warning: It doesn't supports all cases but it's enough for join shinkai-node api urls | ||
export const urlJoin = (...chunks: string[]): string => { | ||
return chunks.map(chunk => chunk.replace(/(^\/+|\/+$)/mg, '')).filter(chunk => !!chunk).join('/') | ||
}; |