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