import {z} from 'zod' export type SvflowPublicapiCreateAccessRequestRequest = {teamId: string, roleId: string, targetUserId?: (string | null) | undefined, accessMinutes?: number | undefined, businessJustification?: (string | null) | undefined, scheduledStartAt?: (string | null) | undefined}; export type CreateApiV2AccessRequestsArgs = {body: SvflowPublicapiCreateAccessRequestRequest}; export type CreateApiV2AccessRequestsOptions = {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 svflowPublicapiCreateAccessRequestResponse = z.object({data: svflowPubapimodelsAccessRequest.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 Access Request * * POST /v2/access-requests */ export const createApiV2AccessRequests = async (args: CreateApiV2AccessRequestsArgs, options?: CreateApiV2AccessRequestsOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(svflowPublicapiCreateAccessRequestResponse, await res.json()) };