Skip to content

Commit

Permalink
start to tackle requests
Browse files Browse the repository at this point in the history
  • Loading branch information
BBB committed Sep 21, 2023
1 parent ee7029a commit db0e6f4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
106 changes: 102 additions & 4 deletions packages/builder/src/effect-schema/EffectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
ResponsesObject,
SchemaObject,
SchemaObjectCodec,
RequestBodyObject,
RequestBodyObjectCodec,
ParameterObject,
} from "@ollierelph/openapi-parser";
import { Result } from "@ollierelph/result4t";
import {
Expand All @@ -28,7 +31,11 @@ import { DefaultDict } from "~/src/lib/DefaultDict";

type PathResponses<T = any> = {
path: string;
body?: T;
request: {
parameters: ReadonlyArray<ReferenceObject | ParameterObject> | undefined;
body: ReadonlyArray<ReferenceObject | RequestBodyObject> | undefined;
};
requestBody?: T;
responses: ResponsesObject;
};

Expand Down Expand Up @@ -151,7 +158,7 @@ class MethodPaths {
.exhaustive();
}

toObjectLiteral() {
public toObjectLiteral() {
const responsesToCodec = (definition: ResponsesObject): ts.Expression => {
return ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
Expand Down Expand Up @@ -232,6 +239,92 @@ class MethodPaths {
"responses",
responsesToCodec(p.responses),
),
ts.factory.createPropertyAssignment(
"request",
ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("pathParameters"),
ts.factory.createStringLiteral("TODO"),
),
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("queryParameters"),
ts.factory.createStringLiteral("TODO"),
),
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("headers"),
ts.factory.createStringLiteral("TODO"),
),
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("body"),
p.request.body
? ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("S"),
ts.factory.createIdentifier("union"),
),
undefined,
p.request.body.flatMap((it) => {
const requestBody = isReferenceObject(it)
? this.resolveReference(
it,
RequestBodyObjectCodec,
).getOrElse((err) => {
throw err;
})
: it;
return Object.entries(
requestBody.content,
).map(([contentType, media]) => {
return ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier("S"),
ts.factory.createIdentifier("struct"),
),
undefined,
[
ts.factory.createObjectLiteralExpression(
[
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier(
"contentType",
),
ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier(
"S",
),
ts.factory.createIdentifier(
"literal",
),
),
undefined,
[
ts.factory.createStringLiteral(
contentType,
),
],
),
),
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier(
"body",
),
this.schemaObjectToCodec(
media.schema,
),
),
],
true,
),
],
);
});
}),
)
: ts.factory.createStringLiteral("never"),
),
]),
),
]),
),
),
Expand Down Expand Up @@ -285,11 +378,16 @@ export class EffectBuilder implements Builder {

visitPathItemObjects(input)((pathItem) => {
for (const verb of httpVerbs) {
const responses = pathItem.node.definition[verb]?.responses;
const definition = pathItem.node.definition[verb];
const responses = definition?.responses;
if (responses) {
methodPaths.addPath(verb, {
path: pathItem.node.path,
responses: responses,
request: {
parameters: definition?.parameters,
body: definition?.requestBody,
},
responses,
});
}
}
Expand Down
23 changes: 23 additions & 0 deletions packages/builder/test/effect-schema/EffectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ it("should create a paths dictionary", () => {
body: S.literal(),
}),
),
request: {
pathParameters: "TODO",
queryParameters: "TODO",
headers: "TODO",
body: S.union(
S.struct({
contentType: S.literal("application/json"),
body: S.literal(),
}),
),
},
},
},
get: {
Expand All @@ -34,6 +45,12 @@ it("should create a paths dictionary", () => {
body: S.literal(),
}),
),
request: {
pathParameters: "TODO",
queryParameters: "TODO",
headers: "TODO",
body: "never",
},
},
"/b": {
responses: S.union(
Expand All @@ -43,6 +60,12 @@ it("should create a paths dictionary", () => {
body: S.struct({ foo: S.literal(), bingo: S.optional(S.literal()) }),
}),
),
request: {
pathParameters: "TODO",
queryParameters: "TODO",
headers: "TODO",
body: "never",
},
},
},
};
Expand Down
4 changes: 4 additions & 0 deletions packages/parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export {
ResponsesObjectCodec,
ResponsesObject,
} from "./parsers/effect-schema/schemas/ResponsesObject";
export {
ParameterObjectCodec,
ParameterObject,
} from "./parsers/effect-schema/schemas/ParameterObject";
export {
PathItemObjectCodec,
PathItemObject,
Expand Down
16 changes: 16 additions & 0 deletions packages/parser/src/parsers/effect-schema/schemas/OpenApiObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ export function buildOpenApi(): OpenApiObject {
},
"/c": {
post: {
parameters: [
{ name: "id", in: "path", required: true },
{ name: "search", in: "query", required: false },
{ name: "x-example", in: "header", required: false },
],
requestBody: [
{
content: {
"application/json": {
schema: {
type: "string",
},
},
},
},
],
responses: {
"200": {
description: "creates the c",
Expand Down

0 comments on commit db0e6f4

Please sign in to comment.