Skip to content

Commit b17d775

Browse files
committed
schema
1 parent bbd3380 commit b17d775

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

src/common/api/api.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference types="jest" />
22
import { API, Contact, OpenAPI, Resource, Schema } from "./types.js";
3-
import {APIClient} from "./api.js";
3+
import { APIClient } from "./api.js";
44
import { fetchOpenAPI, OpenAPIImpl } from "../openapi/openapi.js";
55
import { resourceToZodSchema } from "../../schema.js";
66

src/common/api/openapi.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
import { API, Contact, OpenAPI, Resource, Schema, ListMethod } from "./types.js";
2-
import { convertToOpenAPI, generateParentPatternsWithParams } from "./openapi.js";
1+
import {
2+
API,
3+
Contact,
4+
OpenAPI,
5+
Resource,
6+
Schema,
7+
ListMethod,
8+
} from "./types.js";
9+
import {
10+
convertToOpenAPI,
11+
generateParentPatternsWithParams,
12+
} from "./openapi.js";
313

414
describe("convertToOpenAPI", () => {
515
// Common example API used across tests

src/common/openapi/openapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
RequestBody,
99
} from "./types.js";
1010

11-
import yaml from 'js-yaml';
11+
import yaml from "js-yaml";
1212

1313
export class OpenAPIImpl {
1414
private api: OpenAPI;

src/schema.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { z } from "zod";
22
import { Schema } from "./common/api/types.js";
33
import { OpenAPIImpl } from "./common/openapi/openapi.js";
44

5-
export async function resourceToZodSchema(schema: Schema, oas: OpenAPIImpl): Promise<z.ZodTypeAny> {
6-
if(schema.$ref) {
7-
const newSchema = await oas.dereferenceSchema(schema)
8-
return resourceToZodSchema(newSchema, oas)
5+
export async function resourceToZodSchema(
6+
schema: Schema,
7+
oas: OpenAPIImpl
8+
): Promise<z.ZodTypeAny> {
9+
if (schema.$ref) {
10+
const newSchema = await oas.dereferenceSchema(schema);
11+
return resourceToZodSchema(newSchema, oas);
912
}
1013
switch (schema.type) {
1114
case "boolean":
@@ -33,7 +36,11 @@ export async function resourceToZodSchema(schema: Schema, oas: OpenAPIImpl): Pro
3336

3437
default:
3538
// TODO: handle ref!
36-
console.warn(`could not find item type ${schema.type} for schema ${JSON.stringify(schema)}`);
39+
console.warn(
40+
`could not find item type ${schema.type} for schema ${JSON.stringify(
41+
schema
42+
)}`
43+
);
3744
return z.string();
3845
}
3946
}

src/server.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ async function main() {
4545
for (const [resourceName, resource] of Object.entries(resources)) {
4646
const zodSchema = await resourceToZodSchema(resource.schema, oas);
4747
if (!(zodSchema instanceof z.ZodObject)) {
48-
throw new Error(`Expected object schema but got ${zodSchema.constructor.name}`);
48+
throw new Error(
49+
`Expected object schema but got ${zodSchema.constructor.name}`
50+
);
4951
}
5052
server.tool(
5153
resourceName,
5254
zodSchema._def.shape(),
53-
async (args: Record<string, unknown>, extra: RequestHandlerExtra): Promise<Response> => {
55+
async (
56+
args: Record<string, unknown>,
57+
extra: RequestHandlerExtra
58+
): Promise<Response> => {
5459
console.log(args);
5560
return {
5661
content: [{ type: "text", text: "Success" }],

0 commit comments

Comments
 (0)