|
1 | 1 | import { DVCPopulatedUser } from '@devcycle/js-client-sdk'
|
2 | 2 | import { serializeUserSearchParams } from '../common/serializeUser'
|
3 | 3 | import { cache } from 'react'
|
4 |
| -import { BucketedUserConfig } from '@devcycle/types' |
| 4 | +import { BucketedUserConfig, ConfigBody } from '@devcycle/types' |
5 | 5 |
|
6 | 6 | const getFetchUrl = (sdkKey: string, obfuscated: boolean) =>
|
7 | 7 | `https://config-cdn.devcycle.com/config/v2/server/bootstrap/${
|
8 | 8 | obfuscated ? 'obfuscated/' : ''
|
9 | 9 | }${sdkKey}.json`
|
10 | 10 |
|
11 |
| -export const fetchCDNConfig = async ( |
12 |
| - sdkKey: string, |
13 |
| - clientSDKKey: string, |
14 |
| - obfuscated: boolean, |
15 |
| -): Promise<Response> => { |
16 |
| - return await fetch( |
17 |
| - getFetchUrl(sdkKey, obfuscated), |
18 |
| - // only store for 60 seconds |
19 |
| - { |
20 |
| - next: { |
21 |
| - revalidate: 60, |
22 |
| - tags: [sdkKey, clientSDKKey], |
| 11 | +export const fetchCDNConfig = cache( |
| 12 | + async ( |
| 13 | + sdkKey: string, |
| 14 | + clientSDKKey: string, |
| 15 | + obfuscated: boolean, |
| 16 | + ): Promise<{ config: ConfigBody; headers: Headers }> => { |
| 17 | + const response = await fetch( |
| 18 | + getFetchUrl(sdkKey, obfuscated), |
| 19 | + // only store for 60 seconds |
| 20 | + { |
| 21 | + next: { |
| 22 | + revalidate: 60, |
| 23 | + tags: [sdkKey, clientSDKKey], |
| 24 | + }, |
23 | 25 | },
|
24 |
| - }, |
25 |
| - ) |
26 |
| -} |
| 26 | + ) |
| 27 | + |
| 28 | + if (!response.ok) { |
| 29 | + const responseText = await response.text() |
| 30 | + throw new Error('Could not fetch config: ' + responseText) |
| 31 | + } |
| 32 | + return { |
| 33 | + config: (await response.json()) as ConfigBody, |
| 34 | + headers: response.headers, |
| 35 | + } |
| 36 | + }, |
| 37 | +) |
27 | 38 |
|
28 | 39 | const getSDKAPIUrl = (
|
29 | 40 | sdkKey: string,
|
|
0 commit comments