Implementation:Langgenius Dify Service Common
| Knowledge Sources | |
|---|---|
| Domains | Frontend, API_Service |
| Last Updated | 2026-02-12 07:00 GMT |
Overview
Common API service functions providing typed wrappers around all shared platform endpoints including authentication, workspace management, model provider configuration, member management, and account operations.
Description
common.ts is the primary API service module that aggregates all common, cross-cutting API calls used throughout the Dify frontend. It imports the HTTP method helpers (get, post, put, del, patch) from base.ts and exposes strongly-typed functions for each endpoint. The module covers a wide range of functionality: authentication (login, OAuth, email-code login, forgot password), workspace management (fetch, switch, update workspaces), member management (invite, update roles, delete, ownership transfer), model provider configuration (CRUD for providers, credentials, load balancing, default models), data source integration (Notion sync, API-based extensions, code-based extensions), and account operations (profile updates, delete account, change email). Each function accepts a typed parameter object and returns a typed Promise. Public API variants (webAppLogin, sendWebAppForgotPasswordEmail, etc.) pass isPublicAPI: true to route through the public API prefix.
Usage
Use this module when building UI components that need to interact with common platform APIs. Import individual functions as needed for login flows, workspace settings, model provider management, or member administration. These functions are often consumed by React Query hooks in use-common.ts for data fetching with caching.
Code Reference
Source Location
- Repository: Langgenius_Dify
- File: web/service/common.ts
- Lines: 1-385
Signature
// Authentication
export const login = ({ url, body }: { url: string, body: Record<string, any> }): Promise<LoginResponse>
export const webAppLogin = ({ url, body }: { url: string, body: Record<string, any> }): Promise<LoginResponse>
export const setup = ({ body }: { body: Record<string, any> }): Promise<CommonResponse>
export const initValidate = ({ body }: { body: Record<string, any> }): Promise<CommonResponse>
// User profile
export const fetchUserProfile = ({ url, params }: ...): Promise<UserProfileOriginResponse>
export const updateUserProfile = ({ url, body }: ...): Promise<CommonResponse>
// Workspace management
export const fetchCurrentWorkspace = ({ url, params }: ...): Promise<ICurrentWorkspace>
export const updateCurrentWorkspace = ({ url, body }: ...): Promise<ICurrentWorkspace>
export const fetchWorkspaces = ({ url, params }: ...): Promise<{ workspaces: IWorkspace[] }>
export const switchWorkspace = ({ url, body }: ...): Promise<CommonResponse & { new_tenant: IWorkspace }>
// Member management
export const fetchMembers = ({ url, params }: ...): Promise<{ accounts: Member[] | null }>
export const inviteMember = ({ url, body }: ...): Promise<InvitationResponse>
export const updateMemberRole = ({ url, body }: ...): Promise<CommonResponse>
export const deleteMemberOrCancelInvitation = ({ url }: ...): Promise<CommonResponse>
// Model providers
export const fetchModelProviders = (url: string): Promise<{ data: ModelProvider[] }>
export const fetchModelProviderCredentials = (url: string): Promise<ModelProviderCredentials>
export const fetchModelList = (url: string): Promise<{ data: Model[] }>
export const setModelProvider = ({ url, body }: ...): Promise<CommonResponse>
export const deleteModelProvider = ({ url, body }: ...): Promise<CommonResponse>
export const fetchDefaultModal = (url: string): Promise<{ data: DefaultModelResponse }>
export const updateDefaultModel = ({ url, body }: ...): Promise<CommonResponse>
export const fetchModelParameterRules = (url: string): Promise<{ data: ModelParameterRule[] }>
// Email login and password reset
export const sendEMailLoginCode = (email: string, language?: string): Promise<CommonResponse & { data: string }>
export const emailLoginWithCode = (data: { email: string, code: string, token: string, language: string }): Promise<LoginResponse>
export const sendResetPasswordCode = (email: string, language?: string): Promise<CommonResponse & { data: string }>
// Account operations
export const sendDeleteAccountCode = (): Promise<CommonResponse & { data: string }>
export const verifyDeleteAccountCode = (body: { code: string, token: string }): Promise<CommonResponse & { is_valid: boolean }>
export const uploadRemoteFileInfo = (url: string, isPublic?: boolean, silent?: boolean): Promise<{ id: string, name: string, size: number, mime_type: string, url: string }>
Import
import { login, fetchUserProfile, fetchCurrentWorkspace, fetchMembers } from '@/service/common'
import { fetchModelProviders, setModelProvider, fetchModelList } from '@/service/common'
import { sendEMailLoginCode, emailLoginWithCode } from '@/service/common'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| url | string | Varies | API endpoint path; some functions accept it as a parameter, others use hardcoded paths |
| body | Record<string, any> | Varies | Request body object, varies per endpoint |
| params | Record<string, any> | Varies | Query parameters object for GET requests |
Outputs
| Name | Type | Description |
|---|---|---|
| LoginResponse | LoginSuccess or LoginFail | Login result containing access_token on success or error details on failure |
| CommonResponse | { result: string } | Standard success/failure response |
| ICurrentWorkspace | ICurrentWorkspace | Current workspace details including plan, role, and feature flags |
| Member[] | Member[] | Array of workspace member objects |
| ModelProvider[] | ModelProvider[] | Array of configured model provider objects |
| Model[] | Model[] | Array of available model objects |
Usage Examples
Login with Email
import { login } from '@/service/common'
const result = await login({
url: '/login',
body: { email: 'user@example.com', password: 'password123' },
})
if (result.result === 'success') {
console.log('Logged in successfully')
}
Fetch Workspace Members
import { fetchMembers } from '@/service/common'
const { accounts } = await fetchMembers({
url: '/workspaces/current/members',
params: { page: 1, limit: 50 },
})
Configure Model Provider
import { setModelProvider } from '@/service/common'
await setModelProvider({
url: '/workspaces/current/model-providers/openai',
body: { credentials: { openai_api_key: 'sk-...' } },
})
Related Pages
- Principle:Langgenius_Dify_Service_Layer_Architecture
- Langgenius_Dify_Service_Base - HTTP method wrappers used by all functions in this module
- Langgenius_Dify_Service_Use_Common - React Query hooks that consume these service functions