-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: relative urls and support react 19 (#3407)
Closing #3405
- Loading branch information
Showing
19 changed files
with
923 additions
and
413 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,53 @@ | ||
import { TolgeeCore } from '@tolgee/core'; | ||
import { createFetchingUtility } from './fetchingUtillity'; | ||
import { DevBackend } from '../DevBackend'; | ||
|
||
describe('can handle relative urls in apiUrl', () => { | ||
let f: ReturnType<typeof createFetchingUtility>; | ||
|
||
beforeEach(() => { | ||
f = createFetchingUtility(); | ||
}); | ||
|
||
it('dev backend can resolve relative urls', async () => { | ||
const fetchMock = f.fetchWithResponse({}); | ||
const tolgee = TolgeeCore() | ||
.use(DevBackend()) | ||
.init({ | ||
language: 'en', | ||
availableLanguages: ['en'], | ||
fetch: fetchMock, | ||
apiUrl: '/test', | ||
apiKey: 'test', | ||
}); | ||
await expect(tolgee.loadRecord({ language: 'en' })).resolves.toEqual( | ||
new Map() | ||
); | ||
expect(fetchMock).toHaveBeenCalledTimes(1); | ||
const args = fetchMock.mock.calls[0] as any; | ||
expect(args[0]).toEqual( | ||
'http://localhost/test/v2/projects/translations/en' | ||
); | ||
}); | ||
|
||
it('dev backend can resolve apiUrl with included path', async () => { | ||
const fetchMock = f.fetchWithResponse({}); | ||
const tolgee = TolgeeCore() | ||
.use(DevBackend()) | ||
.init({ | ||
language: 'en', | ||
availableLanguages: ['en'], | ||
fetch: fetchMock, | ||
apiUrl: 'https://test.com/abcd', | ||
apiKey: 'test', | ||
}); | ||
await expect(tolgee.loadRecord({ language: 'en' })).resolves.toEqual( | ||
new Map() | ||
); | ||
expect(fetchMock).toHaveBeenCalledTimes(1); | ||
const args = fetchMock.mock.calls[0] as any; | ||
expect(args[0]).toEqual( | ||
'https://test.com/abcd/v2/projects/translations/en' | ||
); | ||
}); | ||
}); |
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,15 @@ | ||
import { joinUrls } from './url'; | ||
|
||
describe('url module', () => { | ||
it('joins urls without double slash', () => { | ||
expect(joinUrls('a/b/', '/c')).toEqual('a/b/c'); | ||
}); | ||
|
||
it("doesn't remove last slash", () => { | ||
expect(joinUrls('a/b/', '/c/')).toEqual('a/b/c/'); | ||
}); | ||
|
||
it("doesn't touch the protocol", () => { | ||
expect(joinUrls('https://test.com/', '/c/')).toEqual('https://test.com/c/'); | ||
}); | ||
}); |
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,21 @@ | ||
function composeUrl(base: string, path: string): string { | ||
// Ensure the base URL does not end with a slash | ||
base = base.replace(/\/+$/, ''); | ||
// Ensure the path does not start with a slash | ||
path = path.replace(/^\/+/, ''); | ||
// Combine the two parts with a single slash in between | ||
return `${base}/${path}`; | ||
} | ||
|
||
export function joinUrls(...parts: string[]): string { | ||
let result = parts[0]; | ||
parts.slice(1).forEach((part) => { | ||
result = composeUrl(result, part); | ||
}); | ||
return result; | ||
} | ||
|
||
export function createUrl(...parts: string[]): URL { | ||
const base = typeof window === undefined ? undefined : window.location.origin; | ||
return new URL(joinUrls(...parts), base); | ||
} |
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
Oops, something went wrong.