import {z} from 'zod' export type SAMLConnection = {id?: string | undefined, createTime?: string | undefined, updateTime?: string | undefined, primary?: boolean | undefined, spAcsUrl?: string | undefined, spEntityId?: string | undefined, idpRedirectUrl?: string | undefined, idpX509Certificate?: string | undefined, idpEntityId?: string | undefined}; export type CreateApiFrontendV1SamlConnectionsArgs = {body: SAMLConnection}; export type CreateApiFrontendV1SamlConnectionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const sAMLConnection = z.object({id: z.string().optional(), createTime: z.string().optional(), updateTime: z.string().optional(), primary: z.boolean().optional(), spAcsUrl: z.string().optional(), spEntityId: z.string().optional(), idpRedirectUrl: z.string().optional(), idpX509Certificate: z.string().optional(), idpEntityId: z.string().optional()}); export const createSAMLConnectionResponse = z.object({samlConnection: sAMLConnection.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 SAML Connection * * POST /frontend/v1/saml-connections */ export const createApiFrontendV1SamlConnections = async (args: CreateApiFrontendV1SamlConnectionsArgs, options: CreateApiFrontendV1SamlConnectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/frontend/v1/saml-connections${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(createSAMLConnectionResponse, await res.json()) };