import {z} from 'zod' export type GetApiAppsGovFrV1ReportsSiloEntryIdPeriodsArgs = {silo_entry_id: string, kind?: ('tx' | 'py') | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiAppsGovFrV1ReportsSiloEntryIdPeriodsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const reportingPeriod = z.object({id: z.string(), siren: z.string(), flux_kind: z.enum(["tx", "py"]), role: z.enum(["seller", "buyer", ""]), period_start: z.string(), period_end: z.string(), sequence: z.number().int(), previous_period_id: z.string().optional(), status: z.enum(["generated", "submitted", "filed", "rejected"]), flux_code: z.string().optional(), ack_ref: z.string().optional(), submitted_at: z.string().optional(), created_at: z.string(), updated_at: z.string()}); export const listPeriodsResponse = z.object({periods: z.array(reportingPeriod), next_offset: z.number().int().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 }; /** * List e-reporting periods for a party * * GET /apps/gov-fr/v1/reports/{silo_entry_id}/periods */ export const getApiAppsGovFrV1ReportsSiloEntryIdPeriods = async (args: GetApiAppsGovFrV1ReportsSiloEntryIdPeriodsArgs, options?: GetApiAppsGovFrV1ReportsSiloEntryIdPeriodsOptions) => { const {baseUrl = "https://api.invopop.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.kind != null) query.append("kind", String(args.kind)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/apps/gov-fr/v1/reports/${encodeURIComponent(String(args.silo_entry_id))}/periods${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(listPeriodsResponse, await res.json()) };