Skip to content

Commit 7d8b198

Browse files
committed
nats api: refactoring
1 parent b33ec24 commit 7d8b198

File tree

4 files changed

+83
-22
lines changed

4 files changed

+83
-22
lines changed

docs/nats/devlog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ This is working well!
279279
280280
TODO:
281281
282-
- [ ] build full SyncTable on top of my current implementation of synctablekvatomic, to _make sure it is sufficient_
282+
- [x] build full proof of concept SyncTable on top of my current implementation of synctablekvatomic, to _make sure it is sufficient_
283+
- this worked and wasn't too difficult
283284
284285
THEN do the following to make it robust and scalable
285286

src/packages/nats/api/index.ts

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,73 @@ import type { Customize } from "@cocalc/util/db-schema/server-settings";
22
import { isValidUUID } from "@cocalc/util/misc";
33

44
export interface HubApi {
5-
getCustomize: (fields?: string[]) => Promise<Customize>;
6-
userQuery: (opts: {
7-
project_id?: string;
8-
query: any;
9-
options?: any[];
10-
}) => Promise<any>;
5+
system: {
6+
getCustomize: (fields?: string[]) => Promise<Customize>;
7+
};
8+
9+
db: {
10+
userQuery: (opts: {
11+
project_id?: string;
12+
account_id?: string;
13+
query: any;
14+
options?: any[];
15+
}) => Promise<any>;
16+
};
17+
18+
purchases: {
19+
getBalance: ({ account_id }) => Promise<number>;
20+
getMinBalance: (account_id) => Promise<number>;
21+
};
22+
}
23+
24+
const authFirst = ({ args, account_id, project_id }) => {
25+
if (args[0] == null) {
26+
args[0] = {} as any;
27+
}
28+
if (account_id) {
29+
args[0].account_id = account_id;
30+
} else if (project_id) {
31+
args[0].project_id = project_id;
32+
}
33+
return args;
34+
};
35+
36+
const noAuth = ({ args }) => args;
37+
38+
const HubApiStructure = {
39+
system: {
40+
getCustomize: noAuth,
41+
},
42+
db: {
43+
userQuery: authFirst,
44+
},
45+
purchases: {
46+
getBalance: ({ account_id }) => {
47+
return [{ account_id }];
48+
},
49+
getMinBalance: ({ account_id }) => [account_id],
50+
},
51+
} as const;
52+
53+
export function transformArgs({ name, args, account_id, project_id }) {
54+
const [group, functionName] = name.split(".");
55+
return HubApiStructure[group]?.[functionName]({
56+
args,
57+
account_id,
58+
project_id,
59+
});
1160
}
1261

1362
export function initHubApi(callHubApi): HubApi {
1463
const hubApi: any = {};
15-
for (const name of ["getCustomize", "userQuery"]) {
16-
hubApi[name] = async (...args) => await callHubApi({ name, args });
64+
for (const group in HubApiStructure) {
65+
if (hubApi[group] == null) {
66+
hubApi[group] = {};
67+
}
68+
for (const functionName in HubApiStructure[group]) {
69+
hubApi[group][functionName] = async (...args) =>
70+
await callHubApi({ name: `${group}.${functionName}`, args });
71+
}
1772
}
1873
return hubApi as HubApi;
1974
}

src/packages/nats/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"outDir": "dist"
66
},
77
"exclude": ["node_modules", "dist", "test"],
8-
"references": [{ "path": "../database" }]
8+
"references": [{ "path": "../util" }]
99
}

src/packages/server/nats/api.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ To view all requests (and replies) in realtime:
2626

2727
import { JSONCodec } from "nats";
2828
import getLogger from "@cocalc/backend/logger";
29-
import { type HubApi, getUserId } from "@cocalc/nats/api/index";
29+
import { type HubApi, getUserId, transformArgs } from "@cocalc/nats/api/index";
3030

3131
const logger = getLogger("server:nats:api");
3232

@@ -65,23 +65,28 @@ async function handleApiRequest(mesg) {
6565

6666
import userQuery from "@cocalc/database/user-query";
6767
import getCustomize from "@cocalc/database/settings/customize";
68+
import getBalance from "@cocalc/server/purchases/get-balance";
69+
import getMinBalance from "@cocalc/server/purchases/get-min-balance";
6870

6971
const hubApi: HubApi = {
70-
getCustomize,
71-
userQuery,
72+
system: {
73+
getCustomize,
74+
},
75+
db: {
76+
userQuery,
77+
},
78+
purchases: {
79+
getBalance,
80+
getMinBalance,
81+
},
7282
};
7383

7484
async function getResponse({ name, args, account_id, project_id }) {
75-
const f = hubApi[name];
85+
const [group, functionName] = name.split(".");
86+
const f = hubApi[group]?.[functionName];
7687
if (f == null) {
7788
throw Error(`unknown function '${name}'`);
7889
}
79-
if (name == "userQuery" && args[0] != null) {
80-
if (account_id) {
81-
args[0].account_id = account_id;
82-
} else if (project_id) {
83-
args[0].project_id = project_id;
84-
}
85-
}
86-
return await f(...args);
90+
const args2 = transformArgs({ name, args, account_id, project_id });
91+
return await f(...args2);
8792
}

0 commit comments

Comments
 (0)