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