@rebasepro/server-core 0.0.1-canary.eae7889 → 0.1.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/app/frontend/node_modules/esbuild/LICENSE.md +21 -0
- package/app/frontend/node_modules/esbuild/README.md +3 -0
- package/app/frontend/node_modules/esbuild/bin/esbuild +220 -0
- package/app/frontend/node_modules/esbuild/install.js +285 -0
- package/app/frontend/node_modules/esbuild/lib/main.d.ts +705 -0
- package/app/frontend/node_modules/esbuild/lib/main.js +2239 -0
- package/app/frontend/node_modules/esbuild/package.json +46 -0
- package/dist/index.es.js +1186 -1673
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1185 -1672
- package/dist/index.umd.js.map +1 -1
- package/dist/server-core/src/api/rest/api-generator.d.ts +15 -3
- package/dist/server-core/src/auth/admin-routes.d.ts +5 -0
- package/dist/server-core/src/auth/google-oauth.d.ts +36 -3
- package/dist/server-core/src/auth/index.d.ts +1 -0
- package/dist/server-core/src/cron/cron-scheduler.d.ts +45 -0
- package/dist/server-core/src/cron/index.d.ts +1 -1
- package/dist/server-core/src/init.d.ts +11 -1
- package/dist/types/src/controllers/auth.d.ts +8 -2
- package/dist/types/src/controllers/client.d.ts +13 -0
- package/dist/types/src/controllers/collection_registry.d.ts +2 -1
- package/dist/types/src/controllers/data_driver.d.ts +36 -1
- package/dist/types/src/controllers/navigation.d.ts +18 -6
- package/dist/types/src/controllers/registry.d.ts +9 -1
- package/dist/types/src/controllers/side_entity_controller.d.ts +7 -0
- package/dist/types/src/rebase_context.d.ts +17 -0
- package/dist/types/src/types/backend_hooks.d.ts +187 -0
- package/dist/types/src/types/collections.d.ts +31 -11
- package/dist/types/src/types/component_ref.d.ts +47 -0
- package/dist/types/src/types/cron.d.ts +1 -1
- package/dist/types/src/types/entity_views.d.ts +6 -7
- package/dist/types/src/types/formex.d.ts +40 -0
- package/dist/types/src/types/index.d.ts +3 -0
- package/dist/types/src/types/plugins.d.ts +6 -3
- package/dist/types/src/types/properties.d.ts +72 -88
- package/dist/types/src/types/slots.d.ts +20 -10
- package/dist/types/src/types/translations.d.ts +6 -0
- package/examples/firebase/node_modules/esbuild/LICENSE.md +21 -0
- package/examples/firebase/node_modules/esbuild/README.md +3 -0
- package/examples/firebase/node_modules/esbuild/bin/esbuild +220 -0
- package/examples/firebase/node_modules/esbuild/install.js +285 -0
- package/examples/firebase/node_modules/esbuild/lib/main.d.ts +705 -0
- package/examples/firebase/node_modules/esbuild/lib/main.js +2239 -0
- package/examples/firebase/node_modules/esbuild/package.json +46 -0
- package/examples/medmot-staging/frontend/node_modules/esbuild/LICENSE.md +21 -0
- package/examples/medmot-staging/frontend/node_modules/esbuild/README.md +3 -0
- package/examples/medmot-staging/frontend/node_modules/esbuild/bin/esbuild +220 -0
- package/examples/medmot-staging/frontend/node_modules/esbuild/install.js +285 -0
- package/examples/medmot-staging/frontend/node_modules/esbuild/lib/main.d.ts +705 -0
- package/examples/medmot-staging/frontend/node_modules/esbuild/lib/main.js +2239 -0
- package/examples/medmot-staging/frontend/node_modules/esbuild/package.json +46 -0
- package/examples/sdk-demo/node_modules/esbuild/LICENSE.md +21 -0
- package/examples/sdk-demo/node_modules/esbuild/README.md +3 -0
- package/examples/sdk-demo/node_modules/esbuild/bin/esbuild +223 -0
- package/examples/sdk-demo/node_modules/esbuild/install.js +289 -0
- package/examples/sdk-demo/node_modules/esbuild/lib/main.d.ts +716 -0
- package/examples/sdk-demo/node_modules/esbuild/lib/main.js +2242 -0
- package/examples/sdk-demo/node_modules/esbuild/package.json +49 -0
- package/package.json +9 -9
- package/packages/client/node_modules/esbuild/LICENSE.md +21 -0
- package/packages/client/node_modules/esbuild/README.md +3 -0
- package/packages/client/node_modules/esbuild/bin/esbuild +220 -0
- package/packages/client/node_modules/esbuild/install.js +285 -0
- package/packages/client/node_modules/esbuild/lib/main.d.ts +705 -0
- package/packages/client/node_modules/esbuild/lib/main.js +2239 -0
- package/packages/client/node_modules/esbuild/package.json +46 -0
- package/packages/client-postgresql/node_modules/esbuild/LICENSE.md +21 -0
- package/packages/client-postgresql/node_modules/esbuild/README.md +3 -0
- package/packages/client-postgresql/node_modules/esbuild/bin/esbuild +220 -0
- package/packages/client-postgresql/node_modules/esbuild/install.js +285 -0
- package/packages/client-postgresql/node_modules/esbuild/lib/main.d.ts +705 -0
- package/packages/client-postgresql/node_modules/esbuild/lib/main.js +2239 -0
- package/packages/client-postgresql/node_modules/esbuild/package.json +46 -0
- package/packages/common/node_modules/esbuild/LICENSE.md +21 -0
- package/packages/common/node_modules/esbuild/README.md +3 -0
- package/packages/common/node_modules/esbuild/bin/esbuild +220 -0
- package/packages/common/node_modules/esbuild/install.js +285 -0
- package/packages/common/node_modules/esbuild/lib/main.d.ts +705 -0
- package/packages/common/node_modules/esbuild/lib/main.js +2239 -0
- package/packages/common/node_modules/esbuild/package.json +46 -0
- package/packages/server-mongodb/node_modules/esbuild/LICENSE.md +21 -0
- package/packages/server-mongodb/node_modules/esbuild/README.md +3 -0
- package/packages/server-mongodb/node_modules/esbuild/bin/esbuild +220 -0
- package/packages/server-mongodb/node_modules/esbuild/install.js +285 -0
- package/packages/server-mongodb/node_modules/esbuild/lib/main.d.ts +705 -0
- package/packages/server-mongodb/node_modules/esbuild/lib/main.js +2239 -0
- package/packages/server-mongodb/node_modules/esbuild/package.json +46 -0
- package/packages/server-postgresql/node_modules/esbuild/LICENSE.md +21 -0
- package/packages/server-postgresql/node_modules/esbuild/README.md +3 -0
- package/packages/server-postgresql/node_modules/esbuild/bin/esbuild +220 -0
- package/packages/server-postgresql/node_modules/esbuild/install.js +285 -0
- package/packages/server-postgresql/node_modules/esbuild/lib/main.d.ts +705 -0
- package/packages/server-postgresql/node_modules/esbuild/lib/main.js +2239 -0
- package/packages/server-postgresql/node_modules/esbuild/package.json +46 -0
- package/packages/types/node_modules/esbuild/LICENSE.md +21 -0
- package/packages/types/node_modules/esbuild/README.md +3 -0
- package/packages/types/node_modules/esbuild/bin/esbuild +220 -0
- package/packages/types/node_modules/esbuild/install.js +285 -0
- package/packages/types/node_modules/esbuild/lib/main.d.ts +705 -0
- package/packages/types/node_modules/esbuild/lib/main.js +2239 -0
- package/packages/types/node_modules/esbuild/package.json +46 -0
- package/packages/utils/node_modules/esbuild/LICENSE.md +21 -0
- package/packages/utils/node_modules/esbuild/README.md +3 -0
- package/packages/utils/node_modules/esbuild/bin/esbuild +220 -0
- package/packages/utils/node_modules/esbuild/install.js +285 -0
- package/packages/utils/node_modules/esbuild/lib/main.d.ts +705 -0
- package/packages/utils/node_modules/esbuild/lib/main.js +2239 -0
- package/packages/utils/node_modules/esbuild/package.json +46 -0
- package/src/api/errors.ts +3 -2
- package/src/api/rest/api-generator-count.test.ts +113 -0
- package/src/api/rest/api-generator.ts +123 -22
- package/src/api/server.ts +8 -4
- package/src/auth/admin-routes.ts +133 -57
- package/src/auth/apple-oauth.ts +8 -18
- package/src/auth/google-oauth.ts +192 -22
- package/src/auth/index.ts +1 -0
- package/src/auth/rate-limiter.ts +9 -5
- package/src/auth/routes.ts +25 -5
- package/src/collections/loader.ts +3 -3
- package/src/cron/cron-scheduler.test.ts +301 -175
- package/src/cron/cron-scheduler.ts +220 -57
- package/src/cron/index.ts +1 -1
- package/src/init.ts +27 -5
- package/src/storage/LocalStorageController.ts +37 -13
- package/src/storage/S3StorageController.ts +4 -1
- package/src/storage/routes.ts +51 -5
- package/test/backend-hooks-admin.test.ts +394 -0
- package/test/backend-hooks-data.test.ts +408 -0
- package/history_diff.log +0 -385
- package/scratch.ts +0 -9
- package/test-ast.ts +0 -28
- package/test_output.txt +0 -1133
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BackendHooks — Data (REST API) Integration Tests
|
|
3
|
+
*
|
|
4
|
+
* Verifies that DataHooks are correctly applied within RestApiGenerator
|
|
5
|
+
* for all collection CRUD operations.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Hono } from "hono";
|
|
9
|
+
import { RestApiGenerator } from "../src/api/rest/api-generator";
|
|
10
|
+
import { errorHandler } from "../src/api/errors";
|
|
11
|
+
import type { DataDriver } from "@rebasepro/types";
|
|
12
|
+
import type { EntityCollection } from "@rebasepro/types";
|
|
13
|
+
import type { DataHooks } from "@rebasepro/types";
|
|
14
|
+
|
|
15
|
+
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
function createMockDriver(): jest.Mocked<DataDriver> {
|
|
18
|
+
return {
|
|
19
|
+
key: "postgres",
|
|
20
|
+
initialised: true,
|
|
21
|
+
fetchCollection: jest.fn(),
|
|
22
|
+
listenCollection: jest.fn(),
|
|
23
|
+
fetchEntity: jest.fn(),
|
|
24
|
+
listenEntity: jest.fn(),
|
|
25
|
+
saveEntity: jest.fn(),
|
|
26
|
+
deleteEntity: jest.fn(),
|
|
27
|
+
checkUniqueField: jest.fn(),
|
|
28
|
+
countEntities: jest.fn(),
|
|
29
|
+
withAuth: jest.fn(),
|
|
30
|
+
admin: {} as any
|
|
31
|
+
} as unknown as jest.Mocked<DataDriver>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const mockCollections: EntityCollection[] = [
|
|
35
|
+
{ slug: "products", name: "Products", singularName: "Product", properties: {} } as any,
|
|
36
|
+
{ slug: "orders", name: "Orders", singularName: "Order", properties: {} } as any
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
function createApp(mockDriver: jest.Mocked<DataDriver>, hooks?: DataHooks) {
|
|
40
|
+
const app = new Hono();
|
|
41
|
+
app.onError(errorHandler);
|
|
42
|
+
const generator = new RestApiGenerator(mockCollections, mockDriver, hooks);
|
|
43
|
+
app.route("/api", generator.generateRoutes());
|
|
44
|
+
return app;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ═════════════════════════════════════════════════════════════════════════════
|
|
48
|
+
// TESTS
|
|
49
|
+
// ═════════════════════════════════════════════════════════════════════════════
|
|
50
|
+
|
|
51
|
+
describe("DataHooks — REST API", () => {
|
|
52
|
+
let mockDriver: jest.Mocked<DataDriver>;
|
|
53
|
+
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
mockDriver = createMockDriver();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// ── afterRead ─────────────────────────────────────────────────────
|
|
59
|
+
describe("data.afterRead", () => {
|
|
60
|
+
it("transforms entities in GET list response", async () => {
|
|
61
|
+
const hooks: DataHooks = {
|
|
62
|
+
afterRead(slug, entity) {
|
|
63
|
+
// Mask price for non-premium entities
|
|
64
|
+
if (slug === "products") {
|
|
65
|
+
return { ...entity, price: "***" };
|
|
66
|
+
}
|
|
67
|
+
return entity;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const app = createApp(mockDriver, hooks);
|
|
71
|
+
|
|
72
|
+
mockDriver.fetchCollection.mockResolvedValue([
|
|
73
|
+
{ id: "p1", path: "products", values: { name: "Widget", price: 99 } } as any,
|
|
74
|
+
{ id: "p2", path: "products", values: { name: "Gadget", price: 199 } } as any
|
|
75
|
+
]);
|
|
76
|
+
mockDriver.countEntities!.mockResolvedValue(2);
|
|
77
|
+
|
|
78
|
+
const res = await app.request("/api/products");
|
|
79
|
+
expect(res.status).toBe(200);
|
|
80
|
+
|
|
81
|
+
const body = await res.json() as any;
|
|
82
|
+
expect(body.data).toHaveLength(2);
|
|
83
|
+
expect(body.data[0].price).toBe("***");
|
|
84
|
+
expect(body.data[1].price).toBe("***");
|
|
85
|
+
// Original field should still be there
|
|
86
|
+
expect(body.data[0].name).toBe("Widget");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("filters out entities by returning null", async () => {
|
|
90
|
+
const hooks: DataHooks = {
|
|
91
|
+
afterRead(slug, entity) {
|
|
92
|
+
// Hide draft products
|
|
93
|
+
if (entity.status === "draft") return null;
|
|
94
|
+
return entity;
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const app = createApp(mockDriver, hooks);
|
|
98
|
+
|
|
99
|
+
mockDriver.fetchCollection.mockResolvedValue([
|
|
100
|
+
{ id: "p1", path: "products", values: { name: "Published", status: "active" } } as any,
|
|
101
|
+
{ id: "p2", path: "products", values: { name: "Draft", status: "draft" } } as any,
|
|
102
|
+
{ id: "p3", path: "products", values: { name: "Also Published", status: "active" } } as any
|
|
103
|
+
]);
|
|
104
|
+
mockDriver.countEntities!.mockResolvedValue(3);
|
|
105
|
+
|
|
106
|
+
const res = await app.request("/api/products");
|
|
107
|
+
const body = await res.json() as any;
|
|
108
|
+
expect(body.data).toHaveLength(2);
|
|
109
|
+
expect(body.data.map((d: any) => d.name)).toEqual(["Published", "Also Published"]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("transforms single entity GET response", async () => {
|
|
113
|
+
const hooks: DataHooks = {
|
|
114
|
+
afterRead(slug, entity) {
|
|
115
|
+
return { ...entity, _readAt: "2024-01-01" };
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const app = createApp(mockDriver, hooks);
|
|
119
|
+
|
|
120
|
+
mockDriver.fetchEntity.mockResolvedValue(
|
|
121
|
+
{ id: "p1", path: "products", values: { name: "Widget" } } as any
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const res = await app.request("/api/products/p1");
|
|
125
|
+
expect(res.status).toBe(200);
|
|
126
|
+
|
|
127
|
+
const body = await res.json() as any;
|
|
128
|
+
expect(body.name).toBe("Widget");
|
|
129
|
+
expect(body._readAt).toBe("2024-01-01");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("returns 404 when afterRead filters a single entity", async () => {
|
|
133
|
+
const hooks: DataHooks = {
|
|
134
|
+
afterRead(slug, entity) {
|
|
135
|
+
if (entity.id === "hidden") return null;
|
|
136
|
+
return entity;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
const app = createApp(mockDriver, hooks);
|
|
140
|
+
|
|
141
|
+
mockDriver.fetchEntity.mockResolvedValue(
|
|
142
|
+
{ id: "hidden", path: "products", values: { name: "Secret" } } as any
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const res = await app.request("/api/products/hidden");
|
|
146
|
+
expect(res.status).toBe(404);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("only affects targeted collection slug", async () => {
|
|
150
|
+
const hooks: DataHooks = {
|
|
151
|
+
afterRead(slug, entity) {
|
|
152
|
+
if (slug === "products") {
|
|
153
|
+
return { ...entity, hooked: true };
|
|
154
|
+
}
|
|
155
|
+
return entity;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const app = createApp(mockDriver, hooks);
|
|
159
|
+
|
|
160
|
+
// Products should be hooked
|
|
161
|
+
mockDriver.fetchEntity.mockResolvedValueOnce(
|
|
162
|
+
{ id: "p1", path: "products", values: { name: "Widget" } } as any
|
|
163
|
+
);
|
|
164
|
+
const prodRes = await app.request("/api/products/p1");
|
|
165
|
+
const prodBody = await prodRes.json() as any;
|
|
166
|
+
expect(prodBody.hooked).toBe(true);
|
|
167
|
+
|
|
168
|
+
// Orders should NOT be hooked
|
|
169
|
+
mockDriver.fetchEntity.mockResolvedValueOnce(
|
|
170
|
+
{ id: "o1", path: "orders", values: { total: 42 } } as any
|
|
171
|
+
);
|
|
172
|
+
const orderRes = await app.request("/api/orders/o1");
|
|
173
|
+
const orderBody = await orderRes.json() as any;
|
|
174
|
+
expect(orderBody.hooked).toBeUndefined();
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// ── beforeSave ────────────────────────────────────────────────────
|
|
179
|
+
describe("data.beforeSave", () => {
|
|
180
|
+
it("transforms values before POST (create)", async () => {
|
|
181
|
+
const hooks: DataHooks = {
|
|
182
|
+
beforeSave(slug, values, entityId) {
|
|
183
|
+
return { ...values, slug: values.name?.toString().toLowerCase().replace(/\s+/g, "-") };
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const app = createApp(mockDriver, hooks);
|
|
187
|
+
|
|
188
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
189
|
+
{ id: "new-1", path: "products", values: { name: "Cool Widget", slug: "cool-widget" } } as any
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const res = await app.request("/api/products", {
|
|
193
|
+
method: "POST",
|
|
194
|
+
headers: { "Content-Type": "application/json" },
|
|
195
|
+
body: JSON.stringify({ name: "Cool Widget" })
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(res.status).toBe(201);
|
|
199
|
+
expect(mockDriver.saveEntity).toHaveBeenCalledWith(
|
|
200
|
+
expect.objectContaining({
|
|
201
|
+
values: expect.objectContaining({ slug: "cool-widget" })
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("transforms values before PUT (update)", async () => {
|
|
207
|
+
const hooks: DataHooks = {
|
|
208
|
+
beforeSave(slug, values, entityId) {
|
|
209
|
+
// Add an updatedBy field
|
|
210
|
+
return { ...values, updatedBy: "hook" };
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
const app = createApp(mockDriver, hooks);
|
|
214
|
+
|
|
215
|
+
mockDriver.fetchEntity.mockResolvedValue({ id: "p1", path: "products", values: {} } as any);
|
|
216
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
217
|
+
{ id: "p1", path: "products", values: { name: "Updated", updatedBy: "hook" } } as any
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
const res = await app.request("/api/products/p1", {
|
|
221
|
+
method: "PUT",
|
|
222
|
+
headers: { "Content-Type": "application/json" },
|
|
223
|
+
body: JSON.stringify({ name: "Updated" })
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
expect(res.status).toBe(200);
|
|
227
|
+
expect(mockDriver.saveEntity).toHaveBeenCalledWith(
|
|
228
|
+
expect.objectContaining({
|
|
229
|
+
values: expect.objectContaining({ updatedBy: "hook" })
|
|
230
|
+
})
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("receives entityId=undefined on POST, actual id on PUT", async () => {
|
|
235
|
+
const beforeSaveSpy = jest.fn((slug, values, entityId) => values);
|
|
236
|
+
const hooks: DataHooks = { beforeSave: beforeSaveSpy };
|
|
237
|
+
const app = createApp(mockDriver, hooks);
|
|
238
|
+
|
|
239
|
+
// POST
|
|
240
|
+
mockDriver.saveEntity.mockResolvedValueOnce(
|
|
241
|
+
{ id: "new-1", path: "products", values: { name: "A" } } as any
|
|
242
|
+
);
|
|
243
|
+
await app.request("/api/products", {
|
|
244
|
+
method: "POST",
|
|
245
|
+
headers: { "Content-Type": "application/json" },
|
|
246
|
+
body: JSON.stringify({ name: "A" })
|
|
247
|
+
});
|
|
248
|
+
expect(beforeSaveSpy.mock.calls[0][2]).toBeUndefined(); // entityId
|
|
249
|
+
|
|
250
|
+
// PUT
|
|
251
|
+
mockDriver.fetchEntity.mockResolvedValueOnce({ id: "p1", path: "products", values: {} } as any);
|
|
252
|
+
mockDriver.saveEntity.mockResolvedValueOnce(
|
|
253
|
+
{ id: "p1", path: "products", values: { name: "B" } } as any
|
|
254
|
+
);
|
|
255
|
+
await app.request("/api/products/p1", {
|
|
256
|
+
method: "PUT",
|
|
257
|
+
headers: { "Content-Type": "application/json" },
|
|
258
|
+
body: JSON.stringify({ name: "B" })
|
|
259
|
+
});
|
|
260
|
+
expect(beforeSaveSpy.mock.calls[1][2]).toBe("p1"); // entityId
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("aborts save when beforeSave throws", async () => {
|
|
264
|
+
const hooks: DataHooks = {
|
|
265
|
+
beforeSave(slug, values) {
|
|
266
|
+
if (!values.name) throw new Error("Name is required");
|
|
267
|
+
return values;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const app = createApp(mockDriver, hooks);
|
|
271
|
+
|
|
272
|
+
const res = await app.request("/api/products", {
|
|
273
|
+
method: "POST",
|
|
274
|
+
headers: { "Content-Type": "application/json" },
|
|
275
|
+
body: JSON.stringify({ price: 99 })
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Should get an error status
|
|
279
|
+
expect(res.status).toBeGreaterThanOrEqual(400);
|
|
280
|
+
expect(mockDriver.saveEntity).not.toHaveBeenCalled();
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// ── afterSave ─────────────────────────────────────────────────────
|
|
285
|
+
describe("data.afterSave", () => {
|
|
286
|
+
it("fires afterSave after POST", async () => {
|
|
287
|
+
const afterSaveSpy = jest.fn();
|
|
288
|
+
const hooks: DataHooks = { afterSave: afterSaveSpy };
|
|
289
|
+
const app = createApp(mockDriver, hooks);
|
|
290
|
+
|
|
291
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
292
|
+
{ id: "new-1", path: "products", values: { name: "Widget" } } as any
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
const res = await app.request("/api/products", {
|
|
296
|
+
method: "POST",
|
|
297
|
+
headers: { "Content-Type": "application/json" },
|
|
298
|
+
body: JSON.stringify({ name: "Widget" })
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
expect(res.status).toBe(201);
|
|
302
|
+
await new Promise(r => setTimeout(r, 50));
|
|
303
|
+
expect(afterSaveSpy).toHaveBeenCalledTimes(1);
|
|
304
|
+
expect(afterSaveSpy).toHaveBeenCalledWith(
|
|
305
|
+
"products",
|
|
306
|
+
expect.objectContaining({ id: "new-1", name: "Widget" }),
|
|
307
|
+
expect.objectContaining({ method: "POST" })
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("fires afterSave after PUT", async () => {
|
|
312
|
+
const afterSaveSpy = jest.fn();
|
|
313
|
+
const hooks: DataHooks = { afterSave: afterSaveSpy };
|
|
314
|
+
const app = createApp(mockDriver, hooks);
|
|
315
|
+
|
|
316
|
+
mockDriver.fetchEntity.mockResolvedValue({ id: "p1", path: "products", values: {} } as any);
|
|
317
|
+
mockDriver.saveEntity.mockResolvedValue(
|
|
318
|
+
{ id: "p1", path: "products", values: { name: "Updated" } } as any
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
await app.request("/api/products/p1", {
|
|
322
|
+
method: "PUT",
|
|
323
|
+
headers: { "Content-Type": "application/json" },
|
|
324
|
+
body: JSON.stringify({ name: "Updated" })
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
await new Promise(r => setTimeout(r, 50));
|
|
328
|
+
expect(afterSaveSpy).toHaveBeenCalledWith(
|
|
329
|
+
"products",
|
|
330
|
+
expect.objectContaining({ id: "p1" }),
|
|
331
|
+
expect.objectContaining({ method: "PUT" })
|
|
332
|
+
);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
// ── beforeDelete / afterDelete ───────────────────────────────────
|
|
337
|
+
describe("data.beforeDelete / afterDelete", () => {
|
|
338
|
+
it("aborts deletion when beforeDelete throws", async () => {
|
|
339
|
+
const hooks: DataHooks = {
|
|
340
|
+
beforeDelete(slug, entityId) {
|
|
341
|
+
if (entityId === "protected") {
|
|
342
|
+
throw new Error("Cannot delete protected entity");
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
const app = createApp(mockDriver, hooks);
|
|
347
|
+
|
|
348
|
+
mockDriver.fetchEntity.mockResolvedValue(
|
|
349
|
+
{ id: "protected", path: "products", values: {} } as any
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
const res = await app.request("/api/products/protected", { method: "DELETE" });
|
|
353
|
+
expect(res.status).toBe(500);
|
|
354
|
+
expect(mockDriver.deleteEntity).not.toHaveBeenCalled();
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("allows deletion when beforeDelete does not throw", async () => {
|
|
358
|
+
const beforeDeleteSpy = jest.fn();
|
|
359
|
+
const hooks: DataHooks = { beforeDelete: beforeDeleteSpy };
|
|
360
|
+
const app = createApp(mockDriver, hooks);
|
|
361
|
+
|
|
362
|
+
const existingEntity = { id: "p1", path: "products", values: {} } as any;
|
|
363
|
+
mockDriver.fetchEntity.mockResolvedValue(existingEntity);
|
|
364
|
+
mockDriver.deleteEntity.mockResolvedValue();
|
|
365
|
+
|
|
366
|
+
const res = await app.request("/api/products/p1", { method: "DELETE" });
|
|
367
|
+
expect(res.status).toBe(204);
|
|
368
|
+
expect(beforeDeleteSpy).toHaveBeenCalledWith(
|
|
369
|
+
"products", "p1", expect.objectContaining({ method: "DELETE" })
|
|
370
|
+
);
|
|
371
|
+
expect(mockDriver.deleteEntity).toHaveBeenCalled();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("fires afterDelete after successful deletion", async () => {
|
|
375
|
+
const afterDeleteSpy = jest.fn();
|
|
376
|
+
const hooks: DataHooks = { afterDelete: afterDeleteSpy };
|
|
377
|
+
const app = createApp(mockDriver, hooks);
|
|
378
|
+
|
|
379
|
+
mockDriver.fetchEntity.mockResolvedValue({ id: "p1", path: "products", values: {} } as any);
|
|
380
|
+
mockDriver.deleteEntity.mockResolvedValue();
|
|
381
|
+
|
|
382
|
+
await app.request("/api/products/p1", { method: "DELETE" });
|
|
383
|
+
await new Promise(r => setTimeout(r, 50));
|
|
384
|
+
|
|
385
|
+
expect(afterDeleteSpy).toHaveBeenCalledWith(
|
|
386
|
+
"products", "p1", expect.objectContaining({ method: "DELETE" })
|
|
387
|
+
);
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
// ── no hooks (passthrough) ──────────────────────────────────────
|
|
392
|
+
describe("no hooks configured", () => {
|
|
393
|
+
it("returns data unchanged when no hooks are provided", async () => {
|
|
394
|
+
const app = createApp(mockDriver); // no hooks
|
|
395
|
+
mockDriver.fetchCollection.mockResolvedValue([
|
|
396
|
+
{ id: "p1", path: "products", values: { name: "Widget" } } as any
|
|
397
|
+
]);
|
|
398
|
+
mockDriver.countEntities!.mockResolvedValue(1);
|
|
399
|
+
|
|
400
|
+
const res = await app.request("/api/products");
|
|
401
|
+
expect(res.status).toBe(200);
|
|
402
|
+
|
|
403
|
+
const body = await res.json() as any;
|
|
404
|
+
expect(body.data).toHaveLength(1);
|
|
405
|
+
expect(body.data[0].name).toBe("Widget");
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
});
|