Skip to content

Commit d1eaed7

Browse files
committed
update types
1 parent d83748d commit d1eaed7

18 files changed

+175
-162
lines changed

src/core/form-data.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import type { Operation } from "./openapi";
1+
import { getBodyRequest } from "./resolvers/body-request";
2+
import type { OperationObject } from "@omer-x/openapi-types/operation";
23

3-
export function hasFormData(operation: Operation) {
4+
export function hasFormData(operation: OperationObject) {
45
if (!operation.requestBody) return false;
5-
return "multipart/form-data" in operation.requestBody.content;
6+
const body = getBodyRequest(operation.requestBody);
7+
return "multipart/form-data" in body.content;
68
}

src/core/openapi.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/core/renderers/declaration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type { OpenAPI } from "~/core/openapi";
21
import { type DeclaredOperation, resolveDeclaredOperations } from "~/core/resolvers/declared-operation";
32
import { resolveSchemas } from "~/core/resolvers/imported-schema";
43
import getTemplate from "~/core/template";
54
import declarationTemplate from "~/templates/declaration.hbs";
5+
import type { PathsObject } from "@omer-x/openapi-types/paths";
66

77
type DeclarationTemplate = {
88
importedSchemas: string[],
99
operations: DeclaredOperation[],
1010
};
1111

12-
export default function generateDeclaration(paths: OpenAPI["paths"], framework: string | null) {
12+
export default function generateDeclaration(paths: PathsObject, framework: string | null) {
1313
const template = getTemplate<DeclarationTemplate>(declarationTemplate);
1414
return template({
1515
importedSchemas: resolveSchemas(paths),

src/core/renderers/documentation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Handlebars from "handlebars";
2-
import type { OpenAPI } from "~/core/openapi";
32
import { type DocumentedOperation, resolveDocumentedOperations } from "~/core/resolvers/documented-operation";
43
import getTemplate from "~/core/template";
54
import documentationTemplate from "~/templates/documentation.hbs";
5+
import type { PathsObject } from "@omer-x/openapi-types/paths";
66

77
type DocumentationTemplate = {
88
serviceName: string,
@@ -15,7 +15,7 @@ export default function generateDocumentation(
1515
serviceName: string,
1616
packageName: string,
1717
envName: string,
18-
paths: OpenAPI["paths"]
18+
paths: PathsObject
1919
) {
2020
const template = getTemplate<DocumentationTemplate>(documentationTemplate);
2121
return template({

src/core/resolvers/body-request.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { ReferenceObject } from "@omer-x/openapi-types/reference";
2+
import type { RequestBodyObject } from "@omer-x/openapi-types/request-body";
3+
4+
export function getBodyRequest(source: RequestBodyObject | ReferenceObject) {
5+
if ("$ref" in source) throw new Error("Request Body references not implemented yet.");
6+
return source;
7+
}

src/core/resolvers/content.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Content } from "~/core/openapi";
1+
import type { MediaTypeObject } from "@omer-x/openapi-types/media-type";
22

3-
export default function getContentSchema(content: Content) {
3+
export default function getContentSchema(content: Record<string, MediaTypeObject>) {
44
if ("multipart/form-data" in content) {
55
return content["multipart/form-data"].schema;
66
}
7-
return (content["application/json"] as Content["application/json"]).schema;
7+
return content["application/json"].schema;
88
}

src/core/resolvers/declared-operation.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
1-
import type { OpenAPI, Operation } from "~/core/openapi";
21
import getContentSchema from "./content";
32
import resolveEndpoints from "./enpoint";
3+
import { defaultOperationName } from "./operation-name";
44
import { resolveOperationParams } from "./operation-param";
5+
import { getResponse } from "./response";
56
import { resolveSchema } from "./schema-definition";
7+
import type { PathsObject } from "@omer-x/openapi-types/paths";
8+
import type { ResponsesObject } from "@omer-x/openapi-types/response";
69

710
export type DeclaredOperation = {
811
name: string,
912
parameters: string,
1013
result: string,
1114
};
1215

13-
function resolveOperationResult(responses: Operation["responses"]) {
14-
const schemas = Object.values(responses).map(response => {
15-
return response.content ? resolveSchema(getContentSchema(response.content)) : null;
16+
function resolveOperationResult(responses?: ResponsesObject) {
17+
if (!responses) return "unknown";
18+
const schemas = Object.values(responses).map(resp => {
19+
if (!resp) return null;
20+
const response = getResponse(resp);
21+
if (!response.content) return null;
22+
const schema = getContentSchema(response.content);
23+
if (!schema) return null;
24+
return resolveSchema(schema);
1625
});
1726
return schemas.find(s => typeof s === "string") ?? "unknown";
1827
}
1928

20-
export function resolveDeclaredOperations(paths: OpenAPI["paths"], framework: string | null) {
21-
return resolveEndpoints(paths).map<DeclaredOperation>(({ method, operation }) => ({
22-
name: operation.operationId,
29+
export function resolveDeclaredOperations(paths: PathsObject, framework: string | null) {
30+
return resolveEndpoints(paths).map<DeclaredOperation>(({ method, path, operation }) => ({
31+
name: operation.operationId || defaultOperationName(method, path),
2332
parameters: resolveOperationParams(operation, method, true, framework).join(", "),
2433
result: resolveOperationResult(operation.responses),
2534
}));

src/core/resolvers/documented-operation.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import type { OpenAPI, Operation } from "~/core/openapi";
21
import getContentSchema from "./content";
32
import resolveEndpoints from "./enpoint";
3+
import { defaultOperationName } from "./operation-name";
4+
import { getParameter } from "./operation-param";
45
import { resolveResponsesForDocs } from "./response";
56
import { resolveSchema } from "./schema-definition";
7+
import type { PathsObject } from "@omer-x/openapi-types/paths";
8+
import type { ResponsesObject } from "@omer-x/openapi-types/response";
69

710
type DocumentedParam = {
811
name: string,
@@ -26,18 +29,21 @@ export type DocumentedOperation = {
2629
exceptions: DocumentedException[],
2730
};
2831

29-
export function resolveDocumentedOperations(paths: OpenAPI["paths"]) {
30-
return resolveEndpoints(paths).map<DocumentedOperation>(({ operation }) => {
31-
const parameters = (operation.parameters ?? []).map<DocumentedParam>(p => ({
32-
name: p.name,
33-
description: p.description,
34-
type: resolveSchema(p.schema),
35-
optional: !p.required,
36-
}));
32+
export function resolveDocumentedOperations(paths: PathsObject) {
33+
return resolveEndpoints(paths).map<DocumentedOperation>(({ method, path, operation }) => {
34+
const parameters = (operation.parameters ?? []).map<DocumentedParam>(p => {
35+
const param = getParameter(p);
36+
return {
37+
name: param.name,
38+
description: p.description || "missing description",
39+
type: resolveSchema(param.schema),
40+
optional: !param.required,
41+
};
42+
});
3743
return {
38-
name: operation.operationId,
39-
summary: operation.summary,
40-
description: operation.description,
44+
name: operation.operationId || defaultOperationName(method, path),
45+
summary: operation.summary || "missing summary",
46+
description: operation.description || "missing description",
4147
parameters: parameters.map(p => p.name).join(", "),
4248
parametersRaw: parameters,
4349
result: resolveOperationResult(operation.responses),
@@ -46,9 +52,13 @@ export function resolveDocumentedOperations(paths: OpenAPI["paths"]) {
4652
});
4753
}
4854

49-
function resolveOperationResult(responses: Operation["responses"]) {
55+
function resolveOperationResult(responses?: ResponsesObject) {
56+
if (!responses) return "unknown";
5057
const schemas = Object.values(responses).map(response => {
51-
return response.content ? resolveSchema(getContentSchema(response.content)) : null;
58+
if (response && "content" in response && response.content) {
59+
return resolveSchema(getContentSchema(response.content));
60+
}
61+
return null;
5262
});
5363
return schemas.find(s => typeof s === "string") ?? "unknown";
5464
}

src/core/resolvers/enpoint.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import type { OpenAPI } from "~/core/openapi";
1+
import type { OperationObject } from "@omer-x/openapi-types/operation";
2+
import type { PathItemObject, PathsObject } from "@omer-x/openapi-types/paths";
23

34
type ResolvedEndpoint = {
4-
method: keyof OpenAPI["paths"][string],
5-
path: keyof OpenAPI["paths"],
6-
operation: OpenAPI["paths"][string]["get"],
5+
method: keyof PathItemObject,
6+
path: keyof PathsObject,
7+
operation: OperationObject,
78
};
89

9-
export default function resolveEndpoints(paths: OpenAPI["paths"]) {
10+
export default function resolveEndpoints(paths: PathsObject) {
1011
return Object.entries(paths).map(([path, methods]) => {
11-
return Object.entries(methods).map<ResolvedEndpoint>(([method, operation]) => ({
12-
method: method as keyof OpenAPI["paths"][string],
13-
path,
14-
operation,
15-
}));
12+
return Object.entries(methods).reduce((collection, [method, operation]) => {
13+
if (typeof operation === "string" || Array.isArray(operation)) return collection;
14+
return [...collection, {
15+
method: method as keyof PathItemObject,
16+
path,
17+
operation,
18+
}];
19+
}, [] as ResolvedEndpoint[]);
1620
}).flat();
1721
}

src/core/resolvers/imported-schema.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import type { OpenAPI, Operation, SchemaDefinition } from "~/core/openapi";
21
import { getTupleItems, isTuple } from "~/core/tuple";
2+
import { getBodyRequest } from "./body-request";
33
import getContentSchema from "./content";
44
import resolveEndpoints from "./enpoint";
5+
import { getResponse } from "./response";
56
import { filterGenericSchemas, resolveSchema, simplifySchema } from "./schema-definition";
7+
import type { PathsObject } from "@omer-x/openapi-types/paths";
8+
import type { ReferenceObject } from "@omer-x/openapi-types/reference";
9+
import type { RequestBodyObject } from "@omer-x/openapi-types/request-body";
10+
import type { ResponsesObject } from "@omer-x/openapi-types/response";
11+
import type { SchemaObject } from "@omer-x/openapi-types/schema";
612

7-
function resolveRequestSchemas(requestBody: Operation["requestBody"]) {
8-
if (!requestBody) return [];
13+
function resolveRequestSchemas(body?: RequestBodyObject | ReferenceObject) {
14+
if (!body) return [];
15+
const requestBody = getBodyRequest(body);
916
return [resolveSchema(getContentSchema(requestBody.content))];
1017
}
1118

12-
function resolveResponseSchemas(responses: Operation["responses"]) {
13-
return Object.values(responses).map(response => {
19+
function resolveResponseSchemas(responses?: ResponsesObject) {
20+
if (!responses) return [];
21+
return Object.values(responses).map(resp => {
22+
const response = getResponse(resp);
1423
return Object.values(response.content ?? {}).map(content => {
1524
return simplifySchema(resolveSchema(content.schema));
1625
}).flat();
@@ -26,7 +35,7 @@ function extractTuples(collection: string[]) {
2635
}).flat();
2736
}
2837

29-
export function resolveSchemas(paths: OpenAPI["paths"]) {
38+
export function resolveSchemas(paths: PathsObject) {
3039
const collection = resolveEndpoints(paths).map(({ operation }) => ([
3140
...resolveRequestSchemas(operation.requestBody),
3241
...resolveResponseSchemas(operation.responses),
@@ -35,23 +44,23 @@ export function resolveSchemas(paths: OpenAPI["paths"]) {
3544
return filterGenericSchemas(uniqueCollection).toSorted();
3645
}
3746

38-
function resolvePropDefinition(definition: SchemaDefinition) {
47+
function resolvePropDefinition(definition: SchemaObject) {
48+
if (definition.$ref) {
49+
return [definition.$ref.replace("#/components/schemas/", "")];
50+
}
3951
if (definition.type === "array" && definition.items) {
4052
if (Array.isArray(definition.items)) {
4153
return definition.items.map<string[]>(resolvePropDefinition).flat();
4254
}
4355
return [resolveSchema(definition.items)];
4456
}
45-
if (definition.$ref) {
46-
return [definition.$ref.replace("#/components/schemas/", "")];
47-
}
4857
if (definition.oneOf) {
4958
return definition.oneOf.map<string[]>(resolvePropDefinition).flat();
5059
}
5160
return [];
5261
}
5362

54-
export function resolveSchemasFromProps(props: Record<string, SchemaDefinition>) {
63+
export function resolveSchemasFromProps(props: Record<string, SchemaObject>) {
5564
const collection = Object.values(props).map(resolvePropDefinition).flat();
5665
const uniqueCollection = Array.from(new Set(extractTuples(collection)));
5766
return filterGenericSchemas(uniqueCollection).toSorted();

0 commit comments

Comments
 (0)