UNPKG

@solid-mediakit/auth-plugin

Version:

Package for easily creating server rpc functions in solid-start with goodies baked in

25 lines (24 loc) 5.22 kB
import*as v from"@babel/core";import{babel as d}from"@solid-mediakit/shared";var g=(r,n)=>{let{callee:e}=r.node,s=n.isIdentifier(e,{name:"protected$"});return{callee:e,isProtected:s}},h=(r,n)=>{let e=r.findParent(t=>t.isProgram()).node.body,s=e.findLast(t=>t.type==="ImportDeclaration");s?e.splice(e.indexOf(s)+1,0,n):e.unshift(n)},x=(r,n,e,s)=>{d.importIfNotThere(r,n,"query","@solidjs/router"),d.importIfNotThere(r,n,"createAsync","@solidjs/router"),d.importIfNotThere(r,n,"getRequestEvent","solid-js/web"),d.importIfNotThere(r,n,"getSession","@solid-mediakit/auth"),d.importIfNotThere(r,n,"Show","solid-js"),d.importIfNotThere(r,n,e.authOpts.name,e.authOpts.dir),s.fallBack||d.importIfNotThere(r,n,"redirect","@solidjs/router")},E=(r,n)=>{let e=[r.jsxAttribute(r.jsxIdentifier("when"),r.jsxExpressionContainer(n.reverse?r.unaryExpression("!",r.optionalMemberExpression(r.callExpression(r.identifier("_$$session"),[]),r.identifier("user"),!1,!0)):r.optionalMemberExpression(r.callExpression(r.identifier("_$$session"),[]),r.identifier("user"),!1,!0)))];return n.fallBack&&e.push(r.jsxAttribute(r.jsxIdentifier("fallback"),r.jsxExpressionContainer(r.jsxElement(r.jsxOpeningElement(r.jsxIdentifier("_$$RenderFallBack"),[],!0),r.jsxClosingElement(r.jsxIdentifier("_$$RenderFallBack")),[],!0)))),r.jsxElement(r.jsxOpeningElement(r.jsxIdentifier("Show"),e),r.jsxClosingElement(r.jsxIdentifier("Show")),[r.jsxElement(r.jsxOpeningElement(r.jsxIdentifier("_$$RenderProtected"),[],!0),r.jsxClosingElement(r.jsxIdentifier("_$$RenderProtected")),[],!0)])},$=(r,n,e,s,t)=>{var a,c;let i=(a=s==null?void 0:s.userKey)!=null?a:"media-user",o;t.fallBack?o=r`const _$$getUser = query(async () => { 'use server' const event = getRequestEvent() return await getSession(event.request, authOptions) }, '${i}'); `():t.reverse?o=r`const _$$getUser = query(async () => { 'use server' const event = getRequestEvent() const session = await getSession(event.request, authOptions) if (session) { throw redirect('${t.redirectTo}') } return null }, '${i}'); `():o=r`const _$$getUser = query(async () => { 'use server' const event = getRequestEvent() const session = await getSession(event.request, authOptions) if (!session) { throw redirect('${t.redirectTo}') } return session }, '${i}'); `(),h(n,o);let u=(c=n.scope.getBinding("route"))==null?void 0:c.path;if(u){let l=u.node.init.properties.find(p=>p.key.name==="load");l&&(l.value=e.arrowFunctionExpression([],e.arrayExpression([e.callExpression(e.identifier("_$$getUser"),[]),e.isArrowFunctionExpression(l.value)?l.value.body:l.value])))}else n.insertAfter(e.exportNamedDeclaration(e.variableDeclaration("const",[e.variableDeclarator(e.identifier("route"),e.objectExpression([e.objectProperty(e.identifier("load"),e.arrowFunctionExpression([],e.callExpression(e.identifier("_$$getUser"),[])))]))])))},P=(r,n,e)=>{let s=r.node.arguments[0],t,i;return r.node.arguments.length>=2&&(n.isArrowFunctionExpression(r.node.arguments[1])?t=r.node.arguments[1]:i=r.node.arguments[1]),i||(i=e.redirectTo),{page:s,fallBack:t,redirectTo:i,reverse:n.isBooleanLiteral(r.node.arguments[2])?r.node.arguments[2].value:!1}};function A(r){return function({types:e,template:s}){return{visitor:{CallExpression(t){let i=g(t,e);if(i.isProtected){let o=P(t,e,r);if(i.isProtected){let u=t.node.arguments[0];t.traverse({Identifier(p){var m,y;p.node.name==="session$"&&((y=(m=p.scope)==null?void 0:m.path)==null?void 0:y.listKey)!=="params"&&(p.node.name="_$$session()")}}),x(t,e,r,o),$(s,t,e,r,o),u.params=[];let a=u.body,c=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("_$$session"),e.callExpression(e.identifier("createAsync"),[e.arrowFunctionExpression([],e.callExpression(e.identifier("_$$getUser"),[])),e.objectExpression([e.objectProperty(e.identifier("deferStream"),e.booleanLiteral(!0))])]))]),f=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("_$$RenderProtected"),e.arrowFunctionExpression([],e.blockStatement(a.body)))]);if(a.body=[f],o.fallBack){let p=e.variableDeclaration("const",[e.variableDeclarator(e.identifier("_$$RenderFallBack"),o.fallBack)]);a.body.unshift(p)}a.body.unshift(c);let l=e.arrowFunctionExpression([],a);l.body.body.push(e.returnStatement(E(e,o))),l.body.body=[...l.body.body],t.replaceWith(l)}}}}}}}async function b(r,n,e){var o,u,a,c,f;let s=["typescript","jsx"],t=A(e),i=await v.transformAsync(r,{presets:[["@babel/preset-typescript"],...(u=(o=e==null?void 0:e.babel)==null?void 0:o.presets)!=null?u:[]],parserOpts:{plugins:s},plugins:[[t],...(c=(a=e==null?void 0:e.babel)==null?void 0:a.plugins)!=null?c:[]],filename:n});return i?(e!=null&&e.log&&console.log(n,i.code),{code:(f=i.code)!=null?f:"",map:i.map}):null}import{babel as j,repushPlugin as I}from"@solid-mediakit/shared";function B(r){let n=j.getFilter(r==null?void 0:r.filter),e={enforce:"pre",name:"auth",async transform(s,t){if(!n(t))return s;if(s.includes("protected$(")||s.includes("session$"))return await b(s,t,r)},configResolved(s){I(s.plugins,e,["vite-server-references","solid","vinxi:routes"])}};return e}export{B as authVite};