Skip to content

Commit

Permalink
feat(openrpc): support environments (#2015)
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-support authored Jan 15, 2025
1 parent e516628 commit 8449221
Show file tree
Hide file tree
Showing 8 changed files with 10,463 additions and 69 deletions.
2 changes: 1 addition & 1 deletion packages/parsers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fern-api/docs-parsers",
"version": "0.0.35",
"version": "0.0.36",
"repository": {
"type": "git",
"url": "https://github.com/fern-api/fern-platform.git",
Expand Down
18 changes: 13 additions & 5 deletions packages/parsers/src/openrpc/1.x/MethodConverter.node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { isNonNullish } from "@fern-api/ui-core-utils";
import { MethodObject } from "@open-rpc/meta-schema";
import { camelCase } from "es-toolkit";
import { UnreachableCaseError } from "ts-essentials";
import { FernRegistry } from "../../client/generated";
import { SchemaConverterNode } from "../../openapi";
import { SchemaConverterNode, ServerObjectConverterNode } from "../../openapi";
import { maybeSingleValueToArray } from "../../openapi/utils/maybeSingleValueToArray";
import {
BaseOpenrpcConverterNode,
Expand All @@ -17,10 +18,15 @@ export class MethodConverterNode extends BaseOpenrpcConverterNode<
FernRegistry.api.latest.EndpointDefinition
> {
private method: MethodObject;
private servers: ServerObjectConverterNode[] = [];

constructor(args: BaseOpenrpcConverterNodeConstructorArgs<MethodObject>) {
constructor(
args: BaseOpenrpcConverterNodeConstructorArgs<MethodObject>,
servers: ServerObjectConverterNode[]
) {
super(args);
this.method = args.input;
this.servers = servers;
this.safeParse();
}

Expand Down Expand Up @@ -98,7 +104,7 @@ export class MethodConverterNode extends BaseOpenrpcConverterNode<
// This is a basic implementation that needs to be expanded
return {
id: FernRegistry.EndpointId(this.input.name),
displayName: this.input.summary ?? this.input.name,
displayName: camelCase(this.input.name),
method: "POST",
path: [{ type: "literal", value: "" }],
auth: undefined,
Expand Down Expand Up @@ -152,10 +158,12 @@ export class MethodConverterNode extends BaseOpenrpcConverterNode<
}
)
.filter(isNonNullish),
description: this.input.description,
description: this.input.description ?? this.input.summary,
operationId: this.input.name,
defaultEnvironment: undefined,
environments: [],
environments: this.servers
.map((server) => server.convert())
.filter(isNonNullish),
availability: this.method.deprecated ? "Deprecated" : undefined,
requestHeaders: [],
responseHeaders: [],
Expand Down
28 changes: 22 additions & 6 deletions packages/parsers/src/openrpc/1.x/OpenrpcDocumentConverter.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { isNonNullish } from "@fern-api/ui-core-utils";
import { OpenrpcDocument } from "@open-rpc/meta-schema";
import { v4 } from "uuid";
import { FernRegistry } from "../../client/generated";
import { ServerObjectConverterNode } from "../../openapi";
import { ComponentsConverterNode } from "../../openapi/3.1/schemas/ComponentsConverter.node";
import { coalesceServers } from "../../openapi/utils/3.1/coalesceServers";
import {
BaseOpenrpcConverterNode,
BaseOpenrpcConverterNodeConstructorArgs,
Expand All @@ -14,6 +16,7 @@ export class OpenrpcDocumentConverterNode extends BaseOpenrpcConverterNode<
OpenrpcDocument,
FernRegistry.api.latest.ApiDefinition
> {
servers: ServerObjectConverterNode[] = [];
methods: MethodConverterNode[] = [];
components: ComponentsConverterNode | undefined;

Expand All @@ -23,6 +26,15 @@ export class OpenrpcDocumentConverterNode extends BaseOpenrpcConverterNode<
}

parse(): void {
if (this.context.openrpc.servers != null) {
this.servers = coalesceServers(
this.servers,
this.input.servers,
this.context,
this.accessPath
);
}

if (this.input.methods != null) {
for (const method of this.input.methods) {
const resolvedMethod = resolveMethodReference(
Expand All @@ -33,15 +45,19 @@ export class OpenrpcDocumentConverterNode extends BaseOpenrpcConverterNode<
continue;
}
this.methods.push(
new MethodConverterNode({
input: resolvedMethod,
context: this.context,
accessPath: this.accessPath,
pathId: "methods",
})
new MethodConverterNode(
{
input: resolvedMethod,
context: this.context,
accessPath: this.accessPath,
pathId: "methods",
},
this.servers
)
);
}
}

if (this.context.openrpc.components != null) {
this.components = new ComponentsConverterNode({
input: this.context.openrpc.components,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ describe("OpenRPC converter", () => {
const errors = [];
const warnings = [];

expect(parsed.components?.schemas).toBeDefined();

if (parsed.components?.schemas) {
const converter = new OpenrpcDocumentConverterNode({
input: parsed,
Expand All @@ -51,8 +49,6 @@ describe("OpenRPC converter", () => {
if (warnings.length > 0) {
// console.warn("warnings:", warnings);
}

converted.id = "test-uuid-replacement";
await expect(
replaceEndpointUUIDs(JSON.stringify(converted, null, 2))
).toMatchFileSnapshot(`./__snapshots__/${directory}.json`);
Expand Down
Loading

0 comments on commit 8449221

Please sign in to comment.