import {z} from 'zod' export type DeleteApiAppwiseV2AccountsArgs = {appId: string, accountName: string, callbackUrl?: string | undefined}; export type DeleteApiAppwiseV2AccountsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const applicationToDisconnect = z.object({id: z.string().optional()}); export const userToDisconnect = z.object({userId: z.string().optional(), companyId: z.string().optional()}); export const disconnectionDetails = z.object({accountName: z.string().optional(), status: z.string()}); export const userAccountDisonnectionResponse = z.object({app: applicationToDisconnect.optional(), user: userToDisconnect.optional(), connection: disconnectionDetails.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Disconnect user accounts * * DELETE /appwise/v2/accounts */ export const deleteApiAppwiseV2Accounts = async (args: DeleteApiAppwiseV2AccountsArgs, options?: DeleteApiAppwiseV2AccountsOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.appId != null) query.append("appId", String(args.appId)) if (args.accountName != null) query.append("accountName", String(args.accountName)) if (args.callbackUrl != null) query.append("callbackUrl", String(args.callbackUrl)) const url = `${baseUrl.replace(/\/+$/, '')}/appwise/v2/accounts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(userAccountDisonnectionResponse, await res.json()) };