UNPKG

clinicaltrialsgov-ts

Version:

Javascript / Typescript wrapper for Clinicaltrials.gov API

3 lines (2 loc) 8.33 kB
const e={bodySerializer:e=>JSON.stringify(e,(e,t)=>"bigint"==typeof t?t.toString():t)},t=async(e,t)=>{const r="function"==typeof t?await t(e):t;if(r)return"bearer"===e.scheme?`Bearer ${r}`:"basic"===e.scheme?`Basic ${btoa(r)}`:r},r=({allowReserved:e,explode:t,name:r,style:n,value:a})=>{if(!t){const t=(e?a:a.map(e=>encodeURIComponent(e))).join((e=>{switch(e){case"form":default:return",";case"pipeDelimited":return"|";case"spaceDelimited":return"%20"}})(n));switch(n){case"label":return`.${t}`;case"matrix":return`;${r}=${t}`;case"simple":return t;default:return`${r}=${t}`}}const o=(e=>{switch(e){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}})(n),l=a.map(t=>"label"===n||"simple"===n?e?t:encodeURIComponent(t):s({allowReserved:e,name:r,value:t})).join(o);return"label"===n||"matrix"===n?o+l:l},s=({allowReserved:e,name:t,value:r})=>{if(null==r)return"";if("object"==typeof r)throw new Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");return`${t}=${e?r:encodeURIComponent(r)}`},n=({allowReserved:e,explode:t,name:r,style:n,value:a,valueOnly:o})=>{if(a instanceof Date)return o?a.toISOString():`${r}=${a.toISOString()}`;if("deepObject"!==n&&!t){let t=[];Object.entries(a).forEach(([r,s])=>{t=[...t,r,e?s:encodeURIComponent(s)]});const s=t.join(",");switch(n){case"form":return`${r}=${s}`;case"label":return`.${s}`;case"matrix":return`;${r}=${s}`;default:return s}}const l=(e=>{switch(e){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}})(n),i=Object.entries(a).map(([t,a])=>s({allowReserved:e,name:"deepObject"===n?`${r}[${t}]`:t,value:a})).join(l);return"label"===n||"matrix"===n?l+i:i},a=/\{[^{}]+\}/g,o=({allowReserved:e,array:t,object:a}={})=>o=>{const l=[];if(o&&"object"==typeof o)for(const i in o){const u=o[i];if(null!=u)if(Array.isArray(u)){const s=r({allowReserved:e,explode:!0,name:i,style:"form",value:u,...t});s&&l.push(s)}else if("object"==typeof u){const t=n({allowReserved:e,explode:!0,name:i,style:"deepObject",value:u,...a});t&&l.push(t)}else{const t=s({allowReserved:e,name:i,value:u});t&&l.push(t)}}return l.join("&")},l=e=>i({baseUrl:e.baseUrl,path:e.path,query:e.query,querySerializer:"function"==typeof e.querySerializer?e.querySerializer:o(e.querySerializer),url:e.url}),i=({baseUrl:e,path:t,query:o,querySerializer:l,url:i})=>{let u=(null!=e?e:"")+(i.startsWith("/")?i:`/${i}`);t&&(u=(({path:e,url:t})=>{let o=t;const l=t.match(a);if(l)for(const t of l){let a=!1,l=t.substring(1,t.length-1),i="simple";l.endsWith("*")&&(a=!0,l=l.substring(0,l.length-1)),l.startsWith(".")?(l=l.substring(1),i="label"):l.startsWith(";")&&(l=l.substring(1),i="matrix");const u=e[l];if(null==u)continue;if(Array.isArray(u)){o=o.replace(t,r({explode:a,name:l,style:i,value:u}));continue}if("object"==typeof u){o=o.replace(t,n({explode:a,name:l,style:i,value:u,valueOnly:!0}));continue}if("matrix"===i){o=o.replace(t,`;${s({name:l,value:u})}`);continue}const c=encodeURIComponent("label"===i?`.${u}`:u);o=o.replace(t,c)}return o})({path:t,url:u}));let c=o?l(o):"";return c.startsWith("?")&&(c=c.substring(1)),c&&(u+=`?${c}`),u},u=(e,t)=>{var r;const s={...e,...t};return(null===(r=s.baseUrl)||void 0===r?void 0:r.endsWith("/"))&&(s.baseUrl=s.baseUrl.substring(0,s.baseUrl.length-1)),s.headers=c(e.headers,t.headers),s},c=(...e)=>{const t=new Headers;for(const r of e){if(!r||"object"!=typeof r)continue;const e=r instanceof Headers?r.entries():Object.entries(r);for(const[r,s]of e)if(null===s)t.delete(r);else if(Array.isArray(s))for(const e of s)t.append(r,e);else void 0!==s&&t.set(r,"object"==typeof s?JSON.stringify(s):s)}return t};class d{constructor(){Object.defineProperty(this,"_fns",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._fns=[]}clear(){this._fns=[]}getInterceptorIndex(e){return"number"==typeof e?this._fns[e]?e:-1:this._fns.indexOf(e)}exists(e){const t=this.getInterceptorIndex(e);return!!this._fns[t]}eject(e){const t=this.getInterceptorIndex(e);this._fns[t]&&(this._fns[t]=null)}update(e,t){const r=this.getInterceptorIndex(e);return!!this._fns[r]&&(this._fns[r]=t,e)}use(e){return this._fns=[...this._fns,e],this._fns.length-1}}const f=o({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),p={"Content-Type":"application/json"},h=(t={})=>({...e,headers:p,parseAs:"auto",querySerializer:f,...t}),y=((e={})=>{let r=u(h(),e);const s=()=>({...r}),n={error:new d,request:new d,response:new d},a=async e=>{var s,a,o;const i={...r,...e,fetch:null!==(a=null!==(s=e.fetch)&&void 0!==s?s:r.fetch)&&void 0!==a?a:globalThis.fetch,headers:c(r.headers,e.headers)};i.security&&await(async({security:e,...r})=>{var s;for(const n of e){const e=await t(n,r.auth);if(!e)continue;const a=null!==(s=n.name)&&void 0!==s?s:"Authorization";switch(n.in){case"query":r.query||(r.query={}),r.query[a]=e;break;case"cookie":r.headers.append("Cookie",`${a}=${e}`);break;default:r.headers.set(a,e)}return}})({...i,security:i.security}),i.requestValidator&&await i.requestValidator(i),i.body&&i.bodySerializer&&(i.body=i.bodySerializer(i.body)),void 0!==i.body&&""!==i.body||i.headers.delete("Content-Type");const u=l(i),d={redirect:"follow",...i};let f=new Request(u,d);for(const e of n.request._fns)e&&(f=await e(f,i));const p=i.fetch;let h=await p(f);for(const e of n.response._fns)e&&(h=await e(h,f,i));const y={request:f,response:h};if(h.ok){if(204===h.status||"0"===h.headers.get("Content-Length"))return"data"===i.responseStyle?{}:{data:{},...y};const e=null!==(o="auto"===i.parseAs?(e=>{var t;if(!e)return"stream";const r=null===(t=e.split(";")[0])||void 0===t?void 0:t.trim();return r?r.startsWith("application/json")||r.endsWith("+json")?"json":"multipart/form-data"===r?"formData":["application/","audio/","image/","video/"].some(e=>r.startsWith(e))?"blob":r.startsWith("text/")?"text":void 0:void 0})(h.headers.get("Content-Type")):i.parseAs)&&void 0!==o?o:"json";let t;switch(e){case"arrayBuffer":case"blob":case"formData":case"json":case"text":t=await h[e]();break;case"stream":return"data"===i.responseStyle?h.body:{data:h.body,...y}}return"json"===e&&(i.responseValidator&&await i.responseValidator(t),i.responseTransformer&&(t=await i.responseTransformer(t))),"data"===i.responseStyle?t:{data:t,...y}}const v=await h.text();let m;try{m=JSON.parse(v)}catch(e){}const b=null!=m?m:v;let g=b;for(const e of n.error._fns)e&&(g=await e(b,h,f,i));if(g=g||{},i.throwOnError)throw g;return"data"===i.responseStyle?void 0:{error:g,...y}};return{buildUrl:l,connect:e=>a({...e,method:"CONNECT"}),delete:e=>a({...e,method:"DELETE"}),get:e=>a({...e,method:"GET"}),getConfig:s,head:e=>a({...e,method:"HEAD"}),interceptors:n,options:e=>a({...e,method:"OPTIONS"}),patch:e=>a({...e,method:"PATCH"}),post:e=>a({...e,method:"POST"}),put:e=>a({...e,method:"PUT"}),request:a,setConfig:e=>(r=u(r,e),s()),trace:e=>a({...e,method:"TRACE"})}})(h({baseUrl:"https://clinicaltrials.gov/api/v2"})),v=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/studies",...e})},m=e=>{var t;return(null!==(t=e.client)&&void 0!==t?t:y).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/studies/{nctId}",...e})},b=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({url:"/studies/metadata",...e})},g=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({url:"/studies/search-areas",...e})},w=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({url:"/studies/enums",...e})},x=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({url:"/stats/size",...e})},j=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/stats/field/values",...e})},S=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/stats/field/sizes",...e})},q=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:y).get({url:"/version",...e})};export{y as client,w as enums,m as fetchStudy,j as fieldValuesStats,S as listFieldSizesStats,v as listStudies,g as searchAreas,x as sizeStats,b as studiesMetadata,q as version}; //# sourceMappingURL=index.esm.js.map