Skip to content

Commit

Permalink
enumerate the verbs
Browse files Browse the repository at this point in the history
  • Loading branch information
BBB committed Sep 21, 2023
1 parent d795a89 commit ee7029a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
26 changes: 11 additions & 15 deletions packages/builder/src/effect-schema/EffectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,22 @@ import { printAst } from "~/src/lib/printAst";
import { match, P } from "ts-pattern";
import { DefaultDict } from "~/src/lib/DefaultDict";

type PathResponses = {
type PathResponses<T = any> = {
path: string;
body?: T;
responses: ResponsesObject;
};

class MethodPaths {
constructor(private resolveReference: ReturnType<typeof getReference>) {}

private getPaths: Array<PathResponses> = [];
private putPaths: Array<PathResponses> = [];
private patchPaths: Array<PathResponses> = [];
private postPaths: Array<PathResponses> = [];
private deletePaths: Array<PathResponses> = [];

private paths = new DefaultDict<HttpVerb, Array<PathResponses>>(() => []);

public addPath(verb: HttpVerb, path: PathResponses) {
this.paths.set(verb, this.paths.get(verb).concat([path]));
}

schemaObjectToCodec(
private schemaObjectToCodec(
maybeSchema: SchemaObject | ReferenceObject,
): CallExpression | PropertyAccessExpression {
const schema: SchemaObject | undefined = isReferenceObject(maybeSchema)
Expand Down Expand Up @@ -221,15 +216,15 @@ class MethodPaths {
);
};
const createPropertiesForMethod = (
method: string,
method: HttpVerb,
paths: Array<PathResponses>,
) => {
if (paths.length > 0) {
return [
ts.factory.createPropertyAssignment(
method,
ts.factory.createObjectLiteralExpression(
this.getPaths.map((p) =>
paths.map((p) =>
ts.factory.createPropertyAssignment(
ts.factory.createStringLiteral(p.path),
ts.factory.createObjectLiteralExpression([
Expand All @@ -246,11 +241,12 @@ class MethodPaths {
}
return [];
};
const properties = createPropertiesForMethod("get", this.getPaths)
.concat(createPropertiesForMethod("put", this.putPaths))
.concat(createPropertiesForMethod("post", this.postPaths))
.concat(createPropertiesForMethod("patch", this.patchPaths))
.concat(createPropertiesForMethod("delete", this.deletePaths));

const properties = httpVerbs.reduce(
(agg, verb) =>
agg.concat(createPropertiesForMethod(verb, this.paths.get(verb))),
[] as PropertyAssignment[],
);

return [
ts.factory.createImportDeclaration(
Expand Down
34 changes: 33 additions & 1 deletion packages/builder/test/effect-schema/EffectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,39 @@ it("should create a paths dictionary", () => {
`
// typescript
import * as S from "@effect/schema/Schema";
export const paths = {};
export const paths = {
post: {
"/c": {
responses: S.union(
S.struct({
status: S.literal(200),
contentType: S.literal("application/json"),
body: S.literal(),
}),
),
},
},
get: {
"/a": {
responses: S.union(
S.struct({
status: S.literal(200),
contentType: S.literal("application/json"),
body: S.literal(),
}),
),
},
"/b": {
responses: S.union(
S.struct({
status: S.literal(200),
contentType: S.literal("application/json"),
body: S.struct({ foo: S.literal(), bingo: S.optional(S.literal()) }),
}),
),
},
},
};
`,
);
});
Expand Down

0 comments on commit ee7029a

Please sign in to comment.