diff --git a/src/method-typings/index.test.ts b/src/method-typings/index.test.ts index a8109f6f..0a1c79dc 100644 --- a/src/method-typings/index.test.ts +++ b/src/method-typings/index.test.ts @@ -45,6 +45,68 @@ describe("MethodTypings", () => { expect(methodTypings).toBeInstanceOf(MethodTypings); }); + describe("getTypeDefinitionsForMethod", () => { + + it("throws if types not generated yet", () => { + const methodTypings = new MethodTypings(testOpenRPCDocument); + expect(() => methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "typescript")).toThrow(); + }); + + describe("typscript", () => { + + it("returns a string of typings for a method", async () => { + const methodTypings = new MethodTypings(testOpenRPCDocument); + await methodTypings.generateTypings(); + + expect(methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "typescript")).toBe([ + "export type TNiptip = number;", + "export interface IRipslip {", + " reepadoop?: number;", + " [k: string]: any;", + "}", + "", + ].join("\n")); + }); + + }); + + describe("rust", () => { + + it("returns a string of typings where the typeNames are unique", async () => { + const methodTypings = new MethodTypings(testOpenRPCDocument); + await methodTypings.generateTypings(); + + expect(methodTypings.getTypeDefinitionsForMethod(testOpenRPCDocument.methods[0], "rust")).toBe([ + "pub type Niptip = f64;", + "#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]", + "#[cfg_attr(test, derive(Random))]", + "#[serde(untagged)]", + "pub enum Ripslip {", + " AnythingArray(Vec>),", + "", + " Bool(bool),", + "", + " Double(f64),", + "", + " Integer(i64),", + "", + " RipslipClass(RipslipClass),", + "", + " String(String),", + "}", + "#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]", + "#[cfg_attr(test, derive(Random))]", + "pub struct RipslipClass {", + " #[serde(rename = \"reepadoop\")]", + " reepadoop: Option,", + "}", + ].join("\n")); + }); + + }); + + }); + describe("getAllUniqueTypings", () => { it("throws if types not generated yet", () => { diff --git a/src/method-typings/index.ts b/src/method-typings/index.ts index 0e895f20..6bcf26f4 100644 --- a/src/method-typings/index.ts +++ b/src/method-typings/index.ts @@ -43,6 +43,28 @@ export default class MethodTypings { return true; } + /** + * Gives you all the types needed for a particular method. + * + * @param method The method you need the types for. + * @param langeuage The langauge you want the signature to be in. + * + * @returns A string containing all the typings + * + */ + public getTypeDefinitionsForMethod(method: MethodObject, language: TLanguages): string { + if (Object.keys(this.typingMapByLanguage).length === 0) { + throw new Error("typings have not yet been generated. Please run generateTypings first."); + } + + return _.chain(this.typingMapByLanguage[language]) + .values() + .filter(({ typeId }) => _.startsWith(typeId, method.name)) + .map("typing") + .value() + .join(""); + } + /** * A method that returns all the types as a string, useful to directly inserting into code. *