Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hpx7 committed May 19, 2024
1 parent a418251 commit 16a7f4a
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 615 deletions.
82 changes: 82 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"LICENSE"
],
"dependencies": {
"@hathora/delta-pack": "0.0.12",
"@node-loader/core": "hpx7/node-loader-core#patch-1",
"axios": "0.27.2",
"chalk": "4.1.2",
Expand Down
79 changes: 66 additions & 13 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import { load } from "js-yaml";
import { compile } from "handlebars";
import { copySync, existsSync, outputFileSync, readdirSync, readFileSync, statSync } from "fs-extra";
import "./helpers";
import {
Type,
StringType,
IntType,
ChildType,
ReferenceType,
UnionType,
EnumType,
ObjectType,
Modifier,
codegenTypescript,
BooleanType,
} from "@hathora/delta-pack";

const TypeArgs = z.union([z.string(), z.array(z.string()), z.record(z.string())]);
const HathoraConfig = z
Expand Down Expand Up @@ -167,23 +180,63 @@ function getArgsInfo(
}

function enrichDoc(doc: z.infer<typeof HathoraConfig>, plugins: string[], appName: string) {
function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
doc.types["UserId"] = "string";
return {
...doc,
types: Object.fromEntries(
Object.entries(doc.types).map(([key, val]) => {
const argsInfo = getArgsInfo(doc, plugins, val, false, key);
doc.types["IInitializeRequest"] = {};
Object.entries(doc.methods).forEach(([key, val]) => {
doc.types[`I${capitalize(key)}Request`] = val ?? {};
});
const getPrimitiveType = (val: string) => {
if (val === "string") {
return StringType();
} else if (val === "int") {
return IntType();
} else if (val === "float") {
return IntType();
} else if (val === "boolean") {
return BooleanType();
}
throw new Error(`Invalid primitive type ${val}`);
};
const getChildType = (val: string, modifier?: Modifier) => {
return ChildType(val in doc.types ? ReferenceType(val) : getPrimitiveType(val), modifier);
};
const types = Object.fromEntries<Type>(
Object.entries(doc.types).map(([key, val]) => {
if (typeof val === "string") {
return [key, getPrimitiveType(val)];
} else if (Array.isArray(val)) {
if (val.every((v) => v in doc.types)) {
return [key, UnionType(val.map((v) => ReferenceType(v)))];
} else {
return [key, EnumType(val)];
}
} else if (typeof val === "object") {
return [
key,
plugins.includes(key) ? { type: "plugin", typeString: key, alias: false, item: argsInfo } : argsInfo,
ObjectType(
Object.fromEntries(
Object.entries(val).map(([k, v]) => {
if (v.endsWith("?")) {
return [k, getChildType(v.slice(0, -1), Modifier.OPTIONAL)];
} else if (v.endsWith("[]")) {
return [k, getChildType(v.slice(0, -2), Modifier.ARRAY)];
} else {
return [k, getChildType(v)];
}
})
)
),
];
})
),
methods: Object.fromEntries(
Object.entries(doc.methods).map(([key, val]) => {
return [key, getArgsInfo(doc, plugins, val === null ? {} : val, false)];
})
),
}
throw new Error(`Invalid type ${val}`);
})
);
return {
...doc,
deltaPack: codegenTypescript(types),
initializeArgs: getArgsInfo(doc, plugins, doc.initializeArgs ?? {}, false),
error: getArgsInfo(doc, plugins, doc.error, false),
plugins,
Expand Down
22 changes: 18 additions & 4 deletions templates/base/api/base.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import axios from "axios";
import * as T from "./types";

export const NO_DIFF = Symbol("NODIFF");
Expand All @@ -16,6 +15,21 @@ export enum Method {
{{/each}}
}

export enum HathoraEventTypes {
{{#each events}}
{{@key}},
{{/each}}
}
export type HathoraEventPayloads = {
{{#each events}}
[HathoraEventTypes.{{@key}}]: {{> renderTypeArg this}};
{{/each}}
};

export type HathoraEvents = {{#each events}}
| { type: HathoraEventTypes.{{@key}}; val: HathoraEventPayloads[HathoraEventTypes.{{@key}}] }
{{/each}};

export type OkResponse = { type: "ok" };
export type ErrorResponse = { type: "error"; error: {{> renderTypeArg error}} };
export type Response = OkResponse | ErrorResponse;
Expand All @@ -27,13 +41,13 @@ export const Response: { ok: () => OkResponse; error: (error: {{> renderTypeArg
export type ResponseMessage = { type: "response"; msgId: number; response: Response };
export type EventMessage = {
type: "event";
event: T.HathoraEventTypes;
data: T.HathoraEventPayloads[T.HathoraEventTypes];
event: HathoraEventTypes;
data: HathoraEventPayloads[HathoraEventTypes];
};
export type Message = ResponseMessage | EventMessage;
export const Message: {
response: (msgId: number, response: Response) => ResponseMessage;
event: (event: T.HathoraEventTypes, data: T.HathoraEventPayloads[T.HathoraEventTypes]) => EventMessage;
event: (event: HathoraEventTypes, data: HathoraEventPayloads[HathoraEventTypes]) => EventMessage;
} = {
response: (msgId, response) => ({ type: "response", msgId, response }),
event: (event, data) => ({ type: "event", event, data }),
Expand Down
Loading

0 comments on commit 16a7f4a

Please sign in to comment.