import {z} from 'zod' export type WorkspaceMemberRoleEnum = 'Admin' | 'Manager' | 'Member' | 'Collaborator'; export type AddMemberRequest = {user_id: string, role: WorkspaceMemberRoleEnum}; export type CreateApiPublicV1WorkspacesWorkspaceIdMembersArgs = {workspace_id: string, notify_user?: boolean | undefined, notify_ws_admins?: boolean | undefined, body: AddMemberRequest}; export type CreateApiPublicV1WorkspacesWorkspaceIdMembersOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const workspaceMemberRoleEnum = z.enum(["Admin", "Manager", "Member", "Collaborator"]); export const addMemberResponse = z.object({member_id: z.string().optional(), workspace_id: z.string().optional(), role: workspaceMemberRoleEnum.optional(), email: z.string().optional(), first_name: z.string().optional(), last_name: 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 }; /** * Add Member to Workspace * * POST /public/v1/workspaces/{workspace_id}/members * * Also accepts auth: oauth2 */ export const createApiPublicV1WorkspacesWorkspaceIdMembers = async (args: CreateApiPublicV1WorkspacesWorkspaceIdMembersArgs, options?: CreateApiPublicV1WorkspacesWorkspaceIdMembersOptions) => { const {baseUrl = "https://api.pandadoc.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.notify_user != null) query.append("notify_user", String(args.notify_user)) if (args.notify_ws_admins != null) query.append("notify_ws_admins", String(args.notify_ws_admins)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/workspaces/${encodeURIComponent(String(args.workspace_id))}/members${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(addMemberResponse, await res.json()) };