import {z} from 'zod' export type CreateApiV2GrantsGrantIdVoidArgs = {grant_id: string, 'Idempotency-Key'?: string | undefined}; export type CreateApiV2GrantsGrantIdVoidOptions = {baseUrl: string, LedgerUpApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const unixTimestamp = z.number().int(); export const grant = z.object({id: z.string(), description: z.string().max(2048).nullable(), account_id: z.string(), metric_id: z.string().nullable(), currency: z.string().nullable(), amount: z.string(), expires_at: unixTimestamp.nullable(), accepted_at: unixTimestamp, created_at: unixTimestamp, updated_at: unixTimestamp, status: z.enum(["active", "scheduled", "voided", "archived", "expired"]), archived_at: unixTimestamp.nullable(), contract_v1_id: z.string().nullable(), effective_at: unixTimestamp}); 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 }; /** * Void a grant. * * POST /v2/grants/{grant_id}/void */ export const createApiV2GrantsGrantIdVoid = async (args: CreateApiV2GrantsGrantIdVoidArgs, options: CreateApiV2GrantsGrantIdVoidOptions) => { const {baseUrl, fetch: fetchFn = fetch, LedgerUpApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (LedgerUpApiKey !== undefined) headers.set("x-api-key", LedgerUpApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/grants/${encodeURIComponent(String(args.grant_id))}/void${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(grant, await res.json()) };