postgresai 0.14.0-dev.38 → 0.14.0-dev.40

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.
@@ -1,177 +0,0 @@
1
- import * as https from "https";
2
- import { URL } from "url";
3
- import { normalizeBaseUrl } from "./util";
4
-
5
- export class RpcError extends Error {
6
- rpcName: string;
7
- statusCode: number;
8
- payloadText: string;
9
- payloadJson: any | null;
10
-
11
- constructor(params: { rpcName: string; statusCode: number; payloadText: string; payloadJson: any | null }) {
12
- const { rpcName, statusCode, payloadText, payloadJson } = params;
13
- super(`RPC ${rpcName} failed: HTTP ${statusCode}`);
14
- this.name = "RpcError";
15
- this.rpcName = rpcName;
16
- this.statusCode = statusCode;
17
- this.payloadText = payloadText;
18
- this.payloadJson = payloadJson;
19
- }
20
- }
21
-
22
- export function formatRpcErrorForDisplay(err: RpcError): string[] {
23
- const lines: string[] = [];
24
- lines.push(`Error: RPC ${err.rpcName} failed: HTTP ${err.statusCode}`);
25
-
26
- const obj = err.payloadJson && typeof err.payloadJson === "object" ? err.payloadJson : null;
27
- const details = obj && typeof (obj as any).details === "string" ? (obj as any).details : "";
28
- const hint = obj && typeof (obj as any).hint === "string" ? (obj as any).hint : "";
29
- const message = obj && typeof (obj as any).message === "string" ? (obj as any).message : "";
30
-
31
- if (message) lines.push(`Message: ${message}`);
32
- if (details) lines.push(`Details: ${details}`);
33
- if (hint) lines.push(`Hint: ${hint}`);
34
-
35
- // Fallback to raw payload if we couldn't extract anything useful.
36
- if (!message && !details && !hint) {
37
- const t = (err.payloadText || "").trim();
38
- if (t) lines.push(t);
39
- }
40
- return lines;
41
- }
42
-
43
- function unwrapRpcResponse(parsed: unknown): any {
44
- // Some deployments return a plain object, others return an array of rows,
45
- // and some wrap OUT params under a "result" key.
46
- if (Array.isArray(parsed)) {
47
- if (parsed.length === 1) return unwrapRpcResponse(parsed[0]);
48
- return parsed;
49
- }
50
- if (parsed && typeof parsed === "object") {
51
- const obj = parsed as any;
52
- if (obj.result !== undefined) return obj.result;
53
- }
54
- return parsed as any;
55
- }
56
-
57
- async function postRpc<T>(params: {
58
- apiKey: string;
59
- apiBaseUrl: string;
60
- rpcName: string;
61
- bodyObj: Record<string, unknown>;
62
- }): Promise<T> {
63
- const { apiKey, apiBaseUrl, rpcName, bodyObj } = params;
64
- if (!apiKey) throw new Error("API key is required");
65
- const base = normalizeBaseUrl(apiBaseUrl);
66
- const url = new URL(`${base}/rpc/${rpcName}`);
67
- const body = JSON.stringify(bodyObj);
68
-
69
- const headers: Record<string, string> = {
70
- // The backend RPC functions accept access_token in body, but we also set the header
71
- // for compatibility with other endpoints and deployments.
72
- "access-token": apiKey,
73
- "Prefer": "return=representation",
74
- "Content-Type": "application/json",
75
- "Content-Length": Buffer.byteLength(body).toString(),
76
- };
77
-
78
- return new Promise((resolve, reject) => {
79
- const req = https.request(
80
- url,
81
- {
82
- method: "POST",
83
- headers,
84
- },
85
- (res) => {
86
- let data = "";
87
- res.on("data", (chunk) => (data += chunk));
88
- res.on("end", () => {
89
- if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
90
- try {
91
- const parsed = JSON.parse(data);
92
- resolve(unwrapRpcResponse(parsed) as T);
93
- } catch {
94
- reject(new Error(`Failed to parse RPC response: ${data}`));
95
- }
96
- } else {
97
- const statusCode = res.statusCode || 0;
98
- let payloadJson: any | null = null;
99
- if (data) {
100
- try {
101
- payloadJson = JSON.parse(data);
102
- } catch {
103
- payloadJson = null;
104
- }
105
- }
106
- reject(new RpcError({ rpcName, statusCode, payloadText: data, payloadJson }));
107
- }
108
- });
109
- }
110
- );
111
- req.on("error", (err: Error) => reject(err));
112
- req.write(body);
113
- req.end();
114
- });
115
- }
116
-
117
- export async function createCheckupReport(params: {
118
- apiKey: string;
119
- apiBaseUrl: string;
120
- project: string;
121
- epoch: number;
122
- status?: string;
123
- }): Promise<{ reportId: number }> {
124
- const { apiKey, apiBaseUrl, project, epoch, status } = params;
125
- const bodyObj: Record<string, unknown> = {
126
- access_token: apiKey,
127
- project,
128
- epoch,
129
- };
130
- if (status) bodyObj.status = status;
131
-
132
- const resp = await postRpc<any>({
133
- apiKey,
134
- apiBaseUrl,
135
- rpcName: "checkup_report_create",
136
- bodyObj,
137
- });
138
- const reportId = Number(resp?.report_id);
139
- if (!Number.isFinite(reportId) || reportId <= 0) {
140
- throw new Error(`Unexpected checkup_report_create response: ${JSON.stringify(resp)}`);
141
- }
142
- return { reportId };
143
- }
144
-
145
- export async function uploadCheckupReportJson(params: {
146
- apiKey: string;
147
- apiBaseUrl: string;
148
- reportId: number;
149
- filename: string;
150
- checkId: string;
151
- jsonText: string;
152
- }): Promise<{ reportChunkId: number }> {
153
- const { apiKey, apiBaseUrl, reportId, filename, checkId, jsonText } = params;
154
- const bodyObj: Record<string, unknown> = {
155
- access_token: apiKey,
156
- checkup_report_id: reportId,
157
- filename,
158
- check_id: checkId,
159
- data: jsonText,
160
- type: "json",
161
- generate_issue: true,
162
- };
163
-
164
- const resp = await postRpc<any>({
165
- apiKey,
166
- apiBaseUrl,
167
- rpcName: "checkup_report_file_post",
168
- bodyObj,
169
- });
170
- const chunkId = Number(resp?.report_chunck_id ?? resp?.report_chunk_id);
171
- if (!Number.isFinite(chunkId) || chunkId <= 0) {
172
- throw new Error(`Unexpected checkup_report_file_post response: ${JSON.stringify(resp)}`);
173
- }
174
- return { reportChunkId: chunkId };
175
- }
176
-
177
-