@unblind/nextjs 0.1.0-alpha.21 → 0.1.0-alpha.22
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/server/index.d.mts +14 -48
- package/dist/server/index.d.ts +14 -48
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +1 -1
- package/dist/server/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/server/index.d.mts
CHANGED
|
@@ -1,78 +1,44 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NextRequest, NextProxy } from 'next/server';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Custom NextRequest to avoid compatibility issues with older NextJS versions.
|
|
5
|
-
*/
|
|
6
|
-
interface NextRequest {
|
|
7
|
-
method: string;
|
|
8
|
-
headers: Headers;
|
|
9
|
-
text(): Promise<string>;
|
|
10
|
-
nextUrl: {
|
|
11
|
-
href: string;
|
|
12
|
-
pathname: string;
|
|
13
|
-
search: string;
|
|
14
|
-
searchParams: URLSearchParams;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
3
|
/**
|
|
18
4
|
* Unblind API handler for Next.js applications.
|
|
19
5
|
*
|
|
20
6
|
* This handler intercepts requests to `/api/unblind` and proxies them to the Unblind API,
|
|
21
|
-
* automatically adding authentication and tenant context.
|
|
7
|
+
* automatically adding authentication and tenant context. Returns `undefined` for requests
|
|
22
8
|
* that don't match the path prefix, allowing Next.js to continue normal routing.
|
|
23
9
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @param request - The incoming Next.js request object
|
|
27
|
-
* @param tenantId - The tenant identifier to inject into API paths (e.g., user ID, organization ID)
|
|
28
|
-
* @param apiKey - Optional Unblind API key. Falls back to UNBLIND_API_KEY environment variable
|
|
10
|
+
* @param extractTenantId - A function that receives the incoming request and returns a tenant ID string (or undefined)
|
|
29
11
|
*
|
|
30
|
-
* @returns
|
|
12
|
+
* @returns An async request handler that proxies matching requests to the Unblind API
|
|
31
13
|
*
|
|
32
14
|
* @example
|
|
33
|
-
* // Usage as Proxy (
|
|
34
|
-
* import {
|
|
35
|
-
* import { NextRequest } from "next/server";
|
|
36
|
-
*
|
|
37
|
-
* export function proxy(request: NextRequest) {
|
|
38
|
-
* const path = new URL(request.url).pathname;
|
|
15
|
+
* // Usage as Proxy (Next.js 16+, proxy.ts)
|
|
16
|
+
* import { handler } from "@unblind/nextjs/server";
|
|
39
17
|
*
|
|
40
|
-
*
|
|
41
|
-
* // Extract tenant ID from your auth system (e.g., session, JWT, cookies)
|
|
42
|
-
* const tenantId = request.cookies.get("tenantId")?.value;
|
|
43
|
-
* return unblindProxy(request, tenantId);
|
|
44
|
-
* }
|
|
45
|
-
* }
|
|
18
|
+
* export const proxy = handler((req) => req.cookies.get("userId")?.value);
|
|
46
19
|
*
|
|
47
20
|
* export const config = {
|
|
48
21
|
* matcher: "/api/unblind/:path*",
|
|
49
22
|
* };
|
|
50
23
|
*
|
|
51
24
|
* @example
|
|
52
|
-
* // Usage in Middleware
|
|
53
|
-
* import {
|
|
25
|
+
* // Usage in Middleware (Next.js <16, middleware.ts)
|
|
26
|
+
* import { unblindProxy } from "@unblind/nextjs/server";
|
|
54
27
|
* import { NextRequest, NextResponse } from "next/server";
|
|
55
28
|
*
|
|
56
29
|
* export async function middleware(request: NextRequest) {
|
|
57
|
-
*
|
|
58
|
-
* // Let the handler process Unblind API requests
|
|
59
30
|
* if (request.nextUrl.pathname.startsWith("/api/unblind")) {
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* const response = await unblindMiddleware(request, tenantId);
|
|
64
|
-
* if (response) {
|
|
65
|
-
* return response;
|
|
66
|
-
* }
|
|
31
|
+
* const response = await unblindProxy((req) => req.cookies.get("userId")?.value);
|
|
32
|
+
* if (response) return response;
|
|
67
33
|
* }
|
|
68
34
|
*
|
|
69
35
|
* return NextResponse.next();
|
|
70
36
|
* }
|
|
71
37
|
*
|
|
72
38
|
* export const config = {
|
|
73
|
-
* matcher: [
|
|
39
|
+
* matcher: ["/api/unblind/:path*"],
|
|
74
40
|
* };
|
|
75
41
|
*/
|
|
76
|
-
declare function
|
|
42
|
+
declare function proxy(extractTenantId: (request: NextRequest) => string | undefined | Promise<string | undefined>): NextProxy;
|
|
77
43
|
|
|
78
|
-
export {
|
|
44
|
+
export { proxy as unblindProxy };
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,78 +1,44 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NextRequest, NextProxy } from 'next/server';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Custom NextRequest to avoid compatibility issues with older NextJS versions.
|
|
5
|
-
*/
|
|
6
|
-
interface NextRequest {
|
|
7
|
-
method: string;
|
|
8
|
-
headers: Headers;
|
|
9
|
-
text(): Promise<string>;
|
|
10
|
-
nextUrl: {
|
|
11
|
-
href: string;
|
|
12
|
-
pathname: string;
|
|
13
|
-
search: string;
|
|
14
|
-
searchParams: URLSearchParams;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
3
|
/**
|
|
18
4
|
* Unblind API handler for Next.js applications.
|
|
19
5
|
*
|
|
20
6
|
* This handler intercepts requests to `/api/unblind` and proxies them to the Unblind API,
|
|
21
|
-
* automatically adding authentication and tenant context.
|
|
7
|
+
* automatically adding authentication and tenant context. Returns `undefined` for requests
|
|
22
8
|
* that don't match the path prefix, allowing Next.js to continue normal routing.
|
|
23
9
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @param request - The incoming Next.js request object
|
|
27
|
-
* @param tenantId - The tenant identifier to inject into API paths (e.g., user ID, organization ID)
|
|
28
|
-
* @param apiKey - Optional Unblind API key. Falls back to UNBLIND_API_KEY environment variable
|
|
10
|
+
* @param extractTenantId - A function that receives the incoming request and returns a tenant ID string (or undefined)
|
|
29
11
|
*
|
|
30
|
-
* @returns
|
|
12
|
+
* @returns An async request handler that proxies matching requests to the Unblind API
|
|
31
13
|
*
|
|
32
14
|
* @example
|
|
33
|
-
* // Usage as Proxy (
|
|
34
|
-
* import {
|
|
35
|
-
* import { NextRequest } from "next/server";
|
|
36
|
-
*
|
|
37
|
-
* export function proxy(request: NextRequest) {
|
|
38
|
-
* const path = new URL(request.url).pathname;
|
|
15
|
+
* // Usage as Proxy (Next.js 16+, proxy.ts)
|
|
16
|
+
* import { handler } from "@unblind/nextjs/server";
|
|
39
17
|
*
|
|
40
|
-
*
|
|
41
|
-
* // Extract tenant ID from your auth system (e.g., session, JWT, cookies)
|
|
42
|
-
* const tenantId = request.cookies.get("tenantId")?.value;
|
|
43
|
-
* return unblindProxy(request, tenantId);
|
|
44
|
-
* }
|
|
45
|
-
* }
|
|
18
|
+
* export const proxy = handler((req) => req.cookies.get("userId")?.value);
|
|
46
19
|
*
|
|
47
20
|
* export const config = {
|
|
48
21
|
* matcher: "/api/unblind/:path*",
|
|
49
22
|
* };
|
|
50
23
|
*
|
|
51
24
|
* @example
|
|
52
|
-
* // Usage in Middleware
|
|
53
|
-
* import {
|
|
25
|
+
* // Usage in Middleware (Next.js <16, middleware.ts)
|
|
26
|
+
* import { unblindProxy } from "@unblind/nextjs/server";
|
|
54
27
|
* import { NextRequest, NextResponse } from "next/server";
|
|
55
28
|
*
|
|
56
29
|
* export async function middleware(request: NextRequest) {
|
|
57
|
-
*
|
|
58
|
-
* // Let the handler process Unblind API requests
|
|
59
30
|
* if (request.nextUrl.pathname.startsWith("/api/unblind")) {
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* const response = await unblindMiddleware(request, tenantId);
|
|
64
|
-
* if (response) {
|
|
65
|
-
* return response;
|
|
66
|
-
* }
|
|
31
|
+
* const response = await unblindProxy((req) => req.cookies.get("userId")?.value);
|
|
32
|
+
* if (response) return response;
|
|
67
33
|
* }
|
|
68
34
|
*
|
|
69
35
|
* return NextResponse.next();
|
|
70
36
|
* }
|
|
71
37
|
*
|
|
72
38
|
* export const config = {
|
|
73
|
-
* matcher: [
|
|
39
|
+
* matcher: ["/api/unblind/:path*"],
|
|
74
40
|
* };
|
|
75
41
|
*/
|
|
76
|
-
declare function
|
|
42
|
+
declare function proxy(extractTenantId: (request: NextRequest) => string | undefined | Promise<string | undefined>): NextProxy;
|
|
77
43
|
|
|
78
|
-
export {
|
|
44
|
+
export { proxy as unblindProxy };
|
package/dist/server/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var c=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var N=(e,t)=>{for(var o in t)c(e,o,{get:t[o],enumerable:!0})},U=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of E(t))!I.call(e,n)&&n!==o&&c(e,n,{get:()=>t[n],enumerable:!(r=m(t,n))||r.enumerable});return e};var f=e=>U(c({},"__esModule",{value:!0}),e);var y={};N(y,{unblindProxy:()=>x});module.exports=f(y);var a=require("next/server"),h=process.env.UNBLIND_API_KEY,g="https://api.unblind.dev/v1",l="/api/unblind";function x(e){return async t=>{if(!t.nextUrl.pathname.startsWith(l))return a.NextResponse.next();if(!h)return console.error("UNBLIND_API_KEY is missing"),a.NextResponse.json({error:"Internal Server Error"},{status:500});let o=await e(t);try{if(!o&&t.nextUrl.pathname.includes("/tenants/"))return console.error("[Unblind] Unauthorized request (missing tenantId):",{path:t.nextUrl.pathname}),a.NextResponse.json({error:"Internal Server Error"},{status:500});let r=t.nextUrl.pathname.replace(new RegExp(`^${l}`),"").replace(new RegExp("^/tenants/"),"/tenants/"+o+"/"),n=new URL(`${g}${r}`);t.nextUrl.searchParams.forEach((s,u)=>{n.searchParams.append(u,s)});let p={Authorization:`Bearer ${h}`};if(["POST","PUT","PATCH","DELETE"].includes(t.method)){let s=t.headers.get("content-type");s&&(p["Content-Type"]=s)}let d={method:t.method,headers:p};if(["POST","PUT","PATCH","DELETE"].includes(t.method)){let s=await t.text();s&&(d.body=s)}let i=await fetch(n.toString(),d),P=await i.text();return new a.NextResponse(P,{status:i.status,headers:{"Content-Type":i.headers.get("content-type")||"application/json"}})}catch(r){let n=r instanceof Error?r.message:String(r);return console.error("[Unblind]",n),a.NextResponse.json({error:"Internal Server Error"},{status:500})}}}0&&(module.exports={unblindProxy});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/server/index.ts","../../src/server/
|
|
1
|
+
{"version":3,"sources":["../../src/server/index.ts","../../src/server/proxy/index.ts"],"sourcesContent":["export { proxy as unblindProxy } from \"./proxy\";\n","import { NextProxy, NextRequest, NextResponse } from \"next/server\";\n\nconst UNBLIND_API_KEY = process.env.UNBLIND_API_KEY;\nconst UNBLIND_API_ENDPOINT = \"https://api.unblind.dev/v1\";\nconst UNBLIND_PATH_PREFIX = \"/api/unblind\";\n\n/**\n * Unblind API handler for Next.js applications.\n *\n * This handler intercepts requests to `/api/unblind` and proxies them to the Unblind API,\n * automatically adding authentication and tenant context. Returns `undefined` for requests\n * that don't match the path prefix, allowing Next.js to continue normal routing.\n *\n * @param extractTenantId - A function that receives the incoming request and returns a tenant ID string (or undefined)\n *\n * @returns An async request handler that proxies matching requests to the Unblind API\n *\n * @example\n * // Usage as Proxy (Next.js 16+, proxy.ts)\n * import { handler } from \"@unblind/nextjs/server\";\n *\n * export const proxy = handler((req) => req.cookies.get(\"userId\")?.value);\n *\n * export const config = {\n * matcher: \"/api/unblind/:path*\",\n * };\n *\n * @example\n * // Usage in Middleware (Next.js <16, middleware.ts)\n * import { unblindProxy } from \"@unblind/nextjs/server\";\n * import { NextRequest, NextResponse } from \"next/server\";\n *\n * export async function middleware(request: NextRequest) {\n * if (request.nextUrl.pathname.startsWith(\"/api/unblind\")) {\n * const response = await unblindProxy((req) => req.cookies.get(\"userId\")?.value);\n * if (response) return response;\n * }\n *\n * return NextResponse.next();\n * }\n *\n * export const config = {\n * matcher: [\"/api/unblind/:path*\"],\n * };\n */\nexport function proxy(\n extractTenantId: (\n request: NextRequest,\n ) => string | undefined | Promise<string | undefined>,\n): NextProxy {\n return async (request: NextRequest) => {\n if (!request.nextUrl.pathname.startsWith(UNBLIND_PATH_PREFIX)) {\n return NextResponse.next();\n }\n\n if (!UNBLIND_API_KEY) {\n console.error(\"UNBLIND_API_KEY is missing\");\n return NextResponse.json(\n { error: \"Internal Server Error\" },\n { status: 500 },\n );\n }\n\n const tenantId = await extractTenantId(request);\n\n try {\n // Tenant requests use a different path\n if (!tenantId && request.nextUrl.pathname.includes(\"/tenants/\")) {\n console.error(\"[Unblind] Unauthorized request (missing tenantId):\", {\n path: request.nextUrl.pathname,\n });\n\n return NextResponse.json(\n { error: \"Internal Server Error\" },\n { status: 500 },\n );\n }\n\n // Extract the API path (remove the prefix)\n const apiPath = request.nextUrl.pathname\n .replace(new RegExp(`^${UNBLIND_PATH_PREFIX}`), \"\")\n .replace(new RegExp(\"^\\/tenants\\/\"), \"/tenants/\" + tenantId + \"/\");\n const url = new URL(`${UNBLIND_API_ENDPOINT}${apiPath}`);\n\n request.nextUrl.searchParams.forEach((value: string, key: string) => {\n url.searchParams.append(key, value);\n });\n\n const headers: HeadersInit = {\n Authorization: `Bearer ${UNBLIND_API_KEY}`,\n };\n\n // Forward Content-Type for requests with body\n if ([\"POST\", \"PUT\", \"PATCH\", \"DELETE\"].includes(request.method)) {\n const contentType = request.headers.get(\"content-type\");\n if (contentType) {\n headers[\"Content-Type\"] = contentType;\n }\n }\n\n const options: RequestInit = {\n method: request.method,\n headers,\n };\n\n if ([\"POST\", \"PUT\", \"PATCH\", \"DELETE\"].includes(request.method)) {\n const body = await request.text();\n if (body) {\n options.body = body;\n }\n }\n\n const response = await fetch(url.toString(), options);\n const data = await response.text();\n\n return new NextResponse(data, {\n status: response.status,\n headers: {\n \"Content-Type\":\n response.headers.get(\"content-type\") || \"application/json\",\n },\n });\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n console.error(\"[Unblind]\", message);\n return NextResponse.json(\n { error: \"Internal Server Error\" },\n { status: 500 },\n );\n }\n };\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAqD,uBAE/CC,EAAkB,QAAQ,IAAI,gBAC9BC,EAAuB,6BACvBC,EAAsB,eAyCrB,SAASC,EACdC,EAGW,CACX,MAAO,OAAOC,GAAyB,CACrC,GAAI,CAACA,EAAQ,QAAQ,SAAS,WAAWH,CAAmB,EAC1D,OAAO,eAAa,KAAK,EAG3B,GAAI,CAACF,EACH,eAAQ,MAAM,4BAA4B,EACnC,eAAa,KAClB,CAAE,MAAO,uBAAwB,EACjC,CAAE,OAAQ,GAAI,CAChB,EAGF,IAAMM,EAAW,MAAMF,EAAgBC,CAAO,EAE9C,GAAI,CAEF,GAAI,CAACC,GAAYD,EAAQ,QAAQ,SAAS,SAAS,WAAW,EAC5D,eAAQ,MAAM,qDAAsD,CAClE,KAAMA,EAAQ,QAAQ,QACxB,CAAC,EAEM,eAAa,KAClB,CAAE,MAAO,uBAAwB,EACjC,CAAE,OAAQ,GAAI,CAChB,EAIF,IAAME,EAAUF,EAAQ,QAAQ,SAC7B,QAAQ,IAAI,OAAO,IAAIH,CAAmB,EAAE,EAAG,EAAE,EACjD,QAAQ,IAAI,OAAO,YAAc,EAAG,YAAcI,EAAW,GAAG,EAC7DE,EAAM,IAAI,IAAI,GAAGP,CAAoB,GAAGM,CAAO,EAAE,EAEvDF,EAAQ,QAAQ,aAAa,QAAQ,CAACI,EAAeC,IAAgB,CACnEF,EAAI,aAAa,OAAOE,EAAKD,CAAK,CACpC,CAAC,EAED,IAAME,EAAuB,CAC3B,cAAe,UAAUX,CAAe,EAC1C,EAGA,GAAI,CAAC,OAAQ,MAAO,QAAS,QAAQ,EAAE,SAASK,EAAQ,MAAM,EAAG,CAC/D,IAAMO,EAAcP,EAAQ,QAAQ,IAAI,cAAc,EAClDO,IACFD,EAAQ,cAAc,EAAIC,EAE9B,CAEA,IAAMC,EAAuB,CAC3B,OAAQR,EAAQ,OAChB,QAAAM,CACF,EAEA,GAAI,CAAC,OAAQ,MAAO,QAAS,QAAQ,EAAE,SAASN,EAAQ,MAAM,EAAG,CAC/D,IAAMS,EAAO,MAAMT,EAAQ,KAAK,EAC5BS,IACFD,EAAQ,KAAOC,EAEnB,CAEA,IAAMC,EAAW,MAAM,MAAMP,EAAI,SAAS,EAAGK,CAAO,EAC9CG,EAAO,MAAMD,EAAS,KAAK,EAEjC,OAAO,IAAI,eAAaC,EAAM,CAC5B,OAAQD,EAAS,OACjB,QAAS,CACP,eACEA,EAAS,QAAQ,IAAI,cAAc,GAAK,kBAC5C,CACF,CAAC,CACH,OAASE,EAAO,CACd,IAAMC,EAAUD,aAAiB,MAAQA,EAAM,QAAU,OAAOA,CAAK,EACrE,eAAQ,MAAM,YAAaC,CAAO,EAC3B,eAAa,KAClB,CAAE,MAAO,uBAAwB,EACjC,CAAE,OAAQ,GAAI,CAChB,CACF,CACF,CACF","names":["server_exports","__export","proxy","__toCommonJS","import_server","UNBLIND_API_KEY","UNBLIND_API_ENDPOINT","UNBLIND_PATH_PREFIX","proxy","extractTenantId","request","tenantId","apiPath","url","value","key","headers","contentType","options","body","response","data","error","message"]}
|
package/dist/server/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{NextResponse as
|
|
1
|
+
import{NextResponse as r}from"next/server";var p=process.env.UNBLIND_API_KEY,P="https://api.unblind.dev/v1",d="/api/unblind";function u(h){return async t=>{if(!t.nextUrl.pathname.startsWith(d))return r.next();if(!p)return console.error("UNBLIND_API_KEY is missing"),r.json({error:"Internal Server Error"},{status:500});let a=await h(t);try{if(!a&&t.nextUrl.pathname.includes("/tenants/"))return console.error("[Unblind] Unauthorized request (missing tenantId):",{path:t.nextUrl.pathname}),r.json({error:"Internal Server Error"},{status:500});let e=t.nextUrl.pathname.replace(new RegExp(`^${d}`),"").replace(new RegExp("^/tenants/"),"/tenants/"+a+"/"),o=new URL(`${P}${e}`);t.nextUrl.searchParams.forEach((n,x)=>{o.searchParams.append(x,n)});let i={Authorization:`Bearer ${p}`};if(["POST","PUT","PATCH","DELETE"].includes(t.method)){let n=t.headers.get("content-type");n&&(i["Content-Type"]=n)}let c={method:t.method,headers:i};if(["POST","PUT","PATCH","DELETE"].includes(t.method)){let n=await t.text();n&&(c.body=n)}let s=await fetch(o.toString(),c),l=await s.text();return new r(l,{status:s.status,headers:{"Content-Type":s.headers.get("content-type")||"application/json"}})}catch(e){let o=e instanceof Error?e.message:String(e);return console.error("[Unblind]",o),r.json({error:"Internal Server Error"},{status:500})}}}export{u as unblindProxy};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/server/
|
|
1
|
+
{"version":3,"sources":["../../src/server/proxy/index.ts"],"sourcesContent":["import { NextProxy, NextRequest, NextResponse } from \"next/server\";\n\nconst UNBLIND_API_KEY = process.env.UNBLIND_API_KEY;\nconst UNBLIND_API_ENDPOINT = \"https://api.unblind.dev/v1\";\nconst UNBLIND_PATH_PREFIX = \"/api/unblind\";\n\n/**\n * Unblind API handler for Next.js applications.\n *\n * This handler intercepts requests to `/api/unblind` and proxies them to the Unblind API,\n * automatically adding authentication and tenant context. Returns `undefined` for requests\n * that don't match the path prefix, allowing Next.js to continue normal routing.\n *\n * @param extractTenantId - A function that receives the incoming request and returns a tenant ID string (or undefined)\n *\n * @returns An async request handler that proxies matching requests to the Unblind API\n *\n * @example\n * // Usage as Proxy (Next.js 16+, proxy.ts)\n * import { handler } from \"@unblind/nextjs/server\";\n *\n * export const proxy = handler((req) => req.cookies.get(\"userId\")?.value);\n *\n * export const config = {\n * matcher: \"/api/unblind/:path*\",\n * };\n *\n * @example\n * // Usage in Middleware (Next.js <16, middleware.ts)\n * import { unblindProxy } from \"@unblind/nextjs/server\";\n * import { NextRequest, NextResponse } from \"next/server\";\n *\n * export async function middleware(request: NextRequest) {\n * if (request.nextUrl.pathname.startsWith(\"/api/unblind\")) {\n * const response = await unblindProxy((req) => req.cookies.get(\"userId\")?.value);\n * if (response) return response;\n * }\n *\n * return NextResponse.next();\n * }\n *\n * export const config = {\n * matcher: [\"/api/unblind/:path*\"],\n * };\n */\nexport function proxy(\n extractTenantId: (\n request: NextRequest,\n ) => string | undefined | Promise<string | undefined>,\n): NextProxy {\n return async (request: NextRequest) => {\n if (!request.nextUrl.pathname.startsWith(UNBLIND_PATH_PREFIX)) {\n return NextResponse.next();\n }\n\n if (!UNBLIND_API_KEY) {\n console.error(\"UNBLIND_API_KEY is missing\");\n return NextResponse.json(\n { error: \"Internal Server Error\" },\n { status: 500 },\n );\n }\n\n const tenantId = await extractTenantId(request);\n\n try {\n // Tenant requests use a different path\n if (!tenantId && request.nextUrl.pathname.includes(\"/tenants/\")) {\n console.error(\"[Unblind] Unauthorized request (missing tenantId):\", {\n path: request.nextUrl.pathname,\n });\n\n return NextResponse.json(\n { error: \"Internal Server Error\" },\n { status: 500 },\n );\n }\n\n // Extract the API path (remove the prefix)\n const apiPath = request.nextUrl.pathname\n .replace(new RegExp(`^${UNBLIND_PATH_PREFIX}`), \"\")\n .replace(new RegExp(\"^\\/tenants\\/\"), \"/tenants/\" + tenantId + \"/\");\n const url = new URL(`${UNBLIND_API_ENDPOINT}${apiPath}`);\n\n request.nextUrl.searchParams.forEach((value: string, key: string) => {\n url.searchParams.append(key, value);\n });\n\n const headers: HeadersInit = {\n Authorization: `Bearer ${UNBLIND_API_KEY}`,\n };\n\n // Forward Content-Type for requests with body\n if ([\"POST\", \"PUT\", \"PATCH\", \"DELETE\"].includes(request.method)) {\n const contentType = request.headers.get(\"content-type\");\n if (contentType) {\n headers[\"Content-Type\"] = contentType;\n }\n }\n\n const options: RequestInit = {\n method: request.method,\n headers,\n };\n\n if ([\"POST\", \"PUT\", \"PATCH\", \"DELETE\"].includes(request.method)) {\n const body = await request.text();\n if (body) {\n options.body = body;\n }\n }\n\n const response = await fetch(url.toString(), options);\n const data = await response.text();\n\n return new NextResponse(data, {\n status: response.status,\n headers: {\n \"Content-Type\":\n response.headers.get(\"content-type\") || \"application/json\",\n },\n });\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n console.error(\"[Unblind]\", message);\n return NextResponse.json(\n { error: \"Internal Server Error\" },\n { status: 500 },\n );\n }\n };\n}\n"],"mappings":"AAAA,OAAiC,gBAAAA,MAAoB,cAErD,IAAMC,EAAkB,QAAQ,IAAI,gBAC9BC,EAAuB,6BACvBC,EAAsB,eAyCrB,SAASC,EACdC,EAGW,CACX,MAAO,OAAOC,GAAyB,CACrC,GAAI,CAACA,EAAQ,QAAQ,SAAS,WAAWH,CAAmB,EAC1D,OAAOH,EAAa,KAAK,EAG3B,GAAI,CAACC,EACH,eAAQ,MAAM,4BAA4B,EACnCD,EAAa,KAClB,CAAE,MAAO,uBAAwB,EACjC,CAAE,OAAQ,GAAI,CAChB,EAGF,IAAMO,EAAW,MAAMF,EAAgBC,CAAO,EAE9C,GAAI,CAEF,GAAI,CAACC,GAAYD,EAAQ,QAAQ,SAAS,SAAS,WAAW,EAC5D,eAAQ,MAAM,qDAAsD,CAClE,KAAMA,EAAQ,QAAQ,QACxB,CAAC,EAEMN,EAAa,KAClB,CAAE,MAAO,uBAAwB,EACjC,CAAE,OAAQ,GAAI,CAChB,EAIF,IAAMQ,EAAUF,EAAQ,QAAQ,SAC7B,QAAQ,IAAI,OAAO,IAAIH,CAAmB,EAAE,EAAG,EAAE,EACjD,QAAQ,IAAI,OAAO,YAAc,EAAG,YAAcI,EAAW,GAAG,EAC7DE,EAAM,IAAI,IAAI,GAAGP,CAAoB,GAAGM,CAAO,EAAE,EAEvDF,EAAQ,QAAQ,aAAa,QAAQ,CAACI,EAAeC,IAAgB,CACnEF,EAAI,aAAa,OAAOE,EAAKD,CAAK,CACpC,CAAC,EAED,IAAME,EAAuB,CAC3B,cAAe,UAAUX,CAAe,EAC1C,EAGA,GAAI,CAAC,OAAQ,MAAO,QAAS,QAAQ,EAAE,SAASK,EAAQ,MAAM,EAAG,CAC/D,IAAMO,EAAcP,EAAQ,QAAQ,IAAI,cAAc,EAClDO,IACFD,EAAQ,cAAc,EAAIC,EAE9B,CAEA,IAAMC,EAAuB,CAC3B,OAAQR,EAAQ,OAChB,QAAAM,CACF,EAEA,GAAI,CAAC,OAAQ,MAAO,QAAS,QAAQ,EAAE,SAASN,EAAQ,MAAM,EAAG,CAC/D,IAAMS,EAAO,MAAMT,EAAQ,KAAK,EAC5BS,IACFD,EAAQ,KAAOC,EAEnB,CAEA,IAAMC,EAAW,MAAM,MAAMP,EAAI,SAAS,EAAGK,CAAO,EAC9CG,EAAO,MAAMD,EAAS,KAAK,EAEjC,OAAO,IAAIhB,EAAaiB,EAAM,CAC5B,OAAQD,EAAS,OACjB,QAAS,CACP,eACEA,EAAS,QAAQ,IAAI,cAAc,GAAK,kBAC5C,CACF,CAAC,CACH,OAASE,EAAO,CACd,IAAMC,EAAUD,aAAiB,MAAQA,EAAM,QAAU,OAAOA,CAAK,EACrE,eAAQ,MAAM,YAAaC,CAAO,EAC3BnB,EAAa,KAClB,CAAE,MAAO,uBAAwB,EACjC,CAAE,OAAQ,GAAI,CAChB,CACF,CACF,CACF","names":["NextResponse","UNBLIND_API_KEY","UNBLIND_API_ENDPOINT","UNBLIND_PATH_PREFIX","proxy","extractTenantId","request","tenantId","apiPath","url","value","key","headers","contentType","options","body","response","data","error","message"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unblind/nextjs",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.22",
|
|
4
4
|
"description": "Unblind Next.js library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@unblind/react": "0.1.0-alpha.
|
|
25
|
+
"@unblind/react": "0.1.0-alpha.23"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"prettier": "^3.5.3",
|