Skip to content

Commit

Permalink
nats api: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Jan 27, 2025
1 parent b33ec24 commit 7d8b198
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 22 deletions.
3 changes: 2 additions & 1 deletion docs/nats/devlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ This is working well!
TODO:
- [ ] build full SyncTable on top of my current implementation of synctablekvatomic, to _make sure it is sufficient_
- [x] build full proof of concept SyncTable on top of my current implementation of synctablekvatomic, to _make sure it is sufficient_
- this worked and wasn't too difficult
THEN do the following to make it robust and scalable
Expand Down
71 changes: 63 additions & 8 deletions src/packages/nats/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,73 @@ import type { Customize } from "@cocalc/util/db-schema/server-settings";
import { isValidUUID } from "@cocalc/util/misc";

export interface HubApi {
getCustomize: (fields?: string[]) => Promise<Customize>;
userQuery: (opts: {
project_id?: string;
query: any;
options?: any[];
}) => Promise<any>;
system: {
getCustomize: (fields?: string[]) => Promise<Customize>;
};

db: {
userQuery: (opts: {
project_id?: string;
account_id?: string;
query: any;
options?: any[];
}) => Promise<any>;
};

purchases: {
getBalance: ({ account_id }) => Promise<number>;
getMinBalance: (account_id) => Promise<number>;
};
}

const authFirst = ({ args, account_id, project_id }) => {
if (args[0] == null) {
args[0] = {} as any;
}
if (account_id) {
args[0].account_id = account_id;
} else if (project_id) {
args[0].project_id = project_id;
}
return args;
};

const noAuth = ({ args }) => args;

const HubApiStructure = {
system: {
getCustomize: noAuth,
},
db: {
userQuery: authFirst,
},
purchases: {
getBalance: ({ account_id }) => {
return [{ account_id }];
},
getMinBalance: ({ account_id }) => [account_id],
},
} as const;

export function transformArgs({ name, args, account_id, project_id }) {
const [group, functionName] = name.split(".");
return HubApiStructure[group]?.[functionName]({
args,
account_id,
project_id,
});
}

export function initHubApi(callHubApi): HubApi {
const hubApi: any = {};
for (const name of ["getCustomize", "userQuery"]) {
hubApi[name] = async (...args) => await callHubApi({ name, args });
for (const group in HubApiStructure) {
if (hubApi[group] == null) {
hubApi[group] = {};
}
for (const functionName in HubApiStructure[group]) {
hubApi[group][functionName] = async (...args) =>
await callHubApi({ name: `${group}.${functionName}`, args });
}
}
return hubApi as HubApi;
}
Expand Down
2 changes: 1 addition & 1 deletion src/packages/nats/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"outDir": "dist"
},
"exclude": ["node_modules", "dist", "test"],
"references": [{ "path": "../database" }]
"references": [{ "path": "../util" }]
}
29 changes: 17 additions & 12 deletions src/packages/server/nats/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ To view all requests (and replies) in realtime:

import { JSONCodec } from "nats";
import getLogger from "@cocalc/backend/logger";
import { type HubApi, getUserId } from "@cocalc/nats/api/index";
import { type HubApi, getUserId, transformArgs } from "@cocalc/nats/api/index";

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

Expand Down Expand Up @@ -65,23 +65,28 @@ async function handleApiRequest(mesg) {

import userQuery from "@cocalc/database/user-query";
import getCustomize from "@cocalc/database/settings/customize";
import getBalance from "@cocalc/server/purchases/get-balance";
import getMinBalance from "@cocalc/server/purchases/get-min-balance";

const hubApi: HubApi = {
getCustomize,
userQuery,
system: {
getCustomize,
},
db: {
userQuery,
},
purchases: {
getBalance,
getMinBalance,
},
};

async function getResponse({ name, args, account_id, project_id }) {
const f = hubApi[name];
const [group, functionName] = name.split(".");
const f = hubApi[group]?.[functionName];
if (f == null) {
throw Error(`unknown function '${name}'`);
}
if (name == "userQuery" && args[0] != null) {
if (account_id) {
args[0].account_id = account_id;
} else if (project_id) {
args[0].project_id = project_id;
}
}
return await f(...args);
const args2 = transformArgs({ name, args, account_id, project_id });
return await f(...args2);
}

0 comments on commit 7d8b198

Please sign in to comment.