import {z} from 'zod' export type OrganizationLink = {url: string, icon: string, name: string}; export type OrganizationCreate = {organization_name: string, creation_intent?: ('free' | 'team') | undefined, organization_display_name?: string | undefined, github_handle?: string | undefined, location?: string | undefined, avatar?: string | undefined, links?: Array | undefined, description?: string | undefined}; export type CreateApiV1OrganizationsArgs = {body: OrganizationCreate}; export type CreateApiV1OrganizationsOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionPlanId = z.enum(["personal", "team", "growth", "enterprise_cloud", "non_stripe", "enterprise", "internal"]); export const organizationLink = z.object({url: z.string(), icon: z.string(), name: z.string()}); export const organizationReadV1 = z.object({id: z.string(), organization_name: z.string(), subscription_plan: subscriptionPlanId.nullable(), has_admin_panel: z.boolean(), created_at: z.string(), updated_at: z.string(), organization_display_name: z.string().nullable(), github_handle: z.string().nullable(), location: z.string().nullable(), avatar: z.string().nullable(), links: z.array(organizationLink).nullable(), description: z.string().nullable(), spending_cap: z.number().int().nullable(), spending_cap_reached_at: z.string().nullable(), planless_grace_period_ends_at: z.string().nullable(), gateway_enabled: z.boolean(), ai_enabled: z.boolean(), ai_training_enabled: z.boolean()}); 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 Organization * * POST /v1/organizations/ */ export const createApiV1Organizations = async (args: CreateApiV1OrganizationsArgs, options: CreateApiV1OrganizationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organizations/${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(organizationReadV1, await res.json()) };