-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openrpc): start parsing methods in openrpc
- Loading branch information
Showing
7 changed files
with
344 additions
and
4 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
packages/parsers/src/openrpc/1.x/MethodConverter.node.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { isNonNullish } from "@fern-api/ui-core-utils"; | ||
import { MethodObject } from "@open-rpc/meta-schema"; | ||
import { UnreachableCaseError } from "ts-essentials"; | ||
import { FernRegistry } from "../../client/generated"; | ||
import { SchemaConverterNode } from "../../openapi"; | ||
import { maybeSingleValueToArray } from "../../openapi/utils/maybeSingleValueToArray"; | ||
import { | ||
BaseOpenrpcConverterNode, | ||
BaseOpenrpcConverterNodeConstructorArgs, | ||
} from "../BaseOpenrpcConverter.node"; | ||
import { resolveContentDescriptorObject } from "../utils/resolveContentDescriptorObject"; | ||
|
||
export class MethodConverterNode extends BaseOpenrpcConverterNode< | ||
MethodObject, | ||
FernRegistry.api.latest.EndpointDefinition | ||
> { | ||
private method: MethodObject; | ||
|
||
constructor(args: BaseOpenrpcConverterNodeConstructorArgs<MethodObject>) { | ||
super(args); | ||
this.method = args.input; | ||
this.safeParse(); | ||
} | ||
|
||
parse(): void { | ||
// Parse method object | ||
} | ||
|
||
convert(): FernRegistry.api.latest.EndpointDefinition | undefined { | ||
try { | ||
const resolvedResult = this.method.result | ||
? resolveContentDescriptorObject( | ||
this.method.result, | ||
this.context.openrpc | ||
) | ||
: undefined; | ||
|
||
const response = resolvedResult | ||
? new SchemaConverterNode({ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
input: resolvedResult.schema as any, | ||
context: this.context, | ||
accessPath: this.accessPath, | ||
pathId: "result", | ||
}).convert() | ||
: undefined; | ||
|
||
// Convert method to HTTP endpoint | ||
// This is a basic implementation that needs to be expanded | ||
return { | ||
id: FernRegistry.EndpointId(this.input.name), | ||
displayName: this.input.name, | ||
method: "POST", | ||
path: [{ type: "literal", value: "" }], | ||
auth: undefined, | ||
pathParameters: [], | ||
queryParameters: [], | ||
requests: undefined, | ||
responses: | ||
response != null | ||
? [ | ||
this.convertToHttpResponse(response, this.input.description), | ||
].filter(isNonNullish) | ||
: [], | ||
errors: [], | ||
examples: [], | ||
description: this.input.description, | ||
operationId: this.input.name, | ||
defaultEnvironment: undefined, | ||
environments: [], | ||
availability: undefined, | ||
requestHeaders: [], | ||
responseHeaders: [], | ||
snippetTemplates: undefined, | ||
namespace: [], | ||
}; | ||
} catch (_error) { | ||
this.context.errors.error({ | ||
message: "Failed to convert method", | ||
path: this.accessPath, | ||
}); | ||
return undefined; | ||
} | ||
} | ||
|
||
private convertToHttpResponse( | ||
shape: | ||
| FernRegistry.api.latest.TypeShape | ||
| FernRegistry.api.latest.TypeShape[], | ||
description?: string | ||
): FernRegistry.api.latest.HttpResponse | undefined { | ||
if (shape == null) { | ||
return undefined; | ||
} | ||
const maybeShapes = maybeSingleValueToArray(shape); | ||
const validShape = maybeShapes | ||
?.map((shape) => { | ||
const type = shape.type; | ||
switch (type) { | ||
case "alias": | ||
return shape; | ||
case "discriminatedUnion": | ||
case "undiscriminatedUnion": | ||
case "enum": | ||
return undefined; | ||
case "object": | ||
return shape; | ||
default: | ||
new UnreachableCaseError(type); | ||
return undefined; | ||
} | ||
}) | ||
.filter(isNonNullish)[0]; | ||
|
||
if (!validShape) { | ||
return undefined; | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: validShape, | ||
description, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { isNonNullish } from "@fern-api/ui-core-utils"; | ||
import { ReferenceObject } from "@open-rpc/meta-schema"; | ||
|
||
export function isReferenceObject(input: unknown): input is ReferenceObject { | ||
return ( | ||
typeof input === "object" && | ||
isNonNullish(input) && | ||
"$ref" in input && | ||
typeof input.$ref === "string" | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/parsers/src/openrpc/utils/resolveContentDescriptorObject.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
ContentDescriptorObject, | ||
OpenrpcDocument, | ||
ReferenceObject, | ||
} from "@open-rpc/meta-schema"; | ||
import { isReferenceObject } from "./isReferenceObject"; | ||
import { resolveReference } from "./resolveReference"; | ||
|
||
export function resolveContentDescriptorObject( | ||
contentDescriptor: ContentDescriptorObject | ReferenceObject | undefined, | ||
document: OpenrpcDocument | ||
): ContentDescriptorObject | undefined { | ||
if (isReferenceObject(contentDescriptor)) { | ||
return resolveReference<ContentDescriptorObject>( | ||
contentDescriptor, | ||
document, | ||
{ name: "", schema: { type: "object" } } | ||
); | ||
} | ||
return contentDescriptor; | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/parsers/src/openrpc/utils/resolveMethodReference.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
MethodObject, | ||
OpenrpcDocument, | ||
ReferenceObject, | ||
} from "@open-rpc/meta-schema"; | ||
import { isReferenceObject } from "./isReferenceObject"; | ||
import { resolveReference } from "./resolveReference"; | ||
|
||
export function resolveMethodReference( | ||
method: MethodObject | ReferenceObject | undefined, | ||
document: OpenrpcDocument | ||
): MethodObject | undefined { | ||
if (isReferenceObject(method)) { | ||
return resolveReference<MethodObject>(method, document, { | ||
name: "", | ||
params: [], | ||
result: { name: "", schema: { type: "object" } }, | ||
}); | ||
} | ||
return method; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { OpenrpcDocument, ReferenceObject } from "@open-rpc/meta-schema"; | ||
import { isReferenceObject } from "./isReferenceObject"; | ||
|
||
export function resolveReference<Output>( | ||
referenceObject: ReferenceObject, | ||
document: OpenrpcDocument, | ||
defaultOutput: Output | ||
): Output { | ||
const keys = referenceObject.$ref | ||
.substring(2) | ||
.split("/") | ||
.map((key) => key.replace(/~1/g, "/")); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let resolvedSchema: any = document; | ||
for (const key of keys) { | ||
if (typeof resolvedSchema !== "object" || resolvedSchema == null) { | ||
return defaultOutput; | ||
} | ||
resolvedSchema = resolvedSchema[key]; | ||
} | ||
if (resolvedSchema == null) { | ||
return defaultOutput; | ||
} | ||
|
||
// If the result is another reference object, make a recursive call | ||
if (isReferenceObject(resolvedSchema)) { | ||
resolvedSchema = resolveReference(resolvedSchema, document, defaultOutput); | ||
} | ||
|
||
return resolvedSchema; | ||
} |