import {z} from 'zod' export type V1LoadRequestQueryFilterBase = {member?: (string | null) | undefined, operator?: ('equals' | 'notEquals' | 'contains' | 'notContains' | 'startsWith' | 'notStartsWith' | 'endsWith' | 'notEndsWith' | 'gt' | 'gte' | 'lt' | 'lte' | 'set' | 'notSet' | 'inDateRange' | 'notInDateRange' | 'beforeDate' | 'afterDate' | 'measureFilter' | null) | undefined, values?: (Array | null) | undefined}; export type V1LoadRequestQueryFilterLogicalAnd = {and?: (Array | null) | undefined}; export type V1LoadRequestQueryFilterLogicalOr = {or?: (Array | null) | undefined}; export type EmbeddedUrlRequest = {user_identifier: string, canvas_id?: (string | null) | undefined, doc_id?: (string | null) | undefined, drive_file?: (string | null) | undefined, required_filters?: (Array) | undefined, redirect_query_params?: (string | null) | undefined, embed_type?: ('dashboard' | 'route' | 'canvas' | 'doc' | 'data_app' | null) | undefined, clear_session?: (boolean | null) | undefined, timestamp?: (number | null) | undefined, route?: (string | null) | undefined, expires_in?: (number | null) | undefined, allowed_hosts?: (Array | null) | undefined}; export type CreateApiV1GetEmbeddedUrlArgs = {body: EmbeddedUrlRequest}; export type CreateApiV1GetEmbeddedUrlOptions = {baseUrl: string, AsyncAPIKeyHeaderAuthenticator?: string | undefined, fetch?: typeof fetch | undefined}; export const embeddedUrlResponse = z.object({url: z.string(), expires_at: z.string().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 }; /** * Get an embedded url for the given user identifier. * * POST /v1/get_embedded_url */ export const createApiV1GetEmbeddedUrl = async (args: CreateApiV1GetEmbeddedUrlArgs, options: CreateApiV1GetEmbeddedUrlOptions) => { const {baseUrl, fetch: fetchFn = fetch, AsyncAPIKeyHeaderAuthenticator} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AsyncAPIKeyHeaderAuthenticator !== undefined) headers.set("Authorization", AsyncAPIKeyHeaderAuthenticator) const url = `${baseUrl.replace(/\/+$/, '')}/v1/get_embedded_url${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(embeddedUrlResponse, await res.json()) };