import {z} from 'zod' export type GetApiV2CdnDatasourcesIdArgs = {id: string, version?: ('draft' | 'published') | undefined, cv?: number | undefined, token: string}; export type GetApiV2CdnDatasourcesIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dimension = z.object({id: z.number().int(), name: z.string(), entry_value: z.string(), datasource_id: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const datasource = z.object({id: z.number().int(), name: z.string(), slug: z.string(), dimensions: z.array(dimension), created_at: z.string().optional(), updated_at: z.string().optional()}); export const getApiV2CdnDatasourcesIdResponse = z.object({datasource: datasource, cv: z.number().int().nullable()}); 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 }; /** * Retrieve a Single Data Source * * GET /v2/cdn/datasources/{id} */ export const getApiV2CdnDatasourcesId = async (args: GetApiV2CdnDatasourcesIdArgs, options?: GetApiV2CdnDatasourcesIdOptions) => { const {baseUrl = "https://api.storyblok.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.version != null) query.append("version", String(args.version)) if (args.cv != null) query.append("cv", String(args.cv)) if (args.token != null) query.append("token", String(args.token)) if (ApiKeyAuth !== undefined) query.set("token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/cdn/datasources/${encodeURIComponent(String(args.id))}${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(getApiV2CdnDatasourcesIdResponse, await res.json()) };