singularity-core 0.0.0-stage → 0.5.2
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 +15 -2
- package/dist/api/index.d.ts +14 -0
- package/dist/api/index.js +38 -0
- package/dist/api/server.d.ts +24 -0
- package/dist/api/server.js +84 -0
- package/dist/api/v1.d.ts +2180 -0
- package/dist/api/v1.js +5 -0
- package/dist/auth/index.d.ts +14 -0
- package/dist/auth/index.js +7 -0
- package/dist/auth/roles.d.ts +32 -0
- package/dist/auth/roles.js +54 -0
- package/dist/auth/server.d.ts +57 -0
- package/dist/auth/server.js +191 -0
- package/dist/format/index.d.ts +11 -0
- package/dist/format/index.js +54 -0
- package/dist/http/index.d.ts +12 -0
- package/dist/http/index.js +28 -0
- package/dist/images/index.d.ts +42 -0
- package/dist/images/index.js +90 -0
- package/dist/paging/index.d.ts +38 -0
- package/dist/paging/index.js +42 -0
- package/dist/paging/react.d.ts +6 -0
- package/dist/paging/react.js +20 -0
- package/dist/schema/index.d.ts +15 -0
- package/dist/schema/index.js +24 -0
- package/dist/security/index.d.ts +45 -0
- package/dist/security/index.js +89 -0
- package/dist/seo/index.d.ts +38 -0
- package/dist/seo/index.js +39 -0
- package/dist/telemetry/index.d.ts +47 -0
- package/dist/telemetry/index.js +54 -0
- package/dist/theme/index.d.ts +34 -0
- package/dist/theme/index.js +46 -0
- package/dist/theme/react.d.ts +24 -0
- package/dist/theme/react.js +61 -0
- package/dist/theme/server.d.ts +10 -0
- package/dist/theme/server.js +25 -0
- package/package.json +123 -3
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# singularity-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared non-UI code for Singularity TanStack Start apps: cookie-based auth
|
|
4
|
+
sessions against the Singularity API, openapi-fetch middleware, API error
|
|
5
|
+
helpers, paging and theme preference.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// src/utils/auth/auth.server.ts
|
|
9
|
+
import { createCookieSession } from "singularity-core/auth/server";
|
|
10
|
+
import { fetchAuthServerClient } from "@/lib/api/fetch.server";
|
|
11
|
+
|
|
12
|
+
export const { login, logout, getValidSession, hasUserPermission } =
|
|
13
|
+
createCookieSession({ client: fetchAuthServerClient, cookiePrefix: "filion" });
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
See [AGENTS.md](./AGENTS.md) for the subpaths and the rules for adding code.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws when an openapi-fetch response contains an error payload.
|
|
3
|
+
*/
|
|
4
|
+
export declare function throwIfApiError(response: {
|
|
5
|
+
error?: unknown;
|
|
6
|
+
}): void;
|
|
7
|
+
/**
|
|
8
|
+
* Extracts a user-facing message from an openapi-fetch or server function error.
|
|
9
|
+
*/
|
|
10
|
+
export declare function getApiErrorMessage(error: unknown, fallback?: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Returns true when the error represents an HTTP 409 conflict (entity in use).
|
|
13
|
+
*/
|
|
14
|
+
export declare function isConflictError(error: unknown): boolean;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws when an openapi-fetch response contains an error payload.
|
|
3
|
+
*/
|
|
4
|
+
export function throwIfApiError(response) {
|
|
5
|
+
if (response.error) {
|
|
6
|
+
throw response.error;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Extracts a user-facing message from an openapi-fetch or server function error.
|
|
11
|
+
*/
|
|
12
|
+
export function getApiErrorMessage(error, fallback = "Something went wrong") {
|
|
13
|
+
if (error instanceof Error && error.message) {
|
|
14
|
+
return error.message;
|
|
15
|
+
}
|
|
16
|
+
if (typeof error === "object" && error !== null) {
|
|
17
|
+
const problem = error;
|
|
18
|
+
if (typeof problem.error === "string" && problem.error.length > 0) {
|
|
19
|
+
return problem.error;
|
|
20
|
+
}
|
|
21
|
+
if (typeof problem.detail === "string" && problem.detail.length > 0) {
|
|
22
|
+
return problem.detail;
|
|
23
|
+
}
|
|
24
|
+
if (typeof problem.title === "string" && problem.title.length > 0) {
|
|
25
|
+
return problem.title;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return fallback;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns true when the error represents an HTTP 409 conflict (entity in use).
|
|
32
|
+
*/
|
|
33
|
+
export function isConflictError(error) {
|
|
34
|
+
if (typeof error === "object" && error !== null && "status" in error) {
|
|
35
|
+
return error.status === 409;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Middleware } from "openapi-fetch";
|
|
2
|
+
import { visitorIpFromHeaders } from "../http/index.js";
|
|
3
|
+
export { visitorIpFromHeaders };
|
|
4
|
+
/** Logs non-2xx API responses with their body. */
|
|
5
|
+
export declare const errorMiddleware: Middleware;
|
|
6
|
+
/** Sends `Authorization: Bearer <token>` when `getToken` returns one. */
|
|
7
|
+
export declare const createAuthMiddleware: (getToken: () => string | null | Promise<string | null>) => Middleware;
|
|
8
|
+
/**
|
|
9
|
+
* Passes the visitor's IP to the API, which rate limits per client. Without it
|
|
10
|
+
* every SSR call would share this server's one budget.
|
|
11
|
+
*/
|
|
12
|
+
export declare const forwardVisitorIpMiddleware: Middleware;
|
|
13
|
+
/**
|
|
14
|
+
* Forwards a browser POST to an API event-stream endpoint and streams the
|
|
15
|
+
* answer back, so the API key stays on the server. Passes the visitor's IP,
|
|
16
|
+
* since the API rate limits per visitor, and aborts upstream when the browser
|
|
17
|
+
* disconnects.
|
|
18
|
+
*/
|
|
19
|
+
export declare function proxyEventStream({ request, url, apiKey, failureMessage, }: {
|
|
20
|
+
request: Request;
|
|
21
|
+
url: URL | string;
|
|
22
|
+
apiKey: string;
|
|
23
|
+
failureMessage?: string;
|
|
24
|
+
}): Promise<Response>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { getRequestHeader } from "@tanstack/react-start/server";
|
|
2
|
+
import { visitorIpFromHeaders } from "../http/index.js";
|
|
3
|
+
export { visitorIpFromHeaders };
|
|
4
|
+
/** Logs non-2xx API responses with their body. */
|
|
5
|
+
export const errorMiddleware = {
|
|
6
|
+
async onResponse({ response }) {
|
|
7
|
+
if (!response.ok) {
|
|
8
|
+
const body = await response
|
|
9
|
+
.clone()
|
|
10
|
+
.json()
|
|
11
|
+
.catch(() => null);
|
|
12
|
+
console.error("API error", response.status, response.statusText, body);
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
/** Sends `Authorization: Bearer <token>` when `getToken` returns one. */
|
|
17
|
+
export const createAuthMiddleware = (getToken) => ({
|
|
18
|
+
async onRequest({ request }) {
|
|
19
|
+
try {
|
|
20
|
+
const token = await getToken();
|
|
21
|
+
if (token) {
|
|
22
|
+
request.headers.set("Authorization", `Bearer ${token}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
// ignore
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* Passes the visitor's IP to the API, which rate limits per client. Without it
|
|
32
|
+
* every SSR call would share this server's one budget.
|
|
33
|
+
*/
|
|
34
|
+
export const forwardVisitorIpMiddleware = {
|
|
35
|
+
onRequest({ request }) {
|
|
36
|
+
let ip;
|
|
37
|
+
try {
|
|
38
|
+
ip = visitorIpFromHeaders(getRequestHeader);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// Outside a request: nothing to forward.
|
|
42
|
+
}
|
|
43
|
+
if (ip) {
|
|
44
|
+
request.headers.set("X-Forwarded-For", ip);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
const eventStreamHeaders = {
|
|
49
|
+
"Content-Type": "text/event-stream",
|
|
50
|
+
"Cache-Control": "no-cache, no-transform",
|
|
51
|
+
Connection: "keep-alive",
|
|
52
|
+
// nginx would otherwise buffer the stream.
|
|
53
|
+
"X-Accel-Buffering": "no",
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Forwards a browser POST to an API event-stream endpoint and streams the
|
|
57
|
+
* answer back, so the API key stays on the server. Passes the visitor's IP,
|
|
58
|
+
* since the API rate limits per visitor, and aborts upstream when the browser
|
|
59
|
+
* disconnects.
|
|
60
|
+
*/
|
|
61
|
+
export async function proxyEventStream({ request, url, apiKey, failureMessage = "Request failed. Try again.", }) {
|
|
62
|
+
const headers = {
|
|
63
|
+
"Content-Type": "application/json",
|
|
64
|
+
Accept: "text/event-stream",
|
|
65
|
+
"X-API-Key": apiKey,
|
|
66
|
+
};
|
|
67
|
+
const visitorIp = visitorIpFromHeaders((name) => request.headers.get(name));
|
|
68
|
+
if (visitorIp) {
|
|
69
|
+
headers["X-Forwarded-For"] = visitorIp;
|
|
70
|
+
}
|
|
71
|
+
const upstream = await fetch(url, {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers,
|
|
74
|
+
body: await request.text(),
|
|
75
|
+
signal: request.signal,
|
|
76
|
+
});
|
|
77
|
+
if (!upstream.ok || !upstream.body) {
|
|
78
|
+
const error = await upstream.text().catch(() => "");
|
|
79
|
+
return new Response(error || failureMessage, {
|
|
80
|
+
status: upstream.status || 502,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return new Response(upstream.body, { headers: eventStreamHeaders });
|
|
84
|
+
}
|