@enfyra/sdk-nuxt 0.7.4 → 0.7.6
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 +19 -1
- package/dist/module.cjs +3 -5
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +3 -5
- package/dist/runtime/server/api/all.d.ts.map +1 -1
- package/dist/runtime/server/api/all.js +24 -4
- package/dist/runtime/server/plugins/socket-relay.d.ts +3 -0
- package/dist/runtime/server/plugins/socket-relay.d.ts.map +1 -0
- package/dist/runtime/server/plugins/socket-relay.js +69 -0
- package/package.json +6 -2
- package/src/module.ts +4 -6
- package/src/runtime/server/api/all.ts +26 -4
- package/src/runtime/server/plugins/socket-relay.ts +83 -0
- package/dist/runtime/server/api/auth/callback.get.d.ts +0 -3
- package/dist/runtime/server/api/auth/callback.get.d.ts.map +0 -1
- package/dist/runtime/server/api/auth/callback.get.js +0 -29
- package/src/runtime/server/api/auth/callback.get.ts +0 -36
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ Nuxt SDK for Enfyra CMS - A lightweight composable-based API client with full Ty
|
|
|
7
7
|
✅ **Simple & Flexible** - Get base URL and build your own composables
|
|
8
8
|
✅ **Authentication Integration** - Built-in auth composables with automatic header forwarding
|
|
9
9
|
✅ **Asset Proxy** - Automatic `/assets/**` proxy to backend with no configuration needed
|
|
10
|
+
✅ **WebSocket Relay** - Relay Socket.IO through Nuxt at `/ws/:path` (keeps backend hidden)
|
|
10
11
|
✅ **TypeScript Support** - Full type safety with auto-generated declarations
|
|
11
12
|
✅ **SSR Ready** - Works seamlessly with Nuxt's `useFetch` and `$fetch`
|
|
12
13
|
|
|
@@ -43,10 +44,27 @@ The SDK automatically detects your application URL:
|
|
|
43
44
|
|
|
44
45
|
```typescript
|
|
45
46
|
// Get the base URL for your API requests
|
|
46
|
-
const { baseUrl, apiPrefix } = useEnfyra();
|
|
47
|
+
const { baseUrl, apiPrefix, wsBaseUrl, wsPath } = useEnfyra();
|
|
47
48
|
|
|
48
49
|
// baseUrl: "http://localhost:3001/enfyra/api"
|
|
49
50
|
// apiPrefix: "/enfyra/api"
|
|
51
|
+
// wsBaseUrl: "http://localhost:3001" (for WebSocket relay)
|
|
52
|
+
// wsPath: "/ws/socket.io" (engine path)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### WebSocket Relay
|
|
56
|
+
|
|
57
|
+
The SDK relays Socket.IO through Nuxt so the backend can stay non-public. Use `socket.io-client` with `wsBaseUrl` and `wsPath`:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { io } from 'socket.io-client';
|
|
61
|
+
|
|
62
|
+
const { wsBaseUrl, wsPath } = useEnfyra();
|
|
63
|
+
|
|
64
|
+
const socket = io(wsBaseUrl + '/chat', {
|
|
65
|
+
path: wsPath,
|
|
66
|
+
auth: { token }, // if gateway requireAuth
|
|
67
|
+
});
|
|
50
68
|
```
|
|
51
69
|
|
|
52
70
|
### Using with `useFetch` (SSR)
|
package/dist/module.cjs
CHANGED
|
@@ -98,11 +98,6 @@ declare module '#imports' {
|
|
|
98
98
|
handler: resolve("./runtime/server/api/logout.post"),
|
|
99
99
|
method: "post"
|
|
100
100
|
});
|
|
101
|
-
kit.addServerHandler({
|
|
102
|
-
route: `${apiPrefix}/auth/callback`,
|
|
103
|
-
handler: resolve("./runtime/server/api/auth/callback.get"),
|
|
104
|
-
method: "get"
|
|
105
|
-
});
|
|
106
101
|
kit.addServerHandler({
|
|
107
102
|
route: "/assets/**",
|
|
108
103
|
handler: resolve("./runtime/server/api/all")
|
|
@@ -111,6 +106,9 @@ declare module '#imports' {
|
|
|
111
106
|
route: `${apiPrefix}/**`,
|
|
112
107
|
handler: resolve("./runtime/server/api/all")
|
|
113
108
|
});
|
|
109
|
+
nuxt.options.nitro = nuxt.options.nitro || {};
|
|
110
|
+
nuxt.options.nitro.experimental = nuxt.options.nitro.experimental || {};
|
|
111
|
+
nuxt.options.nitro.experimental.websocket = true;
|
|
114
112
|
}
|
|
115
113
|
});
|
|
116
114
|
|
package/dist/module.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;;AAED,
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;;AAED,wBAiIG"}
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -95,11 +95,6 @@ declare module '#imports' {
|
|
|
95
95
|
handler: resolve("./runtime/server/api/logout.post"),
|
|
96
96
|
method: "post"
|
|
97
97
|
});
|
|
98
|
-
addServerHandler({
|
|
99
|
-
route: `${apiPrefix}/auth/callback`,
|
|
100
|
-
handler: resolve("./runtime/server/api/auth/callback.get"),
|
|
101
|
-
method: "get"
|
|
102
|
-
});
|
|
103
98
|
addServerHandler({
|
|
104
99
|
route: "/assets/**",
|
|
105
100
|
handler: resolve("./runtime/server/api/all")
|
|
@@ -108,6 +103,9 @@ declare module '#imports' {
|
|
|
108
103
|
route: `${apiPrefix}/**`,
|
|
109
104
|
handler: resolve("./runtime/server/api/all")
|
|
110
105
|
});
|
|
106
|
+
nuxt.options.nitro = nuxt.options.nitro || {};
|
|
107
|
+
nuxt.options.nitro.experimental = nuxt.options.nitro.experimental || {};
|
|
108
|
+
nuxt.options.nitro.experimental.websocket = true;
|
|
111
109
|
}
|
|
112
110
|
});
|
|
113
111
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"all.d.ts","sourceRoot":"","sources":["../../../../src/runtime/server/api/all.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"all.d.ts","sourceRoot":"","sources":["../../../../src/runtime/server/api/all.ts"],"names":[],"mappings":";AAsBA,wBAmDG"}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
setResponseHeaders
|
|
7
7
|
} from "h3";
|
|
8
8
|
import { useRuntimeConfig } from "#imports";
|
|
9
|
+
import { ENFYRA_API_PREFIX } from "../../constants/config.js";
|
|
9
10
|
import { proxyToAPI } from "../../utils/server/proxy.js";
|
|
10
11
|
const CORS_HEADERS = {
|
|
11
12
|
"Access-Control-Allow-Origin": "*",
|
|
@@ -14,13 +15,33 @@ const CORS_HEADERS = {
|
|
|
14
15
|
"Access-Control-Max-Age": "86400"
|
|
15
16
|
};
|
|
16
17
|
const OAUTH_PROVIDERS = ["google", "facebook", "github"];
|
|
17
|
-
const OAUTH_INIT_PATTERN = new RegExp(`/auth/(${OAUTH_PROVIDERS.join("|")})
|
|
18
|
+
const OAUTH_INIT_PATTERN = new RegExp(`/auth/(${OAUTH_PROVIDERS.join("|")})/?$`);
|
|
19
|
+
const OAUTH_CALLBACK_PATTERN = new RegExp(`/auth/(${OAUTH_PROVIDERS.join("|")})/callback$`);
|
|
18
20
|
export default defineEventHandler(async (event) => {
|
|
19
21
|
setResponseHeaders(event, CORS_HEADERS);
|
|
20
22
|
if (event.method === "OPTIONS") {
|
|
21
23
|
return "";
|
|
22
24
|
}
|
|
23
|
-
const
|
|
25
|
+
const config = useRuntimeConfig();
|
|
26
|
+
const apiUrl = config.public?.enfyraSDK?.apiUrl;
|
|
27
|
+
const fullUrl = event.node?.req?.url || event.path || "";
|
|
28
|
+
const [path, queryString] = fullUrl.split("?");
|
|
29
|
+
const apiPrefix = config.public?.enfyraSDK?.apiPrefix || ENFYRA_API_PREFIX;
|
|
30
|
+
const pathWithoutPrefix = path.replace(new RegExp(`^${apiPrefix}`), "") || "/";
|
|
31
|
+
const oauthCallbackMatch = pathWithoutPrefix.match(OAUTH_CALLBACK_PATTERN);
|
|
32
|
+
if (event.method === "GET" && oauthCallbackMatch) {
|
|
33
|
+
if (!apiUrl) {
|
|
34
|
+
throw createError({ statusCode: 500, message: "API URL not configured" });
|
|
35
|
+
}
|
|
36
|
+
const backendPath = pathWithoutPrefix.startsWith("/") ? pathWithoutPrefix : `/${pathWithoutPrefix}`;
|
|
37
|
+
const backendUrl = `${apiUrl.replace(/\/+$/, "")}${backendPath}${queryString ? `?${queryString}` : ""}`;
|
|
38
|
+
const response = await fetch(backendUrl, { redirect: "manual" });
|
|
39
|
+
const location = response.headers.get("location") || response.headers.get("Location");
|
|
40
|
+
if (location && response.status >= 300 && response.status < 400) {
|
|
41
|
+
return sendRedirect(event, location, response.status);
|
|
42
|
+
}
|
|
43
|
+
throw createError({ statusCode: 502, message: "OAuth callback failed" });
|
|
44
|
+
}
|
|
24
45
|
const oauthMatch = path.match(OAUTH_INIT_PATTERN);
|
|
25
46
|
if (event.method === "GET" && oauthMatch) {
|
|
26
47
|
const provider = oauthMatch[1];
|
|
@@ -29,8 +50,6 @@ export default defineEventHandler(async (event) => {
|
|
|
29
50
|
if (!redirectParam) {
|
|
30
51
|
throw createError({ statusCode: 400, message: "Redirect URL is required" });
|
|
31
52
|
}
|
|
32
|
-
const config = useRuntimeConfig();
|
|
33
|
-
const apiUrl = config.public?.enfyraSDK?.apiUrl;
|
|
34
53
|
if (!apiUrl) {
|
|
35
54
|
throw createError({ statusCode: 500, message: "API URL not configured" });
|
|
36
55
|
}
|
|
@@ -38,6 +57,7 @@ export default defineEventHandler(async (event) => {
|
|
|
38
57
|
const response = await fetch(backendUrl, { redirect: "manual" });
|
|
39
58
|
const location = response.headers.get("location") || response.headers.get("Location");
|
|
40
59
|
if (location && response.status >= 300 && response.status < 400) {
|
|
60
|
+
console.log("[Enfyra OAuth] Redirecting to:", location);
|
|
41
61
|
return sendRedirect(event, location, 302);
|
|
42
62
|
}
|
|
43
63
|
throw createError({ statusCode: 502, message: "Failed to get OAuth URL from backend" });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"socket-relay.d.ts","sourceRoot":"","sources":["../../../../src/runtime/server/plugins/socket-relay.ts"],"names":[],"mappings":";AAUA,wBAwEG"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
2
|
+
import { Server as Engine } from "engine.io";
|
|
3
|
+
import { Server } from "socket.io";
|
|
4
|
+
import { io as ioClient } from "socket.io-client";
|
|
5
|
+
import { defineEventHandler } from "h3";
|
|
6
|
+
import { useRuntimeConfig } from "#imports";
|
|
7
|
+
const ENGINE_PATH = "/socket.io";
|
|
8
|
+
const ENGINE_PATH_SLASH = `${ENGINE_PATH}/`;
|
|
9
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
10
|
+
const config = useRuntimeConfig();
|
|
11
|
+
const apiUrl = config.public?.enfyraSDK?.apiUrl;
|
|
12
|
+
if (!apiUrl) return;
|
|
13
|
+
const backendUrl = String(apiUrl).replace(/\/+$/, "");
|
|
14
|
+
const engine = new Engine();
|
|
15
|
+
const io = new Server();
|
|
16
|
+
io.bind(engine);
|
|
17
|
+
io.on("connection", (clientSocket) => {
|
|
18
|
+
const namespace = clientSocket.nsp.name;
|
|
19
|
+
const backendNamespace = namespace === "/" || namespace === "" ? "/" : namespace;
|
|
20
|
+
const backendNsUrl = `${backendUrl}${backendNamespace}`;
|
|
21
|
+
const backendSocket = ioClient(backendNsUrl, {
|
|
22
|
+
path: "/socket.io",
|
|
23
|
+
auth: clientSocket.handshake.auth,
|
|
24
|
+
transports: ["websocket", "polling"]
|
|
25
|
+
});
|
|
26
|
+
clientSocket.onAny((event, ...args) => {
|
|
27
|
+
if (event !== "disconnect") {
|
|
28
|
+
backendSocket.emit(event, ...args);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
backendSocket.onAny((event, ...args) => {
|
|
32
|
+
clientSocket.emit(event, ...args);
|
|
33
|
+
});
|
|
34
|
+
clientSocket.on("disconnect", () => {
|
|
35
|
+
backendSocket.disconnect();
|
|
36
|
+
});
|
|
37
|
+
backendSocket.on("disconnect", () => {
|
|
38
|
+
clientSocket.disconnect();
|
|
39
|
+
});
|
|
40
|
+
backendSocket.on("connect_error", (err) => {
|
|
41
|
+
clientSocket.emit("connect_error", err.message);
|
|
42
|
+
clientSocket.disconnect();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
nitroApp.router.use(
|
|
46
|
+
`${ENGINE_PATH_SLASH}`,
|
|
47
|
+
defineEventHandler({
|
|
48
|
+
handler(event) {
|
|
49
|
+
const req = event.node.req;
|
|
50
|
+
const res = event.node.res;
|
|
51
|
+
engine.handleRequest(req, res);
|
|
52
|
+
event._handled = true;
|
|
53
|
+
},
|
|
54
|
+
websocket: {
|
|
55
|
+
open(peer) {
|
|
56
|
+
const nodeReq = peer._internal?.nodeReq;
|
|
57
|
+
if (nodeReq) {
|
|
58
|
+
engine.prepare(nodeReq);
|
|
59
|
+
engine.onWebSocket(
|
|
60
|
+
nodeReq,
|
|
61
|
+
nodeReq.socket,
|
|
62
|
+
peer.websocket
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enfyra/sdk-nuxt",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Nuxt SDK for Enfyra CMS",
|
|
6
6
|
"repository": {
|
|
@@ -55,13 +55,17 @@
|
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"@nuxt/kit": "^3.18.1",
|
|
58
|
+
"nitropack": ">=2.0.0",
|
|
58
59
|
"vue": "^3.0.0"
|
|
59
60
|
},
|
|
60
61
|
"dependencies": {
|
|
61
62
|
"defu": "^6.1.4",
|
|
63
|
+
"engine.io": "^6.6.3",
|
|
62
64
|
"h3": "^1.15.4",
|
|
63
65
|
"http-proxy": "^1.18.1",
|
|
64
|
-
"ofetch": "^1.3.3"
|
|
66
|
+
"ofetch": "^1.3.3",
|
|
67
|
+
"socket.io": "^4.8.3",
|
|
68
|
+
"socket.io-client": "^4.8.3"
|
|
65
69
|
},
|
|
66
70
|
"devDependencies": {
|
|
67
71
|
"@nuxt/module-builder": "^0.8.4",
|
package/src/module.ts
CHANGED
|
@@ -130,12 +130,6 @@ declare module '#imports' {
|
|
|
130
130
|
method: "post",
|
|
131
131
|
});
|
|
132
132
|
|
|
133
|
-
addServerHandler({
|
|
134
|
-
route: `${apiPrefix}/auth/callback`,
|
|
135
|
-
handler: resolve("./runtime/server/api/auth/callback.get"),
|
|
136
|
-
method: "get",
|
|
137
|
-
});
|
|
138
|
-
|
|
139
133
|
addServerHandler({
|
|
140
134
|
route: "/assets/**",
|
|
141
135
|
handler: resolve("./runtime/server/api/all"),
|
|
@@ -145,5 +139,9 @@ declare module '#imports' {
|
|
|
145
139
|
route: `${apiPrefix}/**`,
|
|
146
140
|
handler: resolve("./runtime/server/api/all"),
|
|
147
141
|
});
|
|
142
|
+
|
|
143
|
+
nuxt.options.nitro = nuxt.options.nitro || {};
|
|
144
|
+
nuxt.options.nitro.experimental = nuxt.options.nitro.experimental || {};
|
|
145
|
+
nuxt.options.nitro.experimental.websocket = true;
|
|
148
146
|
},
|
|
149
147
|
});
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
setResponseHeaders,
|
|
7
7
|
} from "h3";
|
|
8
8
|
import { useRuntimeConfig } from "#imports";
|
|
9
|
+
import { ENFYRA_API_PREFIX } from "../../constants/config";
|
|
9
10
|
import { proxyToAPI } from "../../utils/server/proxy";
|
|
10
11
|
|
|
11
12
|
const CORS_HEADERS = {
|
|
@@ -16,7 +17,8 @@ const CORS_HEADERS = {
|
|
|
16
17
|
};
|
|
17
18
|
|
|
18
19
|
const OAUTH_PROVIDERS = ["google", "facebook", "github"];
|
|
19
|
-
const OAUTH_INIT_PATTERN = new RegExp(`/auth/(${OAUTH_PROVIDERS.join("|")})
|
|
20
|
+
const OAUTH_INIT_PATTERN = new RegExp(`/auth/(${OAUTH_PROVIDERS.join("|")})/?$`);
|
|
21
|
+
const OAUTH_CALLBACK_PATTERN = new RegExp(`/auth/(${OAUTH_PROVIDERS.join("|")})/callback$`);
|
|
20
22
|
|
|
21
23
|
export default defineEventHandler(async (event) => {
|
|
22
24
|
setResponseHeaders(event, CORS_HEADERS);
|
|
@@ -25,7 +27,28 @@ export default defineEventHandler(async (event) => {
|
|
|
25
27
|
return '';
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
const
|
|
30
|
+
const config = useRuntimeConfig();
|
|
31
|
+
const apiUrl = config.public?.enfyraSDK?.apiUrl;
|
|
32
|
+
const fullUrl = event.node?.req?.url || event.path || "";
|
|
33
|
+
const [path, queryString] = fullUrl.split("?");
|
|
34
|
+
|
|
35
|
+
const apiPrefix = config.public?.enfyraSDK?.apiPrefix || ENFYRA_API_PREFIX;
|
|
36
|
+
const pathWithoutPrefix = path.replace(new RegExp(`^${apiPrefix}`), "") || "/";
|
|
37
|
+
const oauthCallbackMatch = pathWithoutPrefix.match(OAUTH_CALLBACK_PATTERN);
|
|
38
|
+
if (event.method === "GET" && oauthCallbackMatch) {
|
|
39
|
+
if (!apiUrl) {
|
|
40
|
+
throw createError({ statusCode: 500, message: "API URL not configured" });
|
|
41
|
+
}
|
|
42
|
+
const backendPath = pathWithoutPrefix.startsWith("/") ? pathWithoutPrefix : `/${pathWithoutPrefix}`;
|
|
43
|
+
const backendUrl = `${apiUrl.replace(/\/+$/, "")}${backendPath}${queryString ? `?${queryString}` : ""}`;
|
|
44
|
+
const response = await fetch(backendUrl, { redirect: "manual" });
|
|
45
|
+
const location = response.headers.get("location") || response.headers.get("Location");
|
|
46
|
+
if (location && response.status >= 300 && response.status < 400) {
|
|
47
|
+
return sendRedirect(event, location, response.status as 301 | 302 | 307 | 308);
|
|
48
|
+
}
|
|
49
|
+
throw createError({ statusCode: 502, message: "OAuth callback failed" });
|
|
50
|
+
}
|
|
51
|
+
|
|
29
52
|
const oauthMatch = path.match(OAUTH_INIT_PATTERN);
|
|
30
53
|
if (event.method === "GET" && oauthMatch) {
|
|
31
54
|
const provider = oauthMatch[1];
|
|
@@ -34,8 +57,6 @@ export default defineEventHandler(async (event) => {
|
|
|
34
57
|
if (!redirectParam) {
|
|
35
58
|
throw createError({ statusCode: 400, message: "Redirect URL is required" });
|
|
36
59
|
}
|
|
37
|
-
const config = useRuntimeConfig();
|
|
38
|
-
const apiUrl = config.public?.enfyraSDK?.apiUrl;
|
|
39
60
|
if (!apiUrl) {
|
|
40
61
|
throw createError({ statusCode: 500, message: "API URL not configured" });
|
|
41
62
|
}
|
|
@@ -43,6 +64,7 @@ export default defineEventHandler(async (event) => {
|
|
|
43
64
|
const response = await fetch(backendUrl, { redirect: "manual" });
|
|
44
65
|
const location = response.headers.get("location") || response.headers.get("Location");
|
|
45
66
|
if (location && response.status >= 300 && response.status < 400) {
|
|
67
|
+
console.log("[Enfyra OAuth] Redirecting to:", location);
|
|
46
68
|
return sendRedirect(event, location, 302);
|
|
47
69
|
}
|
|
48
70
|
throw createError({ statusCode: 502, message: "Failed to get OAuth URL from backend" });
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// @ts-nocheck - Nitro plugin
|
|
2
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
3
|
+
import { Server as Engine } from "engine.io";
|
|
4
|
+
import { Server } from "socket.io";
|
|
5
|
+
import { io as ioClient } from "socket.io-client";
|
|
6
|
+
import { defineEventHandler } from "h3";
|
|
7
|
+
import { useRuntimeConfig } from "#imports";
|
|
8
|
+
const ENGINE_PATH = "/socket.io";
|
|
9
|
+
const ENGINE_PATH_SLASH = `${ENGINE_PATH}/`;
|
|
10
|
+
|
|
11
|
+
export default defineNitroPlugin((nitroApp) => {
|
|
12
|
+
const config = useRuntimeConfig();
|
|
13
|
+
const apiUrl = config.public?.enfyraSDK?.apiUrl;
|
|
14
|
+
if (!apiUrl) return;
|
|
15
|
+
|
|
16
|
+
const backendUrl = String(apiUrl).replace(/\/+$/, "");
|
|
17
|
+
const engine = new Engine();
|
|
18
|
+
const io = new Server();
|
|
19
|
+
io.bind(engine);
|
|
20
|
+
|
|
21
|
+
io.on("connection", (clientSocket) => {
|
|
22
|
+
const namespace = clientSocket.nsp.name;
|
|
23
|
+
const backendNamespace =
|
|
24
|
+
namespace === "/" || namespace === "" ? "/" : namespace;
|
|
25
|
+
const backendNsUrl = `${backendUrl}${backendNamespace}`;
|
|
26
|
+
|
|
27
|
+
const backendSocket = ioClient(backendNsUrl, {
|
|
28
|
+
path: "/socket.io",
|
|
29
|
+
auth: clientSocket.handshake.auth,
|
|
30
|
+
transports: ["websocket", "polling"],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
clientSocket.onAny((event: string, ...args: unknown[]) => {
|
|
34
|
+
if (event !== "disconnect") {
|
|
35
|
+
backendSocket.emit(event, ...args);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
backendSocket.onAny((event: string, ...args: unknown[]) => {
|
|
40
|
+
clientSocket.emit(event, ...args);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
clientSocket.on("disconnect", () => {
|
|
44
|
+
backendSocket.disconnect();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
backendSocket.on("disconnect", () => {
|
|
48
|
+
clientSocket.disconnect();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
backendSocket.on("connect_error", (err: Error) => {
|
|
52
|
+
clientSocket.emit("connect_error", err.message);
|
|
53
|
+
clientSocket.disconnect();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
nitroApp.router.use(
|
|
58
|
+
`${ENGINE_PATH_SLASH}`,
|
|
59
|
+
defineEventHandler({
|
|
60
|
+
handler(event) {
|
|
61
|
+
const req = event.node.req as Parameters<InstanceType<typeof Engine>["handleRequest"]>[0];
|
|
62
|
+
const res = event.node.res as Parameters<InstanceType<typeof Engine>["handleRequest"]>[1];
|
|
63
|
+
engine.handleRequest(req, res);
|
|
64
|
+
event._handled = true;
|
|
65
|
+
},
|
|
66
|
+
websocket: {
|
|
67
|
+
open(peer) {
|
|
68
|
+
// Nitro/crossws peer with nodeReq for engine.io integration
|
|
69
|
+
const nodeReq = (peer as { _internal?: { nodeReq?: { socket?: unknown } } })._internal?.nodeReq;
|
|
70
|
+
if (nodeReq) {
|
|
71
|
+
(engine as unknown as { prepare(r: unknown): void; onWebSocket(a: unknown, b: unknown, c: WebSocket): void })
|
|
72
|
+
.prepare(nodeReq);
|
|
73
|
+
(engine as unknown as { onWebSocket(a: unknown, b: unknown, c: WebSocket): void }).onWebSocket(
|
|
74
|
+
nodeReq,
|
|
75
|
+
(nodeReq as { socket?: unknown }).socket,
|
|
76
|
+
peer.websocket,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"callback.get.d.ts","sourceRoot":"","sources":["../../../../../src/runtime/server/api/auth/callback.get.ts"],"names":[],"mappings":";AAaA,wBAsBG"}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
defineEventHandler,
|
|
3
|
-
getQuery,
|
|
4
|
-
setCookie,
|
|
5
|
-
sendRedirect
|
|
6
|
-
} from "h3";
|
|
7
|
-
import {
|
|
8
|
-
ACCESS_TOKEN_KEY,
|
|
9
|
-
REFRESH_TOKEN_KEY,
|
|
10
|
-
EXP_TIME_KEY
|
|
11
|
-
} from "../../../constants/auth.js";
|
|
12
|
-
export default defineEventHandler(async (event) => {
|
|
13
|
-
const query = getQuery(event);
|
|
14
|
-
const { accessToken, refreshToken, expTime, redirect } = query;
|
|
15
|
-
if (!accessToken || !refreshToken || !expTime) {
|
|
16
|
-
return sendRedirect(event, "/login?error=oauth_callback_failed");
|
|
17
|
-
}
|
|
18
|
-
const cookieOptions = {
|
|
19
|
-
httpOnly: true,
|
|
20
|
-
secure: true,
|
|
21
|
-
sameSite: "lax",
|
|
22
|
-
path: "/"
|
|
23
|
-
};
|
|
24
|
-
setCookie(event, ACCESS_TOKEN_KEY, accessToken, cookieOptions);
|
|
25
|
-
setCookie(event, REFRESH_TOKEN_KEY, refreshToken, cookieOptions);
|
|
26
|
-
setCookie(event, EXP_TIME_KEY, expTime, cookieOptions);
|
|
27
|
-
const redirectUrl = redirect || "/";
|
|
28
|
-
return sendRedirect(event, redirectUrl);
|
|
29
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
defineEventHandler,
|
|
3
|
-
getQuery,
|
|
4
|
-
setCookie,
|
|
5
|
-
sendRedirect,
|
|
6
|
-
} from "h3";
|
|
7
|
-
import { useRuntimeConfig } from "#imports";
|
|
8
|
-
import {
|
|
9
|
-
ACCESS_TOKEN_KEY,
|
|
10
|
-
REFRESH_TOKEN_KEY,
|
|
11
|
-
EXP_TIME_KEY,
|
|
12
|
-
} from "../../../constants/auth";
|
|
13
|
-
|
|
14
|
-
export default defineEventHandler(async (event) => {
|
|
15
|
-
const query = getQuery(event);
|
|
16
|
-
|
|
17
|
-
const { accessToken, refreshToken, expTime, redirect } = query;
|
|
18
|
-
|
|
19
|
-
if (!accessToken || !refreshToken || !expTime) {
|
|
20
|
-
return sendRedirect(event, "/login?error=oauth_callback_failed");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const cookieOptions = {
|
|
24
|
-
httpOnly: true,
|
|
25
|
-
secure: true,
|
|
26
|
-
sameSite: "lax" as const,
|
|
27
|
-
path: "/",
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
setCookie(event, ACCESS_TOKEN_KEY, accessToken as string, cookieOptions);
|
|
31
|
-
setCookie(event, REFRESH_TOKEN_KEY, refreshToken as string, cookieOptions);
|
|
32
|
-
setCookie(event, EXP_TIME_KEY, expTime as string, cookieOptions);
|
|
33
|
-
|
|
34
|
-
const redirectUrl = (redirect as string) || "/";
|
|
35
|
-
return sendRedirect(event, redirectUrl);
|
|
36
|
-
});
|