import {z} from 'zod' export type GetApiV2AccessRequestsArgs = {teamId?: string | undefined, pageSize?: number | undefined, pageToken?: string | undefined}; export type GetApiV2AccessRequestsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const svflowCommonModelsAccessRequestStatus = z.enum(["ACCESS_REQUEST_STATUS_UNSPECIFIED", "ACCESS_REQUEST_STATUS_PENDING", "ACCESS_REQUEST_STATUS_APPROVED", "ACCESS_REQUEST_STATUS_DENIED", "ACCESS_REQUEST_STATUS_EXPIRED", "ACCESS_REQUEST_STATUS_REVOKED", "ACCESS_REQUEST_STATUS_CANCELED", "ACCESS_REQUEST_STATUS_FAILED"]); export const svflowPubapimodelsAccessRequestTimeAllocationStatus = z.enum(["ACCESS_REQUEST_TIME_ALLOCATION_STATUS_UNSPECIFIED", "ACCESS_REQUEST_TIME_ALLOCATION_STATUS_PENDING", "ACCESS_REQUEST_TIME_ALLOCATION_STATUS_ACTIVE", "ACCESS_REQUEST_TIME_ALLOCATION_STATUS_INVALIDATED"]); export const svflowPubapimodelsAccessRequestTimeAllocationInvalidationReason = z.enum(["ACCESS_REQUEST_TIME_ALLOCATION_INVALIDATION_REASON_UNSPECIFIED", "ACCESS_REQUEST_TIME_ALLOCATION_INVALIDATION_REASON_SUPERSEDED", "ACCESS_REQUEST_TIME_ALLOCATION_INVALIDATION_REASON_CANCELED", "ACCESS_REQUEST_TIME_ALLOCATION_INVALIDATION_REASON_REJECTED", "ACCESS_REQUEST_TIME_ALLOCATION_INVALIDATION_REASON_CONCLUDED"]); export const svflowPubapimodelsAccessRequestTimeAllocation = z.object({id: z.string().optional(), status: svflowPubapimodelsAccessRequestTimeAllocationStatus.optional(), createdAt: z.string().optional(), requestedMinutes: z.number().int().optional(), approvedMinutes: z.number().int().nullable().optional(), businessJustification: z.string().nullable().optional(), requestedByUserId: z.string().optional(), invalidationReason: svflowPubapimodelsAccessRequestTimeAllocationInvalidationReason.optional(), linkedTicketId: z.string().nullable().optional(), approvalRequestId: z.string().nullable().optional()}); export const svflowPubapimodelsAccessRequest = z.object({id: z.string().optional(), teamId: z.string().optional(), status: svflowCommonModelsAccessRequestStatus.optional(), createdAt: z.string().optional(), targetUserId: z.string().optional(), requestedRoleId: z.string().optional(), expiresAt: z.string().nullable().optional(), linkedTicketId: z.string().nullable().optional(), timeAllocations: z.array(svflowPubapimodelsAccessRequestTimeAllocation).optional()}); export const svflowPublicapiListAccessRequestsResponse = z.object({data: z.array(svflowPubapimodelsAccessRequest).optional(), nextPageToken: z.string().nullable().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 Access Requests * * GET /v2/access-requests */ export const getApiV2AccessRequests = async (args: GetApiV2AccessRequestsArgs, options?: GetApiV2AccessRequestsOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.teamId != null) query.append("teamId", String(args.teamId)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/access-requests${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(svflowPublicapiListAccessRequestsResponse, await res.json()) };