import {z} from 'zod' export type BillingAddressRequest = {address1: string, address2?: (string | null) | undefined, city?: (string | null) | undefined, stateId?: (string | null) | undefined, zipCode?: (string | null) | undefined, country: string, phone?: (string | null) | undefined}; export type CreditCardInfo = {paymentMethodId?: (string | null) | undefined}; export type CreateOrganizationBillingInfoRequest = {email: string, name: string, billingAddress: BillingAddressRequest, creditCardInfo?: CreditCardInfo | undefined}; export type CreateApiV4OrganizationsBillingArgs = {body: CreateOrganizationBillingInfoRequest}; export type CreateApiV4OrganizationsBillingOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const creditCardInfo = z.object({paymentMethodId: z.string().nullable().optional()}); export const billingAddress = z.object({addressId: z.number().int().optional(), addressTypeId: z.number().int().optional(), address1: z.string().nullable().optional(), address2: z.string().nullable().optional(), city: z.string().nullable().optional(), stateId: z.string().nullable().optional(), zipCode: z.string().nullable().optional(), country: z.string().nullable().optional(), phone: z.string().nullable().optional(), createDate: z.string().optional(), lastModDate: z.string().optional(), lastModByUserID: z.string().optional()}); export const organizationBillingInfoResponse = z.object({creditCardInfo: creditCardInfo.optional(), billingAddress: billingAddress.optional(), email: z.string().nullable().optional(), name: z.string().nullable().optional(), stripeSubscriptionCustomerId: z.string().nullable().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 billing information for organization * * POST /v4/organizations/billing */ export const createApiV4OrganizationsBilling = async (args: CreateApiV4OrganizationsBillingArgs, options?: CreateApiV4OrganizationsBillingOptions) => { const {baseUrl = "https://submittable-api.submittable.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/billing${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(organizationBillingInfoResponse, await res.json()) };