import {z} from 'zod' export type GetApiV3SearchTypeUsersArgs = {q?: string | undefined, username?: string | undefined, location?: string | undefined, account?: ('free' | 'pro' | 'biz') | undefined, skills?: Array | undefined, sort_by?: ('followerCount' | '-followerCount' | 'modelCount' | '-modelCount' | 'username') | undefined}; export type GetApiV3SearchTypeUsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const skillDetail = z.object({name: z.string().optional(), uri: z.string().optional()}); export const avatarRelated = z.object({images: z.array(z.object({url: z.string().optional(), width: z.number().int().optional(), height: z.number().int().optional(), size: z.number().int().optional()})).optional(), uid: z.string().optional(), uri: z.string().optional()}); export const userSearchList = z.object({username: z.string().optional(), city: z.string().nullable().optional(), followerCount: z.number().int().optional(), displayName: z.string().optional(), uid: z.string().optional(), skills: z.array(skillDetail).optional(), country: z.string().nullable().optional(), account: z.string().optional(), modelCount: z.number().int().optional(), profileUrl: z.string().optional(), avatar: avatarRelated.optional()}); export const userSearchResponse = z.object({results: z.array(userSearchList).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 }; /** * Search and filters in users * * GET /v3/search?type=users */ export const getApiV3SearchTypeUsers = async (args: GetApiV3SearchTypeUsersArgs, options?: GetApiV3SearchTypeUsersOptions) => { const {baseUrl = "https://api.sketchfab.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.username != null) query.append("username", String(args.username)) if (args.location != null) query.append("location", String(args.location)) if (args.account != null) query.append("account", String(args.account)) for (const item of args.skills ?? []) query.append("skills", String(item)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/search?type=users${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(userSearchResponse, await res.json()) };