clinicaltrialsgov-ts
Version:
Javascript / Typescript wrapper for Clinicaltrials.gov API
3 lines (2 loc) • 8.53 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ClinicalTrialsGov={})}(this,function(e){"use strict";const t={bodySerializer:e=>JSON.stringify(e,(e,t)=>"bigint"==typeof t?t.toString():t)},r=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},s=({allowReserved:e,explode:t,name:r,style:s,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"}})(s));switch(s){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"&"}})(s),i=a.map(t=>"label"===s||"simple"===s?e?t:encodeURIComponent(t):n({allowReserved:e,name:r,value:t})).join(o);return"label"===s||"matrix"===s?o+i:i},n=({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)}`},a=({allowReserved:e,explode:t,name:r,style:s,value:a,valueOnly:o})=>{if(a instanceof Date)return o?a.toISOString():`${r}=${a.toISOString()}`;if("deepObject"!==s&&!t){let t=[];Object.entries(a).forEach(([r,s])=>{t=[...t,r,e?s:encodeURIComponent(s)]});const n=t.join(",");switch(s){case"form":return`${r}=${n}`;case"label":return`.${n}`;case"matrix":return`;${r}=${n}`;default:return n}}const i=(e=>{switch(e){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}})(s),l=Object.entries(a).map(([t,a])=>n({allowReserved:e,name:"deepObject"===s?`${r}[${t}]`:t,value:a})).join(i);return"label"===s||"matrix"===s?i+l:l},o=/\{[^{}]+\}/g,i=({allowReserved:e,array:t,object:r}={})=>o=>{const i=[];if(o&&"object"==typeof o)for(const l in o){const u=o[l];if(null!=u)if(Array.isArray(u)){const r=s({allowReserved:e,explode:!0,name:l,style:"form",value:u,...t});r&&i.push(r)}else if("object"==typeof u){const t=a({allowReserved:e,explode:!0,name:l,style:"deepObject",value:u,...r});t&&i.push(t)}else{const t=n({allowReserved:e,name:l,value:u});t&&i.push(t)}}return i.join("&")},l=e=>u({baseUrl:e.baseUrl,path:e.path,query:e.query,querySerializer:"function"==typeof e.querySerializer?e.querySerializer:i(e.querySerializer),url:e.url}),u=({baseUrl:e,path:t,query:r,querySerializer:i,url:l})=>{let u=(null!=e?e:"")+(l.startsWith("/")?l:`/${l}`);t&&(u=(({path:e,url:t})=>{let r=t;const i=t.match(o);if(i)for(const t of i){let o=!1,i=t.substring(1,t.length-1),l="simple";i.endsWith("*")&&(o=!0,i=i.substring(0,i.length-1)),i.startsWith(".")?(i=i.substring(1),l="label"):i.startsWith(";")&&(i=i.substring(1),l="matrix");const u=e[i];if(null==u)continue;if(Array.isArray(u)){r=r.replace(t,s({explode:o,name:i,style:l,value:u}));continue}if("object"==typeof u){r=r.replace(t,a({explode:o,name:i,style:l,value:u,valueOnly:!0}));continue}if("matrix"===l){r=r.replace(t,`;${n({name:i,value:u})}`);continue}const c=encodeURIComponent("label"===l?`.${u}`:u);r=r.replace(t,c)}return r})({path:t,url:u}));let c=r?i(r):"";return c.startsWith("?")&&(c=c.substring(1)),c&&(u+=`?${c}`),u},c=(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=d(e.headers,t.headers),s},d=(...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 f{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 p=i({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),h={"Content-Type":"application/json"},y=(e={})=>({...t,headers:h,parseAs:"auto",querySerializer:p,...e}),v=((e={})=>{let t=c(y(),e);const s=()=>({...t}),n={error:new f,request:new f,response:new f},a=async e=>{var s,a,o;const i={...t,...e,fetch:null!==(a=null!==(s=e.fetch)&&void 0!==s?s:t.fetch)&&void 0!==a?a:globalThis.fetch,headers:d(t.headers,e.headers)};i.security&&await(async({security:e,...t})=>{var s;for(const n of e){const e=await r(n,t.auth);if(!e)continue;const a=null!==(s=n.name)&&void 0!==s?s:"Authorization";switch(n.in){case"query":t.query||(t.query={}),t.query[a]=e;break;case"cookie":t.headers.append("Cookie",`${a}=${e}`);break;default:t.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),c={redirect:"follow",...i};let f=new Request(u,c);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=>(t=c(t,e),s()),trace:e=>a({...e,method:"TRACE"})}})(y({baseUrl:"https://clinicaltrials.gov/api/v2"}));e.client=v,e.enums=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({url:"/studies/enums",...e})},e.fetchStudy=e=>{var t;return(null!==(t=e.client)&&void 0!==t?t:v).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/studies/{nctId}",...e})},e.fieldValuesStats=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/stats/field/values",...e})},e.listFieldSizesStats=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/stats/field/sizes",...e})},e.listStudies=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({querySerializer:{array:{explode:!1,style:"form"}},url:"/studies",...e})},e.searchAreas=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({url:"/studies/search-areas",...e})},e.sizeStats=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({url:"/stats/size",...e})},e.studiesMetadata=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({url:"/studies/metadata",...e})},e.version=e=>{var t;return(null!==(t=null==e?void 0:e.client)&&void 0!==t?t:v).get({url:"/version",...e})}});
//# sourceMappingURL=index.umd.js.map