import {z} from 'zod' export type GetApiAccountProjectsArgs = {page?: number | undefined, page_size?: number | undefined, limit?: number | undefined, offset?: number | undefined, order?: ('published_date' | 'modified_date' | 'views') | undefined, order_direction?: ('asc' | 'desc') | undefined, storage?: ('group' | 'individual') | undefined, roles?: string | undefined}; export type GetApiAccountProjectsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const projectPrivate = z.object({url: z.string(), id: z.number().int(), title: z.string(), created_date: z.string(), modified_date: z.string(), published_date: z.string().optional()}); export const getApiAccountProjectsResponse = z.array(projectPrivate); 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 }; /** * Private Projects * * GET /account/projects */ export const getApiAccountProjects = async (args: GetApiAccountProjectsArgs, options?: GetApiAccountProjectsOptions) => { const {baseUrl = "https://api.figshare.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.order != null) query.append("order", String(args.order)) if (args.order_direction != null) query.append("order_direction", String(args.order_direction)) if (args.storage != null) query.append("storage", String(args.storage)) if (args.roles != null) query.append("roles", String(args.roles)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/account/projects${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(getApiAccountProjectsResponse, await res.json()) };