Skip to content

Commit 67292a4

Browse files
committed
fix(openrpc): set subpackages in openrpc
1 parent cff7b22 commit 67292a4

File tree

4 files changed

+76
-37
lines changed

4 files changed

+76
-37
lines changed

packages/parsers/src/openapi/3.1/OpenApiDocumentConverter.node.ts

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { camelCase } from "es-toolkit";
21
import { OpenAPIV3_1 } from "openapi-types";
32
import { v4 } from "uuid";
43
import { FernRegistry } from "../../client/generated";
4+
import { computeSubpackages } from "../../utils/computeSubpackages";
55
import {
66
BaseOpenApiV3_1ConverterNode,
77
BaseOpenApiV3_1ConverterNodeConstructorArgs,
@@ -146,35 +146,7 @@ export class OpenApiDocumentConverterNode extends BaseOpenApiV3_1ConverterNode<
146146
const subpackages: Record<
147147
FernRegistry.api.v1.SubpackageId,
148148
FernRegistry.api.latest.SubpackageMetadata
149-
> = {};
150-
if (endpoints != null) {
151-
Object.values(endpoints).forEach((endpoint) =>
152-
endpoint.namespace?.forEach((subpackage) => {
153-
const qualifiedPath: string[] = [];
154-
subpackages[FernRegistry.api.v1.SubpackageId(camelCase(subpackage))] =
155-
{
156-
id: FernRegistry.api.v1.SubpackageId(camelCase(subpackage)),
157-
name: [...qualifiedPath, subpackage].join("/"),
158-
displayName: subpackage,
159-
};
160-
qualifiedPath.push(subpackage);
161-
})
162-
);
163-
}
164-
if (webhookEndpoints != null) {
165-
Object.values(webhookEndpoints).forEach((webhook) =>
166-
webhook.namespace?.forEach((subpackage) => {
167-
const qualifiedPath: string[] = [];
168-
subpackages[FernRegistry.api.v1.SubpackageId(camelCase(subpackage))] =
169-
{
170-
id: FernRegistry.api.v1.SubpackageId(camelCase(subpackage)),
171-
name: [...qualifiedPath, subpackage].join("/"),
172-
displayName: subpackage,
173-
};
174-
qualifiedPath.push(subpackage);
175-
})
176-
);
177-
}
149+
> = computeSubpackages({ endpoints, webhookEndpoints });
178150

179151
const types = this.components?.convert();
180152

packages/parsers/src/openrpc/1.x/OpenrpcDocumentConverter.node.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FernRegistry } from "../../client/generated";
55
import { ServerObjectConverterNode } from "../../openapi";
66
import { ComponentsConverterNode } from "../../openapi/3.1/schemas/ComponentsConverter.node";
77
import { coalesceServers } from "../../openapi/utils/3.1/coalesceServers";
8+
import { computeSubpackages } from "../../utils/computeSubpackages";
89
import {
910
BaseOpenrpcConverterNode,
1011
BaseOpenrpcConverterNodeConstructorArgs,
@@ -72,25 +73,27 @@ export class OpenrpcDocumentConverterNode extends BaseOpenrpcConverterNode<
7273
const apiDefinitionId = v4();
7374
const types = this.components?.convert();
7475

75-
console.log(this.methods?.length);
76-
7776
const methods = this.methods
7877
?.map((method) => {
7978
return method.convert();
8079
})
8180
.filter(isNonNullish);
8281

82+
const endpoints = Object.fromEntries(
83+
methods?.map((method) => [method.id, method]) ?? []
84+
);
85+
86+
const subpackages = computeSubpackages({ endpoints });
87+
8388
return {
8489
id: FernRegistry.ApiDefinitionId(apiDefinitionId),
8590
types: Object.fromEntries(
8691
Object.entries(types ?? {}).map(([id, type]) => [id, type])
8792
),
88-
endpoints: Object.fromEntries(
89-
methods?.map((method) => [method.id, method]) ?? []
90-
),
93+
endpoints,
9194
websockets: {},
9295
webhooks: {},
93-
subpackages: {},
96+
subpackages,
9497
auths: {},
9598
globalHeaders: [],
9699
};

packages/parsers/src/openrpc/__test__/__snapshots__/petstore.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,13 @@
419419
},
420420
"websockets": {},
421421
"webhooks": {},
422-
"subpackages": {},
422+
"subpackages": {
423+
"pets": {
424+
"id": "pets",
425+
"name": "pets",
426+
"displayName": "pets"
427+
}
428+
},
423429
"auths": {},
424430
"globalHeaders": []
425431
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { camelCase } from "es-toolkit";
2+
import { FernRegistry } from "../client/generated";
3+
4+
export declare namespace ComputeSubpackages {
5+
interface Args {
6+
endpoints?: Record<
7+
FernRegistry.EndpointId,
8+
FernRegistry.api.latest.EndpointDefinition
9+
>;
10+
webhookEndpoints?: Record<
11+
FernRegistry.EndpointId,
12+
FernRegistry.api.latest.WebhookDefinition
13+
>;
14+
}
15+
}
16+
17+
export function computeSubpackages({
18+
endpoints,
19+
webhookEndpoints,
20+
}: ComputeSubpackages.Args): Record<
21+
FernRegistry.api.v1.SubpackageId,
22+
FernRegistry.api.latest.SubpackageMetadata
23+
> {
24+
const subpackages: Record<
25+
FernRegistry.api.v1.SubpackageId,
26+
FernRegistry.api.latest.SubpackageMetadata
27+
> = {};
28+
29+
if (endpoints != null) {
30+
Object.values(endpoints).forEach((endpoint) =>
31+
endpoint.namespace?.forEach((subpackage) => {
32+
const qualifiedPath: string[] = [];
33+
subpackages[FernRegistry.api.v1.SubpackageId(camelCase(subpackage))] = {
34+
id: FernRegistry.api.v1.SubpackageId(camelCase(subpackage)),
35+
name: [...qualifiedPath, subpackage].join("/"),
36+
displayName: subpackage,
37+
};
38+
qualifiedPath.push(subpackage);
39+
})
40+
);
41+
}
42+
43+
if (webhookEndpoints != null) {
44+
Object.values(webhookEndpoints).forEach((webhook) =>
45+
webhook.namespace?.forEach((subpackage) => {
46+
const qualifiedPath: string[] = [];
47+
subpackages[FernRegistry.api.v1.SubpackageId(camelCase(subpackage))] = {
48+
id: FernRegistry.api.v1.SubpackageId(camelCase(subpackage)),
49+
name: [...qualifiedPath, subpackage].join("/"),
50+
displayName: subpackage,
51+
};
52+
qualifiedPath.push(subpackage);
53+
})
54+
);
55+
}
56+
57+
return subpackages;
58+
}

0 commit comments

Comments
 (0)