import {z} from 'zod' export type GetApiV1HoneytokensHoneytokenIdSourcesArgs = {honeytoken_id: string, cursor?: string | undefined, per_page?: number | undefined, ordering?: ('source_id' | '-source_id') | undefined, provider_metadata_archived?: boolean | undefined}; export type GetApiV1HoneytokensHoneytokenIdSourcesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1HoneytokensHoneytokenIdSourcesResponse = z.array(z.object({provider_metadata: z.object({archived: z.boolean()}).optional(), type: z.enum(["azure_devops", "bitbucket", "bitbucket_cloud", "github", "gitlab"]).optional(), name: z.string().optional(), url: z.string().optional(), open_issues_count: z.number().optional(), total_files_count: z.number().optional(), files: z.array(z.string()).optional(), source_id: z.number().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 }; /** * List sources on an honeytoken * * GET /v1/honeytokens/{honeytoken_id}/sources */ export const getApiV1HoneytokensHoneytokenIdSources = async (args: GetApiV1HoneytokensHoneytokenIdSourcesArgs, options?: GetApiV1HoneytokensHoneytokenIdSourcesOptions) => { const {baseUrl = "https://api.gitguardian.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.provider_metadata_archived != null) query.append("provider_metadata_archived", String(args.provider_metadata_archived)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/honeytokens/${encodeURIComponent(String(args.honeytoken_id))}/sources${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(getApiV1HoneytokensHoneytokenIdSourcesResponse, await res.json()) };