-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Content from base url (#871) RELEASE
## Related Issues Fixes descope/etc#8253
- Loading branch information
Showing
11 changed files
with
918 additions
and
152 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
40 changes: 40 additions & 0 deletions
40
packages/libs/sdk-mixins/src/mixins/staticResourcesMixin/fetchWithFallbacks.ts
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,40 @@ | ||
import { Logger } from '../loggerMixin'; | ||
|
||
type FetchParams = Parameters<typeof fetch>; | ||
const notLastMsgSuffix = 'Trying the next fallback URL...'; | ||
|
||
export const fetchWithFallbacks = async ( | ||
fallbacks: FetchParams['0'] | FetchParams['0'][], | ||
init: FetchParams['1'], | ||
{ | ||
logger, | ||
onSuccess, | ||
}: { logger?: Logger; onSuccess?: (urlIndex: number) => void }, | ||
): ReturnType<typeof fetch> => { | ||
const fallbacksArr = Array.isArray(fallbacks) ? fallbacks : [fallbacks]; | ||
|
||
for (let index = 0; index < fallbacksArr.length; index++) { | ||
const url = fallbacksArr[index]; | ||
const isLast = index === fallbacksArr.length - 1; | ||
|
||
try { | ||
const res = await fetch(url.toString(), init); | ||
if (res.ok) { | ||
onSuccess?.(index); | ||
return res; | ||
} | ||
|
||
const errMsg = `Error fetching URL ${url} [${res.status}]`; | ||
|
||
if (isLast) throw new Error(errMsg); | ||
|
||
logger?.debug(`${errMsg}. ${notLastMsgSuffix}`); | ||
} catch (e) { | ||
const errMsg = `Error fetching URL ${url} [${e.message}]`; | ||
|
||
if (isLast) throw new Error(errMsg); | ||
|
||
logger?.debug(`${errMsg}. ${notLastMsgSuffix}`); | ||
} | ||
} | ||
}; |
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
149 changes: 149 additions & 0 deletions
149
packages/libs/sdk-mixins/test/staticResourcesMixin.test.ts
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,149 @@ | ||
import { staticResourcesMixin } from '../src'; | ||
|
||
const createResponse = async ({ | ||
body, | ||
statusCode = 200, | ||
}: { | ||
body: string; | ||
statusCode: number; | ||
}) => ({ | ||
json: async () => JSON.parse(body), | ||
text: async () => body, | ||
ok: statusCode < 400, | ||
}); | ||
|
||
const createMixin = (config: Record<string, any>) => { | ||
const MixinClass = staticResourcesMixin( | ||
class { | ||
getAttribute(attr: string) { | ||
return config[attr]; | ||
} | ||
} as any, | ||
); | ||
|
||
const mixin = new MixinClass(); | ||
|
||
mixin.logger = { debug: jest.fn() }; | ||
|
||
return mixin; | ||
}; | ||
|
||
describe('staticResourcesMixin', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('should fetch resource from static base url', async () => { | ||
globalThis.fetch = jest | ||
.fn() | ||
.mockResolvedValue(createResponse({ body: '{}', statusCode: 400 })); | ||
const mixin = createMixin({ | ||
'base-static-url': 'https://static.example.com/pages', | ||
'project-id': '123', | ||
'base-url': 'https://example.com', | ||
}); | ||
await mixin.fetchStaticResource('file', 'json'); | ||
|
||
expect(globalThis.fetch).toHaveBeenCalledWith( | ||
'https://static.example.com/pages/123/v2-beta/file', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should try to fetch resource from base url if there is no static content', async () => { | ||
globalThis.fetch = jest | ||
.fn() | ||
.mockResolvedValue(createResponse({ body: '{}', statusCode: 400 })); | ||
const mixin = createMixin({ | ||
'project-id': '123', | ||
'base-url': 'https://example.com', | ||
}); | ||
await mixin.fetchStaticResource('file', 'json'); | ||
|
||
expect(globalThis.fetch).toHaveBeenNthCalledWith( | ||
1, | ||
'https://example.com/pages/123/v2-beta/file', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenNthCalledWith( | ||
2, | ||
'https://static.descope.com/pages/123/v2-beta/file', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should fetch resource from default url if there is no base url and base static url', async () => { | ||
globalThis.fetch = jest | ||
.fn() | ||
.mockResolvedValue(createResponse({ body: '{}', statusCode: 400 })); | ||
const mixin = createMixin({ 'project-id': '123' }); | ||
await mixin.fetchStaticResource('file', 'json'); | ||
|
||
expect(globalThis.fetch).toHaveBeenCalledWith( | ||
'https://static.descope.com/pages/123/v2-beta/file', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should keep fetching content from base url in case it was ok', async () => { | ||
globalThis.fetch = jest | ||
.fn() | ||
.mockResolvedValueOnce(createResponse({ body: '{}', statusCode: 200 })); | ||
const mixin = createMixin({ | ||
'project-id': '123', | ||
'base-url': 'https://example.com', | ||
}); | ||
await mixin.fetchStaticResource('file', 'json'); | ||
|
||
expect(globalThis.fetch).toHaveBeenNthCalledWith( | ||
1, | ||
'https://example.com/pages/123/v2-beta/file', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenCalledTimes(1); | ||
|
||
globalThis.fetch = jest | ||
.fn() | ||
.mockResolvedValueOnce(createResponse({ body: '{}', statusCode: 400 })); | ||
|
||
await mixin.fetchStaticResource('file2', 'json'); | ||
expect(globalThis.fetch).toHaveBeenNthCalledWith( | ||
1, | ||
'https://example.com/pages/123/v2-beta/file2', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not keep fetching content from base url in case it was not ok', async () => { | ||
globalThis.fetch = jest | ||
.fn() | ||
.mockResolvedValueOnce(createResponse({ body: '{}', statusCode: 400 })); | ||
const mixin = createMixin({ | ||
'project-id': '123', | ||
'base-url': 'https://example.com', | ||
}); | ||
await mixin.fetchStaticResource('file', 'json'); | ||
|
||
expect(globalThis.fetch).toHaveBeenNthCalledWith( | ||
1, | ||
'https://example.com/pages/123/v2-beta/file', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenCalledTimes(2); | ||
|
||
globalThis.fetch = jest | ||
.fn() | ||
.mockResolvedValueOnce(createResponse({ body: '{}', statusCode: 400 })); | ||
|
||
await mixin.fetchStaticResource('file2', 'json'); | ||
expect(globalThis.fetch).toHaveBeenNthCalledWith( | ||
1, | ||
'https://static.descope.com/pages/123/v2-beta/file2', | ||
expect.any(Object), | ||
); | ||
expect(globalThis.fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.