@archiva/archiva-nextjs 0.2.82 → 0.2.91

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