import {z} from 'zod' export type DependencySubscribeRequest = {manifest_content: string, manifest_type?: ('package.json' | 'requirements.txt' | 'pyproject.toml' | 'Cargo.toml' | 'go.mod' | 'Gemfile' | null) | undefined, filename?: (string | null) | undefined, include_dev_dependencies?: boolean | undefined, max_new_indexes?: number | undefined}; export type CreateApiDependenciesSubscribeArgs = {body: DependencySubscribeRequest}; export type CreateApiDependenciesSubscribeOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionResultItem = z.object({name: z.string(), action: z.string(), docs_url: z.string().nullable(), global_source_id: z.string().nullable(), namespace: z.string().nullable(), message: z.string()}); export const subscriptionResults = z.object({instant_access: z.array(subscriptionResultItem), wait_for_indexing: z.array(subscriptionResultItem), started_indexing: z.array(subscriptionResultItem), not_found: z.array(subscriptionResultItem), errors: z.array(subscriptionResultItem), skipped: z.array(subscriptionResultItem)}); export const subscriptionSummary = z.object({instant_access: z.number().int(), wait_for_indexing: z.number().int(), started_indexing: z.number().int(), not_found: z.number().int(), errors: z.number().int(), skipped: z.number().int()}); export const routesV2DependenciesSubscribeResponse = z.object({manifest_type: z.string(), total_dependencies: z.number().int(), results: subscriptionResults, summary: subscriptionSummary}); 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 }; /** * Subscribe to documentation for manifest dependencies * * POST /dependencies/subscribe */ export const createApiDependenciesSubscribe = async (args: CreateApiDependenciesSubscribeArgs, options?: CreateApiDependenciesSubscribeOptions) => { const {baseUrl = "https://apigcp.trynia.ai/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/dependencies/subscribe${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(routesV2DependenciesSubscribeResponse, await res.json()) };