import {z} from 'zod' export type List = {id?: number | undefined, created_by?: (number | null) | undefined, updated_by?: (number | null) | undefined, created_at?: string | undefined, updated_at?: string | undefined, parent?: (number | null) | undefined, name: string, description?: (string | null) | undefined, list_order?: (number | null) | undefined, tags?: (Record | null) | undefined, model: string, object_ids?: (Array | null) | undefined, shared: 'private' | 'team' | 'organization' | 'groups_affiliates' | 'all' | 'public', permissions: 'read_write_delete_copy' | 'read_write_copy' | 'read_copy' | 'read_only', incident?: (number | null) | undefined, invalidate_at?: string | undefined}; export type CreateApiListsArgs = {body: List}; export type CreateApiListsOptions = {baseUrl?: string | undefined, Token?: string | undefined, fetch?: typeof fetch | undefined}; export const list = z.object({id: z.number().int().optional(), created_by: z.number().int().nullable().optional(), updated_by: z.number().int().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), parent: z.number().int().nullable().optional(), name: z.string().min(1).max(255), description: z.string().nullable().optional(), list_order: z.number().int().gte(-2147483648).lte(2147483647).nullable().optional(), tags: z.object({}).nullable().optional(), model: z.string().min(1).max(255), object_ids: z.array(z.number().int().gte(-2147483648).lte(2147483647)).nullable().optional(), shared: z.enum(["private", "team", "organization", "groups_affiliates", "all", "public"]), permissions: z.enum(["read_write_delete_copy", "read_write_copy", "read_copy", "read_only"]), incident: z.number().int().nullable().optional(), invalidate_at: 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 list * * POST /lists */ export const createApiLists = async (args: CreateApiListsArgs, options?: CreateApiListsOptions) => { const {baseUrl = "https://crisiscleanup-3-api-production.up.railway.app", fetch: fetchFn = fetch, Token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Token !== undefined) headers.set("Authorization", Token) const url = `${baseUrl.replace(/\/+$/, '')}/lists${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(list, await res.json()) };