Line data Source code
1 : import type {
2 : GlobalSetting,
3 : GlobalSettingKey,
4 : GlobalSettingValue
5 : } from '@comfyorg/ingest-types'
6 : import { zErrorResponse, zGlobalSetting } from '@comfyorg/ingest-types/zod'
7 :
8 : import { getComfyApiBaseUrl } from '@/config/comfyApi'
9 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
10 : import { isCloud } from '@/platform/distribution/types'
11 : import { ingestApiUrl } from '@/platform/remote/ingest/ingestApiUrl'
12 : import { api } from '@/scripts/api'
13 : import {
14 : fetchWithUnifiedRemint,
15 : shouldRemintCloudRequest
16 : } from '@/platform/auth/unified/remintRetry'
17 : import type { WebSessionSend } from '@comfyorg/api-client/webSessionFetch'
18 : import type { AuthHeader } from '@comfyorg/api-client/authTypes'
19 : import { retryingSend } from '@comfyorg/api-client/fetchWithRetry'
20 :
21 2356 : export class GlobalSettingsApiError extends Error {
22 : constructor(
23 : message: string,
24 0 : readonly status?: number
25 : ) {
26 0 : super(message)
27 0 : this.name = 'GlobalSettingsApiError'
28 : }
29 : }
30 :
31 0 : function globalSettingsUrl(key?: GlobalSettingKey): string {
32 0 : const path = `/global-settings${key ? `/${encodeURIComponent(key)}` : ''}`
33 0 : if (useFeatureFlags().flags.openOnCloudEnabled) return ingestApiUrl(path)
34 0 : return isCloud ? api.apiURL(path) : `${getComfyApiBaseUrl()}/api${path}`
35 : }
36 :
37 0 : async function responseBody(response: Response): Promise<unknown> {
38 0 : try {
39 0 : return await response.json()
40 : } catch {
41 0 : throw new GlobalSettingsApiError(
42 : 'Global setting returned invalid JSON',
43 : response.status
44 : )
45 : }
46 : }
47 :
48 : type GlobalSettingsAuth = AuthHeader | WebSessionSend
49 :
50 0 : function sendGlobalSetting(
51 : url: string,
52 : init: RequestInit,
53 : auth: GlobalSettingsAuth,
54 : headers: Record<string, string> = {}
55 : ): Promise<Response> {
56 0 : if (typeof auth === 'function') {
57 0 : return retryingSend(auth)(url, { ...init, headers })
58 : }
59 0 : return shouldRemintCloudRequest().then((remint) =>
60 : fetchWithUnifiedRemint(
61 : url,
62 : { ...init, headers: { ...headers, ...auth } },
63 : remint
64 : )
65 : )
66 : }
67 :
68 0 : async function storedSetting(response: Response): Promise<GlobalSetting> {
69 0 : if (!response.ok) {
70 0 : throw new GlobalSettingsApiError(
71 : `Global setting request failed: ${response.status}`,
72 : response.status
73 : )
74 : }
75 0 : const payload = zGlobalSetting.safeParse(await responseBody(response))
76 0 : if (!payload.success) {
77 0 : throw new GlobalSettingsApiError(
78 : 'Global setting returned an invalid response',
79 : response.status
80 : )
81 : }
82 0 : return payload.data
83 : }
84 :
85 0 : export async function getGlobalSetting(
86 : key: GlobalSettingKey,
87 : auth: GlobalSettingsAuth
88 : ): Promise<GlobalSetting | undefined> {
89 0 : const response = await sendGlobalSetting(
90 : globalSettingsUrl(key),
91 : { cache: 'no-store' },
92 : auth
93 : )
94 0 : if (response.status === 404) {
95 0 : const error = zErrorResponse.safeParse(await responseBody(response))
96 0 : if (error.success && error.data.code === 'NOT_FOUND') return undefined
97 : }
98 0 : return storedSetting(response)
99 : }
100 :
101 0 : export async function setGlobalSetting(
102 : setting: GlobalSettingValue,
103 : auth: GlobalSettingsAuth
104 : ): Promise<GlobalSetting> {
105 : const response = await sendGlobalSetting(
106 : globalSettingsUrl(),
107 : { method: 'POST', body: JSON.stringify(setting) },
108 : auth,
109 : { 'Content-Type': 'application/json' }
110 : )
111 0 : return storedSetting(response)
112 : }
|