Files
dify/web/service/common.ts
T

87 lines
2.3 KiB
TypeScript

import type { CommonResponse } from '@/models/common'
import { post } from './base'
type LoginSuccess = {
result: 'success'
data?: { access_token?: string }
}
type LoginFail = {
result: 'fail'
data: string
code: string
message: string
}
type LoginResponse = LoginSuccess | LoginFail
export const webAppLogin = ({
url,
body,
}: {
url: string
body: Record<string, unknown>
}): Promise<LoginResponse> => {
return post<LoginResponse>(url, { body }, { isPublicAPI: true })
}
export const changeWebAppPasswordWithToken = ({
url,
body,
}: {
url: string
body: { token: string; new_password: string; password_confirm: string }
}): Promise<CommonResponse> => post<CommonResponse>(url, { body }, { isPublicAPI: true })
export const uploadPublicRemoteFileInfo = (url: string, options?: { silent?: boolean }) => {
return post<{ id: string; name: string; size: number; mime_type: string; url: string }>(
'/remote-files/upload',
{ body: { url } },
{ isPublicAPI: true, ...options },
)
}
export const sendWebAppEMailLoginCode = (
email: string,
language = 'en-US',
): Promise<CommonResponse & { data: string }> =>
post<CommonResponse & { data: string }>(
'/email-code-login',
{ body: { email, language } },
{ isPublicAPI: true },
)
export const webAppEmailLoginWithCode = (data: {
email: string
code: string
token: string
}): Promise<LoginResponse> =>
post<LoginResponse>('/email-code-login/validity', { body: data }, { isPublicAPI: true })
export const sendWebAppResetPasswordCode = (
email: string,
language = 'en-US',
): Promise<CommonResponse & { data: string; message?: string; code?: string }> =>
post<CommonResponse & { data: string; message?: string; code?: string }>(
'/forgot-password',
{ body: { email, language } },
{ isPublicAPI: true },
)
export const verifyWebAppResetPasswordCode = (body: {
email: string
code: string
token: string
}): Promise<CommonResponse & { is_valid: boolean; token: string }> =>
post<CommonResponse & { is_valid: boolean; token: string }>(
'/forgot-password/validity',
{ body },
{ isPublicAPI: true },
)
export const getAvatar = async ({
avatar,
}: {
avatar: string
}): Promise<{ avatar_url: string }> => {
const { consoleClient } = await import('@/service/console')
return consoleClient.account.avatar.get({ query: { avatar } })
}