@ketrics/ketrics-cli 0.3.0 → 0.4.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/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +1 -1
- package/templates/HelloWorld/frontend/src/mocks/handlers.ts +149 -0
- package/templates/HelloWorld/frontend/src/mocks/mock-client.ts +45 -0
- package/templates/HelloWorld/frontend/src/services/index.ts +30 -12
- package/templates/HelloWorld/frontend/src/vite-env.d.ts +1 -0
package/dist/src/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.4.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/src/version.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock Backend Handlers
|
|
3
|
+
*
|
|
4
|
+
* Define mock responses for your backend functions here.
|
|
5
|
+
* These are used automatically when running `npm run dev` (local development)
|
|
6
|
+
* and are completely excluded from production builds.
|
|
7
|
+
*
|
|
8
|
+
* Each key corresponds to a backend function name exported from backend/src/index.ts.
|
|
9
|
+
* The handler receives the same payload your backend function would receive
|
|
10
|
+
* and should return the same response shape.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* 1. Add a handler for each backend function you want to mock
|
|
14
|
+
* 2. Run `npm run dev` - the frontend will use these handlers instead of the runtime API
|
|
15
|
+
* 3. Run `npm run build` / `ketrics deploy` - mock code is stripped out entirely
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
type MockHandler = (payload?: unknown) => unknown | Promise<unknown>;
|
|
19
|
+
|
|
20
|
+
const handlers: Record<string, MockHandler> = {
|
|
21
|
+
echo: (payload) => ({
|
|
22
|
+
payload,
|
|
23
|
+
context: {
|
|
24
|
+
tenant: { id: "mock-tenant-id", code: "mock-tenant", name: "Mock Tenant" },
|
|
25
|
+
application: { id: "mock-app-id", code: "mock-app", name: "Mock App", version: "1.0.0", deploymentId: "mock-deploy" },
|
|
26
|
+
requestor: { type: "user", userId: "mock-user-id", email: "dev@localhost", name: "Local Developer", applicationPermissions: [] },
|
|
27
|
+
runtime: { nodeVersion: "18.x", runtime: "mock", region: "local" },
|
|
28
|
+
environment: {},
|
|
29
|
+
},
|
|
30
|
+
}),
|
|
31
|
+
|
|
32
|
+
info: () => ({
|
|
33
|
+
tenant: { id: "mock-tenant-id", code: "mock-tenant", name: "Mock Tenant" },
|
|
34
|
+
application: { id: "mock-app-id", code: "mock-app", name: "Mock App", version: "1.0.0" },
|
|
35
|
+
runtime: { nodeVersion: "18.x", runtime: "mock", region: "local" },
|
|
36
|
+
}),
|
|
37
|
+
|
|
38
|
+
fetchExternalApi: () => ({
|
|
39
|
+
data: { id: 1, title: "Mock Post", body: "This is a mock response from the local dev server." },
|
|
40
|
+
fetchedAt: new Date().toISOString(),
|
|
41
|
+
}),
|
|
42
|
+
|
|
43
|
+
// --- Volumes ---
|
|
44
|
+
|
|
45
|
+
saveFile: () => ({
|
|
46
|
+
jsonFile: { key: "output/data.json", etag: "mock-etag-1", size: 128 },
|
|
47
|
+
textFile: { key: "output/hello.txt", etag: "mock-etag-2", size: 19 },
|
|
48
|
+
}),
|
|
49
|
+
|
|
50
|
+
readFile: () => ({
|
|
51
|
+
contentType: "application/json",
|
|
52
|
+
contentLength: 128,
|
|
53
|
+
lastModified: new Date().toISOString(),
|
|
54
|
+
parsed: { generatedBy: "mock-app", generatedAt: new Date().toISOString(), tenant: "mock-tenant" },
|
|
55
|
+
}),
|
|
56
|
+
|
|
57
|
+
listFiles: (payload: unknown) => {
|
|
58
|
+
const { prefix } = (payload as { prefix?: string }) || {};
|
|
59
|
+
return {
|
|
60
|
+
files: [
|
|
61
|
+
{ key: `${prefix || "output/"}data.json`, size: 128, lastModified: new Date().toISOString(), contentType: "application/json" },
|
|
62
|
+
{ key: `${prefix || "output/"}hello.txt`, size: 19, lastModified: new Date().toISOString(), contentType: "text/plain" },
|
|
63
|
+
],
|
|
64
|
+
count: 2,
|
|
65
|
+
isTruncated: false,
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
generateDownloadUrl: () => ({
|
|
70
|
+
url: "https://example.com/mock-download-url",
|
|
71
|
+
expiresAt: new Date(Date.now() + 3600_000).toISOString(),
|
|
72
|
+
}),
|
|
73
|
+
|
|
74
|
+
copyFile: () => ({
|
|
75
|
+
sourceKey: "output/data.json",
|
|
76
|
+
destinationKey: "backup/data-backup.json",
|
|
77
|
+
etag: "mock-etag-copy",
|
|
78
|
+
}),
|
|
79
|
+
|
|
80
|
+
// --- Database ---
|
|
81
|
+
|
|
82
|
+
queryUsers: (payload: unknown) => {
|
|
83
|
+
const { limit } = (payload as { limit?: number }) || {};
|
|
84
|
+
const users = [
|
|
85
|
+
{ id: 1, name: "Alice Johnson", email: "alice@example.com" },
|
|
86
|
+
{ id: 2, name: "Bob Smith", email: "bob@example.com" },
|
|
87
|
+
{ id: 3, name: "Carol Davis", email: "carol@example.com" },
|
|
88
|
+
{ id: 4, name: "Dan Wilson", email: "dan@example.com" },
|
|
89
|
+
{ id: 5, name: "Eve Martinez", email: "eve@example.com" },
|
|
90
|
+
];
|
|
91
|
+
const sliced = users.slice(0, limit || 10);
|
|
92
|
+
return { users: sliced, rowCount: sliced.length };
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
insertRecord: (payload: unknown) => {
|
|
96
|
+
const { name, email } = (payload as { name?: string; email?: string }) || {};
|
|
97
|
+
if (!name || !email) throw new Error("name and email are required");
|
|
98
|
+
return { affectedRows: 1, insertId: Math.floor(Math.random() * 1000) + 100 };
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
transferFunds: (payload: unknown) => {
|
|
102
|
+
const { amount } = (payload as { amount?: number }) || {};
|
|
103
|
+
if (!amount || amount <= 0) throw new Error("Amount must be positive");
|
|
104
|
+
return { transferred: amount };
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
// --- Documents ---
|
|
108
|
+
|
|
109
|
+
createSimplePdf: () => ({
|
|
110
|
+
downloadUrl: "https://example.com/mock-simple.pdf",
|
|
111
|
+
message: "[Mock] PDF would be generated by the backend",
|
|
112
|
+
}),
|
|
113
|
+
|
|
114
|
+
createInvoicePdf: () => ({
|
|
115
|
+
downloadUrl: "https://example.com/mock-invoice.pdf",
|
|
116
|
+
message: "[Mock] Invoice PDF would be generated by the backend",
|
|
117
|
+
}),
|
|
118
|
+
|
|
119
|
+
createSpreadsheet: () => ({
|
|
120
|
+
downloadUrl: "https://example.com/mock-spreadsheet.xlsx",
|
|
121
|
+
message: "[Mock] Spreadsheet would be generated by the backend",
|
|
122
|
+
}),
|
|
123
|
+
|
|
124
|
+
exportDataToExcel: () => ({
|
|
125
|
+
downloadUrl: "https://example.com/mock-export.xlsx",
|
|
126
|
+
message: "[Mock] Excel export would be generated by the backend",
|
|
127
|
+
}),
|
|
128
|
+
|
|
129
|
+
// --- Messaging & Jobs ---
|
|
130
|
+
|
|
131
|
+
sendNotification: (payload: unknown) => {
|
|
132
|
+
const { subject, body } = (payload as { subject?: string; body?: string }) || {};
|
|
133
|
+
return { sent: true, subject, body, message: "[Mock] Notification would be sent by the backend" };
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
scheduleBackgroundJob: () => ({
|
|
137
|
+
jobId: `mock-job-${Date.now()}`,
|
|
138
|
+
status: "scheduled",
|
|
139
|
+
message: "[Mock] Job would be scheduled by the backend",
|
|
140
|
+
}),
|
|
141
|
+
|
|
142
|
+
getSecret: () => ({
|
|
143
|
+
value: "mock-secret-value",
|
|
144
|
+
message: "[Mock] Secret would be retrieved from the backend",
|
|
145
|
+
}),
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export { handlers };
|
|
149
|
+
export type { MockHandler };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock API Client
|
|
3
|
+
*
|
|
4
|
+
* Replaces the real API client during local development (`npm run dev`).
|
|
5
|
+
* Routes function calls to the mock handlers defined in ./handlers.ts
|
|
6
|
+
* instead of making HTTP requests to the Ketrics Runtime API.
|
|
7
|
+
*
|
|
8
|
+
* This file is tree-shaken out of production builds by Vite.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { handlers } from "./handlers";
|
|
12
|
+
|
|
13
|
+
class MockAPIClient {
|
|
14
|
+
async run(fnName: string, payload?: unknown) {
|
|
15
|
+
const handler = handlers[fnName];
|
|
16
|
+
|
|
17
|
+
if (!handler) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`[Mock] No handler defined for "${fnName}". ` +
|
|
20
|
+
`Add it to src/mocks/handlers.ts to use it in local development.`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Simulate network latency
|
|
25
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const result = await handler(payload);
|
|
29
|
+
console.log(`[Mock] ${fnName}`, { payload, result });
|
|
30
|
+
return { success: true, result };
|
|
31
|
+
} catch (err) {
|
|
32
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
33
|
+
console.error(`[Mock] ${fnName} error:`, message);
|
|
34
|
+
throw new Error(message);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createMockClient() {
|
|
40
|
+
console.log(
|
|
41
|
+
"%c[Ketrics] Running with mock backend — edit src/mocks/handlers.ts to customize responses",
|
|
42
|
+
"color: #f59e0b; font-weight: bold;"
|
|
43
|
+
);
|
|
44
|
+
return new MockAPIClient();
|
|
45
|
+
}
|
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
import { createAuthManager } from "@ketrics/sdk-frontend";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
auth.initAutoRefresh({
|
|
7
|
-
refreshBuffer: 60,
|
|
8
|
-
onTokenUpdated: () => {
|
|
9
|
-
// Token is refreshed automatically and retrieved via getAccessToken()
|
|
10
|
-
},
|
|
11
|
-
});
|
|
3
|
+
interface APIClientInterface {
|
|
4
|
+
run(fnName: string, payload?: unknown): Promise<unknown>;
|
|
5
|
+
}
|
|
12
6
|
|
|
13
|
-
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
// Real API Client — used when deployed (production builds)
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
class APIClient implements APIClientInterface {
|
|
14
11
|
private auth;
|
|
15
12
|
|
|
16
|
-
constructor(authManager: typeof
|
|
13
|
+
constructor(authManager: ReturnType<typeof createAuthManager>) {
|
|
17
14
|
this.auth = authManager;
|
|
18
15
|
}
|
|
19
16
|
|
|
@@ -50,6 +47,27 @@ class APIClient {
|
|
|
50
47
|
}
|
|
51
48
|
}
|
|
52
49
|
|
|
53
|
-
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Client initialization — mock in dev, real in production
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
async function createClient(): Promise<APIClientInterface> {
|
|
54
|
+
if (import.meta.env.DEV) {
|
|
55
|
+
// Dynamic import keeps mock code in a separate chunk that Vite
|
|
56
|
+
// completely excludes from production builds.
|
|
57
|
+
const { createMockClient } = await import("../mocks/mock-client");
|
|
58
|
+
return createMockClient();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const auth = createAuthManager();
|
|
62
|
+
auth.initAutoRefresh({
|
|
63
|
+
refreshBuffer: 60,
|
|
64
|
+
onTokenUpdated: () => {
|
|
65
|
+
// Token is refreshed automatically and retrieved via getAccessToken()
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
return new APIClient(auth);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const apiClient = await createClient();
|
|
54
72
|
|
|
55
73
|
export { apiClient };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|