import {z} from 'zod' export type PostV2OrganizationsOrganizationIdClients = {name: string, emails?: Array | undefined, phone?: string | undefined, address?: string | undefined, project_ids?: Array | undefined, invoice_notes?: string | undefined, net_terms?: number | undefined, metadata?: Array<{key: string, value: string}> | undefined, budget?: ({type: 'cost' | 'hours', rate?: ('bill_rate' | 'pay_rate') | undefined, cost?: number | undefined, hours?: number | undefined, start_date?: string | undefined, recurrence?: ('weekly' | 'monthly') | undefined, recurrence_start?: number | undefined, include_non_billable?: boolean | undefined, stop_timer_when_budget_reached?: boolean | undefined, stop_at: number, alerts?: ({near_limit?: number | undefined, notify_enabled?: boolean | undefined, notify_all_members?: boolean | undefined, notify_group?: ('all_members' | 'org_management') | undefined}) | undefined}) | undefined}; export type CreateApiV2OrganizationsOrganizationIdClientsArgs = {organization_id: number, body: PostV2OrganizationsOrganizationIdClients}; export type CreateApiV2OrganizationsOrganizationIdClientsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const budget = z.object({type: z.string(), rate: z.string().optional(), cost: z.number().optional(), hours: z.number().optional(), start_date: z.string().optional(), alerts: z.object({near_limit: z.number().int(), notify_enabled: z.boolean(), notify_all_members: z.boolean(), notify_group: z.string().optional()}), recurrence: z.string().optional(), recurrence_start: z.number().int().optional(), include_non_billable: z.boolean(), stop_timer_when_budget_reached: z.boolean(), stop_at: z.number().int().optional()}); export const client = z.object({id: z.number().int(), organization_id: z.number().int(), name: z.string(), emails: z.array(z.string()), phone: z.string(), address: z.string(), project_ids: z.array(z.number().int()), inherit_invoice_notes: z.boolean().optional(), invoice_notes: z.string().optional(), inherit_net_terms: z.boolean().optional(), net_terms: z.string().optional(), status: z.string(), created_at: z.string(), updated_at: z.string(), budget: budget.optional(), metadata: z.object({})}); export const createApiV2OrganizationsOrganizationIdClientsResponse = z.object({client: client.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 client * * POST /v2/organizations/{organization_id}/clients */ export const createApiV2OrganizationsOrganizationIdClients = async (args: CreateApiV2OrganizationsOrganizationIdClientsArgs, options?: CreateApiV2OrganizationsOrganizationIdClientsOptions) => { const {baseUrl = "https://api.hubstaff.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(/\/+$/, '')}/v2/organizations/${encodeURIComponent(String(args.organization_id))}/clients${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(createApiV2OrganizationsOrganizationIdClientsResponse, await res.json()) };