liferay-headless-rest-client
Version:
A collection of Headless Clients used in Liferay Portal
2 lines (1 loc) • 7.55 kB
JavaScript
var x=async(F,X)=>{let G=typeof X=="function"?await X(F):X;if(G)return F.scheme==="bearer"?`Bearer ${G}`:F.scheme==="basic"?`Basic ${btoa(G)}`:G},O=(F,X,G)=>{typeof G=="string"||G instanceof Blob?F.append(X,G):F.append(X,JSON.stringify(G))};var c={bodySerializer:(F)=>{let X=new FormData;return Object.entries(F).forEach(([G,B])=>{B!=null&&(Array.isArray(B)?B.forEach((k)=>O(X,G,k)):O(X,G,B))}),X}},_={bodySerializer:(F)=>JSON.stringify(F,(X,G)=>typeof G=="bigint"?G.toString():G)};var w=(F)=>{switch(F){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},A=(F)=>{switch(F){case"form":return",";case"pipeDelimited":return"|";case"spaceDelimited":return"%20";default:return","}},m=(F)=>{switch(F){case"label":return".";case"matrix":return";";case"simple":return",";default:return"&"}},R=({allowReserved:F,explode:X,name:G,style:B,value:k})=>{if(!X){let V=(F?k:k.map((H)=>encodeURIComponent(H))).join(A(B));switch(B){case"label":return`.${V}`;case"matrix":return`;${G}=${V}`;case"simple":return V;default:return`${G}=${V}`}}let E=w(B),Z=k.map((V)=>B==="label"||B==="simple"?F?V:encodeURIComponent(V):D({allowReserved:F,name:G,value:V})).join(E);return B==="label"||B==="matrix"?E+Z:Z},D=({allowReserved:F,name:X,value:G})=>{if(G==null)return"";if(typeof G=="object")throw new Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");return`${X}=${F?G:encodeURIComponent(G)}`},S=({allowReserved:F,explode:X,name:G,style:B,value:k})=>{if(k instanceof Date)return`${G}=${k.toISOString()}`;if(B!=="deepObject"&&!X){let V=[];Object.entries(k).forEach(([P,L])=>{V=[...V,P,F?L:encodeURIComponent(L)]});let H=V.join(",");switch(B){case"form":return`${G}=${H}`;case"label":return`.${H}`;case"matrix":return`;${G}=${H}`;default:return H}}let E=m(B),Z=Object.entries(k).map(([V,H])=>D({allowReserved:F,name:B==="deepObject"?`${G}[${V}]`:V,value:H})).join(E);return B==="label"||B==="matrix"?E+Z:Z},b=/\{[^{}]+\}/g,C=({path:F,url:X})=>{let G=X,B=X.match(b);if(B)for(let k of B){let E=!1,Z=k.substring(1,k.length-1),V="simple";Z.endsWith("*")&&(E=!0,Z=Z.substring(0,Z.length-1)),Z.startsWith(".")?(Z=Z.substring(1),V="label"):Z.startsWith(";")&&(Z=Z.substring(1),V="matrix");let H=F[Z];if(H==null)continue;if(Array.isArray(H)){G=G.replace(k,R({explode:E,name:Z,style:V,value:H}));continue}if(typeof H=="object"){G=G.replace(k,S({explode:E,name:Z,style:V,value:H}));continue}if(V==="matrix"){G=G.replace(k,`;${D({name:Z,value:H})}`);continue}let P=encodeURIComponent(V==="label"?`.${H}`:H);G=G.replace(k,P)}return G},v=({allowReserved:F,array:X,object:G}={})=>(B)=>{let k=[];if(B&&typeof B=="object")for(let E in B){let Z=B[E];if(Z!=null)if(Array.isArray(Z)){let V=R({allowReserved:F,explode:!0,name:E,style:"form",value:Z,...X});V&&k.push(V)}else if(typeof Z=="object"){let V=S({allowReserved:F,explode:!0,name:E,style:"deepObject",value:Z,...G});V&&k.push(V)}else{let V=D({allowReserved:F,name:E,value:Z});V&&k.push(V)}}return k.join("&")},d=(F)=>{if(!F)return"stream";let X=F.split(";")[0]?.trim();if(X){if(X.startsWith("application/json")||X.endsWith("+json"))return"json";if(X==="multipart/form-data")return"formData";if(["application/","audio/","image/","video/"].some((G)=>X.startsWith(G)))return"blob";if(X.startsWith("text/"))return"text"}},g=async({security:F,...X})=>{for(let G of F){let B=await x(G,X.auth);if(!B)continue;let k=G.name??"Authorization";switch(G.in){case"query":X.query||(X.query={}),X.query[k]=B;break;case"cookie":X.headers.append("Cookie",`${k}=${B}`);break;case"header":default:X.headers.set(k,B);break}return}},U=(F)=>f({baseUrl:F.baseUrl,path:F.path,query:F.query,querySerializer:typeof F.querySerializer=="function"?F.querySerializer:v(F.querySerializer),url:F.url}),f=({baseUrl:F,path:X,query:G,querySerializer:B,url:k})=>{let E=k.startsWith("/")?k:`/${k}`,Z=(F??"")+E;X&&(Z=C({path:X,url:Z}));let V=G?B(G):"";return V.startsWith("?")&&(V=V.substring(1)),V&&(Z+=`?${V}`),Z},Y=(F,X)=>{let G={...F,...X};return G.baseUrl?.endsWith("/")&&(G.baseUrl=G.baseUrl.substring(0,G.baseUrl.length-1)),G.headers=z(F.headers,X.headers),G},z=(...F)=>{let X=new Headers;for(let G of F){if(!G||typeof G!="object")continue;let B=G instanceof Headers?G.entries():Object.entries(G);for(let[k,E]of B)if(E===null)X.delete(k);else if(Array.isArray(E))for(let Z of E)X.append(k,Z);else E!==void 0&&X.set(k,typeof E=="object"?JSON.stringify(E):E)}return X},I=class{_fns;constructor(){this._fns=[]}clear(){this._fns=[]}getInterceptorIndex(F){return typeof F=="number"?this._fns[F]?F:-1:this._fns.indexOf(F)}exists(F){let X=this.getInterceptorIndex(F);return!!this._fns[X]}eject(F){let X=this.getInterceptorIndex(F);this._fns[X]&&(this._fns[X]=null)}update(F,X){let G=this.getInterceptorIndex(F);return this._fns[G]?(this._fns[G]=X,F):!1}use(F){return this._fns=[...this._fns,F],this._fns.length-1}},y=()=>({error:new I,request:new I,response:new I}),h=v({allowReserved:!1,array:{explode:!0,style:"form"},object:{explode:!0,style:"deepObject"}}),u={"Content-Type":"application/json"},N=(F={})=>({..._,headers:u,parseAs:"auto",querySerializer:h,...F}),T=(F={})=>{let X=Y(N(),F),G=()=>({...X}),B=(Z)=>(X=Y(X,Z),G()),k=y(),E=async(Z)=>{let V={...X,...Z,fetch:Z.fetch??X.fetch??globalThis.fetch,headers:z(X.headers,Z.headers)};V.security&&await g({...V,security:V.security}),V.body&&V.bodySerializer&&(V.body=V.bodySerializer(V.body)),(V.body===void 0||V.body==="")&&V.headers.delete("Content-Type");let H=U(V),P={redirect:"follow",...V},L=new Request(H,P);for(let K of k.request._fns)K&&(L=await K(L,V));let j=V.fetch,J=await j(L);for(let K of k.response._fns)K&&(J=await K(J,L,V));let $={request:L,response:J};if(J.ok){if(J.status===204||J.headers.get("Content-Length")==="0")return V.responseStyle==="data"?{}:{data:{},...$};let K=(V.parseAs==="auto"?d(J.headers.get("Content-Type")):V.parseAs)??"json";if(K==="stream")return V.responseStyle==="data"?J.body:{data:J.body,...$};let W=await J[K]();return K==="json"&&(V.responseValidator&&await V.responseValidator(W),V.responseTransformer&&(W=await V.responseTransformer(W))),V.responseStyle==="data"?W:{data:W,...$}}let q=await J.text();try{q=JSON.parse(q)}catch{}let Q=q;for(let K of k.error._fns)K&&(Q=await K(q,J,L,V));if(Q=Q||{},V.throwOnError)throw Q;return V.responseStyle==="data"?void 0:{error:Q,...$}};return{buildUrl:U,connect:(Z)=>E({...Z,method:"CONNECT"}),delete:(Z)=>E({...Z,method:"DELETE"}),get:(Z)=>E({...Z,method:"GET"}),getConfig:G,head:(Z)=>E({...Z,method:"HEAD"}),interceptors:k,options:(Z)=>E({...Z,method:"OPTIONS"}),patch:(Z)=>E({...Z,method:"PATCH"}),post:(Z)=>E({...Z,method:"POST"}),put:(Z)=>E({...Z,method:"PUT"}),request:E,setConfig:B,trace:(Z)=>E({...Z,method:"TRACE"})}};var M=T(N({baseUrl:"http://localhost:8080"}));var n=(F)=>{return(F?.client??M).get({url:"/o/dispatch-rest/v1.0/dispatch-triggers",...F})},s=(F)=>{return(F?.client??M).post({url:"/o/dispatch-rest/v1.0/dispatch-triggers",...F,headers:{"Content-Type":"application/json",...F?.headers}})},i=(F)=>{return(F?.client??M).post({url:"/o/dispatch-rest/v1.0/dispatch-triggers/batch",...F,headers:{"Content-Type":"application/json",...F?.headers}})},t=(F)=>{return(F.client??M).post({url:"/o/dispatch-rest/v1.0/dispatch-triggers/{dispatchTriggerId}/run",...F})},e=(F)=>{return(F?.client??M).post({url:"/o/dispatch-rest/v1.0/dispatch-triggers/export-batch",...F})};export{e as postDispatchTriggersPageExportBatch,t as postDispatchTriggerRun,i as postDispatchTriggerBatch,s as postDispatchTrigger,n as getDispatchTriggersPage};