import {z} from 'zod' export type TimeRangeQueryParameter = 'last_day' | 'last_week' | 'last_month' | 'last_3_months' | 'last_6_months' | 'last_12_months' | 'all_time'; export type AssetFilters = {is_tech?: boolean | undefined, is_favicon?: boolean | undefined, is_new?: boolean | undefined, labels?: string | undefined, host?: string | undefined, port?: string | undefined, status_code?: string | undefined, content_length?: string | undefined, title?: string | undefined, domain?: Array | undefined, cname?: string | undefined, technologies?: string | undefined, ip?: string | undefined, is_screenshot?: boolean | undefined, time?: TimeRangeQueryParameter | undefined, start_date?: string | undefined, end_date?: string | undefined, custom_filter?: string | undefined, search?: string | undefined, enumeration_ids?: Array | undefined, only_dns?: boolean | undefined, only_ip?: boolean | undefined, not_seen_for_days?: number | undefined}; export type PatchApiV1AssetLabelsArgs = {update_type?: ('append' | 'replace' | 'delete') | undefined, 'X-Team-Id'?: string | undefined, body: {filters: AssetFilters, labels: Array}}; export type PatchApiV1AssetLabelsOptions = {baseUrl?: string | undefined, XAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiV1AssetLabelsResponse = z.object({message: z.string(), asset_count: z.number().int(), async: z.boolean()}); 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 }; /** * Update Asset Labels * * PATCH /v1/asset/labels */ export const patchApiV1AssetLabels = async (args: PatchApiV1AssetLabelsArgs, options?: PatchApiV1AssetLabelsOptions) => { const {baseUrl = "https://api.projectdiscovery.io", fetch: fetchFn = fetch, XAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.update_type != null) query.append("update_type", String(args.update_type)) if (args['X-Team-Id'] != null) headers.set("X-Team-Id", String(args['X-Team-Id'])) if (XAPIKey !== undefined) headers.set("X-API-Key", XAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/asset/labels${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV1AssetLabelsResponse, await res.json()) };