import {z} from 'zod' export type CreateNewMajorVersionBody = {columns: Array<{name: string, sort_order?: number | undefined, contains_pii?: boolean | undefined}>, data: Array<{[key: string]: unknown}>, notes?: string | undefined, changelog?: string | undefined}; export type AddToListBody = {base_version: {major_version: number, minor_version: number}, added: Array<{[key: string]: unknown}>, notes?: string | undefined, changelog?: string | undefined}; export type DeleteFromListBody = {base_version: {major_version: number, minor_version: number}, deleted: Array, notes?: string | undefined, changelog?: string | undefined}; export type ModifyListBody = {base_version: {major_version: number, minor_version: number}, modified: Array<{list_row_id: string} | {[key: string]: unknown}>, notes?: string | undefined, changelog?: string | undefined}; export type ReplaceListBody = {base_version: {major_version: number, minor_version?: number | undefined}, data: Array<{[key: string]: unknown}>, notes?: string | undefined, changelog?: string | undefined}; export type RestoreAsNewBody = {base_version: {major_version: number, minor_version: number}, notes?: string | undefined}; export type CreateApiCustomListsCustomListTokenVersionsArgs = {body: CreateNewMajorVersionBody | AddToListBody | DeleteFromListBody | ModifyListBody | ReplaceListBody | RestoreAsNewBody}; export type CreateApiCustomListsCustomListTokenVersionsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCustomListsCustomListTokenVersionsResponse = z.object({custom_list_token: z.string().optional(), version: 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 a new Custom List Version * * POST /custom-lists/{customListToken}/versions * * Also accepts auth: basic */ export const createApiCustomListsCustomListTokenVersions = async (args: CreateApiCustomListsCustomListTokenVersionsArgs, options?: CreateApiCustomListsCustomListTokenVersionsOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/custom-lists/${encodeURIComponent(String(args.customListToken))}/versions${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(createApiCustomListsCustomListTokenVersionsResponse, await res.json()) };