Skip to content

Commit

Permalink
feat: add method type alias name interface
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed May 22, 2019
1 parent a859c39 commit e9b1291
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/generator-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export interface IMethodTypingsMap {

export type TGetMethodTypingsMap = (openrpcSchema: OpenRPC) => Promise<IMethodTypingsMap>;
export type TGetMethodTypeAlias = (method: MethodObject, typeDefs: IMethodTypingsMap) => string;
export type TGetMethodAliasName = (method: MethodObject) => string;

export interface IGenerator {
getMethodTypingsMap: TGetMethodTypingsMap;
getMethodTypeAlias: TGetMethodTypeAlias;
getMethodAliasName: TGetMethodAliasName;
}
2 changes: 2 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ describe("MethodTypings", () => {
await methodTypings.generateTypings();

expect(methodTypings.getTypingsForMethod(testOpenRPCDocument.methods[0], "typescript")).toEqual({
methodAliasName: "TJibber",
params: [
{
typeId: "jibber/0",
Expand All @@ -155,6 +156,7 @@ describe("MethodTypings", () => {
await methodTypings.generateTypings();

expect(methodTypings.getTypingsForMethod(testOpenRPCDocument.methods[0], "rust")).toEqual({
methodAliasName: "Jibber",
params: [
{
typeId: "jibber/0",
Expand Down
19 changes: 17 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface ITypingMapByLanguage {
}

export interface IMethodTypings {
methodAliasName: string;
params: IContentDescriptorTyping[];
result: IContentDescriptorTyping;
}
Expand Down Expand Up @@ -51,10 +52,10 @@ export default class MethodTypings {
/**
* Gives you all the [[IMethodTypings]] for a given method.
*
* @param method The method you need the types for.
* @param method The method you need typing for.
* @param langeuage The langauge you want the signature to be in.
*
* @returns A string containing all the typings
* @returns the typings for the method.
*
*/
public getTypingsForMethod(method: MethodObject, language: TLanguages): IMethodTypings {
Expand All @@ -70,6 +71,7 @@ export default class MethodTypings {
const methodTypings = zipObject(["result", "params"], paramsAndResult);

return {
methodAliasName: generators[language].getMethodAliasName(method),
params: methodTypings.params,
result: methodTypings.result[0],
};
Expand Down Expand Up @@ -126,6 +128,18 @@ export default class MethodTypings {
*
* @returns A string containing a type alias for a function signature of
* the same signature as the passed in method.
*
* @example
* ```typescript
*
* const openrpcTypings = new OpenRPCTypings(examples.simpleMath);
* const additionMethod = examples.simpleMath.examples
* .find((method) => method.name === "addition");
* const additionFunctionTypeAlias = openrpcTypings.getMethodTypeAlias(additionMethod, "typescript");
* fs.writeFileSync("./simpleMathAddition.ts", additionFunctionTypeAlias);
* const { TAddition } = require("./simpleMathAddition");
* ```
*
*/
public getMethodTypeAlias(method: MethodObject, language: TLanguages): string {
if (Object.keys(this.typingMapByLanguage).length === 0) {
Expand All @@ -146,4 +160,5 @@ export default class MethodTypings {

return compacted.join("\n");
}

}
5 changes: 4 additions & 1 deletion src/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { quicktype, SchemaTypeSource, TypeSource } from "quicktype";
import { RegexLiteral } from "@babel/types";

import { inspect } from "util"; // for debugging
import { ContentDescriptorObject } from "@open-rpc/meta-schema";
import { ContentDescriptorObject, MethodObject } from "@open-rpc/meta-schema";
import _ from "lodash";

const getTypeName = (contentDescriptor: ContentDescriptorObject): string => {
return _.chain(contentDescriptor.name).camelCase().upperFirst().value();
};

const getMethodAliasName = (method: MethodObject): string => getTypeName({ name: method.name, schema: {} });

const getQuickTypeSources = (contentDescriptors: ContentDescriptorObject[]): SchemaTypeSource[] => {
return _.chain(contentDescriptors)
.map((contentDescriptor) => ({
Expand Down Expand Up @@ -223,6 +225,7 @@ const getMethodTypeAlias: TGetMethodTypeAlias = (method, typeDefs) => {
};

const generator: IGenerator = {
getMethodAliasName,
getMethodTypeAlias,
getMethodTypingsMap,
};
Expand Down
6 changes: 4 additions & 2 deletions src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { generateMethodParamId, generateMethodResultId } from "@open-rpc/schema-
import { compile } from "json-schema-to-typescript";
import { ContentDescriptorObject, MethodObject } from "@open-rpc/meta-schema";

const getFunctionTypeName = ({ name }: MethodObject): string => {
const getMethodAliasName = ({ name }: MethodObject): string => {
return getTypeName({ name, schema: { type: "function" } });
};

const getTypeName = (contentDescriptor: ContentDescriptorObject): string => {
const { schema } = contentDescriptor;

Expand Down Expand Up @@ -88,7 +89,7 @@ const getMethodTypeAlias: TGetMethodTypeAlias = (method, typeDefs) => {
const result = method.result as ContentDescriptorObject;
const resultTypeName = `Promise<${typeDefs[generateMethodResultId(method, result)].typeName}>`;

const functionTypeName = getFunctionTypeName(method);
const functionTypeName = getMethodAliasName(method);

const params = _.map(
method.params as ContentDescriptorObject[],
Expand All @@ -99,6 +100,7 @@ const getMethodTypeAlias: TGetMethodTypeAlias = (method, typeDefs) => {
};

const generator: IGenerator = {
getMethodAliasName,
getMethodTypeAlias,
getMethodTypingsMap,
};
Expand Down

0 comments on commit e9b1291

Please sign in to comment.