import {z} from 'zod' export type GetApiApiV1OfferArgs = {filterByCompanyUserId?: (Array | null) | undefined, all_available?: boolean | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiV1OfferOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ownerDTO = z.object({ownerCompanyUserId: z.string().nullable().optional(), email: z.string(), logoUrl: z.string().nullable().optional()}); export const offerListResponseDTO = z.object({lastEditedBy: z.string().nullable().optional(), id: z.string(), name: z.string().nullable(), offerUrl: z.string(), data: z.record(z.string(), z.unknown()).nullable(), comment: z.string().nullable(), createdAt: z.string(), updatedAt: z.string().nullable(), userId: z.string().nullable(), owner: ownerDTO.nullable().optional()}); export const pageOfferListResponseDTO = z.object({items: z.array(offerListResponseDTO), total: z.number().int().gte(0), page: z.number().int().gte(1), size: z.number().int().gte(1), pages: z.number().int().gte(0)}); 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 all user offers * * GET /api/v1/offer */ export const getApiApiV1Offer = async (args: GetApiApiV1OfferArgs, options: GetApiApiV1OfferOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.filterByCompanyUserId ?? []) query.append("filterByCompanyUserId", String(item)) if (args.all_available != null) query.append("all_available", String(args.all_available)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/offer${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(pageOfferListResponseDTO, await res.json()) };