|
| 1 | +import type { AuthenticationConfig } from '$declarations/satellite/satellite.did'; |
| 2 | +import { setAuthConfig } from '$lib/api/satellites.api'; |
| 3 | +import { setCustomDomain } from '$lib/services/custom-domain.services'; |
| 4 | +import { execute } from '$lib/services/progress.services'; |
| 5 | +import { i18n } from '$lib/stores/i18n.store'; |
| 6 | +import { toasts } from '$lib/stores/toasts.store'; |
| 7 | +import type { OptionIdentity } from '$lib/types/itentity'; |
| 8 | +import { type HostingProgress, HostingProgressStep } from '$lib/types/progress-hosting'; |
| 9 | +import type { Option } from '$lib/types/utils'; |
| 10 | +import { Principal } from '@dfinity/principal'; |
| 11 | +import { assertNonNullish, nonNullish } from '@dfinity/utils'; |
| 12 | +import { get } from 'svelte/store'; |
| 13 | + |
| 14 | +export const configHosting = async ({ |
| 15 | + satelliteId, |
| 16 | + editConfig, |
| 17 | + domainName, |
| 18 | + identity, |
| 19 | + onProgress |
| 20 | +}: { |
| 21 | + domainName: string; |
| 22 | + editConfig: Option<AuthenticationConfig>; |
| 23 | + satelliteId: Principal; |
| 24 | + identity: OptionIdentity; |
| 25 | + onProgress: (progress: HostingProgress | undefined) => void; |
| 26 | +}): Promise<{ success: 'ok' | 'error'; err?: unknown }> => { |
| 27 | + try { |
| 28 | + assertNonNullish(identity, get(i18n).core.not_logged_in); |
| 29 | + |
| 30 | + const configCustomDomain = async () => |
| 31 | + await setCustomDomain({ |
| 32 | + satelliteId, |
| 33 | + domainName |
| 34 | + }); |
| 35 | + |
| 36 | + await execute({ fn: configCustomDomain, onProgress, step: HostingProgressStep.CustomDomain }); |
| 37 | + |
| 38 | + if (nonNullish(editConfig)) { |
| 39 | + const configAuth = async () => |
| 40 | + await setAuthConfig({ |
| 41 | + satelliteId, |
| 42 | + config: editConfig, |
| 43 | + identity |
| 44 | + }); |
| 45 | + |
| 46 | + await execute({ fn: configAuth, onProgress, step: HostingProgressStep.AuthConfig }); |
| 47 | + } |
| 48 | + } catch (err: unknown) { |
| 49 | + const labels = get(i18n); |
| 50 | + |
| 51 | + toasts.error({ |
| 52 | + text: labels.errors.hosting_configuration_issues, |
| 53 | + detail: err |
| 54 | + }); |
| 55 | + |
| 56 | + return { success: 'error', err }; |
| 57 | + } |
| 58 | + |
| 59 | + return { success: 'ok' }; |
| 60 | +}; |
0 commit comments