import {z} from 'zod' export type RouteExposureDescriptorId = string; export type StringMap = {[key: string]: string}; export type PartyRef = {partyId?: string | undefined, externalId?: string | undefined, partyType?: string | undefined, displayName?: string | undefined, metadata?: StringMap | undefined}; export type OperationKind = 'READ' | 'WRITE' | 'UPDATE' | 'DELETE' | 'UPSERT' | 'QUERY' | 'SEARCH' | 'IMPORT' | 'EXPORT' | 'INVOKE' | 'DISCOVER' | 'VALIDATE'; export type RouteExposureGrantStatus = 'ACTIVE' | 'SUSPENDED' | 'REVOKED' | 'EXPIRED'; export type RouteExposureThrottlePolicy = {maxRequests: number, windowSeconds: number, throttleKey?: string | undefined}; export type RouteExposureGrantCreateRequest = {callerParty: PartyRef, grantedBy?: PartyRef | undefined, allowedAuthMethods?: Array | undefined, allowedOperations?: Array | undefined, status?: RouteExposureGrantStatus | undefined, validFrom?: string | undefined, validUntil?: string | undefined, maxUses?: number | undefined, throttlePolicy?: RouteExposureThrottlePolicy | undefined, metadata?: {[key: string]: string} | undefined}; export type CreateApiRouteExposuresExposureDescriptorIdGrantsArgs = {exposureDescriptorId: RouteExposureDescriptorId, body: RouteExposureGrantCreateRequest}; export type CreateApiRouteExposuresExposureDescriptorIdGrantsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const routeExposureGrantId = z.string(); export const routeExposureDescriptorId = z.string(); export const tenantId = z.string(); export const stringMap = z.record(z.string(), z.string()); export const partyRef = z.object({partyId: z.string().optional(), externalId: z.string().optional(), partyType: z.string().optional(), displayName: z.string().optional(), metadata: stringMap.optional()}); export const operationKind = z.enum(["READ", "WRITE", "UPDATE", "DELETE", "UPSERT", "QUERY", "SEARCH", "IMPORT", "EXPORT", "INVOKE", "DISCOVER", "VALIDATE"]); export const routeExposureGrantStatus = z.enum(["ACTIVE", "SUSPENDED", "REVOKED", "EXPIRED"]); export const routeExposureThrottlePolicy = z.object({maxRequests: z.number().int().gte(1), windowSeconds: z.number().int().gte(1), throttleKey: z.string().optional()}); export const routeExposureGrant = z.object({grantId: routeExposureGrantId, exposureDescriptorId: routeExposureDescriptorId, owningTenantId: tenantId, callerParty: partyRef, grantedBy: partyRef.optional(), allowedAuthMethods: z.array(z.string()).optional(), allowedOperations: z.array(operationKind).optional(), status: routeExposureGrantStatus, validFrom: z.string().optional(), validUntil: z.string().optional(), maxUses: z.number().int().optional(), useCount: z.number().int().gte(0), throttlePolicy: routeExposureThrottlePolicy.optional(), metadata: z.record(z.string(), z.string()).optional(), createdAt: z.string().optional(), updatedAt: 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 route exposure grant * * POST /route/exposures/{exposureDescriptorId}/grants */ export const createApiRouteExposuresExposureDescriptorIdGrants = async (args: CreateApiRouteExposuresExposureDescriptorIdGrantsArgs, options?: CreateApiRouteExposuresExposureDescriptorIdGrantsOptions) => { const {baseUrl = "https://api.example.com/api/connector/v1", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/route/exposures/${encodeURIComponent(String(args.exposureDescriptorId))}/grants${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(routeExposureGrant, await res.json()) };