@urun-sh/next 0.2.32 → 0.2.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +43 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var I=Object.create;var g=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var C=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty;var E=(e,n)=>{for(var t in n)g(e,t,{get:n[t],enumerable:!0})},U=(e,n,t,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of P(n))!A.call(e,r)&&r!==t&&g(e,r,{get:()=>n[r],enumerable:!(o=v(n,r))||o.enumerable});return e};var p=(e,n,t)=>(t=e!=null?I(C(e)):{},U(n||!e||!e.__esModule?g(t,"default",{value:e,enumerable:!0}):t,e)),j=e=>U(g({},"__esModule",{value:!0}),e);var J={};E(J,{MissingCredentialError:()=>u,URUN_API_KEY_ENV:()=>b,URUN_API_URL_ENV:()=>N,URUN_APP_ENV:()=>T,URUN_FUNCTION_ENV:()=>_,URUN_GATEWAY_URL_ENV:()=>y,createTokenRoute:()=>B,resolveCredential:()=>f,urunSessionConfig:()=>z,urunTokenPagesHandler:()=>V,urunTokenRoute:()=>Y});module.exports=j(J);var l=require("@urun-sh/core");var u=class extends Error{constructor(){super("[urun] no credential \u2014 set URUN_API_KEY in the server env (e.g. frontend/.env.local) or run `urun login`"),this.name="MissingCredentialError"}};function F(e){return e.replace(/\/+$/,"").replace(/\/v\d+$/,"")||void 0}function K(e,n,t){let o=e.URUN_CONFIG_HOME?.trim();if(o)return t(o,"credentials.json");let r=e.XDG_CONFIG_HOME?.trim();return r?t(r,"urun","credentials.json"):t(n(),".config","urun","credentials.json")}var k;function h(e){k!==e&&(k=e,console.info(e))}async function f(e=process.env){let n=e.URUN_API_KEY?.trim();if(n)return h("[urun] token route: using URUN_API_KEY from the server environment"),{apiKey:n,source:"env"};if(e.NODE_ENV!=="development")throw new u;let[{readFile:t},{homedir:o},{join:r}]=await Promise.all([import("fs/promises"),import("os"),import("path")]),s=K(e,o,r),i;try{i=JSON.parse(await t(s,"utf8"))}catch{throw new u}let a=typeof i.api_key=="string"?i.api_key.trim():"";if(!a)throw new u;let d=typeof i.api_url=="string"?i.api_url.trim():"";return h(`[urun] token route: dev fallback \u2014 using the urun CLI login credential (${s})`),{apiKey:a,source:"urun-login",gatewayUrl:d?F(d):void 0}}var b="URUN_API_KEY",y="URUN_GATEWAY_URL",N="URUN_API_URL",T="URUN_APP",_="URUN_FUNCTION";function z(e=process.env){return{baseUrl:e[y]?.trim()||l.DEFAULT_GATEWAY_URL,appId:e[T]?.trim()||void 0,functionName:e[_]?.trim()||void 0}}var q=60,c=class extends Error{};function $(e){return Array.isArray(e)&&e.every(n=>typeof n=="string")}function L(e,n){let t=n&&typeof n=="object"&&!Array.isArray(n)?n:{},o={expiresIn:e.expiresIn,allowedFunctions:e.allowedFunctions,allowedOrigins:e.allowedOrigins,maxSessionS:e.maxSessionS},r=t.expiresIn;if(r!==void 0){if(typeof r!="number"||!Number.isInteger(r)||r<1)throw new c("expiresIn must be a positive integer");let a=e.expiresIn??q;if(r>a)throw new c(`expiresIn may not exceed the configured ${a}s`);o.expiresIn=r}let s=(a,d,w)=>{if(a===void 0)return d;if(!$(a))throw new c(`${w} must be an array of strings`);if(d){let R=a.filter(S=>!d.includes(S));if(R.length>0)throw new c(`${w} may only narrow the configured allowlist (rejected: ${R.join(", ")})`)}return a};o.allowedFunctions=s(t.allowedFunctions,e.allowedFunctions,"allowedFunctions"),o.allowedOrigins=s(t.allowedOrigins,e.allowedOrigins,"allowedOrigins");let i=t.maxSessionS;if(i!==void 0){if(typeof i!="number"||!Number.isInteger(i)||i<1)throw new c("maxSessionS must be a positive integer");o.maxSessionS=e.maxSessionS===void 0?i:Math.min(e.maxSessionS,i)}return o}async function O(e,n){return e.authorize?await e.authorize(n)?void 0:{status:401,body:{message:"unauthorized"}}:(console.error("[urun] token route: options.authorize is required before minting"),{status:401,body:{message:"unauthorized"}})}async function m(e,n,t){let o=e.apiKey;if(!o)try{o=(await f()).apiKey}catch(s){let i=s instanceof u?s.message:`[urun] token route: no API key \u2014 set ${b} or pass options.apiKey`;return console.error(i),{status:500,body:{message:process.env.NODE_ENV==="development"?i:"token route not configured"}}}let r;try{r=L(e,n)}catch(s){if(s instanceof c)return{status:400,body:{message:s.message}};throw s}try{let s=await(0,l.createClientToken)(o,{baseUrl:e.baseUrl,expiresIn:r.expiresIn,allowedFunctions:r.allowedFunctions,allowedOrigins:r.allowedOrigins,maxSessionS:r.maxSessionS,metadata:e.metadata,subject:t,fetch:e.fetch});return{status:200,body:{token:s.token,expiresAt:s.expiresAt.toISOString(),orgId:s.orgId}}}catch(s){let i=s instanceof l.ClientTokenError?`${s.message} (status ${s.status??"n/a"})`:s instanceof Error?s.message:String(s);return console.error(`[urun] token route: mint failed: ${i}`),{status:502,body:{message:"token mint failed"}}}}function Y(e){return async n=>{let t;try{t=await n.json()}catch{t=void 0}let o=await O(e,n);if(o)return new Response(JSON.stringify(o.body),{status:o.status,headers:{"content-type":"application/json"}});let r=e.subject?await e.subject(n):void 0,{status:s,body:i}=await m(e,t,r);return new Response(JSON.stringify(i),{status:s,headers:{"content-type":"application/json"}})}}function G(e){return(process.env[y]?.trim()||e?.gatewayUrl||l.DEFAULT_GATEWAY_URL).replace(/\/+$/,"")}function M(e){return(process.env[N]?.trim()||e?.gatewayUrl||l.DEFAULT_GATEWAY_URL).replace(/\/+$/,"").replace(/\/v1$/,"")}var x=!1;function B(e={}){return async n=>{let t;try{t=await n.json()}catch{t=void 0}if(e.authorize){if(!await e.authorize(n))return new Response(JSON.stringify({message:"unauthorized"}),{status:401,headers:{"content-type":"application/json"}})}else x||(x=!0,console.warn("[urun] createTokenRoute: minting for ANY caller \u2014 pass { authorize } to restrict the endpoint to your authenticated users"));let o=e.subject?await e.subject(n):void 0,r;if(!e.apiKey)try{r=await f()}catch{r=void 0}let s=G(r),{status:i,body:a}=await m({...e,apiKey:e.apiKey??r?.apiKey,baseUrl:M(r)},t,o);if(i!==200)return new Response(JSON.stringify(a),{status:i,headers:{"content-type":"application/json"}});let d={...a,gatewayUrl:s};return new Response(JSON.stringify(d),{status:200,headers:{"content-type":"application/json"}})}}function V(e){return async(n,t)=>{if((n.method??"GET").toUpperCase()!=="POST"){t.status(405).json({message:"method not allowed"});return}let o=await O(e,n);if(o){t.status(o.status).json(o.body);return}let{status:r,body:s}=await m(e,n.body,void 0);t.status(r).json(s)}}0&&(module.exports={MissingCredentialError,URUN_API_KEY_ENV,URUN_API_URL_ENV,URUN_APP_ENV,URUN_FUNCTION_ENV,URUN_GATEWAY_URL_ENV,createTokenRoute,resolveCredential,urunSessionConfig,urunTokenPagesHandler,urunTokenRoute});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,45 @@
|
|
|
1
1
|
import { CreateClientTokenOptions } from '@urun-sh/core';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* LOCAL-DEV credential resolution for the token route (owner ruling
|
|
5
|
+
* 2026-07-15: raw `pnpm dev` works without the CLI).
|
|
6
|
+
*
|
|
7
|
+
* Precedence:
|
|
8
|
+
* 1. `URUN_API_KEY` in the server env — the production contract, always.
|
|
9
|
+
* 2. DEV ONLY (`NODE_ENV === 'development'`, i.e. `next dev`): the urun
|
|
10
|
+
* CLI's own stored login credential, `~/.config/urun/credentials.json` —
|
|
11
|
+
* the SAME file `urun login` writes (same path rules: `URUN_CONFIG_HOME`,
|
|
12
|
+
* then `XDG_CONFIG_HOME/urun`, then `~/.config/urun`; same JSON shape:
|
|
13
|
+
* `{ api_key, api_url, ... }`). Never a second format, never active in
|
|
14
|
+
* production builds or deployed frontends.
|
|
15
|
+
*
|
|
16
|
+
* The login file also carries the control-plane `api_url`, from which the
|
|
17
|
+
* org's session gateway derives exactly like the CLI's `resolve_gateway_url`
|
|
18
|
+
* (strip a trailing `/v<N>` version segment) — so a dev-cluster login dials
|
|
19
|
+
* the dev gateway instead of silently defaulting to the prod front door.
|
|
20
|
+
*/
|
|
21
|
+
/** Thrown when no credential exists anywhere — carries the actionable fix. */
|
|
22
|
+
declare class MissingCredentialError extends Error {
|
|
23
|
+
constructor();
|
|
24
|
+
}
|
|
25
|
+
interface ResolvedCredential {
|
|
26
|
+
apiKey: string;
|
|
27
|
+
/** Where the key came from — logged once, honestly. */
|
|
28
|
+
source: 'env' | 'urun-login';
|
|
29
|
+
/**
|
|
30
|
+
* Gateway derived FROM THE CREDENTIAL ITSELF (login-file `api_url` with the
|
|
31
|
+
* `/v<N>` control-plane path stripped). Undefined for env keys — the
|
|
32
|
+
* deployed contract stays `URUN_GATEWAY_URL`-or-platform-default.
|
|
33
|
+
*/
|
|
34
|
+
gatewayUrl?: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resolve the token route's org API key: env first, then (dev server only)
|
|
38
|
+
* the urun CLI login file. Exactly one honest log line states the source.
|
|
39
|
+
* Throws {@link MissingCredentialError} when neither exists.
|
|
40
|
+
*/
|
|
41
|
+
declare function resolveCredential(env?: Record<string, string | undefined>): Promise<ResolvedCredential>;
|
|
42
|
+
|
|
3
43
|
/**
|
|
4
44
|
* @urun-sh/next — the Next.js on-ramp for uRun scoped client tokens.
|
|
5
45
|
*
|
|
@@ -33,6 +73,8 @@ declare const URUN_API_KEY_ENV = "URUN_API_KEY";
|
|
|
33
73
|
* provider as props.
|
|
34
74
|
*/
|
|
35
75
|
declare const URUN_GATEWAY_URL_ENV = "URUN_GATEWAY_URL";
|
|
76
|
+
/** Server env var naming the API front door the token route MINTS against. */
|
|
77
|
+
declare const URUN_API_URL_ENV = "URUN_API_URL";
|
|
36
78
|
declare const URUN_APP_ENV = "URUN_APP";
|
|
37
79
|
declare const URUN_FUNCTION_ENV = "URUN_FUNCTION";
|
|
38
80
|
/** The frontend's resolved session-plane config (see {@link urunSessionConfig}). */
|
|
@@ -158,4 +200,4 @@ declare function createTokenRoute(options?: CreateTokenRouteOptions): (request:
|
|
|
158
200
|
*/
|
|
159
201
|
declare function urunTokenPagesHandler(options: UrunTokenRouteOptions): (req: UrunTokenPagesRequest, res: UrunTokenPagesResponse) => Promise<void>;
|
|
160
202
|
|
|
161
|
-
export { type CreateTokenRouteOptions, type CreateTokenRouteResponseBody, URUN_API_KEY_ENV, URUN_APP_ENV, URUN_FUNCTION_ENV, URUN_GATEWAY_URL_ENV, type UrunSessionConfig, type UrunTokenPagesRequest, type UrunTokenPagesResponse, type UrunTokenResponseBody, type UrunTokenRouteOptions, createTokenRoute, urunSessionConfig, urunTokenPagesHandler, urunTokenRoute };
|
|
203
|
+
export { type CreateTokenRouteOptions, type CreateTokenRouteResponseBody, MissingCredentialError, URUN_API_KEY_ENV, URUN_API_URL_ENV, URUN_APP_ENV, URUN_FUNCTION_ENV, URUN_GATEWAY_URL_ENV, type UrunSessionConfig, type UrunTokenPagesRequest, type UrunTokenPagesResponse, type UrunTokenResponseBody, type UrunTokenRouteOptions, createTokenRoute, resolveCredential, urunSessionConfig, urunTokenPagesHandler, urunTokenRoute };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import { CreateClientTokenOptions } from '@urun-sh/core';
|
|
2
2
|
|
|
3
|
+
declare class MissingCredentialError extends Error {
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
interface ResolvedCredential {
|
|
7
|
+
apiKey: string;
|
|
8
|
+
|
|
9
|
+
source: 'env' | 'urun-login';
|
|
10
|
+
|
|
11
|
+
gatewayUrl?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function resolveCredential(env?: Record<string, string | undefined>): Promise<ResolvedCredential>;
|
|
15
|
+
|
|
3
16
|
declare const URUN_API_KEY_ENV = "URUN_API_KEY";
|
|
4
17
|
|
|
5
18
|
declare const URUN_GATEWAY_URL_ENV = "URUN_GATEWAY_URL";
|
|
19
|
+
|
|
20
|
+
declare const URUN_API_URL_ENV = "URUN_API_URL";
|
|
6
21
|
declare const URUN_APP_ENV = "URUN_APP";
|
|
7
22
|
declare const URUN_FUNCTION_ENV = "URUN_FUNCTION";
|
|
8
23
|
|
|
@@ -58,4 +73,4 @@ declare function createTokenRoute(options?: CreateTokenRouteOptions): (request:
|
|
|
58
73
|
|
|
59
74
|
declare function urunTokenPagesHandler(options: UrunTokenRouteOptions): (req: UrunTokenPagesRequest, res: UrunTokenPagesResponse) => Promise<void>;
|
|
60
75
|
|
|
61
|
-
export { type CreateTokenRouteOptions, type CreateTokenRouteResponseBody, URUN_API_KEY_ENV, URUN_APP_ENV, URUN_FUNCTION_ENV, URUN_GATEWAY_URL_ENV, type UrunSessionConfig, type UrunTokenPagesRequest, type UrunTokenPagesResponse, type UrunTokenResponseBody, type UrunTokenRouteOptions, createTokenRoute, urunSessionConfig, urunTokenPagesHandler, urunTokenRoute };
|
|
76
|
+
export { type CreateTokenRouteOptions, type CreateTokenRouteResponseBody, MissingCredentialError, URUN_API_KEY_ENV, URUN_API_URL_ENV, URUN_APP_ENV, URUN_FUNCTION_ENV, URUN_GATEWAY_URL_ENV, type UrunSessionConfig, type UrunTokenPagesRequest, type UrunTokenPagesResponse, type UrunTokenResponseBody, type UrunTokenRouteOptions, createTokenRoute, resolveCredential, urunSessionConfig, urunTokenPagesHandler, urunTokenRoute };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createClientToken as
|
|
1
|
+
import{createClientToken as N,ClientTokenError as T,DEFAULT_GATEWAY_URL as f}from"@urun-sh/core";var d=class extends Error{constructor(){super("[urun] no credential \u2014 set URUN_API_KEY in the server env (e.g. frontend/.env.local) or run `urun login`"),this.name="MissingCredentialError"}};function x(e){return e.replace(/\/+$/,"").replace(/\/v\d+$/,"")||void 0}function b(e,n,s){let o=e.URUN_CONFIG_HOME?.trim();if(o)return s(o,"credentials.json");let r=e.XDG_CONFIG_HOME?.trim();return r?s(r,"urun","credentials.json"):s(n(),".config","urun","credentials.json")}var m;function w(e){m!==e&&(m=e,console.info(e))}async function l(e=process.env){let n=e.URUN_API_KEY?.trim();if(n)return w("[urun] token route: using URUN_API_KEY from the server environment"),{apiKey:n,source:"env"};if(e.NODE_ENV!=="development")throw new d;let[{readFile:s},{homedir:o},{join:r}]=await Promise.all([import("fs/promises"),import("os"),import("path")]),t=b(e,o,r),i;try{i=JSON.parse(await s(t,"utf8"))}catch{throw new d}let a=typeof i.api_key=="string"?i.api_key.trim():"";if(!a)throw new d;let u=typeof i.api_url=="string"?i.api_url.trim():"";return w(`[urun] token route: dev fallback \u2014 using the urun CLI login credential (${t})`),{apiKey:a,source:"urun-login",gatewayUrl:u?x(u):void 0}}var _="URUN_API_KEY",U="URUN_GATEWAY_URL",O="URUN_API_URL",S="URUN_APP",I="URUN_FUNCTION";function z(e=process.env){return{baseUrl:e[U]?.trim()||f,appId:e[S]?.trim()||void 0,functionName:e[I]?.trim()||void 0}}var v=60,c=class extends Error{};function P(e){return Array.isArray(e)&&e.every(n=>typeof n=="string")}function C(e,n){let s=n&&typeof n=="object"&&!Array.isArray(n)?n:{},o={expiresIn:e.expiresIn,allowedFunctions:e.allowedFunctions,allowedOrigins:e.allowedOrigins,maxSessionS:e.maxSessionS},r=s.expiresIn;if(r!==void 0){if(typeof r!="number"||!Number.isInteger(r)||r<1)throw new c("expiresIn must be a positive integer");let a=e.expiresIn??v;if(r>a)throw new c(`expiresIn may not exceed the configured ${a}s`);o.expiresIn=r}let t=(a,u,p)=>{if(a===void 0)return u;if(!P(a))throw new c(`${p} must be an array of strings`);if(u){let y=a.filter(h=>!u.includes(h));if(y.length>0)throw new c(`${p} may only narrow the configured allowlist (rejected: ${y.join(", ")})`)}return a};o.allowedFunctions=t(s.allowedFunctions,e.allowedFunctions,"allowedFunctions"),o.allowedOrigins=t(s.allowedOrigins,e.allowedOrigins,"allowedOrigins");let i=s.maxSessionS;if(i!==void 0){if(typeof i!="number"||!Number.isInteger(i)||i<1)throw new c("maxSessionS must be a positive integer");o.maxSessionS=e.maxSessionS===void 0?i:Math.min(e.maxSessionS,i)}return o}async function k(e,n){return e.authorize?await e.authorize(n)?void 0:{status:401,body:{message:"unauthorized"}}:(console.error("[urun] token route: options.authorize is required before minting"),{status:401,body:{message:"unauthorized"}})}async function g(e,n,s){let o=e.apiKey;if(!o)try{o=(await l()).apiKey}catch(t){let i=t instanceof d?t.message:`[urun] token route: no API key \u2014 set ${_} or pass options.apiKey`;return console.error(i),{status:500,body:{message:process.env.NODE_ENV==="development"?i:"token route not configured"}}}let r;try{r=C(e,n)}catch(t){if(t instanceof c)return{status:400,body:{message:t.message}};throw t}try{let t=await N(o,{baseUrl:e.baseUrl,expiresIn:r.expiresIn,allowedFunctions:r.allowedFunctions,allowedOrigins:r.allowedOrigins,maxSessionS:r.maxSessionS,metadata:e.metadata,subject:s,fetch:e.fetch});return{status:200,body:{token:t.token,expiresAt:t.expiresAt.toISOString(),orgId:t.orgId}}}catch(t){let i=t instanceof T?`${t.message} (status ${t.status??"n/a"})`:t instanceof Error?t.message:String(t);return console.error(`[urun] token route: mint failed: ${i}`),{status:502,body:{message:"token mint failed"}}}}function q(e){return async n=>{let s;try{s=await n.json()}catch{s=void 0}let o=await k(e,n);if(o)return new Response(JSON.stringify(o.body),{status:o.status,headers:{"content-type":"application/json"}});let r=e.subject?await e.subject(n):void 0,{status:t,body:i}=await g(e,s,r);return new Response(JSON.stringify(i),{status:t,headers:{"content-type":"application/json"}})}}function A(e){return(process.env[U]?.trim()||e?.gatewayUrl||f).replace(/\/+$/,"")}function E(e){return(process.env[O]?.trim()||e?.gatewayUrl||f).replace(/\/+$/,"").replace(/\/v1$/,"")}var R=!1;function $(e={}){return async n=>{let s;try{s=await n.json()}catch{s=void 0}if(e.authorize){if(!await e.authorize(n))return new Response(JSON.stringify({message:"unauthorized"}),{status:401,headers:{"content-type":"application/json"}})}else R||(R=!0,console.warn("[urun] createTokenRoute: minting for ANY caller \u2014 pass { authorize } to restrict the endpoint to your authenticated users"));let o=e.subject?await e.subject(n):void 0,r;if(!e.apiKey)try{r=await l()}catch{r=void 0}let t=A(r),{status:i,body:a}=await g({...e,apiKey:e.apiKey??r?.apiKey,baseUrl:E(r)},s,o);if(i!==200)return new Response(JSON.stringify(a),{status:i,headers:{"content-type":"application/json"}});let u={...a,gatewayUrl:t};return new Response(JSON.stringify(u),{status:200,headers:{"content-type":"application/json"}})}}function L(e){return async(n,s)=>{if((n.method??"GET").toUpperCase()!=="POST"){s.status(405).json({message:"method not allowed"});return}let o=await k(e,n);if(o){s.status(o.status).json(o.body);return}let{status:r,body:t}=await g(e,n.body,void 0);s.status(r).json(t)}}export{d as MissingCredentialError,_ as URUN_API_KEY_ENV,O as URUN_API_URL_ENV,S as URUN_APP_ENV,I as URUN_FUNCTION_ENV,U as URUN_GATEWAY_URL_ENV,$ as createTokenRoute,l as resolveCredential,z as urunSessionConfig,L as urunTokenPagesHandler,q as urunTokenRoute};
|