UNPKG

@adisuper94/orcid

Version:
196 lines (195 loc) 6.81 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tryCatch = tryCatch; exports.parseEmploymentResp = parseEmploymentResp; exports.parseEducationResp = parseEducationResp; const z = __importStar(require("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"]))), })), }); 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))]; } } 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]; } } 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]; } }