import {z} from 'zod' export type GetApiApiSignalArgs = {company: string}; export type GetApiApiSignalOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const startup = z.object({name: z.string().optional(), description: z.string().optional(), stage: z.enum(["Pre-seed", "Seed", "Series A/B", "Growth"]).optional(), geography: z.string().optional(), commitVelocity14d: z.number().int().optional(), commitVelocityChange: z.string().optional(), contributors: z.number().int().optional(), contributorGrowth: z.string().optional(), newRepos: z.number().int().optional(), signalType: z.enum(["Engineering hiring burst", "Infrastructure buildout", "Deploy frequency spike", "Framework migration"]).optional(), githubUrl: z.string().optional(), websiteUrl: z.string().optional(), linkedinUrl: z.string().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 }; /** * Single startup signal by company name * * GET /api/signal */ export const getApiApiSignal = async (args: GetApiApiSignalArgs, options?: GetApiApiSignalOptions) => { const {baseUrl = "https://signals.gitdealflow.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.company != null) query.append("company", String(args.company)) const url = `${baseUrl.replace(/\/+$/, '')}/api/signal${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(startup, await res.json()) };