import {z} from 'zod' export type CreateResourceRequest = {INPUT_DATA?: ({operation?: ({Details?: ({RESOURCENAME: string, ACCOUNTNAME: string, RESOURCETYPE?: string | undefined, PASSWORD?: string | undefined, NOTES?: string | undefined, RESOURCEURL?: string | undefined, LOCATION?: string | undefined, DNSNAME?: string | undefined, DEPARTMENT?: string | undefined, RESOURCEDESCRIPTION?: string | undefined, DOMAINNAME?: string | undefined, RESOURCEGROUPNAME?: string | undefined, ENABLEPRIVATEKEY?: boolean | undefined, RESOURCEPASSWORDPOLICY?: string | undefined, ACCOUNTPASSWORDPOLICY?: string | undefined, OWNERNAME?: string | undefined, RECORD_CONFIGURED_RESOURCE_URL?: boolean | undefined, DISABLE_TAB_DUPLICATION?: boolean | undefined, WEBSITE_SESSION_RECORDING?: boolean | undefined, GATEWAY_SESSION_RECORDING?: boolean | undefined, SAP_USER_TYPE?: ('A' | 'B' | 'C' | 'S' | 'L') | undefined, RESOURCECUSTOMFIELD?: (Array<{CUSTOMLABEL?: string | undefined, CUSTOMVALUE?: string | undefined}>) | undefined, ACCOUNTCUSTOMFIELD?: (Array<{CUSTOMLABEL?: string | undefined, CUSTOMVALUE?: string | undefined}>) | undefined}) | undefined}) | undefined}) | undefined}; export type CreateApiRestapiJsonV1ResourcesArgs = {orgName?: string | undefined, body: CreateResourceRequest}; export type CreateApiRestapiJsonV1ResourcesOptions = {baseUrl?: string | undefined, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const result = z.object({status: z.enum(["Success", "Failed"]).optional(), message: z.string().optional(), statusCode: z.number().int().optional()}); export const resourceCreateResponse = z.object({operation: z.object({name: z.string().optional(), result: result.optional(), Details: z.object({RESOURCEID: z.number().int().optional()}).optional()}).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 new resource * * POST /restapi/json/v1/resources */ export const createApiRestapiJsonV1Resources = async (args: CreateApiRestapiJsonV1ResourcesArgs, options?: CreateApiRestapiJsonV1ResourcesOptions) => { const {baseUrl = "https://Hostname:Port", fetch: fetchFn = fetch, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (args.orgName != null) headers.set("orgName", String(args.orgName)) if (AuthToken !== undefined) headers.set("AUTHTOKEN", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/json/v1/resources${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(resourceCreateResponse, await res.json()) };