import {z} from 'zod' export type GeoPoint = {lat: string, lon: string}; export type Location = {geopoint?: GeoPoint | undefined, radius?: number | undefined, unit?: ('m' | 'mi' | 'km' | 'ft') | undefined, saved_location_id?: string | undefined}; export type Industry = 'accommodation' | 'cpg' | 'tourism' | 'marketing' | 'parking' | 'restaurants' | 'retail' | 'transportation' | 'other'; export type Interval = 'day' | 'week'; export type Weekday = 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday'; export type CurrencyCode = 'USD' | 'EUR' | 'GBP' | 'AUD' | 'CAD'; export type DemandType = {industry?: Industry | undefined, interval?: Interval | undefined, week_start_day?: Weekday | undefined, unit_descriptor?: string | undefined, currency_code?: CurrencyCode | undefined, unit_currency_multiplier?: number | undefined}; export type RankType = 'phq' | 'local'; export type RankLevel = {min: number, max?: number | undefined}; export type RankLevels = {phq?: RankLevel | undefined, local?: RankLevel | undefined}; export type Rank = {type: RankType, levels?: RankLevels | undefined}; export type PutAnalysisRequest = {name: string, location: Location, demand_type?: DemandType | undefined, external_id?: string | undefined, label?: Array | undefined, rank?: Rank | undefined}; export type CreateApiV1BeamAnalysesArgs = {body: PutAnalysisRequest}; export type CreateApiV1BeamAnalysesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const analysisIdResponse = z.object({analysis_id: 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 }; /** * Create an Analysis * * POST /v1/beam/analyses */ export const createApiV1BeamAnalyses = async (args: CreateApiV1BeamAnalysesArgs, options?: CreateApiV1BeamAnalysesOptions) => { const {baseUrl = "https://api.predicthq.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/beam/analyses${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(analysisIdResponse, await res.json()) };