import {z} from 'zod' export type TMCreateTeamRequest = {public?: boolean | undefined, name: string, description?: string | undefined, members?: (Array<{id?: string | undefined, email?: string | undefined}>) | undefined}; export type CreateApiTeamMessagingV1TeamsArgs = {body: TMCreateTeamRequest}; export type CreateApiTeamMessagingV1TeamsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const tMTeamInfo = z.object({id: z.string().optional(), type: z.literal("Team").optional(), public: z.boolean().optional(), name: z.string().optional(), description: z.string().optional(), status: z.enum(["Active", "Archived"]).optional(), creationTime: z.string().optional(), lastModifiedTime: z.string().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 }; /** * Create Team * * POST /team-messaging/v1/teams */ export const createApiTeamMessagingV1Teams = async (args: CreateApiTeamMessagingV1TeamsArgs, options?: CreateApiTeamMessagingV1TeamsOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/team-messaging/v1/teams${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tMTeamInfo, await res.json()) };