import {z} from 'zod' export type TagTypeEnum = 'environment' | 'general' | 'pipelines' | 'transforms' | 'topics' | 'services' | 'sources' | 'users' | 'tenant' | 'destinations'; export type TagsSearchReqBody = {tag_ids?: (Array | null) | undefined} | {[key: string]: unknown}; export type GetApiTagsArgs = {tag_ids?: (Array | null) | undefined, tag_name?: (string | null) | undefined, tag_type?: (Array | null) | undefined, body: TagsSearchReqBody | null}; export type GetApiTagsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiTagsResponse = z.record(z.string(), z.unknown()); 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 }; /** * Get Tags For Specific Tenant * * GET /tags */ export const getApiTags = async (args: GetApiTagsArgs, options?: GetApiTagsOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.tag_ids ?? []) query.append("tag_ids", String(item)) if (args.tag_name != null) query.append("tag_name", String(args.tag_name)) for (const item of args.tag_type ?? []) query.append("tag_type", String(item)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/tags${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(getApiTagsResponse, await res.json()) };