@elasticbottle/trpc-post-message
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
"use strict";var m=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames,x=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var O=(t,e,r)=>e in t?m(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,v=(t,e)=>{for(var r in e||(e={}))w.call(e,r)&&O(t,r,e[r]);if(x)for(var r of x(e))N.call(e,r)&&O(t,r,e[r]);return t};var S=(t,e)=>{for(var r in e)m(t,r,{get:e[r],enumerable:!0})},j=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of _(e))!w.call(t,a)&&a!==r&&m(t,a,{get:()=>e[a],enumerable:!(o=L(e,a))||o.enumerable});return t};var U=t=>j(m({},"__esModule",{value:!0}),t);var B={};S(B,{createPostMessageHandler:()=>F});module.exports=U(B);var h=require("@trpc/server"),A=require("@trpc/server/observable");var k=require("@trpc/server");function E(t){if(t instanceof Error&&t.name==="TRPCError")return t;let e,r;t instanceof Error&&(e=t,r=t.stack);let o=new k.TRPCError({message:"Internal server error",code:"INTERNAL_SERVER_ERROR",cause:e});return r&&(o.stack=r),o}var F=t=>{let{router:e,createContext:r,onError:o,addEventListener:a,postMessage:q}=t,{transformer:C}=e._def._config,l=new Map;a(async f=>{let{data:T}=f;if(!("trpc"in T))return;let{trpc:n}=T;if(!n||!("id"in n)||typeof n.id!="number"&&typeof n.id!="string"||"jsonrpc"in n&&n.jsonrpc!=="2.0"&&n.jsonrpc!==void 0||!("method"in n)||n.method!=="query"&&n.method!=="mutation"&&n.method!=="subscription"&&n.method!=="subscription.stop")return;let{id:d,jsonrpc:H,method:u}=n,p=y=>{q({message:{trpc:v({id:d,jsonrpc:H},y)},opts:{event:f}})},s,c,g;try{if(u==="subscription.stop"){let i=l.get(d);i&&(i.unsubscribe(),p({result:{type:"stopped"}}),l.delete(d));return}if({params:s}=n,!s)return;c=C.input.deserialize(s.input),g=await(r==null?void 0:r({req:f,res:void 0}));let y=e.createCaller(g),b=await s.path.split(".").reduce((i,R)=>i[R],y)(c);if(u!=="subscription"){let i=C.output.serialize(b);p({result:{type:"data",data:i}});return}if(!(0,A.isObservable)(b))throw new h.TRPCError({message:`Subscription ${s.path} did not return an observable`,code:"INTERNAL_SERVER_ERROR"});let M=b.subscribe({next:i=>{p({result:{type:"data",data:i}})},error:i=>{let R=E(i);o==null||o({error:R,type:u,path:s==null?void 0:s.path,input:c,ctx:g,req:f}),p({error:e.getErrorShape({error:R,type:u,path:s==null?void 0:s.path,input:c,ctx:g})})},complete:()=>{p({result:{type:"stopped"}})}});if(l.has(d))throw M.unsubscribe(),p({result:{type:"stopped"}}),new h.TRPCError({message:`Duplicate id ${d}`,code:"BAD_REQUEST"});l.set(d,M),p({result:{type:"started"}});return}catch(y){let P=E(y);o==null||o({error:P,type:u,path:s==null?void 0:s.path,input:c,ctx:g,req:f}),p({error:e.getErrorShape({error:P,type:u,path:s==null?void 0:s.path,input:c,ctx:g})})}})};0&&(module.exports={createPostMessageHandler}); | ||
"use strict";var m=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames,x=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var O=(t,e,r)=>e in t?m(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,v=(t,e)=>{for(var r in e||(e={}))w.call(e,r)&&O(t,r,e[r]);if(x)for(var r of x(e))S.call(e,r)&&O(t,r,e[r]);return t};var j=(t,e)=>{for(var r in e)m(t,r,{get:e[r],enumerable:!0})},U=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let d of N(e))!w.call(t,d)&&d!==r&&m(t,d,{get:()=>e[d],enumerable:!(o=_(e,d))||o.enumerable});return t};var F=t=>U(m({},"__esModule",{value:!0}),t);var I={};j(I,{createPostMessageHandler:()=>B});module.exports=F(I);var h=require("@trpc/server"),A=require("@trpc/server/observable");var k=require("@trpc/server");function E(t){if(t instanceof Error&&t.name==="TRPCError")return t;let e,r;t instanceof Error&&(e=t,r=t.stack);let o=new k.TRPCError({message:"Internal server error",code:"INTERNAL_SERVER_ERROR",cause:e});return r&&(o.stack=r),o}var B=t=>{let{router:e,createContext:r,onError:o,addEventListener:d,postMessage:q}=t,{transformer:C}=e._def._config,u=new Map;d(async l=>{var M;let{data:T}=l;if(!("trpc"in T))return;let{trpc:n}=T;if(!n||!("id"in n)||typeof n.id!="number"&&typeof n.id!="string"||"jsonrpc"in n&&n.jsonrpc!=="2.0"&&n.jsonrpc!==void 0||!("method"in n)||n.method!=="query"&&n.method!=="mutation"&&n.method!=="subscription"&&n.method!=="subscription.stop")return;let{id:p,jsonrpc:H,method:c}=n,a=y=>{q({message:{trpc:v({id:p,jsonrpc:H},y)},opts:{event:l}})},s,g,f;try{if(c==="subscription.stop"){let i=u.get(p);i&&(i.unsubscribe(),a({result:{type:"stopped"}}),u.delete(p));return}if({params:s}=n,!s)return;g=C.input.deserialize(s.input),f=await(r==null?void 0:r({req:l,res:void 0}));let y=e.createCaller(f),b=await s.path.split(".").reduce((i,R)=>i[R],y)(g);if(c!=="subscription"){let i=C.output.serialize(b);a({result:{type:"data",data:i}});return}if(!(0,A.isObservable)(b))throw new h.TRPCError({message:`Subscription ${s.path} did not return an observable`,code:"INTERNAL_SERVER_ERROR"});if(u.has(p))throw(M=u.get(p))==null||M.unsubscribe(),a({result:{type:"stopped"}}),u.delete(p),new h.TRPCError({message:`Duplicate id ${p}`,code:"BAD_REQUEST"});let L=b.subscribe({next:i=>{a({result:{type:"data",data:i}})},error:i=>{let R=E(i);o==null||o({error:R,type:c,path:s==null?void 0:s.path,input:g,ctx:f,req:l}),a({error:e.getErrorShape({error:R,type:c,path:s==null?void 0:s.path,input:g,ctx:f})})},complete:()=>{a({result:{type:"stopped"}})}});u.set(p,L),a({result:{type:"started"}});return}catch(y){let P=E(y);o==null||o({error:P,type:c,path:s==null?void 0:s.path,input:g,ctx:f,req:l}),a({error:e.getErrorShape({error:P,type:c,path:s==null?void 0:s.path,input:g,ctx:f})})}})};0&&(module.exports={createPostMessageHandler}); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";var p=Object.defineProperty,k=Object.defineProperties,C=Object.getOwnPropertyDescriptor,j=Object.getOwnPropertyDescriptors,x=Object.getOwnPropertyNames,P=Object.getOwnPropertySymbols;var M=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var L=(t,e,r)=>e in t?p(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,u=(t,e)=>{for(var r in e||(e={}))M.call(e,r)&&L(t,r,e[r]);if(P)for(var r of P(e))b.call(e,r)&&L(t,r,e[r]);return t},v=(t,e)=>k(t,j(e));var z=(t,e)=>{for(var r in e)p(t,r,{get:e[r],enumerable:!0})},q=(t,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of x(e))!M.call(t,n)&&n!==r&&p(t,n,{get:()=>e[n],enumerable:!(i=C(e,n))||i.enumerable});return t};var w=t=>q(p({},"__esModule",{value:!0}),t);var O={};z(O,{postMessageLink:()=>A});module.exports=w(O);var f=require("@trpc/client"),E=require("@trpc/server/observable");var A=t=>e=>{let{addEventListener:r,postMessage:i,removeEventListener:n}=t;return({op:m})=>(0,E.observable)(a=>{let g=[],{id:c,type:d,path:R}=m;try{let o=e.transformer.serialize(m.input),l=T=>{let{data:y}=T;if(!("trpc"in y))return;let{trpc:s}=y;if(s&&!(!("id"in s)||typeof s.id!="number"&&typeof s.id!="string")&&!("jsonrpc"in s&&s.jsonrpc!=="2.0"&&s.jsonrpc!==void 0)&&c===s.id){if("error"in s){let h=e.transformer.deserialize(s.error);a.error(f.TRPCClientError.from(v(u({},s),{error:h})));return}"result"in s&&(a.next({result:u(u({},s.result),(!s.result.type||s.result.type==="data")&&{type:"data",data:e.transformer.deserialize(s.result.data)})}),(d!=="subscription"||s.result.type==="stopped")&&a.complete())}};r(l),g.push(()=>n(l)),i({message:{trpc:{id:c,jsonrpc:void 0,method:d,params:{path:R,input:o}}}})}catch(o){a.error(new f.TRPCClientError(o instanceof Error?o.message:"Unknown error"))}return()=>{g.forEach(o=>o()),d==="subscription"&&i({message:{trpc:{id:c,jsonrpc:void 0,method:"subscription.stop"}}})}})};0&&(module.exports={postMessageLink}); | ||
"use strict";var p=Object.defineProperty,C=Object.defineProperties,j=Object.getOwnPropertyDescriptor,b=Object.getOwnPropertyDescriptors,x=Object.getOwnPropertyNames,P=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,w=Object.prototype.propertyIsEnumerable;var M=(t,e,r)=>e in t?p(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,u=(t,e)=>{for(var r in e||(e={}))v.call(e,r)&&M(t,r,e[r]);if(P)for(var r of P(e))w.call(e,r)&&M(t,r,e[r]);return t},E=(t,e)=>C(t,b(e));var z=(t,e)=>{for(var r in e)p(t,r,{get:e[r],enumerable:!0})},q=(t,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of x(e))!v.call(t,n)&&n!==r&&p(t,n,{get:()=>e[n],enumerable:!(i=j(e,n))||i.enumerable});return t};var A=t=>q(p({},"__esModule",{value:!0}),t);var N={};z(N,{postMessageLink:()=>O});module.exports=A(N);var f=require("@trpc/client"),R=require("@trpc/server/observable");var O=t=>e=>{let{addEventListener:r,postMessage:i,removeEventListener:n}=t;return({op:m})=>(0,R.observable)(a=>{let g=[],{id:c,type:d,path:T}=m;try{let o=e.transformer.serialize(m.input),y=h=>{let{data:L}=h;if(!("trpc"in L))return;let{trpc:s}=L;if(s&&!(!("id"in s)||typeof s.id!="number"&&typeof s.id!="string")&&!("jsonrpc"in s&&s.jsonrpc!=="2.0"&&s.jsonrpc!==void 0)&&c===s.id){if("error"in s){let k=e.transformer.deserialize(s.error);a.error(f.TRPCClientError.from(E(u({},s),{error:k})));return}"result"in s&&(a.next({result:u(u({},s.result),(!s.result.type||s.result.type==="data")&&{type:"data",data:e.transformer.deserialize(s.result.data)})}),(d!=="subscription"||s.result.type==="stopped")&&a.complete())}},l=r(y);g.push(l?()=>n(l):()=>n(y)),i({message:{trpc:{id:c,jsonrpc:void 0,method:d,params:{path:T,input:o}}}})}catch(o){a.error(new f.TRPCClientError(o instanceof Error?o.message:"Unknown error"))}return()=>{d==="subscription"&&i({message:{trpc:{id:c,jsonrpc:void 0,method:"subscription.stop"}}}),g.forEach(o=>o())}})};0&&(module.exports={postMessageLink}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@elasticbottle/trpc-post-message", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "tRPC adapter for post messages 📨", | ||
@@ -51,3 +51,3 @@ "author": "Winston Yeo <winstonyeo99@yahoo.com>", | ||
"scripts": { | ||
"test": "tsup && jest --verbose", | ||
"test": "pnpm lint && jest --verbose", | ||
"clean": "rimraf dist && rimraf adapter && rimraf link && rimraf types", | ||
@@ -54,0 +54,0 @@ "build": "tsup && mv dist/* . && rimraf dist", |
@@ -75,4 +75,4 @@ <div align="center"> | ||
postMessage: ({ message }) => window.postMessage(message, "your_targeted_url"), | ||
addEventListener: (listener) => | ||
window.addEventListener("message", (e) => { | ||
addEventListener: (listener) => { | ||
const customerListener = (e) => { | ||
if (e.origin !== 'your_whitelisted_domain') { | ||
@@ -82,3 +82,7 @@ return; | ||
listener(e); | ||
}), | ||
} | ||
window.addEventListener("message", customerListener) | ||
// if you don't return anything it is assumed that the default listener was used | ||
return customerListener; | ||
}, | ||
removeEventListener: (listener) => | ||
@@ -85,0 +89,0 @@ window.removeEventListener("message", listener), |
import { TRPCClientOutgoingMessage, TRPCResultMessage, TRPCErrorResponse } from '@trpc/server/rpc'; | ||
type PostMessageEventListener = (listener: (e: MessageEvent) => any) => void; | ||
type EventListener = (e: MessageEvent) => any; | ||
type PostMessageEventListener = (listener: EventListener) => EventListener | void; | ||
type TRPCPostMessageRequest = { | ||
@@ -13,2 +14,2 @@ trpc: TRPCClientOutgoingMessage; | ||
export { PostMessageEventListener, TRPCPostMessageErrorResponse, TRPCPostMessageRequest, TRPCPostMessageResponse, TRPCPostMessageSuccessResponse }; | ||
export { EventListener, PostMessageEventListener, TRPCPostMessageErrorResponse, TRPCPostMessageRequest, TRPCPostMessageResponse, TRPCPostMessageSuccessResponse }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var r=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var n=Object.prototype.hasOwnProperty;var g=(s,e,p,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of P(e))!n.call(s,t)&&t!==p&&r(s,t,{get:()=>e[t],enumerable:!(o=R(e,t))||o.enumerable});return s};var a=s=>g(r({},"__esModule",{value:!0}),s);var C={};module.exports=a(C); | ||
"use strict";var o=Object.defineProperty;var n=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var g=(s,e,p,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of R(e))!P.call(s,t)&&t!==p&&o(s,t,{get:()=>e[t],enumerable:!(r=n(e,t))||r.enumerable});return s};var a=s=>g(o({},"__esModule",{value:!0}),s);var C={};module.exports=a(C); | ||
//# sourceMappingURL=index.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52041
2.07%84
2.44%136
3.03%