import {z} from 'zod' export type AccessToken = {attributes?: ({'created-at'?: string | undefined, description?: (string | null) | undefined, 'expires-at'?: (string | null) | undefined, 'expires-in'?: (number | null) | undefined, 'is-expired'?: boolean | undefined, 'last-used-at'?: (string | null) | undefined, name?: (string | null) | undefined, token?: (string | null) | undefined}) | undefined, id?: string | undefined, links?: ({self?: string | undefined}) | undefined, relationships?: ({'created-by'?: ({data: {id: string, type: 'users' | ''} | null}) | undefined}) | undefined, type: 'access-tokens'}; export type AccessTokenDocument = {data?: AccessToken | undefined, included?: Array> | undefined, meta?: Record | undefined}; export type CreateApiAgentPoolsAgentPoolAccessTokensArgs = {agent_pool: string, include?: Array<'created-by'> | undefined, body: AccessTokenDocument}; export type CreateApiAgentPoolsAgentPoolAccessTokensOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const accessToken = z.object({attributes: z.object({"created-at": z.string().optional(), description: z.string().max(255).nullable().optional(), "expires-at": z.string().nullable().optional(), "expires-in": z.number().int().nullable().optional(), "is-expired": z.boolean().optional(), "last-used-at": z.string().nullable().optional(), name: z.string().max(64).nullable().optional(), token: z.string().nullable().optional()}).optional(), id: z.string().optional(), links: z.object({self: z.string().optional()}).optional(), relationships: z.object({"created-by": z.object({data: z.object({id: z.string(), type: z.enum(["users", ""])}).nullable()}).optional()}).optional(), type: z.literal("access-tokens")}); export const accessTokenDocument = z.object({data: accessToken.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 Agent Pool Access Token * * POST /agent-pools/{agent_pool}/access-tokens */ export const createApiAgentPoolsAgentPoolAccessTokens = async (args: CreateApiAgentPoolsAgentPoolAccessTokensArgs, options?: CreateApiAgentPoolsAgentPoolAccessTokensOptions) => { 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(/\/+$/, '')}/agent-pools/${encodeURIComponent(String(args.agent_pool))}/access-tokens${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(accessTokenDocument, await res.json()) };