UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

245 lines (244 loc) 8.54 kB
import { ENTITY as p, ENTITY_ID as u, ENTITY_ID_VERSIONS as g, ENTITY_ID_VERSION as k, ENTITY_SCHEMA_BINDING as w, ENTITY_JSON as f, ENTITY_HEADERS as $, ENTITY_BUNDLE_V2 as m } from "../../../utils/APIConstants.js"; import { normalizeSynPrefix as I } from "../../../utils/functions/EntityTypeUtils.js"; import { uniqueId as E } from "lodash-es"; import { http as i, HttpResponse as c } from "msw"; import v, { mockProjectsEntityData as D } from "../../entity/index.js"; import { MOCK_INVALID_PROJECT_NAME as T } from "../../entity/mockEntity.js"; import { mockSynapseStorageUploadDestination as h, mockUploadDestinations as j } from "../../mock_upload_destination.js"; import { mockSchemaBinding as N } from "../../mockSchema.js"; function S(r) { const t = r.map(I); return v.filter( (o) => t.includes(I(o.id)) ); } function d(r) { const t = S([r]); if (t.length != 0) return t[0]; } function b(r, t) { return i.post( `${r}${m(":entityId")}`, ({ params: o }) => { let n = 404, e = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity bundle with ID ${o.entityId}` }; if (t) e = t, n = 200; else { const s = d(o.entityId); s?.bundle && (e = s.bundle, n = 200); } return c.json(e, { status: n }); } ); } function V(r, t) { return i.get( `${r}${f(":entityId")}`, () => { const o = t; return c.json(o, { status: 200 }); } ); } function R(r, t) { return i.post( `${r}${m(":entityId", ":versionNumber")}`, ({ params: o }) => { const n = o.entityId, e = parseInt(o.versionNumber); let s = 404, l = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity bundle with ID ${n}` }; if (t) l = t, s = 200; else { const a = d(n); if (a) { const y = a.bundle; a.versions && a.versions[e] ? l = { ...y, entity: a.versions[e] } : l = y, s = 200; } } return c.json(l, { status: s }); } ); } const P = (r) => [ /** * Create a new entity */ i.post(`${r}${p}`, async ({ request: t }) => { let o = 200; const n = await t.json(); let e = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: "..." }; return n ? n.name === T ? (e.reason = "Invalid project name", o = 403) : e = { id: E("syn"), ...n } : (o = 400, e = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock service worker received the following malformed body for PUT ${p} : ${JSON.stringify( n )}` }), c.json(e, { status: o }); }), /** * Get entity by ID */ i.get(`${r}${u(":entityId")}`, ({ params: t }) => { let o = 404, n = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity with ID ${t.entityId}` }; const e = d(t.entityId); return e && (n = e.entity, o = 200), c.json(n, { status: o }); }), i.get( `${r}${g(":entityId")}`, ({ params: t }) => { let o = 404, n = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find mock entity versions for ID ${t.entityId}` }; const e = d(t.entityId); return e && e.versionInfo && (n = { results: e.versionInfo }, o = 200), c.json(n, { status: o }); } ), i.get( `${r}${k(":entityId", ":versionNumber")}`, ({ params: t }) => { let o = 404; const n = t.entityId, e = t.versionNumber.toString(), s = parseInt(e); let l = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock versioned entity with ID ${n}.${e}` }; const a = d(t.entityId); return a && a.versions && a.versions[s] && (l = a.versions[s], o = 200), c.json(l, { status: o }); } ), b(r), R(r), i.get(`${r}${w(":entityId")}`, () => c.json(N, { status: 200 })), i.get( `${r}${f(":entityId")}`, ({ params: t }) => { let o = 404, n = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity bundle with ID ${t.entityId}` }; const e = d(t.entityId); return e?.json && (n = e.json, o = 200), c.json(n, { status: o }); } ), i.post( `${r}${$}`, async ({ params: t, request: o }) => { let n = 404, e = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity bundle with ID ${t.entityId}` }; const l = (await o.json()).references.map((a) => { const y = d(a.targetId)?.entityHeader; if (y) return { ...y, // ID should match what is passed in (i.e. may or may not have syn prefix) id: a.targetId }; }).filter((a) => !!a); return l && (e = { results: l }, n = 200), c.json(e, { status: n }); } ), i.get( `${r}${u(":entityId")}/path`, ({ params: t }) => { let o = 404, n = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity path using ID ${t.entityId}` }; const e = d(t.entityId); return e && e.path && (n = e.path, o = 200), c.json(n, { status: o }); } ), i.get(`${r}/file/v1/entity/:id/uploadDestination`, () => { const t = h; return c.json(t, { status: 200 }); }), i.get( `${r}/file/v1/entity/:id/uploadDestination/:storageLocationId`, ({ params: t }) => { let o = 404, n = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find an uploadDestination using storageLocationId ${t.storageLocationId}` }; const e = j.find( (s) => Number(t.storageLocationId) === s.storageLocationId ); return e && (n = e, o = 200), c.json(n, { status: o }); } ), i.get(`${r}/repo/v1/projects`, () => { const t = { results: D.map((o) => ({ name: o.name, id: o.id, lastActivity: "2024-01-04T21:11:59.000Z", modifiedBy: parseInt(o.entity.modifiedBy), modifiedOn: o.entity.modifiedOn })) }; return c.json(t, { status: 200 }); }), i.post( `${r}${u(":entityId")}/acl`, async ({ params: t, request: o }) => { const n = d(t.entityId); let e, s; return n ? n.bundle?.accessControlList ? (e = 403, s = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: "Resource already has an ACL." }) : (s = await o.json(), e = 201) : (e = 404, s = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity bundle with ID ${t.entityId}` }), c.json(s, { status: e }); } ), i.put( `${r}${u(":entityId")}/acl`, async ({ params: t, request: o }) => { const n = d(t.entityId); let e, s; return n ? n?.bundle?.accessControlList ? (s = await o.json(), e = 200) : (s = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: "Cannot update ACL for a resource which inherits its permissions." }, e = 403) : (e = 404, s = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity bundle with ID ${t.entityId}` }), c.json(s, { status: e }); } ), i.delete(`${r}${u(":entityId")}/acl`, ({ params: t }) => { const o = d(t.entityId); let n, e; return o ? o?.bundle?.accessControlList ? (e = "", n = 200) : (e = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: "Cannot delete ACL for a resource which inherits its permissions." }, n = 403) : (n = 404, e = { concreteType: "org.sagebionetworks.repo.model.ErrorResponse", reason: `Mock Service worker could not find a mock entity bundle with ID ${t.entityId}` }), c.json(e, { status: n }); }) ]; export { b as getEntityBundleHandler, P as getEntityHandlers, V as getEntityJsonHandler, R as getVersionedEntityBundleHandler }; //# sourceMappingURL=entityHandlers.js.map