|
1 | 1 | import type { ApiClient } from '../api'; |
2 | | -import { API_URL, API_VERSION } from '../constants'; |
| 2 | +import { mergePreDefinedOptions } from '../util/mergePreDefinedOptions'; |
3 | 3 | import type { LoadInterstitialOptions } from './interstitial'; |
4 | 4 | import { loadInterstitialFromLocal } from './interstitial'; |
5 | 5 | import type { AuthenticateRequestOptions } from './request'; |
6 | 6 | import { authenticateRequest as authenticateRequestOriginal, debugRequestState } from './request'; |
7 | 7 |
|
| 8 | +type RunTimeOptions = Omit<AuthenticateRequestOptions, 'apiUrl' | 'apiVersion'>; |
| 9 | + |
| 10 | +type BuildTimeOptions = Partial< |
| 11 | + Pick< |
| 12 | + AuthenticateRequestOptions, |
| 13 | + | 'apiUrl' |
| 14 | + | 'apiVersion' |
| 15 | + | 'audience' |
| 16 | + | 'domain' |
| 17 | + | 'isSatellite' |
| 18 | + | 'jwtKey' |
| 19 | + | 'proxyUrl' |
| 20 | + | 'publishableKey' |
| 21 | + | 'secretKey' |
| 22 | + > |
| 23 | +>; |
| 24 | + |
| 25 | +const defaultOptions = { |
| 26 | + secretKey: '', |
| 27 | + jwtKey: '', |
| 28 | + apiUrl: undefined, |
| 29 | + apiVersion: undefined, |
| 30 | + proxyUrl: '', |
| 31 | + publishableKey: '', |
| 32 | + isSatellite: false, |
| 33 | + domain: '', |
| 34 | + audience: '', |
| 35 | +} satisfies BuildTimeOptions; |
| 36 | + |
8 | 37 | export type CreateAuthenticateRequestOptions = { |
9 | | - options: Partial< |
10 | | - Pick< |
11 | | - AuthenticateRequestOptions, |
12 | | - | 'audience' |
13 | | - | 'secretKey' |
14 | | - | 'apiUrl' |
15 | | - | 'apiVersion' |
16 | | - | 'publishableKey' |
17 | | - | 'jwtKey' |
18 | | - | 'proxyUrl' |
19 | | - | 'domain' |
20 | | - | 'isSatellite' |
21 | | - > |
22 | | - >; |
| 38 | + options: BuildTimeOptions; |
23 | 39 | apiClient: ApiClient; |
24 | 40 | }; |
25 | 41 |
|
26 | 42 | export function createAuthenticateRequest(params: CreateAuthenticateRequestOptions) { |
27 | 43 | const { apiClient } = params; |
28 | | - const { |
29 | | - secretKey: buildtimeSecretKey = '', |
30 | | - jwtKey: buildtimeJwtKey = '', |
31 | | - apiUrl = API_URL, |
32 | | - apiVersion = API_VERSION, |
33 | | - proxyUrl: buildProxyUrl = '', |
34 | | - publishableKey: buildtimePublishableKey = '', |
35 | | - isSatellite: buildtimeIsSatellite = false, |
36 | | - domain: buildtimeDomain = '', |
37 | | - audience: buildtimeAudience = '', |
38 | | - } = params.options; |
| 44 | + const buildTimeOptions = mergePreDefinedOptions(defaultOptions, params.options); |
39 | 45 |
|
40 | | - const authenticateRequest = ({ |
41 | | - secretKey: runtimeSecretKey, |
42 | | - audience: runtimeAudience, |
43 | | - proxyUrl: runtimeProxyUrl, |
44 | | - publishableKey: runtimePublishableKey, |
45 | | - jwtKey: runtimeJwtKey, |
46 | | - isSatellite: runtimeIsSatellite, |
47 | | - domain: runtimeDomain, |
48 | | - ...rest |
49 | | - }: Omit<AuthenticateRequestOptions, 'apiUrl' | 'apiVersion'>) => { |
| 46 | + const authenticateRequest = (options: RunTimeOptions) => { |
| 47 | + const { apiUrl, apiVersion } = buildTimeOptions; |
| 48 | + const runTimeOptions = mergePreDefinedOptions(buildTimeOptions, options); |
50 | 49 | return authenticateRequestOriginal({ |
51 | | - ...rest, |
52 | | - secretKey: runtimeSecretKey || buildtimeSecretKey, |
53 | | - audience: runtimeAudience || buildtimeAudience, |
| 50 | + ...options, |
| 51 | + ...runTimeOptions, |
| 52 | + // We should add all the omitted props from options here (eg apiUrl / apiVersion) |
| 53 | + // to avoid runtime options override them. |
54 | 54 | apiUrl, |
55 | 55 | apiVersion, |
56 | | - proxyUrl: runtimeProxyUrl || buildProxyUrl, |
57 | | - publishableKey: runtimePublishableKey || buildtimePublishableKey, |
58 | | - isSatellite: runtimeIsSatellite || buildtimeIsSatellite, |
59 | | - domain: runtimeDomain || buildtimeDomain, |
60 | | - jwtKey: runtimeJwtKey || buildtimeJwtKey, |
61 | 56 | }); |
62 | 57 | }; |
63 | 58 |
|
64 | | - const localInterstitial = ({ |
65 | | - publishableKey: runtimePublishableKey, |
66 | | - proxyUrl: runtimeProxyUrl, |
67 | | - isSatellite: runtimeIsSatellite, |
68 | | - domain: runtimeDomain, |
69 | | - ...rest |
70 | | - }: Omit<LoadInterstitialOptions, 'apiUrl'>) => |
71 | | - loadInterstitialFromLocal({ |
72 | | - ...rest, |
73 | | - proxyUrl: runtimeProxyUrl || buildProxyUrl, |
74 | | - publishableKey: runtimePublishableKey || buildtimePublishableKey, |
75 | | - isSatellite: runtimeIsSatellite || buildtimeIsSatellite, |
76 | | - domain: runtimeDomain || buildtimeDomain, |
77 | | - }); |
| 59 | + const localInterstitial = (options: Omit<LoadInterstitialOptions, 'apiUrl'>) => { |
| 60 | + const runTimeOptions = mergePreDefinedOptions(buildTimeOptions, options); |
| 61 | + return loadInterstitialFromLocal({ ...options, ...runTimeOptions }); |
| 62 | + }; |
78 | 63 |
|
79 | 64 | // TODO: Replace this function with remotePublicInterstitial |
80 | 65 | const remotePrivateInterstitial = () => apiClient.interstitial.getInterstitial(); |
|
0 commit comments