import {z} from 'zod' export type GetApiV2PartsFieldsArgs = {fields?: string | undefined, name?: string | undefined, parts?: number | undefined, start?: number | undefined, end?: number | undefined, cursor?: number | undefined, limit?: number | undefined, value?: string | undefined, locations?: string | undefined, values?: number | undefined, page?: number | undefined}; export type GetApiV2PartsFieldsOptions = {baseUrl: string, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV2PartsFieldsResponse = z.array(z.object({fieldID: z.number().int().optional(), partID: z.number().int().optional(), fieldName: z.string().optional(), value: z.unknown(), valueID: z.number().int().optional(), fieldType: z.string().optional(), lastEdited: z.number().int().optional(), locationID: z.number().int().optional(), files: z.array(z.unknown()).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 }; /** * Part Fields * * GET /v2/parts/fields */ export const getApiV2PartsFields = async (args: GetApiV2PartsFieldsArgs, options: GetApiV2PartsFieldsOptions) => { const {baseUrl, fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fields != null) query.append("fields", String(args.fields)) if (args.name != null) query.append("name", String(args.name)) if (args.parts != null) query.append("parts", String(args.parts)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.value != null) query.append("value", String(args.value)) if (args.locations != null) query.append("locations", String(args.locations)) if (args.values != null) query.append("values", String(args.values)) if (args.page != null) query.append("page", String(args.page)) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/parts/fields${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(getApiV2PartsFieldsResponse, await res.json()) };