qwksearch-api-client 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api-client.js +819 -0
- package/dist/api-client.umd.cjs +1 -0
- package/dist/index.d.ts +763 -0
- package/package.json +30 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!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)["api-client"]={})}(this,function(e){"use strict";const t={bodySerializer:e=>JSON.stringify(e,(e,t)=>"bigint"==typeof t?t.toString():t)},r=({allowReserved:e,explode:t,name:r,style:a,value:n})=>{if(!t){const t=(e?n:n.map(e=>encodeURIComponent(e))).join((e=>{switch(e){case"form":default:return",";case"pipeDelimited":return"|";case"spaceDelimited":return"%20"}})(a));switch(a){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"&"}})(a),i=n.map(t=>"label"===a||"simple"===a?e?t:encodeURIComponent(t):s({allowReserved:e,name:r,value:t})).join(o);return"label"===a||"matrix"===a?o+i:i},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)}`},a=({allowReserved:e,explode:t,name:r,style:a,value:n,valueOnly:o})=>{if(n instanceof Date)return o?n.toISOString():`${r}=${n.toISOString()}`;if("deepObject"!==a&&!t){let t=[];Object.entries(n).forEach(([r,s])=>{t=[...t,r,e?s:encodeURIComponent(s)]});const s=t.join(",");switch(a){case"form":return`${r}=${s}`;case"label":return`.${s}`;case"matrix":return`;${r}=${s}`;default:return s}}const i=(e=>{switch(e){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}})(a),l=Object.entries(n).map(([t,n])=>s({allowReserved:e,name:"deepObject"===a?`${r}[${t}]`:t,value:n})).join(i);return"label"===a||"matrix"===a?i+l:l},n=/\{[^{}]+\}/g,o=({baseUrl:e,path:t,query:o,querySerializer:i,url:l})=>{let c=(e??"")+(l.startsWith("/")?l:`/${l}`);t&&(c=(({path:e,url:t})=>{let o=t;const i=t.match(n);if(i)for(const n of i){let t=!1,i=n.substring(1,n.length-1),l="simple";i.endsWith("*")&&(t=!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 c=e[i];if(null==c)continue;if(Array.isArray(c)){o=o.replace(n,r({explode:t,name:i,style:l,value:c}));continue}if("object"==typeof c){o=o.replace(n,a({explode:t,name:i,style:l,value:c,valueOnly:!0}));continue}if("matrix"===l){o=o.replace(n,`;${s({name:i,value:c})}`);continue}const d=encodeURIComponent("label"===l?`.${c}`:c);o=o.replace(n,d)}return o})({path:t,url:c}));let d=o?i(o):"";return d.startsWith("?")&&(d=d.substring(1)),d&&(c+=`?${d}`),c};function i(e){const t=void 0!==e.body;if(t&&e.bodySerializer){if("serializedBody"in e){return void 0!==e.serializedBody&&""!==e.serializedBody?e.serializedBody:null}return""!==e.body?e.body:null}if(t)return e.body}const l=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},c=({parameters:e={},...t}={})=>n=>{const o=[];if(n&&"object"==typeof n)for(const i in n){const l=n[i];if(null==l)continue;const c=e[i]||t;if(Array.isArray(l)){const e=r({allowReserved:c.allowReserved,explode:!0,name:i,style:"form",value:l,...c.array});e&&o.push(e)}else if("object"==typeof l){const e=a({allowReserved:c.allowReserved,explode:!0,name:i,style:"deepObject",value:l,...c.object});e&&o.push(e)}else{const e=s({allowReserved:c.allowReserved,name:i,value:l});e&&o.push(e)}}return o.join("&")},d=(e,t)=>!!t&&!!(e.headers.has(t)||e.query?.[t]||e.headers.get("Cookie")?.includes(`${t}=`)),u=e=>o({baseUrl:e.baseUrl,path:e.path,query:e.query,querySerializer:"function"==typeof e.querySerializer?e.querySerializer:c(e.querySerializer),url:e.url}),f=(e,t)=>{const r={...e,...t};return r.baseUrl?.endsWith("/")&&(r.baseUrl=r.baseUrl.substring(0,r.baseUrl.length-1)),r.headers=h(e.headers,t.headers),r},p=e=>{const t=[];return e.forEach((e,r)=>{t.push([r,e])}),t},h=(...e)=>{const t=new Headers;for(const r of e){if(!r)continue;const e=r instanceof Headers?p(r):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 y{fns=[];clear(){this.fns=[]}eject(e){const t=this.getInterceptorIndex(e);this.fns[t]&&(this.fns[t]=null)}exists(e){const t=this.getInterceptorIndex(e);return Boolean(this.fns[t])}getInterceptorIndex(e){return"number"==typeof e?this.fns[e]?e:-1:this.fns.indexOf(e)}update(e,t){const r=this.getInterceptorIndex(e);return!!this.fns[r]&&(this.fns[r]=t,e)}use(e){return this.fns.push(e),this.fns.length-1}}const b=c({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),m={"Content-Type":"application/json"},w=(e={})=>({...t,headers:m,parseAs:"auto",querySerializer:b,...e}),v=((e={})=>{let t=f(w(),e);const r=()=>({...t}),s={error:new y,request:new y,response:new y},a=async e=>{const r={...t,...e,fetch:e.fetch??t.fetch??globalThis.fetch,headers:h(t.headers,e.headers),serializedBody:void 0};r.security&&await(async({security:e,...t})=>{for(const r of e){if(d(t,r.name))continue;const e=await l(r,t.auth);if(!e)continue;const s=r.name??"Authorization";switch(r.in){case"query":t.query||(t.query={}),t.query[s]=e;break;case"cookie":t.headers.append("Cookie",`${s}=${e}`);break;default:t.headers.set(s,e)}}})({...r,security:r.security}),r.requestValidator&&await r.requestValidator(r),void 0!==r.body&&r.bodySerializer&&(r.serializedBody=r.bodySerializer(r.body)),void 0!==r.body&&""!==r.serializedBody||r.headers.delete("Content-Type");return{opts:r,url:u(r)}},n=async e=>{const{opts:t,url:r}=await a(e),n={redirect:"follow",...t,body:i(t)};let o=new Request(r,n);for(const a of s.request.fns)a&&(o=await a(o,t));const l=t.fetch;let c;try{c=await l(o)}catch(y){let e=y;for(const r of s.error.fns)r&&(e=await r(y,void 0,o,t));if(e=e||{},t.throwOnError)throw e;return"data"===t.responseStyle?void 0:{error:e,request:o,response:void 0}}for(const a of s.response.fns)a&&(c=await a(c,o,t));const d={request:o,response:c};if(c.ok){const e=("auto"===t.parseAs?(e=>{if(!e)return"stream";const t=e.split(";")[0]?.trim();return t?t.startsWith("application/json")||t.endsWith("+json")?"json":"multipart/form-data"===t?"formData":["application/","audio/","image/","video/"].some(e=>t.startsWith(e))?"blob":t.startsWith("text/")?"text":void 0:void 0})(c.headers.get("Content-Type")):t.parseAs)??"json";if(204===c.status||"0"===c.headers.get("Content-Length")){let r;switch(e){case"arrayBuffer":case"blob":case"text":r=await c[e]();break;case"formData":r=new FormData;break;case"stream":r=c.body;break;default:r={}}return"data"===t.responseStyle?r:{data:r,...d}}let r;switch(e){case"arrayBuffer":case"blob":case"formData":case"json":case"text":r=await c[e]();break;case"stream":return"data"===t.responseStyle?c.body:{data:c.body,...d}}return"json"===e&&(t.responseValidator&&await t.responseValidator(r),t.responseTransformer&&(r=await t.responseTransformer(r))),"data"===t.responseStyle?r:{data:r,...d}}const u=await c.text();let f;try{f=JSON.parse(u)}catch{}const p=f??u;let h=p;for(const a of s.error.fns)a&&(h=await a(p,c,o,t));if(h=h||{},t.throwOnError)throw h;return"data"===t.responseStyle?void 0:{error:h,...d}},o=e=>t=>n({...t,method:e}),c=e=>async t=>{const{opts:r,url:n}=await a(t);return(({onRequest:e,onSseError:t,onSseEvent:r,responseTransformer:s,responseValidator:a,sseDefaultRetryDelay:n,sseMaxRetryAttempts:o,sseMaxRetryDelay:i,sseSleepFn:l,url:c,...d})=>{let u;const f=l??(e=>new Promise(t=>setTimeout(t,e)));return{stream:async function*(){let l=n??3e3,p=0;const h=d.signal??(new AbortController).signal;for(;!h.aborted;){p++;const n=d.headers instanceof Headers?d.headers:new Headers(d.headers);void 0!==u&&n.set("Last-Event-ID",u);try{const t={redirect:"follow",...d,body:d.serializedBody,headers:n,signal:h};let o=new Request(c,t);e&&(o=await e(c,t));const i=d.fetch??globalThis.fetch,f=await i(o);if(!f.ok)throw new Error(`SSE failed: ${f.status} ${f.statusText}`);if(!f.body)throw new Error("No body in SSE response");const p=f.body.pipeThrough(new TextDecoderStream).getReader();let y="";const b=()=>{try{p.cancel()}catch{}};h.addEventListener("abort",b);try{for(;;){const{done:e,value:t}=await p.read();if(e)break;y+=t;const n=y.split("\n\n");y=n.pop()??"";for(const o of n){const e=o.split("\n"),t=[];let n,i;for(const r of e)if(r.startsWith("data:"))t.push(r.replace(/^data:\s*/,""));else if(r.startsWith("event:"))n=r.replace(/^event:\s*/,"");else if(r.startsWith("id:"))u=r.replace(/^id:\s*/,"");else if(r.startsWith("retry:")){const e=Number.parseInt(r.replace(/^retry:\s*/,""),10);Number.isNaN(e)||(l=e)}let c=!1;if(t.length){const e=t.join("\n");try{i=JSON.parse(e),c=!0}catch{i=e}}c&&(a&&await a(i),s&&(i=await s(i))),r?.({data:i,event:n,id:u,retry:l}),t.length&&(yield i)}}}finally{h.removeEventListener("abort",b),p.releaseLock()}break}catch(y){if(t?.(y),void 0!==o&&p>=o)break;const e=Math.min(l*2**(p-1),i??3e4);await f(e)}}}()}})({...r,body:r.body,headers:r.headers,method:e,onRequest:async(e,t)=>{let a=new Request(e,t);for(const n of s.request.fns)n&&(a=await n(a,r));return a},url:n})};return{buildUrl:u,connect:o("CONNECT"),delete:o("DELETE"),get:o("GET"),getConfig:r,head:o("HEAD"),interceptors:s,options:o("OPTIONS"),patch:o("PATCH"),post:o("POST"),put:o("PUT"),request:n,setConfig:e=>(t=f(t,e),r()),sse:{connect:c("CONNECT"),delete:c("DELETE"),get:c("GET"),head:c("HEAD"),options:c("OPTIONS"),patch:c("PATCH"),post:c("POST"),put:c("PUT"),trace:c("TRACE")},trace:o("TRACE")}})((g=w(),{...g,baseUrl:"https://qwksearch.com/api"}));var g;e.extractContent=e=>(e.client??v).get({url:"/extract",...e}),e.searchWeb=e=>(e.client??v).get({url:"/search",...e}),e.writeLanguage=e=>(e.client??v).post({url:"/agents",...e,headers:{"Content-Type":"application/json",...e.headers}}),Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
|