@archiva/archiva-nextjs 0.2.93 → 0.3.0
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/chunk-5ZJUGBTU.mjs +281 -0
- package/dist/chunk-A3Q4YKTK.mjs +268 -0
- package/dist/chunk-LOKBGU6S.mjs +88 -0
- package/dist/chunk-WA7TMG65.mjs +81 -0
- package/dist/index-Bk4DxULy.d.mts +139 -0
- package/dist/index-Bk4DxULy.d.ts +139 -0
- package/dist/index-DfJ2Gf0Y.d.mts +148 -0
- package/dist/index-DfJ2Gf0Y.d.ts +148 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +100 -15
- package/dist/index.mjs +2 -1
- package/dist/react/client.js +93 -18
- package/dist/react/client.mjs +11 -18
- package/dist/server/index.d.mts +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +100 -15
- package/dist/server/index.mjs +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeAuditEventItem
|
|
3
|
+
} from "./chunk-LOKBGU6S.mjs";
|
|
4
|
+
|
|
5
|
+
// src/types.ts
|
|
6
|
+
var ArchivaError = class extends Error {
|
|
7
|
+
constructor(params) {
|
|
8
|
+
super(params.message);
|
|
9
|
+
this.statusCode = params.statusCode;
|
|
10
|
+
this.code = params.code;
|
|
11
|
+
this.retryAfterSeconds = params.retryAfterSeconds;
|
|
12
|
+
this.details = params.details;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/client.ts
|
|
17
|
+
var DEFAULT_BASE_URL = "https://api.archiva.app";
|
|
18
|
+
function buildHeaders(apiKey, overrides) {
|
|
19
|
+
const headers = new Headers(overrides);
|
|
20
|
+
headers.set("X-Project-Key", apiKey);
|
|
21
|
+
return headers;
|
|
22
|
+
}
|
|
23
|
+
async function parseError(response) {
|
|
24
|
+
const retryAfterHeader = response.headers.get("Retry-After");
|
|
25
|
+
const retryAfterSeconds = retryAfterHeader ? Number(retryAfterHeader) : void 0;
|
|
26
|
+
let payload = void 0;
|
|
27
|
+
try {
|
|
28
|
+
payload = await response.json();
|
|
29
|
+
} catch {
|
|
30
|
+
payload = void 0;
|
|
31
|
+
}
|
|
32
|
+
const errorMessage = typeof payload === "object" && payload !== null && "error" in payload ? String(payload.error) : response.statusText;
|
|
33
|
+
let code = "HTTP_ERROR";
|
|
34
|
+
if (response.status === 401) {
|
|
35
|
+
code = "UNAUTHORIZED";
|
|
36
|
+
} else if (response.status === 403) {
|
|
37
|
+
code = "FORBIDDEN";
|
|
38
|
+
} else if (response.status === 413) {
|
|
39
|
+
code = "PAYLOAD_TOO_LARGE";
|
|
40
|
+
} else if (response.status === 429) {
|
|
41
|
+
code = "RATE_LIMITED";
|
|
42
|
+
} else if (response.status === 409) {
|
|
43
|
+
code = "IDEMPOTENCY_CONFLICT";
|
|
44
|
+
}
|
|
45
|
+
throw new ArchivaError({
|
|
46
|
+
statusCode: response.status,
|
|
47
|
+
code,
|
|
48
|
+
message: errorMessage,
|
|
49
|
+
retryAfterSeconds,
|
|
50
|
+
details: payload
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function createRequestId() {
|
|
54
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
55
|
+
return crypto.randomUUID();
|
|
56
|
+
}
|
|
57
|
+
return `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
|
|
58
|
+
}
|
|
59
|
+
function createIdempotencyKey() {
|
|
60
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
61
|
+
return `idem_${crypto.randomUUID()}`;
|
|
62
|
+
}
|
|
63
|
+
return `idem_${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
|
|
64
|
+
}
|
|
65
|
+
async function loadEvents(apiKey, params, baseUrl = DEFAULT_BASE_URL) {
|
|
66
|
+
const url = new URL(`${baseUrl}/api/events`);
|
|
67
|
+
if (params.entityId) {
|
|
68
|
+
url.searchParams.set("entityId", params.entityId);
|
|
69
|
+
}
|
|
70
|
+
if (params.actorId) {
|
|
71
|
+
url.searchParams.set("actorId", params.actorId);
|
|
72
|
+
}
|
|
73
|
+
if (params.entityType) {
|
|
74
|
+
url.searchParams.set("entityType", params.entityType);
|
|
75
|
+
}
|
|
76
|
+
if (params.actorType) {
|
|
77
|
+
if (Array.isArray(params.actorType)) {
|
|
78
|
+
url.searchParams.set("actorType", params.actorType.join(","));
|
|
79
|
+
} else {
|
|
80
|
+
url.searchParams.set("actorType", params.actorType);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (params.tenantId) {
|
|
84
|
+
url.searchParams.set("tenantId", params.tenantId);
|
|
85
|
+
}
|
|
86
|
+
if (params.actionKey) {
|
|
87
|
+
url.searchParams.set("actionKey", params.actionKey);
|
|
88
|
+
}
|
|
89
|
+
if (params.q) {
|
|
90
|
+
url.searchParams.set("q", params.q);
|
|
91
|
+
}
|
|
92
|
+
if (params.limit) {
|
|
93
|
+
url.searchParams.set("limit", String(params.limit));
|
|
94
|
+
}
|
|
95
|
+
if (params.cursor) {
|
|
96
|
+
url.searchParams.set("cursor", params.cursor);
|
|
97
|
+
}
|
|
98
|
+
const response = await fetch(url.toString(), {
|
|
99
|
+
headers: buildHeaders(apiKey)
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
await parseError(response);
|
|
103
|
+
}
|
|
104
|
+
const payload = await response.json();
|
|
105
|
+
if (!payload || typeof payload !== "object" || !Array.isArray(payload.items)) {
|
|
106
|
+
throw new ArchivaError({
|
|
107
|
+
statusCode: response.status,
|
|
108
|
+
code: "HTTP_ERROR",
|
|
109
|
+
message: "Invalid response format",
|
|
110
|
+
details: payload
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const items = payload.items.map((item) => {
|
|
114
|
+
if (typeof item !== "object" || item === null) {
|
|
115
|
+
throw new ArchivaError({
|
|
116
|
+
statusCode: response.status,
|
|
117
|
+
code: "HTTP_ERROR",
|
|
118
|
+
message: "Invalid item format in response",
|
|
119
|
+
details: item
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return normalizeAuditEventItem(item);
|
|
123
|
+
});
|
|
124
|
+
return {
|
|
125
|
+
items,
|
|
126
|
+
nextCursor: typeof payload.nextCursor === "string" ? payload.nextCursor : void 0
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
async function createEvent(apiKey, event, options, baseUrl = DEFAULT_BASE_URL) {
|
|
130
|
+
const idempotencyKey = options?.idempotencyKey ?? createIdempotencyKey();
|
|
131
|
+
const requestId = options?.requestId ?? createRequestId();
|
|
132
|
+
const headers = buildHeaders(apiKey, {
|
|
133
|
+
"Content-Type": "application/json",
|
|
134
|
+
"Idempotency-Key": idempotencyKey,
|
|
135
|
+
"X-Request-Id": requestId
|
|
136
|
+
});
|
|
137
|
+
const response = await fetch(`${baseUrl}/api/ingest/event`, {
|
|
138
|
+
method: "POST",
|
|
139
|
+
headers,
|
|
140
|
+
body: JSON.stringify(event)
|
|
141
|
+
});
|
|
142
|
+
if (!response.ok) {
|
|
143
|
+
await parseError(response);
|
|
144
|
+
}
|
|
145
|
+
const payload = await response.json();
|
|
146
|
+
if (!payload || typeof payload !== "object" || typeof payload.eventId !== "string") {
|
|
147
|
+
throw new ArchivaError({
|
|
148
|
+
statusCode: response.status,
|
|
149
|
+
code: "HTTP_ERROR",
|
|
150
|
+
message: "Invalid response format",
|
|
151
|
+
details: payload
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
eventId: payload.eventId,
|
|
156
|
+
replayed: response.status === 200
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
async function createEvents(apiKey, events, options, baseUrl = DEFAULT_BASE_URL) {
|
|
160
|
+
const results = await Promise.all(
|
|
161
|
+
events.map(
|
|
162
|
+
(event, index) => createEvent(
|
|
163
|
+
apiKey,
|
|
164
|
+
event,
|
|
165
|
+
{
|
|
166
|
+
...options,
|
|
167
|
+
idempotencyKey: options?.idempotencyKey ? `${options.idempotencyKey}_${index}` : void 0
|
|
168
|
+
},
|
|
169
|
+
baseUrl
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
);
|
|
173
|
+
return {
|
|
174
|
+
eventIds: results.map((r) => r.eventId)
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// src/actions.ts
|
|
179
|
+
var DEFAULT_BASE_URL2 = "https://api.archiva.app";
|
|
180
|
+
function getApiKey(apiKey) {
|
|
181
|
+
const resolvedKey = apiKey || process.env.ARCHIVA_SECRET_KEY;
|
|
182
|
+
if (!resolvedKey) {
|
|
183
|
+
throw new Error("ARCHIVA_SECRET_KEY environment variable is required, or provide apiKey prop to ArchivaProvider");
|
|
184
|
+
}
|
|
185
|
+
return resolvedKey;
|
|
186
|
+
}
|
|
187
|
+
async function loadEvents2(params, apiKey) {
|
|
188
|
+
const resolvedApiKey = getApiKey(apiKey);
|
|
189
|
+
return loadEvents(resolvedApiKey, params, DEFAULT_BASE_URL2);
|
|
190
|
+
}
|
|
191
|
+
async function createEvent2(event, options, apiKey) {
|
|
192
|
+
const resolvedApiKey = getApiKey(apiKey);
|
|
193
|
+
return createEvent(resolvedApiKey, event, options, DEFAULT_BASE_URL2);
|
|
194
|
+
}
|
|
195
|
+
async function createEvents2(events, options, apiKey) {
|
|
196
|
+
const resolvedApiKey = getApiKey(apiKey);
|
|
197
|
+
return createEvents(resolvedApiKey, events, options, DEFAULT_BASE_URL2);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/server/frontendTokens.ts
|
|
201
|
+
import "server-only";
|
|
202
|
+
var DEFAULT_API_BASE_URL = "https://api.archiva.app";
|
|
203
|
+
async function createFrontendTokenGET(projectId, apiBaseUrl = DEFAULT_API_BASE_URL) {
|
|
204
|
+
const secretKey = process.env.ARCHIVA_SECRET_KEY;
|
|
205
|
+
if (!secretKey || secretKey.trim().length === 0) {
|
|
206
|
+
const exists = process.env.ARCHIVA_SECRET_KEY !== void 0;
|
|
207
|
+
throw new Error(
|
|
208
|
+
`ARCHIVA_SECRET_KEY environment variable is ${exists ? "empty" : "not configured"}. Please set it in your .env.local file (or .env) with a valid value and restart your Next.js dev server. The variable must be in the project root directory and must not be empty.`
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
const keyPrefix = secretKey.substring(0, 10);
|
|
212
|
+
console.log("[Archiva] Using API key prefix:", keyPrefix + "...", "Length:", secretKey.length);
|
|
213
|
+
const url = new URL(`${apiBaseUrl}/api/v1/frontend-tokens`);
|
|
214
|
+
const response = await fetch(url.toString(), {
|
|
215
|
+
method: "POST",
|
|
216
|
+
headers: {
|
|
217
|
+
"Content-Type": "application/json",
|
|
218
|
+
"Authorization": `Bearer ${secretKey}`
|
|
219
|
+
},
|
|
220
|
+
body: JSON.stringify({
|
|
221
|
+
scopes: ["timeline:read"],
|
|
222
|
+
...projectId && { projectId }
|
|
223
|
+
})
|
|
224
|
+
});
|
|
225
|
+
if (!response.ok) {
|
|
226
|
+
const error = await response.json().catch(() => ({ error: response.statusText }));
|
|
227
|
+
const errorMessage = error.error || `Failed to fetch frontend token: ${response.status}`;
|
|
228
|
+
if (errorMessage.includes("ARCHIVA_SECRET_KEY") || response.status === 401 || response.status === 403) {
|
|
229
|
+
throw new Error(
|
|
230
|
+
`Archiva API authentication failed. Check that your ARCHIVA_SECRET_KEY is valid and has the correct permissions. API error: ${errorMessage}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
throw new Error(errorMessage);
|
|
234
|
+
}
|
|
235
|
+
const data = await response.json();
|
|
236
|
+
if (!data.token || typeof data.expiresAt !== "number") {
|
|
237
|
+
throw new Error("Invalid token response format");
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
token: data.token,
|
|
241
|
+
expiresAt: data.expiresAt
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// src/server/handlers/createFrontendTokenRoute.ts
|
|
246
|
+
import "server-only";
|
|
247
|
+
import { NextResponse } from "next/server";
|
|
248
|
+
var DEFAULT_API_BASE_URL2 = "https://api.archiva.app";
|
|
249
|
+
function createFrontendTokenRoute(options) {
|
|
250
|
+
return async function GET2(request) {
|
|
251
|
+
try {
|
|
252
|
+
const searchParams = request.nextUrl.searchParams;
|
|
253
|
+
const projectId = searchParams.get("projectId") || void 0;
|
|
254
|
+
const apiBaseUrl = options?.apiBaseUrl || DEFAULT_API_BASE_URL2;
|
|
255
|
+
const tokenResponse = await createFrontendTokenGET(projectId, apiBaseUrl);
|
|
256
|
+
return NextResponse.json(tokenResponse);
|
|
257
|
+
} catch (error) {
|
|
258
|
+
console.error("Error fetching frontend token:", error);
|
|
259
|
+
const message = error instanceof Error ? error.message : "Internal server error";
|
|
260
|
+
const statusCode = message.includes("ARCHIVA_SECRET_KEY") ? 500 : 500;
|
|
261
|
+
return NextResponse.json(
|
|
262
|
+
{ error: message },
|
|
263
|
+
{ status: statusCode }
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
async function GET(request) {
|
|
269
|
+
const handler = createFrontendTokenRoute();
|
|
270
|
+
return handler(request);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export {
|
|
274
|
+
ArchivaError,
|
|
275
|
+
loadEvents2 as loadEvents,
|
|
276
|
+
createEvent2 as createEvent,
|
|
277
|
+
createEvents2 as createEvents,
|
|
278
|
+
createFrontendTokenGET,
|
|
279
|
+
createFrontendTokenRoute,
|
|
280
|
+
GET
|
|
281
|
+
};
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeAuditEventItem
|
|
3
|
+
} from "./chunk-WA7TMG65.mjs";
|
|
4
|
+
|
|
5
|
+
// src/types.ts
|
|
6
|
+
var ArchivaError = class extends Error {
|
|
7
|
+
constructor(params) {
|
|
8
|
+
super(params.message);
|
|
9
|
+
this.statusCode = params.statusCode;
|
|
10
|
+
this.code = params.code;
|
|
11
|
+
this.retryAfterSeconds = params.retryAfterSeconds;
|
|
12
|
+
this.details = params.details;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/client.ts
|
|
17
|
+
var DEFAULT_BASE_URL = "https://api.archiva.app";
|
|
18
|
+
function buildHeaders(apiKey, overrides) {
|
|
19
|
+
const headers = new Headers(overrides);
|
|
20
|
+
headers.set("X-Project-Key", apiKey);
|
|
21
|
+
return headers;
|
|
22
|
+
}
|
|
23
|
+
async function parseError(response) {
|
|
24
|
+
const retryAfterHeader = response.headers.get("Retry-After");
|
|
25
|
+
const retryAfterSeconds = retryAfterHeader ? Number(retryAfterHeader) : void 0;
|
|
26
|
+
let payload = void 0;
|
|
27
|
+
try {
|
|
28
|
+
payload = await response.json();
|
|
29
|
+
} catch {
|
|
30
|
+
payload = void 0;
|
|
31
|
+
}
|
|
32
|
+
const errorMessage = typeof payload === "object" && payload !== null && "error" in payload ? String(payload.error) : response.statusText;
|
|
33
|
+
let code = "HTTP_ERROR";
|
|
34
|
+
if (response.status === 401) {
|
|
35
|
+
code = "UNAUTHORIZED";
|
|
36
|
+
} else if (response.status === 403) {
|
|
37
|
+
code = "FORBIDDEN";
|
|
38
|
+
} else if (response.status === 413) {
|
|
39
|
+
code = "PAYLOAD_TOO_LARGE";
|
|
40
|
+
} else if (response.status === 429) {
|
|
41
|
+
code = "RATE_LIMITED";
|
|
42
|
+
} else if (response.status === 409) {
|
|
43
|
+
code = "IDEMPOTENCY_CONFLICT";
|
|
44
|
+
}
|
|
45
|
+
throw new ArchivaError({
|
|
46
|
+
statusCode: response.status,
|
|
47
|
+
code,
|
|
48
|
+
message: errorMessage,
|
|
49
|
+
retryAfterSeconds,
|
|
50
|
+
details: payload
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function createRequestId() {
|
|
54
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
55
|
+
return crypto.randomUUID();
|
|
56
|
+
}
|
|
57
|
+
return `${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
|
|
58
|
+
}
|
|
59
|
+
function createIdempotencyKey() {
|
|
60
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
61
|
+
return `idem_${crypto.randomUUID()}`;
|
|
62
|
+
}
|
|
63
|
+
return `idem_${Date.now()}-${Math.random().toString(36).substring(2, 15)}`;
|
|
64
|
+
}
|
|
65
|
+
async function loadEvents(apiKey, params, baseUrl = DEFAULT_BASE_URL) {
|
|
66
|
+
const url = new URL(`${baseUrl}/api/events`);
|
|
67
|
+
if (params.entityId) {
|
|
68
|
+
url.searchParams.set("entityId", params.entityId);
|
|
69
|
+
}
|
|
70
|
+
if (params.actorId) {
|
|
71
|
+
url.searchParams.set("actorId", params.actorId);
|
|
72
|
+
}
|
|
73
|
+
if (params.entityType) {
|
|
74
|
+
url.searchParams.set("entityType", params.entityType);
|
|
75
|
+
}
|
|
76
|
+
if (params.actorType) {
|
|
77
|
+
url.searchParams.set("actorType", params.actorType);
|
|
78
|
+
}
|
|
79
|
+
if (params.limit) {
|
|
80
|
+
url.searchParams.set("limit", String(params.limit));
|
|
81
|
+
}
|
|
82
|
+
if (params.cursor) {
|
|
83
|
+
url.searchParams.set("cursor", params.cursor);
|
|
84
|
+
}
|
|
85
|
+
const response = await fetch(url.toString(), {
|
|
86
|
+
headers: buildHeaders(apiKey)
|
|
87
|
+
});
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
await parseError(response);
|
|
90
|
+
}
|
|
91
|
+
const payload = await response.json();
|
|
92
|
+
if (!payload || typeof payload !== "object" || !Array.isArray(payload.items)) {
|
|
93
|
+
throw new ArchivaError({
|
|
94
|
+
statusCode: response.status,
|
|
95
|
+
code: "HTTP_ERROR",
|
|
96
|
+
message: "Invalid response format",
|
|
97
|
+
details: payload
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const items = payload.items.map((item) => {
|
|
101
|
+
if (typeof item !== "object" || item === null) {
|
|
102
|
+
throw new ArchivaError({
|
|
103
|
+
statusCode: response.status,
|
|
104
|
+
code: "HTTP_ERROR",
|
|
105
|
+
message: "Invalid item format in response",
|
|
106
|
+
details: item
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return normalizeAuditEventItem(item);
|
|
110
|
+
});
|
|
111
|
+
return {
|
|
112
|
+
items,
|
|
113
|
+
nextCursor: typeof payload.nextCursor === "string" ? payload.nextCursor : void 0
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
async function createEvent(apiKey, event, options, baseUrl = DEFAULT_BASE_URL) {
|
|
117
|
+
const idempotencyKey = options?.idempotencyKey ?? createIdempotencyKey();
|
|
118
|
+
const requestId = options?.requestId ?? createRequestId();
|
|
119
|
+
const headers = buildHeaders(apiKey, {
|
|
120
|
+
"Content-Type": "application/json",
|
|
121
|
+
"Idempotency-Key": idempotencyKey,
|
|
122
|
+
"X-Request-Id": requestId
|
|
123
|
+
});
|
|
124
|
+
const response = await fetch(`${baseUrl}/api/ingest/event`, {
|
|
125
|
+
method: "POST",
|
|
126
|
+
headers,
|
|
127
|
+
body: JSON.stringify(event)
|
|
128
|
+
});
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
await parseError(response);
|
|
131
|
+
}
|
|
132
|
+
const payload = await response.json();
|
|
133
|
+
if (!payload || typeof payload !== "object" || typeof payload.eventId !== "string") {
|
|
134
|
+
throw new ArchivaError({
|
|
135
|
+
statusCode: response.status,
|
|
136
|
+
code: "HTTP_ERROR",
|
|
137
|
+
message: "Invalid response format",
|
|
138
|
+
details: payload
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
eventId: payload.eventId,
|
|
143
|
+
replayed: response.status === 200
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
async function createEvents(apiKey, events, options, baseUrl = DEFAULT_BASE_URL) {
|
|
147
|
+
const results = await Promise.all(
|
|
148
|
+
events.map(
|
|
149
|
+
(event, index) => createEvent(
|
|
150
|
+
apiKey,
|
|
151
|
+
event,
|
|
152
|
+
{
|
|
153
|
+
...options,
|
|
154
|
+
idempotencyKey: options?.idempotencyKey ? `${options.idempotencyKey}_${index}` : void 0
|
|
155
|
+
},
|
|
156
|
+
baseUrl
|
|
157
|
+
)
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
return {
|
|
161
|
+
eventIds: results.map((r) => r.eventId)
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/actions.ts
|
|
166
|
+
var DEFAULT_BASE_URL2 = "https://api.archiva.app";
|
|
167
|
+
function getApiKey(apiKey) {
|
|
168
|
+
const resolvedKey = apiKey || process.env.ARCHIVA_SECRET_KEY;
|
|
169
|
+
if (!resolvedKey) {
|
|
170
|
+
throw new Error("ARCHIVA_SECRET_KEY environment variable is required, or provide apiKey prop to ArchivaProvider");
|
|
171
|
+
}
|
|
172
|
+
return resolvedKey;
|
|
173
|
+
}
|
|
174
|
+
async function loadEvents2(params, apiKey) {
|
|
175
|
+
const resolvedApiKey = getApiKey(apiKey);
|
|
176
|
+
return loadEvents(resolvedApiKey, params, DEFAULT_BASE_URL2);
|
|
177
|
+
}
|
|
178
|
+
async function createEvent2(event, options, apiKey) {
|
|
179
|
+
const resolvedApiKey = getApiKey(apiKey);
|
|
180
|
+
return createEvent(resolvedApiKey, event, options, DEFAULT_BASE_URL2);
|
|
181
|
+
}
|
|
182
|
+
async function createEvents2(events, options, apiKey) {
|
|
183
|
+
const resolvedApiKey = getApiKey(apiKey);
|
|
184
|
+
return createEvents(resolvedApiKey, events, options, DEFAULT_BASE_URL2);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/server/frontendTokens.ts
|
|
188
|
+
import "server-only";
|
|
189
|
+
var DEFAULT_API_BASE_URL = "https://api.archiva.app";
|
|
190
|
+
async function createFrontendTokenGET(projectId, apiBaseUrl = DEFAULT_API_BASE_URL) {
|
|
191
|
+
const secretKey = process.env.ARCHIVA_SECRET_KEY;
|
|
192
|
+
if (!secretKey || secretKey.trim().length === 0) {
|
|
193
|
+
const exists = process.env.ARCHIVA_SECRET_KEY !== void 0;
|
|
194
|
+
throw new Error(
|
|
195
|
+
`ARCHIVA_SECRET_KEY environment variable is ${exists ? "empty" : "not configured"}. Please set it in your .env.local file (or .env) with a valid value and restart your Next.js dev server. The variable must be in the project root directory and must not be empty.`
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
const keyPrefix = secretKey.substring(0, 10);
|
|
199
|
+
console.log("[Archiva] Using API key prefix:", keyPrefix + "...", "Length:", secretKey.length);
|
|
200
|
+
const url = new URL(`${apiBaseUrl}/api/v1/frontend-tokens`);
|
|
201
|
+
const response = await fetch(url.toString(), {
|
|
202
|
+
method: "POST",
|
|
203
|
+
headers: {
|
|
204
|
+
"Content-Type": "application/json",
|
|
205
|
+
"Authorization": `Bearer ${secretKey}`
|
|
206
|
+
},
|
|
207
|
+
body: JSON.stringify({
|
|
208
|
+
scopes: ["timeline:read"],
|
|
209
|
+
...projectId && { projectId }
|
|
210
|
+
})
|
|
211
|
+
});
|
|
212
|
+
if (!response.ok) {
|
|
213
|
+
const error = await response.json().catch(() => ({ error: response.statusText }));
|
|
214
|
+
const errorMessage = error.error || `Failed to fetch frontend token: ${response.status}`;
|
|
215
|
+
if (errorMessage.includes("ARCHIVA_SECRET_KEY") || response.status === 401 || response.status === 403) {
|
|
216
|
+
throw new Error(
|
|
217
|
+
`Archiva API authentication failed. Check that your ARCHIVA_SECRET_KEY is valid and has the correct permissions. API error: ${errorMessage}`
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
throw new Error(errorMessage);
|
|
221
|
+
}
|
|
222
|
+
const data = await response.json();
|
|
223
|
+
if (!data.token || typeof data.expiresAt !== "number") {
|
|
224
|
+
throw new Error("Invalid token response format");
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
token: data.token,
|
|
228
|
+
expiresAt: data.expiresAt
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// src/server/handlers/createFrontendTokenRoute.ts
|
|
233
|
+
import "server-only";
|
|
234
|
+
import { NextResponse } from "next/server";
|
|
235
|
+
var DEFAULT_API_BASE_URL2 = "https://api.archiva.app";
|
|
236
|
+
function createFrontendTokenRoute(options) {
|
|
237
|
+
return async function GET2(request) {
|
|
238
|
+
try {
|
|
239
|
+
const searchParams = request.nextUrl.searchParams;
|
|
240
|
+
const projectId = searchParams.get("projectId") || void 0;
|
|
241
|
+
const apiBaseUrl = options?.apiBaseUrl || DEFAULT_API_BASE_URL2;
|
|
242
|
+
const tokenResponse = await createFrontendTokenGET(projectId, apiBaseUrl);
|
|
243
|
+
return NextResponse.json(tokenResponse);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
console.error("Error fetching frontend token:", error);
|
|
246
|
+
const message = error instanceof Error ? error.message : "Internal server error";
|
|
247
|
+
const statusCode = message.includes("ARCHIVA_SECRET_KEY") ? 500 : 500;
|
|
248
|
+
return NextResponse.json(
|
|
249
|
+
{ error: message },
|
|
250
|
+
{ status: statusCode }
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
async function GET(request) {
|
|
256
|
+
const handler = createFrontendTokenRoute();
|
|
257
|
+
return handler(request);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export {
|
|
261
|
+
ArchivaError,
|
|
262
|
+
loadEvents2 as loadEvents,
|
|
263
|
+
createEvent2 as createEvent,
|
|
264
|
+
createEvents2 as createEvents,
|
|
265
|
+
createFrontendTokenGET,
|
|
266
|
+
createFrontendTokenRoute,
|
|
267
|
+
GET
|
|
268
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// src/eventNormalizer.ts
|
|
2
|
+
var coerceString = (value) => {
|
|
3
|
+
if (value === null || value === void 0) {
|
|
4
|
+
return void 0;
|
|
5
|
+
}
|
|
6
|
+
if (typeof value === "string") {
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
10
|
+
return String(value);
|
|
11
|
+
}
|
|
12
|
+
return void 0;
|
|
13
|
+
};
|
|
14
|
+
var coerceNullableString = (value) => {
|
|
15
|
+
if (value === null || value === void 0) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === "string") {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
22
|
+
return String(value);
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
};
|
|
26
|
+
var coerceActorType = (value) => {
|
|
27
|
+
if (typeof value !== "string") {
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
const normalized = value.toLowerCase();
|
|
31
|
+
if (normalized === "user" || normalized === "service" || normalized === "system") {
|
|
32
|
+
return normalized;
|
|
33
|
+
}
|
|
34
|
+
return void 0;
|
|
35
|
+
};
|
|
36
|
+
var getActorValue = (event, actorRecord, keys) => {
|
|
37
|
+
for (const key of keys) {
|
|
38
|
+
if (Object.prototype.hasOwnProperty.call(event, key)) {
|
|
39
|
+
return event[key];
|
|
40
|
+
}
|
|
41
|
+
if (actorRecord && Object.prototype.hasOwnProperty.call(actorRecord, key)) {
|
|
42
|
+
return actorRecord[key];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return void 0;
|
|
46
|
+
};
|
|
47
|
+
var normalizeAuditEventItem = (event) => {
|
|
48
|
+
const actorRecord = typeof event.actor === "object" && event.actor !== null ? event.actor : void 0;
|
|
49
|
+
const actorDisplayValue = getActorValue(event, actorRecord, [
|
|
50
|
+
"actorDisplay",
|
|
51
|
+
"actor_display",
|
|
52
|
+
"display",
|
|
53
|
+
"display_name",
|
|
54
|
+
"name"
|
|
55
|
+
]);
|
|
56
|
+
const actorTypeValue = getActorValue(event, actorRecord, [
|
|
57
|
+
"actorType",
|
|
58
|
+
"actor_type",
|
|
59
|
+
"type"
|
|
60
|
+
]);
|
|
61
|
+
const actorIdValue = getActorValue(event, actorRecord, [
|
|
62
|
+
"actorId",
|
|
63
|
+
"actor_id",
|
|
64
|
+
"id"
|
|
65
|
+
]);
|
|
66
|
+
const actionKeyValue = coerceString(event.actionKey ?? event.action_key ?? event.action);
|
|
67
|
+
const actionDescriptionValue = coerceString(event.actionDescription ?? event.action_description);
|
|
68
|
+
const tenantIdValue = coerceString(event.tenantId ?? event.tenant_id);
|
|
69
|
+
return {
|
|
70
|
+
id: coerceString(event.id) ?? "",
|
|
71
|
+
receivedAt: coerceString(event.receivedAt ?? event.received_at) ?? "",
|
|
72
|
+
actionKey: actionKeyValue ?? "",
|
|
73
|
+
actionDescription: actionDescriptionValue ?? void 0,
|
|
74
|
+
tenantId: tenantIdValue ?? void 0,
|
|
75
|
+
// Legacy support: include action for backward compatibility
|
|
76
|
+
action: actionKeyValue ?? "",
|
|
77
|
+
entityType: coerceString(event.entityType ?? event.entity_type) ?? "",
|
|
78
|
+
entityId: coerceString(event.entityId ?? event.entity_id) ?? "",
|
|
79
|
+
actorId: coerceNullableString(actorIdValue),
|
|
80
|
+
actorType: coerceActorType(actorTypeValue),
|
|
81
|
+
actorDisplay: coerceString(actorDisplayValue) ?? "",
|
|
82
|
+
source: coerceNullableString(event.source)
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
normalizeAuditEventItem
|
|
88
|
+
};
|