import {z} from 'zod' export type StaleFlagReminderScope = 'all' | 'watchedByMe'; export type StaleFlagStaleInEnvironmentsType = 'staleInAnyEnvironments' | 'staleInAllEnvironments'; export type GetApiV1ProductsProductIdStaleflagsArgs = {productId: string, scope?: StaleFlagReminderScope | undefined, staleFlagAgeDays?: number | undefined, staleFlagStaleInEnvironmentsType?: StaleFlagStaleInEnvironmentsType | undefined, ignoredEnvironmentIds?: Array | undefined, ignoredTagIds?: Array | undefined}; export type GetApiV1ProductsProductIdStaleflagsOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const evaluationVersion = z.enum(["v1", "v2"]); export const staleFlagSettingTagModel = z.object({tagId: z.number().int(), settingTagId: z.number().int()}); export const staleFlagSettingValueModel = z.object({environmentId: z.string(), updatedAt: z.string().nullable(), isStale: z.boolean()}); export const staleFlagSettingModel = z.object({settingId: z.number().int(), name: z.string(), key: z.string(), hint: z.string().nullable(), hasCodeReferences: z.boolean(), tags: z.array(staleFlagSettingTagModel), settingValues: z.array(staleFlagSettingValueModel)}); export const staleFlagConfigModel = z.object({configId: z.string(), name: z.string(), evaluationVersion: evaluationVersion, hasCodeReferences: z.boolean(), settings: z.array(staleFlagSettingModel)}); export const staleFlagEnvironmentModel = z.object({environmentId: z.string(), name: z.string()}); export const staleFlagProductModel = z.object({productId: z.string(), name: z.string(), configs: z.array(staleFlagConfigModel), environments: z.array(staleFlagEnvironmentModel)}); 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 }; /** * List Zombie (stale) flags for Product * * GET /v1/products/{productId}/staleflags */ export const getApiV1ProductsProductIdStaleflags = async (args: GetApiV1ProductsProductIdStaleflagsArgs, options?: GetApiV1ProductsProductIdStaleflagsOptions) => { const {baseUrl = "https://api.configcat.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.scope != null) query.append("scope", String(args.scope)) if (args.staleFlagAgeDays != null) query.append("staleFlagAgeDays", String(args.staleFlagAgeDays)) if (args.staleFlagStaleInEnvironmentsType != null) query.append("staleFlagStaleInEnvironmentsType", String(args.staleFlagStaleInEnvironmentsType)) for (const item of args.ignoredEnvironmentIds ?? []) query.append("ignoredEnvironmentIds", String(item)) for (const item of args.ignoredTagIds ?? []) query.append("ignoredTagIds", String(item)) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/products/${encodeURIComponent(String(args.productId))}/staleflags${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(staleFlagProductModel, await res.json()) };