import {z} from 'zod' export type GetApiOrgsFetchArgs = {'X-Phantombuster-Org'?: string | undefined, withGlobalObject?: string | undefined, withProxies?: string | undefined, withCrmIntegrations?: string | undefined, withCustomPrompts?: string | undefined}; export type GetApiOrgsFetchOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiOrgsFetchResponse = z.object({id: z.string(), name: z.string(), dailyExecutionTime: z.number(), dailyMail: z.number(), dailyCaptcha: z.number(), dailyDiscoveredMail: z.number(), dailyAiCredit: z.number(), s3Storage: z.number(), s3Folder: z.string(), planSlug: z.string(), planStartedAt: z.number(), timezone: z.string().optional(), company: z.string().nullable().optional(), billingAddress: z.object({street: z.string().max(1000), city: z.string().max(100), zip: z.string().max(100), countryCode: z.string().max(100)}).nullable().optional(), billingMail: z.string().nullable().optional(), billingFirstName: z.string().nullable(), billingLastName: z.string().nullable(), vatId: z.string().max(100).nullable().optional(), additionalInvoiceInformation: z.string().max(100).nullable().optional(), globalObject: z.string().nullable().optional(), proxies: z.record(z.string(), z.unknown()).nullable().optional(), crmIntegrations: z.array(z.object({id: z.string().optional(), crmName: z.enum(["hubspot", "salesforce", "pipedrive"]), refreshToken: z.string().max(300), crmAccountId: z.string().max(100), contactUpdateOnly: z.boolean().optional(), createdAt: z.number().optional(), expireAt: z.number().nullable().optional(), updatedAt: z.number().optional()})).nullable().optional(), qualificationFlow: z.object({email: z.string().nullable().optional(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), jobTitle: z.string().nullable().optional(), sessionCookie: z.string().nullable().optional(), prospectionCountries: z.array(z.string()).nullable().optional(), company: z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), website: z.string().nullable().optional(), typeOfOrganisation: z.enum(["B2B", "B2C", "Hybrid"]).nullable().optional(), market: z.array(z.string()).nullable().optional(), productServices: z.array(z.object({id: z.string().nullable().optional(), name: z.string().nullable().optional(), typeService: z.string().nullable().optional(), description: z.string().nullable().optional(), keyFeatures: z.array(z.string()).nullable().optional(), problemsSolvedForCustomers: z.array(z.string()).nullable().optional(), competitors: z.array(z.object({id: z.string().nullable(), companyId: z.string(), linkedInCompanyUrl: z.string(), name: z.string().nullable().optional(), followerCount: z.number().nullable().optional(), isMonitored: z.boolean().nullable().optional(), logoUrl: z.string().nullable().optional(), monitoringPhantomId: z.string().nullable().optional(), sourcingPhantomId: z.string().nullable().optional(), websiteUrl: z.string().nullable().optional()})).nullable().optional()})).nullable().optional(), linkedInCompanyUrl: z.string().nullable().optional()}).nullable().optional()}).nullable().optional(), createdAt: z.number(), plan: z.object({name: z.string().max(100), s3Storage: z.number(), mongoStorage: z.number(), parallelism: z.number(), agents: z.number(), scripts: z.number(), dailyExecutionTime: z.number(), dailyMails: z.number(), dailyCaptchas: z.number(), dailyDiscoveredMails: z.number(), dailyAiCredits: z.number(), dailySerpCredits: z.number().optional(), monthlyExecutionTime: z.number().gte(0).optional(), monthlyMails: z.number().gte(0).optional(), monthlyCaptchas: z.number().gte(0).optional(), monthlyDiscoveredMails: z.number().gte(0).optional(), monthlyAiCredits: z.number().gte(0).optional(), monthlySerpCredits: z.number().gte(0).optional(), isAvailable: z.boolean(), isUpgradable: z.boolean(), duration: z.number(), monthlyPrice: z.number(), yearlyPrice: z.number(), monthlyPrices: z.record(z.string(), z.number()).optional(), yearlyPrices: z.record(z.string(), z.number()).optional()}), displayName: z.string(), planShouldCancelAt: z.number().nullable().optional(), owner: z.object({userId: z.string(), notifyUsage: z.boolean(), role: z.enum(["owner", "admin", "member"]), lastName: z.string(), firstName: z.string(), email: z.string()}).optional(), identityTokens: z.array(z.object({token: z.string(), expire_at: z.number(), magic_link: z.string().optional()})).optional(), isV3: z.boolean().nullable().optional(), customPromptOrg: z.string().max(10000).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 }; /** * Gets the current organization. * * GET /orgs/fetch */ export const getApiOrgsFetch = async (args: GetApiOrgsFetchArgs, options?: GetApiOrgsFetchOptions) => { const {baseUrl = "https://api.phantombuster.com/api/v2", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Phantombuster-Org'] != null) headers.set("X-Phantombuster-Org", String(args['X-Phantombuster-Org'])) if (args.withGlobalObject != null) query.append("withGlobalObject", String(args.withGlobalObject)) if (args.withProxies != null) query.append("withProxies", String(args.withProxies)) if (args.withCrmIntegrations != null) query.append("withCrmIntegrations", String(args.withCrmIntegrations)) if (args.withCustomPrompts != null) query.append("withCustomPrompts", String(args.withCustomPrompts)) if (apiKeyAuth !== undefined) headers.set("X-Phantombuster-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/orgs/fetch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiOrgsFetchResponse, await res.json()) };