import {z} from 'zod' export type AccessPolicy = {attributes?: ({'is-system'?: boolean | undefined}) | undefined, id?: string | undefined, links?: {[key: string]: string} | undefined, relationships: {account?: ({data: {id: string, type: 'accounts' | ''} | null}) | undefined, environment?: ({data: {id: string, type: 'environments' | ''} | null}) | undefined, roles: {data: Array<{id: string, type: 'roles' | ''}>}, 'service-account'?: ({data: {id: string, type: 'service-accounts' | ''} | null}) | undefined, team?: ({data: {id: string, type: 'teams' | ''} | null}) | undefined, user?: ({data: {id: string, type: 'users' | ''} | null}) | undefined, workspace?: ({data: {id: string, type: 'workspaces' | ''} | null}) | undefined}, type: 'access-policies'}; export type AccessPolicyDocument = {data?: AccessPolicy | undefined, included?: Array> | undefined, meta?: Record | undefined}; export type CreateApiAccessPoliciesArgs = {include?: (Array<'account' | 'environment' | 'roles' | 'service-account' | 'team' | 'user' | 'workspace'>) | undefined, body: AccessPolicyDocument}; export type CreateApiAccessPoliciesOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const accessPolicy = z.object({attributes: z.object({"is-system": z.boolean().optional()}).optional(), id: z.string().optional(), links: z.record(z.string(), z.string()).optional(), relationships: z.object({account: z.object({data: z.object({id: z.string(), type: z.enum(["accounts", ""])}).nullable()}).optional(), environment: z.object({data: z.object({id: z.string(), type: z.enum(["environments", ""])}).nullable()}).optional(), roles: z.object({data: z.array(z.object({id: z.string(), type: z.enum(["roles", ""])}))}), "service-account": z.object({data: z.object({id: z.string(), type: z.enum(["service-accounts", ""])}).nullable()}).optional(), team: z.object({data: z.object({id: z.string(), type: z.enum(["teams", ""])}).nullable()}).optional(), user: z.object({data: z.object({id: z.string(), type: z.enum(["users", ""])}).nullable()}).optional(), workspace: z.object({data: z.object({id: z.string(), type: z.enum(["workspaces", ""])}).nullable()}).optional()}), type: z.literal("access-policies")}); export const accessPolicyDocument = z.object({data: accessPolicy.optional(), included: z.array(z.object({})).optional(), meta: z.object({}).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 an Access Policy * * POST /access-policies */ export const createApiAccessPolicies = async (args: CreateApiAccessPoliciesArgs, options?: CreateApiAccessPoliciesOptions) => { const {baseUrl = "https://example.scalr.io/api/iacp/v3", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (args.include != null) query.append("include", args.include.join(',')) if (APIKey !== undefined) headers.set('authorization', `Bearer ${APIKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/access-policies${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(accessPolicyDocument, await res.json()) };