import {z} from 'zod' export type GetApiV3OrgsOrgUidEnvironmentsArgs = {sort_by?: ('createdAt' | '-createdAt') | undefined}; export type GetApiV3OrgsOrgUidEnvironmentsOptions = {baseUrl?: string | undefined, Token?: string | undefined, fetch?: typeof fetch | undefined}; export const orgTextureImageDetail = z.object({width: z.number().int().optional(), name: z.string().optional(), uid: z.string().optional(), file: z.string().optional(), height: z.number().int().optional()}); export const orgTextureDetail = z.object({images: z.array(orgTextureImageDetail).optional(), format: z.string().optional(), type: z.string().optional(), encoding: z.string().optional()}); export const orgTextureList = z.array(orgTextureDetail); export const processingStatus = z.enum(["pending", "processing", "succeeded", "failed"]); export const orgLightDetail = z.object({direction: z.array(z.number()).optional(), area: z.object({y: z.number().optional(), h: z.number().optional(), z: z.number().optional(), w: z.number().optional(), x: z.number().optional()}).optional(), color: z.array(z.number()).optional(), luminosity: z.number().optional(), sum: z.number().optional(), lum_ratio: z.number().optional(), error: z.number().int().optional(), variance: z.number().optional()}); export const orgLightList = z.array(orgLightDetail); export const orgEnvironmentDetail = z.object({textures: orgTextureList.optional(), uid: z.string().optional(), brightness: z.number().optional(), processing: processingStatus.optional(), uri: z.string().optional(), lights: orgLightList.optional(), isDefault: z.boolean().optional(), diffuseSPH: z.array(z.number()).optional(), createdAt: z.string().optional(), name: z.string().optional()}); export const orgEnvironmentListResponse = z.object({results: z.array(orgEnvironmentDetail).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 }; /** * Returns a list of environments from the given organization. Can take either the organization UID (orgUid) or name preceded by a @ (@orgName). * * GET /v3/orgs/{orgUid}/environments */ export const getApiV3OrgsOrgUidEnvironments = async (args: GetApiV3OrgsOrgUidEnvironmentsArgs, options?: GetApiV3OrgsOrgUidEnvironmentsOptions) => { const {baseUrl = "https://api.sketchfab.com", fetch: fetchFn = fetch, Token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (Token !== undefined) headers.set("Authorization", Token) const url = `${baseUrl.replace(/\/+$/, '')}/v3/orgs/${encodeURIComponent(String(args.orgUid))}/environments${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(orgEnvironmentListResponse, await res.json()) };