@markwharton/pwa-core 1.2.0 → 1.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.
Files changed (83) hide show
  1. package/dist/__tests__/auth/apiKey.test.d.ts +1 -0
  2. package/dist/__tests__/auth/apiKey.test.js +80 -0
  3. package/dist/__tests__/auth/token.test.d.ts +1 -0
  4. package/dist/__tests__/auth/token.test.js +212 -0
  5. package/dist/__tests__/auth/types.test.d.ts +1 -0
  6. package/dist/__tests__/auth/types.test.js +77 -0
  7. package/dist/__tests__/client/api.test.d.ts +1 -0
  8. package/dist/__tests__/client/api.test.js +334 -0
  9. package/dist/__tests__/client/apiError.test.d.ts +1 -0
  10. package/dist/__tests__/client/apiError.test.js +58 -0
  11. package/dist/__tests__/http/responses.test.d.ts +1 -0
  12. package/dist/__tests__/http/responses.test.js +112 -0
  13. package/dist/__tests__/http/status.test.d.ts +1 -0
  14. package/dist/__tests__/http/status.test.js +27 -0
  15. package/dist/__tests__/server/auth/apiKey.test.d.ts +1 -0
  16. package/dist/__tests__/server/auth/apiKey.test.js +80 -0
  17. package/dist/__tests__/server/auth/token.test.d.ts +1 -0
  18. package/dist/__tests__/server/auth/token.test.js +212 -0
  19. package/dist/__tests__/server/http/responses.test.d.ts +1 -0
  20. package/dist/__tests__/server/http/responses.test.js +112 -0
  21. package/dist/__tests__/server/storage/client.test.d.ts +1 -0
  22. package/dist/__tests__/server/storage/client.test.js +173 -0
  23. package/dist/__tests__/server/storage/keys.test.d.ts +1 -0
  24. package/dist/__tests__/server/storage/keys.test.js +47 -0
  25. package/dist/__tests__/shared/auth/types.test.d.ts +1 -0
  26. package/dist/__tests__/shared/auth/types.test.js +77 -0
  27. package/dist/__tests__/shared/http/status.test.d.ts +1 -0
  28. package/dist/__tests__/shared/http/status.test.js +27 -0
  29. package/dist/__tests__/storage/client.test.d.ts +1 -0
  30. package/dist/__tests__/storage/client.test.js +173 -0
  31. package/dist/__tests__/storage/keys.test.d.ts +1 -0
  32. package/dist/__tests__/storage/keys.test.js +47 -0
  33. package/dist/__tests__/types.test.d.ts +1 -0
  34. package/dist/__tests__/types.test.js +56 -0
  35. package/dist/auth/apiKey.d.ts +24 -7
  36. package/dist/auth/apiKey.js +24 -7
  37. package/dist/auth/token.d.ts +37 -10
  38. package/dist/auth/token.js +37 -10
  39. package/dist/auth/types.d.ts +21 -3
  40. package/dist/auth/types.js +21 -3
  41. package/dist/client/api.d.ts +70 -9
  42. package/dist/client/api.js +70 -9
  43. package/dist/client/apiError.d.ts +22 -5
  44. package/dist/client/apiError.js +22 -5
  45. package/dist/http/responses.d.ts +57 -8
  46. package/dist/http/responses.js +57 -8
  47. package/dist/index.d.ts +2 -4
  48. package/dist/index.js +6 -5
  49. package/dist/server/auth/apiKey.d.ts +44 -0
  50. package/dist/server/auth/apiKey.js +59 -0
  51. package/dist/server/auth/index.d.ts +2 -0
  52. package/dist/server/auth/index.js +15 -0
  53. package/dist/server/auth/token.d.ts +56 -0
  54. package/dist/server/auth/token.js +104 -0
  55. package/dist/server/http/index.d.ts +1 -0
  56. package/dist/server/http/index.js +12 -0
  57. package/dist/server/http/responses.d.ts +82 -0
  58. package/dist/server/http/responses.js +132 -0
  59. package/dist/server/index.d.ts +3 -0
  60. package/dist/server/index.js +33 -0
  61. package/dist/server/storage/client.d.ts +48 -0
  62. package/dist/server/storage/client.js +107 -0
  63. package/dist/server/storage/index.d.ts +2 -0
  64. package/dist/server/storage/index.js +11 -0
  65. package/dist/server/storage/keys.d.ts +8 -0
  66. package/dist/server/storage/keys.js +14 -0
  67. package/dist/shared/auth/index.d.ts +2 -0
  68. package/dist/shared/auth/index.js +7 -0
  69. package/dist/shared/auth/types.d.ts +63 -0
  70. package/dist/shared/auth/types.js +41 -0
  71. package/dist/shared/http/index.d.ts +3 -0
  72. package/dist/shared/http/index.js +5 -0
  73. package/dist/shared/http/status.d.ts +17 -0
  74. package/dist/shared/http/status.js +19 -0
  75. package/dist/shared/http/types.d.ts +10 -0
  76. package/dist/shared/http/types.js +5 -0
  77. package/dist/shared/index.d.ts +5 -0
  78. package/dist/shared/index.js +10 -0
  79. package/dist/storage/client.d.ts +29 -6
  80. package/dist/storage/client.js +29 -6
  81. package/dist/types.d.ts +16 -3
  82. package/dist/types.js +16 -3
  83. package/package.json +9 -13
@@ -0,0 +1,334 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const vitest_1 = require("vitest");
37
+ // Mock fetch globally
38
+ const mockFetch = vitest_1.vi.fn();
39
+ global.fetch = mockFetch;
40
+ (0, vitest_1.describe)('API client', () => {
41
+ (0, vitest_1.beforeEach)(() => {
42
+ vitest_1.vi.resetModules();
43
+ mockFetch.mockReset();
44
+ });
45
+ (0, vitest_1.describe)('initApiClient', () => {
46
+ (0, vitest_1.it)('sets token getter', async () => {
47
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
48
+ initApiClient({ getToken: () => 'test-token' });
49
+ mockFetch.mockResolvedValue({
50
+ ok: true,
51
+ text: () => Promise.resolve('{"data": "test"}')
52
+ });
53
+ await apiCall('/api/test');
54
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/test', vitest_1.expect.objectContaining({
55
+ headers: vitest_1.expect.objectContaining({
56
+ Authorization: 'Bearer test-token'
57
+ })
58
+ }));
59
+ });
60
+ (0, vitest_1.it)('sets onUnauthorized callback', async () => {
61
+ const onUnauthorized = vitest_1.vi.fn();
62
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
63
+ initApiClient({ getToken: () => 'token', onUnauthorized });
64
+ mockFetch.mockResolvedValue({
65
+ ok: false,
66
+ status: 401,
67
+ json: () => Promise.resolve({ error: 'Unauthorized' })
68
+ });
69
+ await (0, vitest_1.expect)(apiCall('/api/test')).rejects.toThrow();
70
+ (0, vitest_1.expect)(onUnauthorized).toHaveBeenCalled();
71
+ });
72
+ });
73
+ (0, vitest_1.describe)('apiCall', () => {
74
+ (0, vitest_1.it)('makes authenticated request', async () => {
75
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
76
+ initApiClient({ getToken: () => 'my-token' });
77
+ mockFetch.mockResolvedValue({
78
+ ok: true,
79
+ text: () => Promise.resolve('{"result": "success"}')
80
+ });
81
+ const result = await apiCall('/api/endpoint');
82
+ (0, vitest_1.expect)(result).toEqual({ result: 'success' });
83
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/endpoint', vitest_1.expect.objectContaining({
84
+ headers: vitest_1.expect.objectContaining({
85
+ 'Content-Type': 'application/json',
86
+ Authorization: 'Bearer my-token'
87
+ })
88
+ }));
89
+ });
90
+ (0, vitest_1.it)('handles request without token', async () => {
91
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
92
+ initApiClient({ getToken: () => null });
93
+ mockFetch.mockResolvedValue({
94
+ ok: true,
95
+ text: () => Promise.resolve('{}')
96
+ });
97
+ await apiCall('/api/public');
98
+ const headers = mockFetch.mock.calls[0][1].headers;
99
+ (0, vitest_1.expect)(headers.Authorization).toBeUndefined();
100
+ });
101
+ (0, vitest_1.it)('throws ApiError on non-ok response', async () => {
102
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
103
+ const { ApiError: LocalApiError } = await Promise.resolve().then(() => __importStar(require('../../client/apiError')));
104
+ initApiClient({ getToken: () => 'token' });
105
+ mockFetch.mockResolvedValue({
106
+ ok: false,
107
+ status: 400,
108
+ json: () => Promise.resolve({ error: 'Invalid input' })
109
+ });
110
+ await (0, vitest_1.expect)(apiCall('/api/test')).rejects.toThrow(LocalApiError);
111
+ await (0, vitest_1.expect)(apiCall('/api/test')).rejects.toMatchObject({
112
+ status: 400,
113
+ message: 'Invalid input'
114
+ });
115
+ });
116
+ (0, vitest_1.it)('handles empty response body', async () => {
117
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
118
+ initApiClient({ getToken: () => 'token' });
119
+ mockFetch.mockResolvedValue({
120
+ ok: true,
121
+ text: () => Promise.resolve('')
122
+ });
123
+ const result = await apiCall('/api/delete');
124
+ (0, vitest_1.expect)(result).toEqual({});
125
+ });
126
+ (0, vitest_1.it)('handles JSON parse error in error response', async () => {
127
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
128
+ initApiClient({ getToken: () => 'token' });
129
+ mockFetch.mockResolvedValue({
130
+ ok: false,
131
+ status: 500,
132
+ json: () => Promise.reject(new Error('Invalid JSON'))
133
+ });
134
+ await (0, vitest_1.expect)(apiCall('/api/test')).rejects.toMatchObject({
135
+ status: 500,
136
+ message: 'Request failed'
137
+ });
138
+ });
139
+ (0, vitest_1.it)('uses default error message when error field is missing', async () => {
140
+ const { initApiClient, apiCall } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
141
+ initApiClient({ getToken: () => 'token' });
142
+ mockFetch.mockResolvedValue({
143
+ ok: false,
144
+ status: 400,
145
+ json: () => Promise.resolve({ message: 'not the error field' })
146
+ });
147
+ await (0, vitest_1.expect)(apiCall('/api/test')).rejects.toMatchObject({
148
+ status: 400,
149
+ message: 'Request failed'
150
+ });
151
+ });
152
+ });
153
+ (0, vitest_1.describe)('HTTP method helpers', () => {
154
+ (0, vitest_1.beforeEach)(async () => {
155
+ const { initApiClient } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
156
+ initApiClient({ getToken: () => 'token' });
157
+ mockFetch.mockResolvedValue({
158
+ ok: true,
159
+ text: () => Promise.resolve('{"data": "test"}')
160
+ });
161
+ });
162
+ (0, vitest_1.it)('apiGet uses GET method', async () => {
163
+ const { apiGet } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
164
+ await apiGet('/api/resource');
165
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
166
+ method: 'GET'
167
+ }));
168
+ });
169
+ (0, vitest_1.it)('apiPost uses POST method with body', async () => {
170
+ const { apiPost } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
171
+ await apiPost('/api/resource', { name: 'test' });
172
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
173
+ method: 'POST',
174
+ body: '{"name":"test"}'
175
+ }));
176
+ });
177
+ (0, vitest_1.it)('apiPost works without body', async () => {
178
+ const { apiPost } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
179
+ await apiPost('/api/resource');
180
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
181
+ method: 'POST',
182
+ body: undefined
183
+ }));
184
+ });
185
+ (0, vitest_1.it)('apiPut uses PUT method with body', async () => {
186
+ const { apiPut } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
187
+ await apiPut('/api/resource', { name: 'updated' });
188
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
189
+ method: 'PUT',
190
+ body: '{"name":"updated"}'
191
+ }));
192
+ });
193
+ (0, vitest_1.it)('apiPut works without body', async () => {
194
+ const { apiPut } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
195
+ await apiPut('/api/resource');
196
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
197
+ method: 'PUT',
198
+ body: undefined
199
+ }));
200
+ });
201
+ (0, vitest_1.it)('apiPatch uses PATCH method with body', async () => {
202
+ const { apiPatch } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
203
+ await apiPatch('/api/resource', { field: 'value' });
204
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
205
+ method: 'PATCH',
206
+ body: '{"field":"value"}'
207
+ }));
208
+ });
209
+ (0, vitest_1.it)('apiPatch works without body', async () => {
210
+ const { apiPatch } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
211
+ await apiPatch('/api/resource');
212
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
213
+ method: 'PATCH',
214
+ body: undefined
215
+ }));
216
+ });
217
+ (0, vitest_1.it)('apiDelete uses DELETE method', async () => {
218
+ const { apiDelete } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
219
+ await apiDelete('/api/resource');
220
+ (0, vitest_1.expect)(mockFetch).toHaveBeenCalledWith('/api/resource', vitest_1.expect.objectContaining({
221
+ method: 'DELETE'
222
+ }));
223
+ });
224
+ });
225
+ (0, vitest_1.describe)('apiCallSafe', () => {
226
+ (0, vitest_1.it)('returns ok response with data', async () => {
227
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
228
+ initApiClient({ getToken: () => 'token' });
229
+ mockFetch.mockResolvedValue({
230
+ ok: true,
231
+ status: 200,
232
+ text: () => Promise.resolve('{"user": "john"}')
233
+ });
234
+ const result = await apiCallSafe('/api/user');
235
+ (0, vitest_1.expect)(result.ok).toBe(true);
236
+ (0, vitest_1.expect)(result.status).toBe(200);
237
+ (0, vitest_1.expect)(result.data).toEqual({ user: 'john' });
238
+ });
239
+ (0, vitest_1.it)('preserves actual status code', async () => {
240
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
241
+ initApiClient({ getToken: () => 'token' });
242
+ mockFetch.mockResolvedValue({
243
+ ok: true,
244
+ status: 201,
245
+ text: () => Promise.resolve('{"id": "123"}')
246
+ });
247
+ const result = await apiCallSafe('/api/create');
248
+ (0, vitest_1.expect)(result.status).toBe(201);
249
+ });
250
+ (0, vitest_1.it)('returns error response for non-ok status', async () => {
251
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
252
+ initApiClient({ getToken: () => 'token' });
253
+ mockFetch.mockResolvedValue({
254
+ ok: false,
255
+ status: 404,
256
+ json: () => Promise.resolve({ error: 'Not found' })
257
+ });
258
+ const result = await apiCallSafe('/api/missing');
259
+ (0, vitest_1.expect)(result.ok).toBe(false);
260
+ (0, vitest_1.expect)(result.status).toBe(404);
261
+ (0, vitest_1.expect)(result.error).toBe('Not found');
262
+ });
263
+ (0, vitest_1.it)('handles network errors', async () => {
264
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
265
+ initApiClient({ getToken: () => 'token' });
266
+ mockFetch.mockRejectedValue(new Error('Network failure'));
267
+ const result = await apiCallSafe('/api/test');
268
+ (0, vitest_1.expect)(result.ok).toBe(false);
269
+ (0, vitest_1.expect)(result.status).toBe(0);
270
+ (0, vitest_1.expect)(result.error).toBe('Network error');
271
+ });
272
+ (0, vitest_1.it)('calls onUnauthorized for 401', async () => {
273
+ const onUnauthorized = vitest_1.vi.fn();
274
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
275
+ initApiClient({ getToken: () => 'token', onUnauthorized });
276
+ mockFetch.mockResolvedValue({
277
+ ok: false,
278
+ status: 401,
279
+ json: () => Promise.resolve({ error: 'Token expired' })
280
+ });
281
+ await apiCallSafe('/api/protected');
282
+ (0, vitest_1.expect)(onUnauthorized).toHaveBeenCalled();
283
+ });
284
+ (0, vitest_1.it)('handles empty success response', async () => {
285
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
286
+ initApiClient({ getToken: () => 'token' });
287
+ mockFetch.mockResolvedValue({
288
+ ok: true,
289
+ status: 204,
290
+ text: () => Promise.resolve('')
291
+ });
292
+ const result = await apiCallSafe('/api/delete');
293
+ (0, vitest_1.expect)(result.ok).toBe(true);
294
+ (0, vitest_1.expect)(result.status).toBe(204);
295
+ (0, vitest_1.expect)(result.data).toBeUndefined();
296
+ });
297
+ (0, vitest_1.it)('handles request without token', async () => {
298
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
299
+ initApiClient({ getToken: () => null });
300
+ mockFetch.mockResolvedValue({
301
+ ok: true,
302
+ status: 200,
303
+ text: () => Promise.resolve('{}')
304
+ });
305
+ await apiCallSafe('/api/public');
306
+ const headers = mockFetch.mock.calls[0][1].headers;
307
+ (0, vitest_1.expect)(headers.Authorization).toBeUndefined();
308
+ });
309
+ (0, vitest_1.it)('uses default error message when error field is missing', async () => {
310
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
311
+ initApiClient({ getToken: () => 'token' });
312
+ mockFetch.mockResolvedValue({
313
+ ok: false,
314
+ status: 400,
315
+ json: () => Promise.resolve({ details: 'not the error field' })
316
+ });
317
+ const result = await apiCallSafe('/api/test');
318
+ (0, vitest_1.expect)(result.ok).toBe(false);
319
+ (0, vitest_1.expect)(result.error).toBe('Request failed');
320
+ });
321
+ (0, vitest_1.it)('handles JSON parse error in error response', async () => {
322
+ const { initApiClient, apiCallSafe } = await Promise.resolve().then(() => __importStar(require('../../client/api')));
323
+ initApiClient({ getToken: () => 'token' });
324
+ mockFetch.mockResolvedValue({
325
+ ok: false,
326
+ status: 500,
327
+ json: () => Promise.reject(new Error('Invalid JSON'))
328
+ });
329
+ const result = await apiCallSafe('/api/test');
330
+ (0, vitest_1.expect)(result.ok).toBe(false);
331
+ (0, vitest_1.expect)(result.error).toBe('Request failed');
332
+ });
333
+ });
334
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const apiError_1 = require("../../client/apiError");
5
+ (0, vitest_1.describe)('ApiError', () => {
6
+ (0, vitest_1.describe)('constructor', () => {
7
+ (0, vitest_1.it)('sets status, message, and name', () => {
8
+ const error = new apiError_1.ApiError(404, 'Not found');
9
+ (0, vitest_1.expect)(error.status).toBe(404);
10
+ (0, vitest_1.expect)(error.message).toBe('Not found');
11
+ (0, vitest_1.expect)(error.name).toBe('ApiError');
12
+ });
13
+ (0, vitest_1.it)('sets optional details', () => {
14
+ const error = new apiError_1.ApiError(400, 'Validation failed', 'Field "email" is required');
15
+ (0, vitest_1.expect)(error.details).toBe('Field "email" is required');
16
+ });
17
+ (0, vitest_1.it)('extends Error', () => {
18
+ const error = new apiError_1.ApiError(500, 'Server error');
19
+ (0, vitest_1.expect)(error).toBeInstanceOf(Error);
20
+ });
21
+ });
22
+ (0, vitest_1.describe)('isUnauthorized', () => {
23
+ (0, vitest_1.it)('returns true for 401 status', () => {
24
+ const error = new apiError_1.ApiError(401, 'Unauthorized');
25
+ (0, vitest_1.expect)(error.isUnauthorized()).toBe(true);
26
+ });
27
+ (0, vitest_1.it)('returns false for other statuses', () => {
28
+ (0, vitest_1.expect)(new apiError_1.ApiError(400, 'Bad request').isUnauthorized()).toBe(false);
29
+ (0, vitest_1.expect)(new apiError_1.ApiError(403, 'Forbidden').isUnauthorized()).toBe(false);
30
+ (0, vitest_1.expect)(new apiError_1.ApiError(404, 'Not found').isUnauthorized()).toBe(false);
31
+ (0, vitest_1.expect)(new apiError_1.ApiError(500, 'Server error').isUnauthorized()).toBe(false);
32
+ });
33
+ });
34
+ (0, vitest_1.describe)('isNotFound', () => {
35
+ (0, vitest_1.it)('returns true for 404 status', () => {
36
+ const error = new apiError_1.ApiError(404, 'Not found');
37
+ (0, vitest_1.expect)(error.isNotFound()).toBe(true);
38
+ });
39
+ (0, vitest_1.it)('returns false for other statuses', () => {
40
+ (0, vitest_1.expect)(new apiError_1.ApiError(400, 'Bad request').isNotFound()).toBe(false);
41
+ (0, vitest_1.expect)(new apiError_1.ApiError(401, 'Unauthorized').isNotFound()).toBe(false);
42
+ (0, vitest_1.expect)(new apiError_1.ApiError(403, 'Forbidden').isNotFound()).toBe(false);
43
+ (0, vitest_1.expect)(new apiError_1.ApiError(500, 'Server error').isNotFound()).toBe(false);
44
+ });
45
+ });
46
+ (0, vitest_1.describe)('isBadRequest', () => {
47
+ (0, vitest_1.it)('returns true for 400 status', () => {
48
+ const error = new apiError_1.ApiError(400, 'Bad request');
49
+ (0, vitest_1.expect)(error.isBadRequest()).toBe(true);
50
+ });
51
+ (0, vitest_1.it)('returns false for other statuses', () => {
52
+ (0, vitest_1.expect)(new apiError_1.ApiError(401, 'Unauthorized').isBadRequest()).toBe(false);
53
+ (0, vitest_1.expect)(new apiError_1.ApiError(403, 'Forbidden').isBadRequest()).toBe(false);
54
+ (0, vitest_1.expect)(new apiError_1.ApiError(404, 'Not found').isBadRequest()).toBe(false);
55
+ (0, vitest_1.expect)(new apiError_1.ApiError(500, 'Server error').isBadRequest()).toBe(false);
56
+ });
57
+ });
58
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const responses_1 = require("../../http/responses");
5
+ const status_1 = require("../../http/status");
6
+ (0, vitest_1.describe)('HTTP response helpers', () => {
7
+ (0, vitest_1.describe)('badRequestResponse', () => {
8
+ (0, vitest_1.it)('returns 400 status with error message', () => {
9
+ const response = (0, responses_1.badRequestResponse)('Invalid input');
10
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.BAD_REQUEST);
11
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'Invalid input' });
12
+ });
13
+ });
14
+ (0, vitest_1.describe)('unauthorizedResponse', () => {
15
+ (0, vitest_1.it)('returns 401 status with default message', () => {
16
+ const response = (0, responses_1.unauthorizedResponse)();
17
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.UNAUTHORIZED);
18
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'Unauthorized' });
19
+ });
20
+ (0, vitest_1.it)('returns 401 status with custom message', () => {
21
+ const response = (0, responses_1.unauthorizedResponse)('Token expired');
22
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.UNAUTHORIZED);
23
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'Token expired' });
24
+ });
25
+ });
26
+ (0, vitest_1.describe)('forbiddenResponse', () => {
27
+ (0, vitest_1.it)('returns 403 status with default message', () => {
28
+ const response = (0, responses_1.forbiddenResponse)();
29
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.FORBIDDEN);
30
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'Forbidden' });
31
+ });
32
+ (0, vitest_1.it)('returns 403 status with custom message', () => {
33
+ const response = (0, responses_1.forbiddenResponse)('Admin access required');
34
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.FORBIDDEN);
35
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'Admin access required' });
36
+ });
37
+ });
38
+ (0, vitest_1.describe)('notFoundResponse', () => {
39
+ (0, vitest_1.it)('returns 404 status with resource name', () => {
40
+ const response = (0, responses_1.notFoundResponse)('User');
41
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.NOT_FOUND);
42
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'User not found' });
43
+ });
44
+ });
45
+ (0, vitest_1.describe)('conflictResponse', () => {
46
+ (0, vitest_1.it)('returns 409 status with error message', () => {
47
+ const response = (0, responses_1.conflictResponse)('Resource already exists');
48
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.CONFLICT);
49
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'Resource already exists' });
50
+ });
51
+ });
52
+ (0, vitest_1.describe)('handleFunctionError', () => {
53
+ (0, vitest_1.it)('logs error and returns 500 response', () => {
54
+ const mockContext = {
55
+ error: vitest_1.vi.fn()
56
+ };
57
+ const error = new Error('Database connection failed');
58
+ const response = (0, responses_1.handleFunctionError)(error, mockContext);
59
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.INTERNAL_ERROR);
60
+ (0, vitest_1.expect)(response.jsonBody).toEqual({ error: 'Internal server error' });
61
+ (0, vitest_1.expect)(mockContext.error).toHaveBeenCalledWith('Function error: Database connection failed');
62
+ });
63
+ (0, vitest_1.it)('handles non-Error objects', () => {
64
+ const mockContext = {
65
+ error: vitest_1.vi.fn()
66
+ };
67
+ const response = (0, responses_1.handleFunctionError)('string error', mockContext);
68
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.INTERNAL_ERROR);
69
+ (0, vitest_1.expect)(mockContext.error).toHaveBeenCalledWith('Function error: Unknown error');
70
+ });
71
+ (0, vitest_1.it)('handles null/undefined errors', () => {
72
+ const mockContext = {
73
+ error: vitest_1.vi.fn()
74
+ };
75
+ const response = (0, responses_1.handleFunctionError)(null, mockContext);
76
+ (0, vitest_1.expect)(response.status).toBe(status_1.HTTP_STATUS.INTERNAL_ERROR);
77
+ (0, vitest_1.expect)(mockContext.error).toHaveBeenCalledWith('Function error: Unknown error');
78
+ });
79
+ });
80
+ (0, vitest_1.describe)('isNotFoundError', () => {
81
+ (0, vitest_1.it)('returns true for 404 error', () => {
82
+ const error = Object.assign(new Error('Not found'), { statusCode: 404 });
83
+ (0, vitest_1.expect)((0, responses_1.isNotFoundError)(error)).toBe(true);
84
+ });
85
+ (0, vitest_1.it)('returns false for other status codes', () => {
86
+ const error = Object.assign(new Error('Conflict'), { statusCode: 409 });
87
+ (0, vitest_1.expect)((0, responses_1.isNotFoundError)(error)).toBe(false);
88
+ });
89
+ (0, vitest_1.it)('returns false for non-Error objects', () => {
90
+ (0, vitest_1.expect)((0, responses_1.isNotFoundError)({ statusCode: 404 })).toBe(false);
91
+ });
92
+ (0, vitest_1.it)('returns false for errors without statusCode', () => {
93
+ (0, vitest_1.expect)((0, responses_1.isNotFoundError)(new Error('Not found'))).toBe(false);
94
+ });
95
+ });
96
+ (0, vitest_1.describe)('isConflictError', () => {
97
+ (0, vitest_1.it)('returns true for 409 error', () => {
98
+ const error = Object.assign(new Error('Conflict'), { statusCode: 409 });
99
+ (0, vitest_1.expect)((0, responses_1.isConflictError)(error)).toBe(true);
100
+ });
101
+ (0, vitest_1.it)('returns false for other status codes', () => {
102
+ const error = Object.assign(new Error('Not found'), { statusCode: 404 });
103
+ (0, vitest_1.expect)((0, responses_1.isConflictError)(error)).toBe(false);
104
+ });
105
+ (0, vitest_1.it)('returns false for non-Error objects', () => {
106
+ (0, vitest_1.expect)((0, responses_1.isConflictError)({ statusCode: 409 })).toBe(false);
107
+ });
108
+ (0, vitest_1.it)('returns false for errors without statusCode', () => {
109
+ (0, vitest_1.expect)((0, responses_1.isConflictError)(new Error('Conflict'))).toBe(false);
110
+ });
111
+ });
112
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const status_1 = require("../../http/status");
5
+ (0, vitest_1.describe)('HTTP_STATUS', () => {
6
+ (0, vitest_1.it)('has correct status codes', () => {
7
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.OK).toBe(200);
8
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.CREATED).toBe(201);
9
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.NO_CONTENT).toBe(204);
10
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.BAD_REQUEST).toBe(400);
11
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.UNAUTHORIZED).toBe(401);
12
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.FORBIDDEN).toBe(403);
13
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.NOT_FOUND).toBe(404);
14
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.CONFLICT).toBe(409);
15
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.GONE).toBe(410);
16
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.INTERNAL_ERROR).toBe(500);
17
+ (0, vitest_1.expect)(status_1.HTTP_STATUS.SERVICE_UNAVAILABLE).toBe(503);
18
+ });
19
+ (0, vitest_1.it)('is immutable (const assertion)', () => {
20
+ // TypeScript const assertion makes the object readonly
21
+ // This test verifies the values haven't been accidentally modified
22
+ const statusCodes = Object.values(status_1.HTTP_STATUS);
23
+ (0, vitest_1.expect)(statusCodes).toContain(200);
24
+ (0, vitest_1.expect)(statusCodes).toContain(404);
25
+ (0, vitest_1.expect)(statusCodes).toContain(500);
26
+ });
27
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const apiKey_1 = require("../../../server/auth/apiKey");
5
+ (0, vitest_1.describe)('API Key utilities', () => {
6
+ (0, vitest_1.describe)('extractApiKey', () => {
7
+ (0, vitest_1.it)('extracts API key from X-API-Key header', () => {
8
+ const request = {
9
+ headers: {
10
+ get: (name) => name === 'X-API-Key' ? 'test-api-key' : null
11
+ }
12
+ };
13
+ (0, vitest_1.expect)((0, apiKey_1.extractApiKey)(request)).toBe('test-api-key');
14
+ });
15
+ (0, vitest_1.it)('returns null when header is missing', () => {
16
+ const request = {
17
+ headers: {
18
+ get: () => null
19
+ }
20
+ };
21
+ (0, vitest_1.expect)((0, apiKey_1.extractApiKey)(request)).toBeNull();
22
+ });
23
+ });
24
+ (0, vitest_1.describe)('hashApiKey', () => {
25
+ (0, vitest_1.it)('produces consistent SHA-256 hash', () => {
26
+ const key = 'test-api-key';
27
+ const hash1 = (0, apiKey_1.hashApiKey)(key);
28
+ const hash2 = (0, apiKey_1.hashApiKey)(key);
29
+ (0, vitest_1.expect)(hash1).toBe(hash2);
30
+ });
31
+ (0, vitest_1.it)('produces 64-character hex string', () => {
32
+ const hash = (0, apiKey_1.hashApiKey)('any-key');
33
+ (0, vitest_1.expect)(hash).toHaveLength(64);
34
+ (0, vitest_1.expect)(hash).toMatch(/^[a-f0-9]+$/);
35
+ });
36
+ (0, vitest_1.it)('produces different hashes for different keys', () => {
37
+ const hash1 = (0, apiKey_1.hashApiKey)('key1');
38
+ const hash2 = (0, apiKey_1.hashApiKey)('key2');
39
+ (0, vitest_1.expect)(hash1).not.toBe(hash2);
40
+ });
41
+ });
42
+ (0, vitest_1.describe)('validateApiKey', () => {
43
+ (0, vitest_1.it)('returns ok for matching key', () => {
44
+ const key = 'valid-api-key';
45
+ const hash = (0, apiKey_1.hashApiKey)(key);
46
+ const result = (0, apiKey_1.validateApiKey)(key, hash);
47
+ (0, vitest_1.expect)(result.ok).toBe(true);
48
+ });
49
+ (0, vitest_1.it)('returns error for mismatched key', () => {
50
+ const hash = (0, apiKey_1.hashApiKey)('correct-key');
51
+ const result = (0, apiKey_1.validateApiKey)('wrong-key', hash);
52
+ (0, vitest_1.expect)(result.ok).toBe(false);
53
+ (0, vitest_1.expect)(result.error).toBe('Invalid API key');
54
+ });
55
+ (0, vitest_1.it)('returns error for empty key', () => {
56
+ const hash = (0, apiKey_1.hashApiKey)('some-key');
57
+ const result = (0, apiKey_1.validateApiKey)('', hash);
58
+ (0, vitest_1.expect)(result.ok).toBe(false);
59
+ });
60
+ });
61
+ (0, vitest_1.describe)('generateApiKey', () => {
62
+ (0, vitest_1.it)('generates 64-character hex string', () => {
63
+ const key = (0, apiKey_1.generateApiKey)();
64
+ (0, vitest_1.expect)(key).toHaveLength(64);
65
+ (0, vitest_1.expect)(key).toMatch(/^[a-f0-9]+$/);
66
+ });
67
+ (0, vitest_1.it)('generates unique keys', () => {
68
+ const key1 = (0, apiKey_1.generateApiKey)();
69
+ const key2 = (0, apiKey_1.generateApiKey)();
70
+ (0, vitest_1.expect)(key1).not.toBe(key2);
71
+ });
72
+ (0, vitest_1.it)('generates cryptographically random keys', () => {
73
+ const keys = new Set();
74
+ for (let i = 0; i < 100; i++) {
75
+ keys.add((0, apiKey_1.generateApiKey)());
76
+ }
77
+ (0, vitest_1.expect)(keys.size).toBe(100);
78
+ });
79
+ });
80
+ });
@@ -0,0 +1 @@
1
+ export {};