import {z} from 'zod' export type GetApiJournalsIssnArgs = {issn: Array}; export type GetApiJournalsIssnOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const doiCounts = z.object({"total-dois": z.number().int(), "current-dois": z.number().int(), "backfile-dois": z.number().int()}); export const breakdownsObject = z.object({"dois-by-issued-year": z.array(z.array(z.number().int()))}); export const coverageFull = z.object({"affiliations-current": z.number(), "similarity-checking-current": z.number(), "descriptions-current": z.number(), "ror-ids-current": z.number(), "references-backfie": z.number(), "funders-backfile": z.number(), "licenses-backfile": z.number(), "funders-current": z.number(), "affiliations-backfile": z.number(), "resource-links-backfile": z.number(), "orcids-backfile": z.number(), "update-policies-current": z.number(), "ror-ids-backfile": z.number(), "orcids-current": z.number(), "similarity-checking-backfile": z.number(), "descriptions-backfile": z.number(), "award-numbers-backfile": z.number(), "update-policies-backfile": z.number(), "licenses-current": z.number(), "award-numbers-current": z.number(), "abstracts-backfile": z.number(), "resource-links-current": z.number(), "abstracts-current": z.number(), "references-current": z.number()}); export const coverage = z.object({"last-status-check-time": z.number().int(), affiliations: z.number(), abstracts: z.number(), orcids: z.number(), licenses: z.number(), references: z.number(), funders: z.number(), "similarity-checking": z.number(), "award-numbers": z.number(), "ror-ids": z.number(), "update-policies": z.number(), "resource-links": z.number(), descriptions: z.number()}); export const coverageObject = z.record(z.string(), coverage); export const coverageTypeObject = z.object({all: coverageObject, current: coverageObject, backfile: coverageObject}); export const flags = z.object({"deposits-abstracts-current": z.boolean(), "deposits-orcids-current": z.boolean(), deposits: z.boolean(), "deposits-affiliations-backfile": z.boolean(), "deposits-update-policies-backfile": z.boolean(), "deposits-award-numbers-current": z.boolean(), "deposits-resource-links-current": z.boolean(), "deposits-ror-ids-current": z.boolean(), "deposits-articles": z.boolean(), "deposits-affiliations-current": z.boolean(), "deposits-funders-current": z.boolean(), "deposits-references-backfile": z.boolean(), "deposits-ror-ids-backfile": z.boolean(), "deposits-abstracts-backfile": z.boolean(), "deposits-licenses-backfile": z.boolean(), "deposits-award-numbers-backfile": z.boolean(), "deposits-descriptions-current": z.boolean(), "deposits-references-current": z.boolean(), "deposits-resource-links-backfile": z.boolean(), "deposits-descriptions-backfile": z.boolean(), "deposits-orcids-backfile": z.boolean(), "deposits-funders-backfile": z.boolean(), "deposits-update-policies-current": z.boolean(), "deposits-licenses-current": z.boolean()}); export const journalIssnType = z.object({value: z.string(), type: z.string()}); export const journal = z.object({"last-status-check-time": z.number().int(), counts: doiCounts, breakdowns: breakdownsObject, publisher: z.string(), coverage: coverageFull, title: z.string(), subjects: z.array(z.string()), "coverage-type": coverageTypeObject, flags: flags, ISSN: z.array(z.string()), "issn-type": journalIssnType}); export const journalMessage = z.object({status: z.string(), "message-type": z.string(), "message-version": z.string(), message: journal}); 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 information about a journal with the given ISSN, as an example use ISSN 03064530 * * GET /journals/{issn} */ export const getApiJournalsIssn = async (args: GetApiJournalsIssnArgs, options: GetApiJournalsIssnOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/journals/${encodeURIComponent(String(args.issn))}${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(journalMessage, await res.json()) };