azirid-react 0.13.2 → 0.14.1
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/README.md +54 -196
- package/dist/index.cjs +43 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -67
- package/dist/index.d.ts +12 -67
- package/dist/index.js +43 -65
- package/dist/index.js.map +1 -1
- package/dist/next-proxy.cjs +19 -3
- package/dist/next-proxy.cjs.map +1 -1
- package/dist/next-proxy.d.cts +19 -14
- package/dist/next-proxy.d.ts +19 -14
- package/dist/next-proxy.js +19 -3
- package/dist/next-proxy.js.map +1 -1
- package/dist/server.cjs +0 -35
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.cts +1 -25
- package/dist/server.d.ts +1 -25
- package/dist/server.js +1 -35
- package/dist/server.js.map +1 -1
- package/package.json +8 -25
- package/dist/next-handlers.cjs +0 -148
- package/dist/next-handlers.cjs.map +0 -1
- package/dist/next-handlers.d.cts +0 -121
- package/dist/next-handlers.d.ts +0 -121
- package/dist/next-handlers.js +0 -138
- package/dist/next-handlers.js.map +0 -1
- package/dist/next.cjs +0 -182
- package/dist/next.cjs.map +0 -1
- package/dist/next.d.cts +0 -182
- package/dist/next.d.ts +0 -182
- package/dist/next.js +0 -170
- package/dist/next.js.map +0 -1
package/dist/next-handlers.cjs
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/server.ts
|
|
4
|
-
function createSessionSyncHandler(options) {
|
|
5
|
-
const name = options?.cookieName ?? "__session";
|
|
6
|
-
const secure = options?.secure ?? false;
|
|
7
|
-
const maxAge = options?.maxAge ?? 3600;
|
|
8
|
-
return {
|
|
9
|
-
async POST(req) {
|
|
10
|
-
const { token } = await req.json();
|
|
11
|
-
if (!token || typeof token !== "string") {
|
|
12
|
-
return new Response(JSON.stringify({ error: "Missing token" }), {
|
|
13
|
-
status: 400,
|
|
14
|
-
headers: { "Content-Type": "application/json" }
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
const parts = [`${name}=${token}`, "Path=/", "HttpOnly", "SameSite=Lax", `Max-Age=${maxAge}`];
|
|
18
|
-
if (secure) parts.push("Secure");
|
|
19
|
-
return new Response(JSON.stringify({ ok: true }), {
|
|
20
|
-
headers: {
|
|
21
|
-
"Content-Type": "application/json",
|
|
22
|
-
"Set-Cookie": parts.join("; ")
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
},
|
|
26
|
-
async DELETE() {
|
|
27
|
-
const parts = [`${name}=`, "Path=/", "HttpOnly", "SameSite=Lax", "Max-Age=0"];
|
|
28
|
-
if (secure) parts.push("Secure");
|
|
29
|
-
return new Response(JSON.stringify({ ok: true }), {
|
|
30
|
-
headers: {
|
|
31
|
-
"Content-Type": "application/json",
|
|
32
|
-
"Set-Cookie": parts.join("; ")
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// src/next-handlers.ts
|
|
40
|
-
var AZIRID_API_URL = process.env.AZIRID_API_URL ?? "https://api.azirid.com";
|
|
41
|
-
function createHandler(options) {
|
|
42
|
-
const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\/$/, "");
|
|
43
|
-
const debug = options?.debug ?? false;
|
|
44
|
-
return async function handler(request, context) {
|
|
45
|
-
const resolvedParams = context.params instanceof Promise ? await context.params : context.params;
|
|
46
|
-
const path = resolvedParams.path.join("/");
|
|
47
|
-
const targetUrl = `${destination}/v1/users/auth/${path}`;
|
|
48
|
-
if (debug) {
|
|
49
|
-
console.log(`[azirid-proxy] ${request.method} /api/auth/${path} \u2192 ${targetUrl}`);
|
|
50
|
-
console.log(`[azirid-proxy] Cookies:`, request.headers.get("cookie") ?? "(none)");
|
|
51
|
-
}
|
|
52
|
-
const forwardHeaders = new Headers();
|
|
53
|
-
request.headers.forEach((value, key) => {
|
|
54
|
-
const lower = key.toLowerCase();
|
|
55
|
-
if (lower !== "host" && lower !== "connection") {
|
|
56
|
-
forwardHeaders.set(key, value);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
const body = request.method !== "GET" && request.method !== "HEAD" ? await request.text() : void 0;
|
|
60
|
-
const apiResponse = await fetch(targetUrl, {
|
|
61
|
-
method: request.method,
|
|
62
|
-
headers: forwardHeaders,
|
|
63
|
-
body
|
|
64
|
-
});
|
|
65
|
-
const responseHeaders = new Headers();
|
|
66
|
-
apiResponse.headers.forEach((value, key) => {
|
|
67
|
-
if (key.toLowerCase() !== "set-cookie") {
|
|
68
|
-
responseHeaders.set(key, value);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
const isSecure = request.headers.get("x-forwarded-proto") === "https" || new URL(request.url).protocol === "https:";
|
|
72
|
-
const setCookies = typeof apiResponse.headers.getSetCookie === "function" ? apiResponse.headers.getSetCookie() : splitSetCookieHeader(apiResponse.headers.get("set-cookie") ?? "");
|
|
73
|
-
for (const cookie of setCookies) {
|
|
74
|
-
let fixed = cookie.replace(/;\s*[Dd]omain=[^;]*/g, "").replace(/;\s*[Pp]ath=[^;]*/g, "").replace(/[Ss]ame[Ss]ite=[Nn]one/g, "SameSite=Lax");
|
|
75
|
-
fixed += "; Path=/";
|
|
76
|
-
if (!isSecure) {
|
|
77
|
-
fixed = fixed.replace(/;\s*[Ss]ecure/g, "");
|
|
78
|
-
}
|
|
79
|
-
responseHeaders.append("set-cookie", fixed);
|
|
80
|
-
}
|
|
81
|
-
if (debug) {
|
|
82
|
-
console.log(`[azirid-proxy] API responded: ${apiResponse.status}`);
|
|
83
|
-
const responseCookies = [];
|
|
84
|
-
responseHeaders.forEach((value, key) => {
|
|
85
|
-
if (key.toLowerCase() === "set-cookie") responseCookies.push(value);
|
|
86
|
-
});
|
|
87
|
-
if (responseCookies.length > 0) {
|
|
88
|
-
console.log(`[azirid-proxy] Set-Cookie:`, responseCookies);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return new Response(apiResponse.body, {
|
|
92
|
-
status: apiResponse.status,
|
|
93
|
-
statusText: apiResponse.statusText,
|
|
94
|
-
headers: responseHeaders
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
function splitSetCookieHeader(header) {
|
|
99
|
-
if (!header) return [];
|
|
100
|
-
const cookies = [];
|
|
101
|
-
let current = "";
|
|
102
|
-
for (const part of header.split(", ")) {
|
|
103
|
-
if (current && /^[a-zA-Z0-9_\-.]+=/.test(part)) {
|
|
104
|
-
cookies.push(current);
|
|
105
|
-
current = part;
|
|
106
|
-
} else {
|
|
107
|
-
current = current ? `${current}, ${part}` : part;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (current) cookies.push(current);
|
|
111
|
-
return cookies;
|
|
112
|
-
}
|
|
113
|
-
function createAziridRouteHandlers(options) {
|
|
114
|
-
const handler = createHandler(options);
|
|
115
|
-
return {
|
|
116
|
-
GET: handler,
|
|
117
|
-
POST: handler,
|
|
118
|
-
PUT: handler,
|
|
119
|
-
PATCH: handler,
|
|
120
|
-
DELETE: handler
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
var defaultHandlers = createAziridRouteHandlers();
|
|
124
|
-
var GET = defaultHandlers.GET;
|
|
125
|
-
var POST = defaultHandlers.POST;
|
|
126
|
-
var PUT = defaultHandlers.PUT;
|
|
127
|
-
var PATCH = defaultHandlers.PATCH;
|
|
128
|
-
var DELETE = defaultHandlers.DELETE;
|
|
129
|
-
function createAziridProxyHandler(apiUrl = AZIRID_API_URL) {
|
|
130
|
-
return createHandler({ apiUrl });
|
|
131
|
-
}
|
|
132
|
-
function withAziridProxy(apiUrl = AZIRID_API_URL) {
|
|
133
|
-
return (nextConfig = {}) => ({
|
|
134
|
-
...nextConfig
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
exports.DELETE = DELETE;
|
|
139
|
-
exports.GET = GET;
|
|
140
|
-
exports.PATCH = PATCH;
|
|
141
|
-
exports.POST = POST;
|
|
142
|
-
exports.PUT = PUT;
|
|
143
|
-
exports.createAziridProxyHandler = createAziridProxyHandler;
|
|
144
|
-
exports.createAziridRouteHandlers = createAziridRouteHandlers;
|
|
145
|
-
exports.createSessionSyncHandler = createSessionSyncHandler;
|
|
146
|
-
exports.withAziridProxy = withAziridProxy;
|
|
147
|
-
//# sourceMappingURL=next-handlers.cjs.map
|
|
148
|
-
//# sourceMappingURL=next-handlers.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server.ts","../src/next-handlers.ts"],"names":[],"mappings":";;;AA2HO,SAAS,yBAAyB,OAAA,EAA8B;AACrE,EAAA,MAAM,IAAA,GAAO,SAAS,UAAA,IAAc,WAAA;AACpC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,KAAA;AAClC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,IAAA;AAElC,EAAA,OAAO;AAAA,IACL,MAAM,KAAK,GAAA,EAAc;AACvB,MAAA,MAAM,EAAE,KAAA,EAAM,GAAI,MAAM,IAAI,IAAA,EAAK;AACjC,MAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,QAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,KAAA,EAAO,eAAA,EAAiB,CAAA,EAAG;AAAA,UAC9D,MAAA,EAAQ,GAAA;AAAA,UACR,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA;AAAmB,SAC/C,CAAA;AAAA,MACH;AACA,MAAA,MAAM,KAAA,GAAQ,CAAC,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA,EAAI,QAAA,EAAU,UAAA,EAAY,cAAA,EAAgB,CAAA,QAAA,EAAW,MAAM,CAAA,CAAE,CAAA;AAC5F,MAAA,IAAI,MAAA,EAAQ,KAAA,CAAM,IAAA,CAAK,QAAQ,CAAA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AAO,eAAA,CAAgB;AAC7B,IAAM,MAAM,eAAA,CAAgB;AAC5B,IAAM,QAAQ,eAAA,CAAgB;AAC9B,IAAM,SAAS,eAAA,CAAgB;AAU/B,SAAS,wBAAA,CAAyB,SAAiB,cAAA,EAA8B;AACtF,EAAA,OAAO,aAAA,CAAc,EAAE,MAAA,EAAQ,CAAA;AACjC;AAYO,SAAS,eAAA,CAAgB,SAAiB,cAAA,EAAgB;AAG/D,EAAA,OAAO,CAAC,UAAA,GAAsC,EAAC,MAAO;AAAA,IACpD,GAAG;AAAA,GACL,CAAA;AACF","file":"next-handlers.cjs","sourcesContent":["/**\n * Server-side utilities for Next.js (App Router).\n *\n * Use this entry point in Server Components, Server Actions, and Route Handlers\n * to read the authenticated user's session token from the httpOnly `__session` cookie.\n *\n * @example\n * ```ts\n * // lib/access-server.ts\n * import { cookies } from \"next/headers\";\n * import { createServerAccess } from \"azirid-react/server\";\n * export const { getSessionToken } = createServerAccess({ cookies });\n * ```\n *\n * ```ts\n * // app/actions/profile.ts\n * \"use server\";\n * import { getSessionToken } from \"@/lib/access-server\";\n *\n * export async function getProfile() {\n * const token = await getSessionToken();\n * if (!token) throw new Error(\"Not authenticated\");\n *\n * const res = await fetch(\"https://api.myapp.com/v1/users/auth/me\", {\n * headers: { Authorization: `Bearer ${token}` },\n * });\n * return res.json();\n * }\n * ```\n *\n * @packageDocumentation\n */\n\n/** Minimal cookie store interface (subset of Next.js ReadonlyRequestCookies) */\ninterface CookieStore {\n get(name: string): { value: string } | undefined\n}\n\n/** The `cookies` function from `next/headers` */\ntype CookiesFn = () => CookieStore | Promise<CookieStore>\n\nexport interface ServerAccessOptions {\n /**\n * The `cookies` function imported from `next/headers`.\n * Required — pass it explicitly to avoid bundler issues with dynamic imports.\n *\n * @example\n * ```ts\n * import { cookies } from \"next/headers\";\n * createServerAccess({ cookies });\n * ```\n */\n cookies: CookiesFn\n\n /**\n * Name of the httpOnly cookie that carries the access token.\n * Must match the cookie set by the Azirid API.\n * @default \"__session\"\n */\n cookieName?: string\n}\n\nexport interface ServerAccess {\n /**\n * Read the raw JWT access token from the session cookie.\n * Returns `null` when no cookie is present or when called\n * outside a Next.js server context.\n */\n getSessionToken: () => Promise<string | null>\n\n /** Alias for {@link getSessionToken}. */\n getAccessToken: () => Promise<string | null>\n}\n\n/**\n * Create server-side helpers bound to the session cookie.\n *\n * Call once at module scope, then use the returned functions\n * in any Server Component, Server Action, or Route Handler.\n */\nexport function createServerAccess(options: ServerAccessOptions): ServerAccess {\n const { cookies, cookieName = '__session' } = options\n\n async function getSessionToken(): Promise<string | null> {\n try {\n const result = cookies()\n // Next.js 15+: cookies() returns a Promise\n const cookieStore = result instanceof Promise ? await result : result\n return cookieStore.get(cookieName)?.value ?? null\n } catch {\n return null\n }\n }\n\n return {\n getSessionToken,\n getAccessToken: getSessionToken,\n }\n}\n\n// ─── Session Sync Handler (cross-origin dev support) ─────────\n\nexport interface SessionSyncOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Set Secure flag on cookie (default: false) */\n secure?: boolean\n /** Cookie max age in seconds (default: 3600 = 1h) */\n maxAge?: number\n}\n\n/**\n * Create a route handler that syncs the access token to a local httpOnly cookie.\n *\n * Use this in a Next.js App Router route to bridge cross-origin sessions:\n *\n * @example\n * ```ts\n * // app/api/auth/session/route.ts\n * import { createSessionSyncHandler } from \"azirid-react/server\";\n * export const { POST, DELETE } = createSessionSyncHandler();\n * ```\n */\nexport function createSessionSyncHandler(options?: SessionSyncOptions) {\n const name = options?.cookieName ?? '__session'\n const secure = options?.secure ?? false\n const maxAge = options?.maxAge ?? 3600\n\n return {\n async POST(req: Request) {\n const { token } = await req.json()\n if (!token || typeof token !== 'string') {\n return new Response(JSON.stringify({ error: 'Missing token' }), {\n status: 400,\n headers: { 'Content-Type': 'application/json' },\n })\n }\n const parts = [`${name}=${token}`, 'Path=/', 'HttpOnly', 'SameSite=Lax', `Max-Age=${maxAge}`]\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n async DELETE() {\n const parts = [`${name}=`, 'Path=/', 'HttpOnly', 'SameSite=Lax', 'Max-Age=0']\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n }\n}\n","/**\n * Next.js route handlers for the Azirid auth proxy.\n *\n * This entry point uses **only standard Web APIs** (Request, Response, fetch)\n * and has **zero dependency on `next/server`**. This makes it safe to use in\n * Next.js standalone builds (`output: 'standalone'`) where external `next/server`\n * imports may fail at runtime.\n *\n * @example Route handler (simplest — one line)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n *\n * @example Route handler (custom API URL)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n *\n * @packageDocumentation\n */\n\n// ─── Constants ───────────────────────────────────────────────\n\n/**\n * Resolve the Azirid API URL.\n *\n * Priority:\n * 1. `AZIRID_API_URL` env var (server-only, recommended)\n * 2. `https://api.azirid.com` (production default)\n *\n * For local development, set `AZIRID_API_URL=http://localhost:3000` in your `.env`.\n */\nconst AZIRID_API_URL = process.env.AZIRID_API_URL ?? 'https://api.azirid.com'\n\n// ─── Types ───────────────────────────────────────────────────\n\ntype RouteHandler = (\n request: Request,\n context: { params: Promise<{ path: string[] }> | { path: string[] } },\n) => Promise<Response>\n\nexport interface AziridRouteHandlerOptions {\n /** Override the Azirid API URL (default: production API) */\n apiUrl?: string\n /** Enable debug logging of proxy requests (default: false) */\n debug?: boolean\n}\n\n// ─── Proxy Route Handler (core logic) ────────────────────────\n\nfunction createHandler(options?: AziridRouteHandlerOptions): RouteHandler {\n const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\\/$/, '')\n const debug = options?.debug ?? false\n\n return async function handler(request, context) {\n // Handle Next.js 14 (sync params), 15 (async params), and 16+ (always async)\n const resolvedParams = context.params instanceof Promise ? await context.params : context.params\n const path = resolvedParams.path.join('/')\n const targetUrl = `${destination}/v1/users/auth/${path}`\n\n if (debug) {\n console.log(`[azirid-proxy] ${request.method} /api/auth/${path} → ${targetUrl}`)\n console.log(`[azirid-proxy] Cookies:`, request.headers.get('cookie') ?? '(none)')\n }\n\n // Forward request headers (strip host so the API sees its own host)\n const forwardHeaders = new Headers()\n request.headers.forEach((value, key) => {\n const lower = key.toLowerCase()\n if (lower !== 'host' && lower !== 'connection') {\n forwardHeaders.set(key, value)\n }\n })\n\n // Forward body for non-GET methods\n const body =\n request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined\n\n // Proxy request to the Azirid API\n const apiResponse = await fetch(targetUrl, {\n method: request.method,\n headers: forwardHeaders,\n body,\n })\n\n // Build response headers, fixing Set-Cookie for first-party context\n const responseHeaders = new Headers()\n apiResponse.headers.forEach((value, key) => {\n if (key.toLowerCase() !== 'set-cookie') {\n responseHeaders.set(key, value)\n }\n })\n\n // Fix Set-Cookie headers: strip Domain, adjust SameSite/Secure for proxy\n const isSecure =\n request.headers.get('x-forwarded-proto') === 'https' ||\n new URL(request.url).protocol === 'https:'\n\n const setCookies: string[] =\n typeof apiResponse.headers.getSetCookie === 'function'\n ? apiResponse.headers.getSetCookie()\n : splitSetCookieHeader(apiResponse.headers.get('set-cookie') ?? '')\n\n for (const cookie of setCookies) {\n let fixed = cookie\n // Strip Domain — cookie will default to the proxy's domain (first-party)\n .replace(/;\\s*[Dd]omain=[^;]*/g, '')\n // Normalize Path to \"/\" — the API may set restrictive paths (e.g. /v1/users/auth/login)\n // which prevents cookies from being sent to other auth endpoints like /refresh or /bootstrap.\n // In proxy mode all auth cookies must be available on all routes.\n .replace(/;\\s*[Pp]ath=[^;]*/g, '')\n // Change SameSite=None to Lax (now same-origin, None is unnecessary)\n .replace(/[Ss]ame[Ss]ite=[Nn]one/g, 'SameSite=Lax')\n\n // Always set Path=/ for first-party proxy cookies\n fixed += '; Path=/'\n\n // In development (HTTP), remove Secure flag so cookies work on localhost\n if (!isSecure) {\n fixed = fixed.replace(/;\\s*[Ss]ecure/g, '')\n }\n\n responseHeaders.append('set-cookie', fixed)\n }\n\n if (debug) {\n console.log(`[azirid-proxy] API responded: ${apiResponse.status}`)\n const responseCookies: string[] = []\n responseHeaders.forEach((value, key) => {\n if (key.toLowerCase() === 'set-cookie') responseCookies.push(value)\n })\n if (responseCookies.length > 0) {\n console.log(`[azirid-proxy] Set-Cookie:`, responseCookies)\n }\n }\n\n return new Response(apiResponse.body, {\n status: apiResponse.status,\n statusText: apiResponse.statusText,\n headers: responseHeaders,\n })\n }\n}\n\n/** Split a raw Set-Cookie header string into individual cookies */\nfunction splitSetCookieHeader(header: string): string[] {\n if (!header) return []\n const cookies: string[] = []\n let current = ''\n for (const part of header.split(', ')) {\n if (current && /^[a-zA-Z0-9_\\-.]+=/.test(part)) {\n cookies.push(current)\n current = part\n } else {\n current = current ? `${current}, ${part}` : part\n }\n }\n if (current) cookies.push(current)\n return cookies\n}\n\n// ─── Route Handlers (ready to re-export) ─────────────────────\n\n/**\n * Create all route handlers for the Azirid auth proxy.\n *\n * Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported\n * from your `app/api/auth/[...path]/route.ts`.\n *\n * @example Default (zero config)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();\n * ```\n *\n * @example Custom options\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n */\nexport function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {\n GET: RouteHandler\n POST: RouteHandler\n PUT: RouteHandler\n PATCH: RouteHandler\n DELETE: RouteHandler\n} {\n const handler = createHandler(options)\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n }\n}\n\n/**\n * Pre-built route handlers with default config.\n *\n * Just re-export these directly — no setup required:\n *\n * @example\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n */\nconst defaultHandlers = createAziridRouteHandlers()\nexport const GET = defaultHandlers.GET\nexport const POST = defaultHandlers.POST\nexport const PUT = defaultHandlers.PUT\nexport const PATCH = defaultHandlers.PATCH\nexport const DELETE = defaultHandlers.DELETE\n\n/**\n * Create a single Next.js route handler function.\n *\n * Use this if you need a single handler reference (advanced use).\n * For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.\n *\n * @deprecated Use `createAziridRouteHandlers()` instead.\n */\nexport function createAziridProxyHandler(apiUrl: string = AZIRID_API_URL): RouteHandler {\n return createHandler({ apiUrl })\n}\n\n// ─── Config Helper ───────────────────────────────────────────\n\n/**\n * Wrap your Next.js config with Azirid settings.\n *\n * In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.\n * This helper is kept for backward compatibility with Next.js 14/15.\n *\n * @param apiUrl - Override the Azirid API URL (default: production API)\n */\nexport function withAziridProxy(apiUrl: string = AZIRID_API_URL) {\n void apiUrl\n\n return (nextConfig: Record<string, unknown> = {}) => ({\n ...nextConfig,\n })\n}\n\n// ─── Re-export session sync for backward compat ──────────────\n\nexport { createSessionSyncHandler } from './server'\nexport type { SessionSyncOptions } from './server'\n"]}
|
package/dist/next-handlers.d.cts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
interface SessionSyncOptions {
|
|
2
|
-
/** Cookie name (default: "__session") */
|
|
3
|
-
cookieName?: string;
|
|
4
|
-
/** Set Secure flag on cookie (default: false) */
|
|
5
|
-
secure?: boolean;
|
|
6
|
-
/** Cookie max age in seconds (default: 3600 = 1h) */
|
|
7
|
-
maxAge?: number;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Create a route handler that syncs the access token to a local httpOnly cookie.
|
|
11
|
-
*
|
|
12
|
-
* Use this in a Next.js App Router route to bridge cross-origin sessions:
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* // app/api/auth/session/route.ts
|
|
17
|
-
* import { createSessionSyncHandler } from "azirid-react/server";
|
|
18
|
-
* export const { POST, DELETE } = createSessionSyncHandler();
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
declare function createSessionSyncHandler(options?: SessionSyncOptions): {
|
|
22
|
-
POST(req: Request): Promise<Response>;
|
|
23
|
-
DELETE(): Promise<Response>;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Next.js route handlers for the Azirid auth proxy.
|
|
28
|
-
*
|
|
29
|
-
* This entry point uses **only standard Web APIs** (Request, Response, fetch)
|
|
30
|
-
* and has **zero dependency on `next/server`**. This makes it safe to use in
|
|
31
|
-
* Next.js standalone builds (`output: 'standalone'`) where external `next/server`
|
|
32
|
-
* imports may fail at runtime.
|
|
33
|
-
*
|
|
34
|
-
* @example Route handler (simplest — one line)
|
|
35
|
-
* ```ts
|
|
36
|
-
* // app/api/auth/[...path]/route.ts
|
|
37
|
-
* export { GET, POST, PUT, PATCH, DELETE } from "azirid-react/next/handlers";
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example Route handler (custom API URL)
|
|
41
|
-
* ```ts
|
|
42
|
-
* // app/api/auth/[...path]/route.ts
|
|
43
|
-
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
44
|
-
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
45
|
-
* apiUrl: "https://my-custom-api.com",
|
|
46
|
-
* debug: true,
|
|
47
|
-
* });
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* @packageDocumentation
|
|
51
|
-
*/
|
|
52
|
-
type RouteHandler = (request: Request, context: {
|
|
53
|
-
params: Promise<{
|
|
54
|
-
path: string[];
|
|
55
|
-
}> | {
|
|
56
|
-
path: string[];
|
|
57
|
-
};
|
|
58
|
-
}) => Promise<Response>;
|
|
59
|
-
interface AziridRouteHandlerOptions {
|
|
60
|
-
/** Override the Azirid API URL (default: production API) */
|
|
61
|
-
apiUrl?: string;
|
|
62
|
-
/** Enable debug logging of proxy requests (default: false) */
|
|
63
|
-
debug?: boolean;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Create all route handlers for the Azirid auth proxy.
|
|
67
|
-
*
|
|
68
|
-
* Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported
|
|
69
|
-
* from your `app/api/auth/[...path]/route.ts`.
|
|
70
|
-
*
|
|
71
|
-
* @example Default (zero config)
|
|
72
|
-
* ```ts
|
|
73
|
-
* // app/api/auth/[...path]/route.ts
|
|
74
|
-
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
75
|
-
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* @example Custom options
|
|
79
|
-
* ```ts
|
|
80
|
-
* // app/api/auth/[...path]/route.ts
|
|
81
|
-
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
82
|
-
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
83
|
-
* apiUrl: "https://my-custom-api.com",
|
|
84
|
-
* debug: true,
|
|
85
|
-
* });
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
declare function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {
|
|
89
|
-
GET: RouteHandler;
|
|
90
|
-
POST: RouteHandler;
|
|
91
|
-
PUT: RouteHandler;
|
|
92
|
-
PATCH: RouteHandler;
|
|
93
|
-
DELETE: RouteHandler;
|
|
94
|
-
};
|
|
95
|
-
declare const GET: RouteHandler;
|
|
96
|
-
declare const POST: RouteHandler;
|
|
97
|
-
declare const PUT: RouteHandler;
|
|
98
|
-
declare const PATCH: RouteHandler;
|
|
99
|
-
declare const DELETE: RouteHandler;
|
|
100
|
-
/**
|
|
101
|
-
* Create a single Next.js route handler function.
|
|
102
|
-
*
|
|
103
|
-
* Use this if you need a single handler reference (advanced use).
|
|
104
|
-
* For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.
|
|
105
|
-
*
|
|
106
|
-
* @deprecated Use `createAziridRouteHandlers()` instead.
|
|
107
|
-
*/
|
|
108
|
-
declare function createAziridProxyHandler(apiUrl?: string): RouteHandler;
|
|
109
|
-
/**
|
|
110
|
-
* Wrap your Next.js config with Azirid settings.
|
|
111
|
-
*
|
|
112
|
-
* In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.
|
|
113
|
-
* This helper is kept for backward compatibility with Next.js 14/15.
|
|
114
|
-
*
|
|
115
|
-
* @param apiUrl - Override the Azirid API URL (default: production API)
|
|
116
|
-
*/
|
|
117
|
-
declare function withAziridProxy(apiUrl?: string): (nextConfig?: Record<string, unknown>) => {
|
|
118
|
-
[x: string]: unknown;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
export { type AziridRouteHandlerOptions, DELETE, GET, PATCH, POST, PUT, type SessionSyncOptions, createAziridProxyHandler, createAziridRouteHandlers, createSessionSyncHandler, withAziridProxy };
|
package/dist/next-handlers.d.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
interface SessionSyncOptions {
|
|
2
|
-
/** Cookie name (default: "__session") */
|
|
3
|
-
cookieName?: string;
|
|
4
|
-
/** Set Secure flag on cookie (default: false) */
|
|
5
|
-
secure?: boolean;
|
|
6
|
-
/** Cookie max age in seconds (default: 3600 = 1h) */
|
|
7
|
-
maxAge?: number;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Create a route handler that syncs the access token to a local httpOnly cookie.
|
|
11
|
-
*
|
|
12
|
-
* Use this in a Next.js App Router route to bridge cross-origin sessions:
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* // app/api/auth/session/route.ts
|
|
17
|
-
* import { createSessionSyncHandler } from "azirid-react/server";
|
|
18
|
-
* export const { POST, DELETE } = createSessionSyncHandler();
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
declare function createSessionSyncHandler(options?: SessionSyncOptions): {
|
|
22
|
-
POST(req: Request): Promise<Response>;
|
|
23
|
-
DELETE(): Promise<Response>;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Next.js route handlers for the Azirid auth proxy.
|
|
28
|
-
*
|
|
29
|
-
* This entry point uses **only standard Web APIs** (Request, Response, fetch)
|
|
30
|
-
* and has **zero dependency on `next/server`**. This makes it safe to use in
|
|
31
|
-
* Next.js standalone builds (`output: 'standalone'`) where external `next/server`
|
|
32
|
-
* imports may fail at runtime.
|
|
33
|
-
*
|
|
34
|
-
* @example Route handler (simplest — one line)
|
|
35
|
-
* ```ts
|
|
36
|
-
* // app/api/auth/[...path]/route.ts
|
|
37
|
-
* export { GET, POST, PUT, PATCH, DELETE } from "azirid-react/next/handlers";
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @example Route handler (custom API URL)
|
|
41
|
-
* ```ts
|
|
42
|
-
* // app/api/auth/[...path]/route.ts
|
|
43
|
-
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
44
|
-
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
45
|
-
* apiUrl: "https://my-custom-api.com",
|
|
46
|
-
* debug: true,
|
|
47
|
-
* });
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* @packageDocumentation
|
|
51
|
-
*/
|
|
52
|
-
type RouteHandler = (request: Request, context: {
|
|
53
|
-
params: Promise<{
|
|
54
|
-
path: string[];
|
|
55
|
-
}> | {
|
|
56
|
-
path: string[];
|
|
57
|
-
};
|
|
58
|
-
}) => Promise<Response>;
|
|
59
|
-
interface AziridRouteHandlerOptions {
|
|
60
|
-
/** Override the Azirid API URL (default: production API) */
|
|
61
|
-
apiUrl?: string;
|
|
62
|
-
/** Enable debug logging of proxy requests (default: false) */
|
|
63
|
-
debug?: boolean;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Create all route handlers for the Azirid auth proxy.
|
|
67
|
-
*
|
|
68
|
-
* Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported
|
|
69
|
-
* from your `app/api/auth/[...path]/route.ts`.
|
|
70
|
-
*
|
|
71
|
-
* @example Default (zero config)
|
|
72
|
-
* ```ts
|
|
73
|
-
* // app/api/auth/[...path]/route.ts
|
|
74
|
-
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
75
|
-
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* @example Custom options
|
|
79
|
-
* ```ts
|
|
80
|
-
* // app/api/auth/[...path]/route.ts
|
|
81
|
-
* import { createAziridRouteHandlers } from "azirid-react/next/handlers";
|
|
82
|
-
* export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({
|
|
83
|
-
* apiUrl: "https://my-custom-api.com",
|
|
84
|
-
* debug: true,
|
|
85
|
-
* });
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
declare function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {
|
|
89
|
-
GET: RouteHandler;
|
|
90
|
-
POST: RouteHandler;
|
|
91
|
-
PUT: RouteHandler;
|
|
92
|
-
PATCH: RouteHandler;
|
|
93
|
-
DELETE: RouteHandler;
|
|
94
|
-
};
|
|
95
|
-
declare const GET: RouteHandler;
|
|
96
|
-
declare const POST: RouteHandler;
|
|
97
|
-
declare const PUT: RouteHandler;
|
|
98
|
-
declare const PATCH: RouteHandler;
|
|
99
|
-
declare const DELETE: RouteHandler;
|
|
100
|
-
/**
|
|
101
|
-
* Create a single Next.js route handler function.
|
|
102
|
-
*
|
|
103
|
-
* Use this if you need a single handler reference (advanced use).
|
|
104
|
-
* For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.
|
|
105
|
-
*
|
|
106
|
-
* @deprecated Use `createAziridRouteHandlers()` instead.
|
|
107
|
-
*/
|
|
108
|
-
declare function createAziridProxyHandler(apiUrl?: string): RouteHandler;
|
|
109
|
-
/**
|
|
110
|
-
* Wrap your Next.js config with Azirid settings.
|
|
111
|
-
*
|
|
112
|
-
* In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.
|
|
113
|
-
* This helper is kept for backward compatibility with Next.js 14/15.
|
|
114
|
-
*
|
|
115
|
-
* @param apiUrl - Override the Azirid API URL (default: production API)
|
|
116
|
-
*/
|
|
117
|
-
declare function withAziridProxy(apiUrl?: string): (nextConfig?: Record<string, unknown>) => {
|
|
118
|
-
[x: string]: unknown;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
export { type AziridRouteHandlerOptions, DELETE, GET, PATCH, POST, PUT, type SessionSyncOptions, createAziridProxyHandler, createAziridRouteHandlers, createSessionSyncHandler, withAziridProxy };
|
package/dist/next-handlers.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
// src/server.ts
|
|
2
|
-
function createSessionSyncHandler(options) {
|
|
3
|
-
const name = options?.cookieName ?? "__session";
|
|
4
|
-
const secure = options?.secure ?? false;
|
|
5
|
-
const maxAge = options?.maxAge ?? 3600;
|
|
6
|
-
return {
|
|
7
|
-
async POST(req) {
|
|
8
|
-
const { token } = await req.json();
|
|
9
|
-
if (!token || typeof token !== "string") {
|
|
10
|
-
return new Response(JSON.stringify({ error: "Missing token" }), {
|
|
11
|
-
status: 400,
|
|
12
|
-
headers: { "Content-Type": "application/json" }
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
const parts = [`${name}=${token}`, "Path=/", "HttpOnly", "SameSite=Lax", `Max-Age=${maxAge}`];
|
|
16
|
-
if (secure) parts.push("Secure");
|
|
17
|
-
return new Response(JSON.stringify({ ok: true }), {
|
|
18
|
-
headers: {
|
|
19
|
-
"Content-Type": "application/json",
|
|
20
|
-
"Set-Cookie": parts.join("; ")
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
},
|
|
24
|
-
async DELETE() {
|
|
25
|
-
const parts = [`${name}=`, "Path=/", "HttpOnly", "SameSite=Lax", "Max-Age=0"];
|
|
26
|
-
if (secure) parts.push("Secure");
|
|
27
|
-
return new Response(JSON.stringify({ ok: true }), {
|
|
28
|
-
headers: {
|
|
29
|
-
"Content-Type": "application/json",
|
|
30
|
-
"Set-Cookie": parts.join("; ")
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// src/next-handlers.ts
|
|
38
|
-
var AZIRID_API_URL = process.env.AZIRID_API_URL ?? "https://api.azirid.com";
|
|
39
|
-
function createHandler(options) {
|
|
40
|
-
const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\/$/, "");
|
|
41
|
-
const debug = options?.debug ?? false;
|
|
42
|
-
return async function handler(request, context) {
|
|
43
|
-
const resolvedParams = context.params instanceof Promise ? await context.params : context.params;
|
|
44
|
-
const path = resolvedParams.path.join("/");
|
|
45
|
-
const targetUrl = `${destination}/v1/users/auth/${path}`;
|
|
46
|
-
if (debug) {
|
|
47
|
-
console.log(`[azirid-proxy] ${request.method} /api/auth/${path} \u2192 ${targetUrl}`);
|
|
48
|
-
console.log(`[azirid-proxy] Cookies:`, request.headers.get("cookie") ?? "(none)");
|
|
49
|
-
}
|
|
50
|
-
const forwardHeaders = new Headers();
|
|
51
|
-
request.headers.forEach((value, key) => {
|
|
52
|
-
const lower = key.toLowerCase();
|
|
53
|
-
if (lower !== "host" && lower !== "connection") {
|
|
54
|
-
forwardHeaders.set(key, value);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
const body = request.method !== "GET" && request.method !== "HEAD" ? await request.text() : void 0;
|
|
58
|
-
const apiResponse = await fetch(targetUrl, {
|
|
59
|
-
method: request.method,
|
|
60
|
-
headers: forwardHeaders,
|
|
61
|
-
body
|
|
62
|
-
});
|
|
63
|
-
const responseHeaders = new Headers();
|
|
64
|
-
apiResponse.headers.forEach((value, key) => {
|
|
65
|
-
if (key.toLowerCase() !== "set-cookie") {
|
|
66
|
-
responseHeaders.set(key, value);
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
const isSecure = request.headers.get("x-forwarded-proto") === "https" || new URL(request.url).protocol === "https:";
|
|
70
|
-
const setCookies = typeof apiResponse.headers.getSetCookie === "function" ? apiResponse.headers.getSetCookie() : splitSetCookieHeader(apiResponse.headers.get("set-cookie") ?? "");
|
|
71
|
-
for (const cookie of setCookies) {
|
|
72
|
-
let fixed = cookie.replace(/;\s*[Dd]omain=[^;]*/g, "").replace(/;\s*[Pp]ath=[^;]*/g, "").replace(/[Ss]ame[Ss]ite=[Nn]one/g, "SameSite=Lax");
|
|
73
|
-
fixed += "; Path=/";
|
|
74
|
-
if (!isSecure) {
|
|
75
|
-
fixed = fixed.replace(/;\s*[Ss]ecure/g, "");
|
|
76
|
-
}
|
|
77
|
-
responseHeaders.append("set-cookie", fixed);
|
|
78
|
-
}
|
|
79
|
-
if (debug) {
|
|
80
|
-
console.log(`[azirid-proxy] API responded: ${apiResponse.status}`);
|
|
81
|
-
const responseCookies = [];
|
|
82
|
-
responseHeaders.forEach((value, key) => {
|
|
83
|
-
if (key.toLowerCase() === "set-cookie") responseCookies.push(value);
|
|
84
|
-
});
|
|
85
|
-
if (responseCookies.length > 0) {
|
|
86
|
-
console.log(`[azirid-proxy] Set-Cookie:`, responseCookies);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return new Response(apiResponse.body, {
|
|
90
|
-
status: apiResponse.status,
|
|
91
|
-
statusText: apiResponse.statusText,
|
|
92
|
-
headers: responseHeaders
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function splitSetCookieHeader(header) {
|
|
97
|
-
if (!header) return [];
|
|
98
|
-
const cookies = [];
|
|
99
|
-
let current = "";
|
|
100
|
-
for (const part of header.split(", ")) {
|
|
101
|
-
if (current && /^[a-zA-Z0-9_\-.]+=/.test(part)) {
|
|
102
|
-
cookies.push(current);
|
|
103
|
-
current = part;
|
|
104
|
-
} else {
|
|
105
|
-
current = current ? `${current}, ${part}` : part;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if (current) cookies.push(current);
|
|
109
|
-
return cookies;
|
|
110
|
-
}
|
|
111
|
-
function createAziridRouteHandlers(options) {
|
|
112
|
-
const handler = createHandler(options);
|
|
113
|
-
return {
|
|
114
|
-
GET: handler,
|
|
115
|
-
POST: handler,
|
|
116
|
-
PUT: handler,
|
|
117
|
-
PATCH: handler,
|
|
118
|
-
DELETE: handler
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
var defaultHandlers = createAziridRouteHandlers();
|
|
122
|
-
var GET = defaultHandlers.GET;
|
|
123
|
-
var POST = defaultHandlers.POST;
|
|
124
|
-
var PUT = defaultHandlers.PUT;
|
|
125
|
-
var PATCH = defaultHandlers.PATCH;
|
|
126
|
-
var DELETE = defaultHandlers.DELETE;
|
|
127
|
-
function createAziridProxyHandler(apiUrl = AZIRID_API_URL) {
|
|
128
|
-
return createHandler({ apiUrl });
|
|
129
|
-
}
|
|
130
|
-
function withAziridProxy(apiUrl = AZIRID_API_URL) {
|
|
131
|
-
return (nextConfig = {}) => ({
|
|
132
|
-
...nextConfig
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export { DELETE, GET, PATCH, POST, PUT, createAziridProxyHandler, createAziridRouteHandlers, createSessionSyncHandler, withAziridProxy };
|
|
137
|
-
//# sourceMappingURL=next-handlers.js.map
|
|
138
|
-
//# sourceMappingURL=next-handlers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server.ts","../src/next-handlers.ts"],"names":[],"mappings":";AA2HO,SAAS,yBAAyB,OAAA,EAA8B;AACrE,EAAA,MAAM,IAAA,GAAO,SAAS,UAAA,IAAc,WAAA;AACpC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,KAAA;AAClC,EAAA,MAAM,MAAA,GAAS,SAAS,MAAA,IAAU,IAAA;AAElC,EAAA,OAAO;AAAA,IACL,MAAM,KAAK,GAAA,EAAc;AACvB,MAAA,MAAM,EAAE,KAAA,EAAM,GAAI,MAAM,IAAI,IAAA,EAAK;AACjC,MAAA,IAAI,CAAC,KAAA,IAAS,OAAO,KAAA,KAAU,QAAA,EAAU;AACvC,QAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,KAAA,EAAO,eAAA,EAAiB,CAAA,EAAG;AAAA,UAC9D,MAAA,EAAQ,GAAA;AAAA,UACR,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAA;AAAmB,SAC/C,CAAA;AAAA,MACH;AACA,MAAA,MAAM,KAAA,GAAQ,CAAC,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI,KAAK,CAAA,CAAA,EAAI,QAAA,EAAU,UAAA,EAAY,cAAA,EAAgB,CAAA,QAAA,EAAW,MAAM,CAAA,CAAE,CAAA;AAC5F,MAAA,IAAI,MAAA,EAAQ,KAAA,CAAM,IAAA,CAAK,QAAQ,CAAA;AAC/B,MAAA,OAAO,IAAI,SAAS,IAAA,CAAK,SAAA,CAAU,EAAE,EAAA,EAAI,IAAA,EAAM,CAAA,EAAG;AAAA,QAChD,OAAA,EAAS;AAAA,UACP,cAAA,EAAgB,kBAAA;AAAA,UAChB,YAAA,EAAc,KAAA,C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AA,CAAc,EAAE,MAAA,EAAQ,CAAA;AACjC;AAYO,SAAS,eAAA,CAAgB,SAAiB,cAAA,EAAgB;AAG/D,EAAA,OAAO,CAAC,UAAA,GAAsC,EAAC,MAAO;AAAA,IACpD,GAAG;AAAA,GACL,CAAA;AACF","file":"next-handlers.js","sourcesContent":["/**\n * Server-side utilities for Next.js (App Router).\n *\n * Use this entry point in Server Components, Server Actions, and Route Handlers\n * to read the authenticated user's session token from the httpOnly `__session` cookie.\n *\n * @example\n * ```ts\n * // lib/access-server.ts\n * import { cookies } from \"next/headers\";\n * import { createServerAccess } from \"azirid-react/server\";\n * export const { getSessionToken } = createServerAccess({ cookies });\n * ```\n *\n * ```ts\n * // app/actions/profile.ts\n * \"use server\";\n * import { getSessionToken } from \"@/lib/access-server\";\n *\n * export async function getProfile() {\n * const token = await getSessionToken();\n * if (!token) throw new Error(\"Not authenticated\");\n *\n * const res = await fetch(\"https://api.myapp.com/v1/users/auth/me\", {\n * headers: { Authorization: `Bearer ${token}` },\n * });\n * return res.json();\n * }\n * ```\n *\n * @packageDocumentation\n */\n\n/** Minimal cookie store interface (subset of Next.js ReadonlyRequestCookies) */\ninterface CookieStore {\n get(name: string): { value: string } | undefined\n}\n\n/** The `cookies` function from `next/headers` */\ntype CookiesFn = () => CookieStore | Promise<CookieStore>\n\nexport interface ServerAccessOptions {\n /**\n * The `cookies` function imported from `next/headers`.\n * Required — pass it explicitly to avoid bundler issues with dynamic imports.\n *\n * @example\n * ```ts\n * import { cookies } from \"next/headers\";\n * createServerAccess({ cookies });\n * ```\n */\n cookies: CookiesFn\n\n /**\n * Name of the httpOnly cookie that carries the access token.\n * Must match the cookie set by the Azirid API.\n * @default \"__session\"\n */\n cookieName?: string\n}\n\nexport interface ServerAccess {\n /**\n * Read the raw JWT access token from the session cookie.\n * Returns `null` when no cookie is present or when called\n * outside a Next.js server context.\n */\n getSessionToken: () => Promise<string | null>\n\n /** Alias for {@link getSessionToken}. */\n getAccessToken: () => Promise<string | null>\n}\n\n/**\n * Create server-side helpers bound to the session cookie.\n *\n * Call once at module scope, then use the returned functions\n * in any Server Component, Server Action, or Route Handler.\n */\nexport function createServerAccess(options: ServerAccessOptions): ServerAccess {\n const { cookies, cookieName = '__session' } = options\n\n async function getSessionToken(): Promise<string | null> {\n try {\n const result = cookies()\n // Next.js 15+: cookies() returns a Promise\n const cookieStore = result instanceof Promise ? await result : result\n return cookieStore.get(cookieName)?.value ?? null\n } catch {\n return null\n }\n }\n\n return {\n getSessionToken,\n getAccessToken: getSessionToken,\n }\n}\n\n// ─── Session Sync Handler (cross-origin dev support) ─────────\n\nexport interface SessionSyncOptions {\n /** Cookie name (default: \"__session\") */\n cookieName?: string\n /** Set Secure flag on cookie (default: false) */\n secure?: boolean\n /** Cookie max age in seconds (default: 3600 = 1h) */\n maxAge?: number\n}\n\n/**\n * Create a route handler that syncs the access token to a local httpOnly cookie.\n *\n * Use this in a Next.js App Router route to bridge cross-origin sessions:\n *\n * @example\n * ```ts\n * // app/api/auth/session/route.ts\n * import { createSessionSyncHandler } from \"azirid-react/server\";\n * export const { POST, DELETE } = createSessionSyncHandler();\n * ```\n */\nexport function createSessionSyncHandler(options?: SessionSyncOptions) {\n const name = options?.cookieName ?? '__session'\n const secure = options?.secure ?? false\n const maxAge = options?.maxAge ?? 3600\n\n return {\n async POST(req: Request) {\n const { token } = await req.json()\n if (!token || typeof token !== 'string') {\n return new Response(JSON.stringify({ error: 'Missing token' }), {\n status: 400,\n headers: { 'Content-Type': 'application/json' },\n })\n }\n const parts = [`${name}=${token}`, 'Path=/', 'HttpOnly', 'SameSite=Lax', `Max-Age=${maxAge}`]\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n async DELETE() {\n const parts = [`${name}=`, 'Path=/', 'HttpOnly', 'SameSite=Lax', 'Max-Age=0']\n if (secure) parts.push('Secure')\n return new Response(JSON.stringify({ ok: true }), {\n headers: {\n 'Content-Type': 'application/json',\n 'Set-Cookie': parts.join('; '),\n },\n })\n },\n }\n}\n","/**\n * Next.js route handlers for the Azirid auth proxy.\n *\n * This entry point uses **only standard Web APIs** (Request, Response, fetch)\n * and has **zero dependency on `next/server`**. This makes it safe to use in\n * Next.js standalone builds (`output: 'standalone'`) where external `next/server`\n * imports may fail at runtime.\n *\n * @example Route handler (simplest — one line)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n *\n * @example Route handler (custom API URL)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n *\n * @packageDocumentation\n */\n\n// ─── Constants ───────────────────────────────────────────────\n\n/**\n * Resolve the Azirid API URL.\n *\n * Priority:\n * 1. `AZIRID_API_URL` env var (server-only, recommended)\n * 2. `https://api.azirid.com` (production default)\n *\n * For local development, set `AZIRID_API_URL=http://localhost:3000` in your `.env`.\n */\nconst AZIRID_API_URL = process.env.AZIRID_API_URL ?? 'https://api.azirid.com'\n\n// ─── Types ───────────────────────────────────────────────────\n\ntype RouteHandler = (\n request: Request,\n context: { params: Promise<{ path: string[] }> | { path: string[] } },\n) => Promise<Response>\n\nexport interface AziridRouteHandlerOptions {\n /** Override the Azirid API URL (default: production API) */\n apiUrl?: string\n /** Enable debug logging of proxy requests (default: false) */\n debug?: boolean\n}\n\n// ─── Proxy Route Handler (core logic) ────────────────────────\n\nfunction createHandler(options?: AziridRouteHandlerOptions): RouteHandler {\n const destination = (options?.apiUrl ?? AZIRID_API_URL).replace(/\\/$/, '')\n const debug = options?.debug ?? false\n\n return async function handler(request, context) {\n // Handle Next.js 14 (sync params), 15 (async params), and 16+ (always async)\n const resolvedParams = context.params instanceof Promise ? await context.params : context.params\n const path = resolvedParams.path.join('/')\n const targetUrl = `${destination}/v1/users/auth/${path}`\n\n if (debug) {\n console.log(`[azirid-proxy] ${request.method} /api/auth/${path} → ${targetUrl}`)\n console.log(`[azirid-proxy] Cookies:`, request.headers.get('cookie') ?? '(none)')\n }\n\n // Forward request headers (strip host so the API sees its own host)\n const forwardHeaders = new Headers()\n request.headers.forEach((value, key) => {\n const lower = key.toLowerCase()\n if (lower !== 'host' && lower !== 'connection') {\n forwardHeaders.set(key, value)\n }\n })\n\n // Forward body for non-GET methods\n const body =\n request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined\n\n // Proxy request to the Azirid API\n const apiResponse = await fetch(targetUrl, {\n method: request.method,\n headers: forwardHeaders,\n body,\n })\n\n // Build response headers, fixing Set-Cookie for first-party context\n const responseHeaders = new Headers()\n apiResponse.headers.forEach((value, key) => {\n if (key.toLowerCase() !== 'set-cookie') {\n responseHeaders.set(key, value)\n }\n })\n\n // Fix Set-Cookie headers: strip Domain, adjust SameSite/Secure for proxy\n const isSecure =\n request.headers.get('x-forwarded-proto') === 'https' ||\n new URL(request.url).protocol === 'https:'\n\n const setCookies: string[] =\n typeof apiResponse.headers.getSetCookie === 'function'\n ? apiResponse.headers.getSetCookie()\n : splitSetCookieHeader(apiResponse.headers.get('set-cookie') ?? '')\n\n for (const cookie of setCookies) {\n let fixed = cookie\n // Strip Domain — cookie will default to the proxy's domain (first-party)\n .replace(/;\\s*[Dd]omain=[^;]*/g, '')\n // Normalize Path to \"/\" — the API may set restrictive paths (e.g. /v1/users/auth/login)\n // which prevents cookies from being sent to other auth endpoints like /refresh or /bootstrap.\n // In proxy mode all auth cookies must be available on all routes.\n .replace(/;\\s*[Pp]ath=[^;]*/g, '')\n // Change SameSite=None to Lax (now same-origin, None is unnecessary)\n .replace(/[Ss]ame[Ss]ite=[Nn]one/g, 'SameSite=Lax')\n\n // Always set Path=/ for first-party proxy cookies\n fixed += '; Path=/'\n\n // In development (HTTP), remove Secure flag so cookies work on localhost\n if (!isSecure) {\n fixed = fixed.replace(/;\\s*[Ss]ecure/g, '')\n }\n\n responseHeaders.append('set-cookie', fixed)\n }\n\n if (debug) {\n console.log(`[azirid-proxy] API responded: ${apiResponse.status}`)\n const responseCookies: string[] = []\n responseHeaders.forEach((value, key) => {\n if (key.toLowerCase() === 'set-cookie') responseCookies.push(value)\n })\n if (responseCookies.length > 0) {\n console.log(`[azirid-proxy] Set-Cookie:`, responseCookies)\n }\n }\n\n return new Response(apiResponse.body, {\n status: apiResponse.status,\n statusText: apiResponse.statusText,\n headers: responseHeaders,\n })\n }\n}\n\n/** Split a raw Set-Cookie header string into individual cookies */\nfunction splitSetCookieHeader(header: string): string[] {\n if (!header) return []\n const cookies: string[] = []\n let current = ''\n for (const part of header.split(', ')) {\n if (current && /^[a-zA-Z0-9_\\-.]+=/.test(part)) {\n cookies.push(current)\n current = part\n } else {\n current = current ? `${current}, ${part}` : part\n }\n }\n if (current) cookies.push(current)\n return cookies\n}\n\n// ─── Route Handlers (ready to re-export) ─────────────────────\n\n/**\n * Create all route handlers for the Azirid auth proxy.\n *\n * Returns `{ GET, POST, PUT, PATCH, DELETE }` ready to be re-exported\n * from your `app/api/auth/[...path]/route.ts`.\n *\n * @example Default (zero config)\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers();\n * ```\n *\n * @example Custom options\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * import { createAziridRouteHandlers } from \"azirid-react/next/handlers\";\n * export const { GET, POST, PUT, PATCH, DELETE } = createAziridRouteHandlers({\n * apiUrl: \"https://my-custom-api.com\",\n * debug: true,\n * });\n * ```\n */\nexport function createAziridRouteHandlers(options?: AziridRouteHandlerOptions): {\n GET: RouteHandler\n POST: RouteHandler\n PUT: RouteHandler\n PATCH: RouteHandler\n DELETE: RouteHandler\n} {\n const handler = createHandler(options)\n return {\n GET: handler,\n POST: handler,\n PUT: handler,\n PATCH: handler,\n DELETE: handler,\n }\n}\n\n/**\n * Pre-built route handlers with default config.\n *\n * Just re-export these directly — no setup required:\n *\n * @example\n * ```ts\n * // app/api/auth/[...path]/route.ts\n * export { GET, POST, PUT, PATCH, DELETE } from \"azirid-react/next/handlers\";\n * ```\n */\nconst defaultHandlers = createAziridRouteHandlers()\nexport const GET = defaultHandlers.GET\nexport const POST = defaultHandlers.POST\nexport const PUT = defaultHandlers.PUT\nexport const PATCH = defaultHandlers.PATCH\nexport const DELETE = defaultHandlers.DELETE\n\n/**\n * Create a single Next.js route handler function.\n *\n * Use this if you need a single handler reference (advanced use).\n * For most cases, prefer `createAziridRouteHandlers()` or the pre-built exports.\n *\n * @deprecated Use `createAziridRouteHandlers()` instead.\n */\nexport function createAziridProxyHandler(apiUrl: string = AZIRID_API_URL): RouteHandler {\n return createHandler({ apiUrl })\n}\n\n// ─── Config Helper ───────────────────────────────────────────\n\n/**\n * Wrap your Next.js config with Azirid settings.\n *\n * In Next.js 16+ with Turbopack, `transpilePackages` is no longer needed.\n * This helper is kept for backward compatibility with Next.js 14/15.\n *\n * @param apiUrl - Override the Azirid API URL (default: production API)\n */\nexport function withAziridProxy(apiUrl: string = AZIRID_API_URL) {\n void apiUrl\n\n return (nextConfig: Record<string, unknown> = {}) => ({\n ...nextConfig,\n })\n}\n\n// ─── Re-export session sync for backward compat ──────────────\n\nexport { createSessionSyncHandler } from './server'\nexport type { SessionSyncOptions } from './server'\n"]}
|