import {z} from 'zod' export type GetApiAuthorArgs = {name?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, vision_id?: string | undefined, branch?: string | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiAuthorOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAuthorResponse = z.object({links: z.object({self: z.string(), first: z.string(), last: z.string(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}), data: z.array(z.object({id: z.string(), type: z.literal("author"), attributes: z.object({name: z.string(), job_grade: z.object({name: z.string(), associated_press_name: z.string(), abbreviation: z.string(), branch: z.enum(["army", "navy", "air-force", "marines", "coast-guard", "joint"]).nullable().optional(), job_grade: z.string().nullable().optional(), nato_code: z.string().nullable().optional(), country_code: z.string().nullable().optional()}).optional(), vision_id: z.string().nullable()}), relationships: z.object({service_units: z.object({data: z.array(z.object({id: z.string(), type: z.literal("service-unit")}))})})}))}); 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 }; /** * Get a list of available authors with paging and filtering. * * GET /author */ export const getApiAuthor = async (args: GetApiAuthorArgs, options?: GetApiAuthorOptions) => { const {baseUrl = "https://submitapi.dvidshub.net", fetch: fetchFn = fetch, DVIDS} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.name != null) query.append("name", String(args.name)) if (args.first_name != null) query.append("first_name", String(args.first_name)) if (args.last_name != null) query.append("last_name", String(args.last_name)) if (args.vision_id != null) query.append("vision_id", String(args.vision_id)) if (args.branch != null) query.append("branch", String(args.branch)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (DVIDS !== undefined) headers.set('authorization', `Bearer ${DVIDS}`) const url = `${baseUrl.replace(/\/+$/, '')}/author${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(getApiAuthorResponse, await res.json()) };