UNPKG

dandi

Version:

A JavaScript API for the Distributed Archives for Neurophysiology Data Integration (DANDI)

2 lines (1 loc) 6.14 kB
(function(n,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(n=typeof globalThis<"u"?globalThis:n||self,d(n.dandi={}))})(this,function(n){"use strict";var G=(n,d,c)=>{if(!d.has(n))throw TypeError("Cannot "+c)};var a=(n,d,c)=>(G(n,d,"read from private field"),c?c.call(n):d.get(n)),l=(n,d,c)=>{if(d.has(n))throw TypeError("Cannot add the same private member more than once");d instanceof WeakSet?d.add(n):d.set(n,c)},u=(n,d,c,p)=>(G(n,d,"write to private field"),p?p.call(n,c):d.set(n,c),c);var y,w,O,j,$,b,g,S,f,U,k;const d={main:"api.dandiarchive.org",sandbox:"api.sandbox.dandiarchive.org"},c=async(s,t)=>{var i,r,o;const e=await P(s,t);if(e){const h=(i=e.most_recent_published_version)==null?void 0:i.version;if(h)return h;if(((r=e.draft_version)==null?void 0:r.status)==="Valid")return(o=e.draft_version)==null?void 0:o.version}else return null},p=s=>typeof s=="string"?d[s]:d.main,L=(s,t)=>(s.startsWith("http")&&new URL(s),new URL(s,`https://${p(t)}/api/`)),m=(s,t)=>v(s,{options:t}),R=s=>`dandisets/${s}`,D=s=>{var t;return`dandisets/${s.dandiset}/versions/${((t=s.options)==null?void 0:t.version)||"draft"}/assets/${s.id}`},P=(s,t)=>m(R(s),t),E=(s,t={})=>`${R(s)}/versions/${t!=null&&t.version?t.version:"draft"}`,M=async(s,t={})=>{const e=t.version??await c(s,t);if(e)return m(E(s,{...t,version:e}),t)},_=async(s,t)=>{const e=[];if(s.results&&e.push(...s.results),s.next){const i=await m(s.next,t);e.push(...await _(i,t))}return e},H=Object.freeze(Object.defineProperty({__proto__:null,getAssetUrl:D,getBase:P,getDandisetURL:R,getInfo:M,getInfoURL:E,getInstance:p,getJSON:m,getLatestVersion:c,getURL:L,paginate:_},Symbol.toStringTag,{value:"Module"})),W=s=>{const t={accept:"application/json","Content-Type":"application/json"};return s&&(t.Authorization=`token ${s}`),t},v=async(s,t)=>{const{options:e={}}=t,i=L(s,e.type),r={...W(e.token),...t.headers??{}};if(t.method&&t.method!=="GET"){const o=t.body||JSON.stringify(t.json??{});if(!e.token)throw new Error("No DANDI API token provided.");return await fetch(i,{method:t.method,headers:r,body:o}).then(h=>{if(h.ok)return h.json();throw new Error(`Request failed with status code ${h.status}`)}).catch(h=>(h instanceof TypeError&&console.error("[dandi]: Backend does not have CORS enabled for POST / PUT requests..."),null))}else return await fetch(i,{headers:r}).then(o=>o.json())};class N{constructor(t,e,i={}){l(this,y,void 0);l(this,w,void 0);l(this,O,void 0);l(this,j,void 0);u(this,O,o=>{Object.assign(this,o),this.asset_id=o.asset_id}),u(this,j,()=>({id:this.asset_id,dandiset:a(this,w),options:a(this,y)})),this.update=async(o={})=>{const h=Object.assign(Object.assign({},this.metadata),o),A=a(this,j).call(this),J=this.zarr?{zarr_id:this.zarr}:{blob_id:this.blob},I=await v(D(A),{options:a(this,y),method:"PUT",body:JSON.stringify({metadata:h,...J})});return Object.assign(this,I),I};const r=e&&typeof e=="object"&&!(e instanceof String);r&&a(this,O).call(this,e),this.asset_id=r?this.asset_id=e.asset_id:e,u(this,y,i),u(this,w,t)}async get(t=a(this,w),e=this.asset_id,i=a(this,y)){u(this,w,t),u(this,y,i),this.asset_id=e;const r=await T(a(this,j).call(this));return r&&Object.assign(this,r),r}}y=new WeakMap,w=new WeakMap,O=new WeakMap,j=new WeakMap;const T=async s=>{const t=await v(`${D(s)}/info`,{options:s.options});return new N(s.dandiset,t,s.options)};class z{constructor(t,e){l(this,$,{});l(this,b,{});l(this,g,{});l(this,S,!1);l(this,f,{});l(this,U,t=>{u(this,$,t),Object.assign(this,t),this.identifier=t.identifier});const i=t&&typeof t=="object"&&!(t instanceof String);i&&a(this,U).call(this,t),this.identifier=i?t.identifier:t,u(this,f,e)}async get(t=this.identifier){if(t!==this.identifier){this.identifier=t,u(this,b,{}),u(this,g,{});const e=await P(this.identifier,a(this,f));e&&Object.assign(this,e)}return a(this,$)}async getInfo(t=a(this,f)){return t={...a(this,f),...t},Object.keys(a(this,b)).length===0&&u(this,b,await M(this.identifier,t)),a(this,b)}async getAsset(t,e=a(this,f)){return e={...a(this,f),...e},a(this,g)[t]||(a(this,g)[t]=await T({dandiset:this.identifier,id:t,options:e})),a(this,g)[t]}async getAssets(t=a(this,f)){if(!a(this,S)){const e=await C({dandiset:this.identifier,options:t});e==null||e.forEach(i=>a(this,g)[i.asset_id]=i),u(this,S,!0)}return a(this,g)}}$=new WeakMap,b=new WeakMap,g=new WeakMap,S=new WeakMap,f=new WeakMap,U=new WeakMap;const q=(s,t={})=>`${E(s,t)}/assets`,C=async s=>{const t=typeof s=="string"?{dandiset:s}:s;let e;const i=t.dandiset;"options"in t?e=t.options:e={};const r=e.version??await c(i,e);if(r){const o=`${q(i,{...e,version:r})}`,h=await m(o,e);return await Promise.all((await _(h,e)).map(async A=>T({dandiset:i,options:e,id:A.asset_id})))}},F=async(s={})=>{const t=`https://${p(s.type)}/api/dandisets`,e=await m(t,s);return(await _(e,s)).map(r=>new z(r,s))},V=async(s,t={})=>{const e=await P(s,t);return e?new z(e,t):null},K=async(s={},t={})=>{const{draft:e=!0,empty:i=!0,embargoed:r=!1}=t,o=new URLSearchParams;o.append("user","me"),o.append("draft",e?"true":"false"),o.append("empty",i?"true":"false"),o.append("embargoed",r?"true":"false");const h=`dandisets?${o.toString()}`,A=await v(h,{options:s});return(await _(A,s)).map(I=>new z(I,s))};class Q{constructor({token:t,type:e="main"}){l(this,k,void 0);this.authorized=!1,u(this,k,()=>{if(!this.authorized)throw new Error("API is not authorized. Please provide a valid token.")}),this.get=async(i,r={})=>V(i,{...r,...this}),this.create=async(i,r={},o=!1)=>{a(this,k).call(this);const h=await v(`dandisets/?embargo=${o}`,{options:this,method:"POST",json:{name:i,metadata:r}});if(h.detail)throw new Error(h.detail);return this.get(h.identifier)},this.authorize=async(i=this.token)=>{const r=await B({token:i,type:this.type}).catch(o=>(console.error(o),!1));return this.token=i,this.authorized=r},this.token=t,this.type=e}}k=new WeakMap;const B=async s=>v("auth/token",{options:s});n.API=Q,n.Asset=N,n.Dandiset=z,n.get=V,n.getAll=F,n.getAsset=T,n.getAssets=C,n.getAssetsUrl=q,n.getMine=K,n.utils=H,n.validateToken=B,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});