import {z} from 'zod' export type ServiceNamespace = {created_at?: string | undefined, deleted_at?: string | undefined, id?: string | undefined, name?: string | undefined, type?: string | undefined, updated_at?: string | undefined}; export type ServiceValue = {created_at?: string | undefined, deleted_at?: string | undefined, id?: string | undefined, value?: string | undefined}; export type ServiceTag = {compartment_id?: string | undefined, created_at?: string | undefined, deleted_at?: string | undefined, id?: string | undefined, key?: string | undefined, namespace?: ServiceNamespace | undefined, regexp?: string | undefined, regexp_description?: string | undefined, status?: string | undefined, type?: string | undefined, updated_at?: string | undefined, values?: Array | undefined, version?: number | undefined}; export type CreateApiTagsArgs = {body: ServiceTag}; export type CreateApiTagsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceNamespace = z.object({created_at: z.string().optional(), deleted_at: z.string().optional(), id: z.string().optional(), name: z.string().optional(), type: z.string().optional(), updated_at: z.string().optional()}); export const serviceValue = z.object({created_at: z.string().optional(), deleted_at: z.string().optional(), id: z.string().optional(), value: z.string().optional()}); export const serviceTag = z.object({compartment_id: z.string().optional(), created_at: z.string().optional(), deleted_at: z.string().optional(), id: z.string().optional(), key: z.string().optional(), namespace: serviceNamespace.optional(), regexp: z.string().optional(), regexp_description: z.string().optional(), status: z.string().optional(), type: z.string().optional(), updated_at: z.string().optional(), values: z.array(serviceValue).optional(), version: z.number().int().optional()}); export const serviceCreateTagResponse = z.object({result: serviceTag.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 tag definition * * POST /tags */ export const createApiTags = async (args: CreateApiTagsArgs, options: CreateApiTagsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/tags${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(serviceCreateTagResponse, await res.json()) };