UNPKG

@adisuper94/orcid

Version:
168 lines (167 loc) 5.59 kB
import * as z from "zod/v4-mini"; z.config(z.locales.en()); const DisambiguatedOrgSchema = z.pipe(z.object({ "disambiguated-organization-identifier": z.string(), "disambiguation-source": z.string(), }), z.transform((disambiguatedOrg) => ({ id: disambiguatedOrg["disambiguated-organization-identifier"], source: disambiguatedOrg["disambiguation-source"], }))); function _testTypeDisambiguatedOrg(disambiguatedOrg) { return disambiguatedOrg; } function __testTypeDisambiguatedOrg(disambiguatedOrg) { return disambiguatedOrg; } const OrgSchema = z.pipe(z.object({ name: z.string(), address: z.object({ city: z.nullable(z.string()), region: z.nullable(z.string()), country: z.nullish(z.string()), }), "disambiguated-organization": z.nullable(DisambiguatedOrgSchema), }), z.transform((org) => { const { "disambiguated-organization": _, ...rest } = org; const disambiguatedOrg = org["disambiguated-organization"]; return { ...rest, disambiguatedOrg, }; })); function _testTypeOrg(org) { return org; } function __testTypeOrg(org) { return org; } const SourceOrcidSchema = z.object({ uri: z.string(), path: z.string(), host: z.string(), }); function _testTypeSourceOrcid(sourceOrcid) { return sourceOrcid; } function __testTypeSourceOrcid(sourceOrcid) { return sourceOrcid; } const SourceSchema = z.pipe(z.object({ "orcid": z.optional(SourceOrcidSchema), "source-client-id": z.nullable(z.string()), "source-name": z.object({ value: z.string(), }), }), z.transform((source) => { const name = source["source-name"].value; const clientId = source["source-client-id"]; const orcid = source.orcid; const transformedSource = { orcid, clientId, name }; return transformedSource; })); function _testTypeSource(source) { return source; } function __testTypeSource(source) { return source; } const DateSchema = z.pipe(z.object({ year: z.object({ value: z.coerce.number(), }), month: z.nullish(z.object({ value: z.coerce.number(), })), day: z.nullish(z.object({ value: z.coerce.number(), })), }), z.transform((date) => { const year = date.year.value; return new Date(year, date.month?.value ?? 0, date.day?.value ?? 1); })); const AffiliationGroupSchema = z.pipe(z.object({ "put-code": z.number(), "department-name": z.nullish(z.string()), "role-title": z.nullable(z.string()), "start-date": z.nullable(DateSchema), "end-date": z.nullable(DateSchema), path: z.string(), visibility: z.string(), organization: z.nullable(OrgSchema), url: z.nullable(z.object({ value: z.string().check(z.url()) })), source: SourceSchema, "created-date": z.pipe(z.object({ value: z.number().check(z.minimum(0)) }), z.transform((time) => new Date(time.value))), "last-modified-date": z.pipe(z.object({ value: z.number().check(z.minimum(0)) }), z.transform((time) => new Date(time.value))), }), z.transform((emp) => { const { "put-code": putCode, "role-title": roleTitle, "start-date": startDate, "end-date": endDate, "department-name": departmentName, path, visibility, } = emp; const org = emp.organization; const source = emp.source; const url = emp.url?.value; const affiliationGroup = { source, putCode, departmentName: departmentName ?? undefined, roleTitle: roleTitle ?? undefined, startDate: startDate ?? undefined, endDate: endDate ?? undefined, path, visibility, org: org ?? undefined, url: url ?? undefined, createdDate: emp["created-date"], modifiedDate: emp["last-modified-date"], }; return affiliationGroup; })); function _testTypeAffiliationGroup(affiliationGroup) { return affiliationGroup; } function __testTypeAffiliationGroup(affiliationGroup) { return affiliationGroup; } const EmploymentRespSchema = z.object({ "last-modified-date": z.object({ value: z.number().check(z.minimum(0)) }), path: z.string(), "affiliation-group": z.array(z.object({ summaries: z.array(z.pipe(z.object({ "employment-summary": AffiliationGroupSchema }), z.transform((empSummary) => empSummary["employment-summary"]))), })), }); const EducationRespSchema = z.object({ "last-modified-date": z.nullable(z.object({ value: z.number().check(z.minimum(0)) })), path: z.string(), "affiliation-group": z.array(z.object({ summaries: z.array(z.pipe(z.object({ "education-summary": AffiliationGroupSchema }), z.transform((empSummary) => empSummary["education-summary"]))), })), }); export async function tryCatch(promise) { try { const t = await promise; return [t, undefined]; } catch (err) { if (err instanceof Error) { return [undefined, err]; } return [undefined, new Error(String(err))]; } } export function parseEmploymentResp(obj) { const emp = EmploymentRespSchema.safeParse(obj); if (emp.success) { const empRecords = emp.data["affiliation-group"].map((ag) => ag.summaries[0]); return [empRecords, undefined]; } else { return [undefined, emp.error]; } } export function parseEducationResp(obj) { const emp = EducationRespSchema.safeParse(obj); if (emp.success) { const empRecords = emp.data["affiliation-group"].map((ag) => ag.summaries[0]); return [empRecords, undefined]; } else { return [undefined, emp.error]; } }