Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ease DocumentationError constructor #2185

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions src/documentation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,8 @@ export const depictCatch: Depicter = (
export const depictAny: Depicter = () => ({ format: "any" });

export const depictUpload: Depicter = ({}: UploadSchema, ctx) => {
if (ctx.isResponse) {
throw new DocumentationError({
message: "Please use ez.upload() only for input.",
...ctx,
});
}
if (ctx.isResponse)
throw new DocumentationError("Please use ez.upload() only for input.", ctx);
return { type: "string", format: "binary" };
};

Expand Down Expand Up @@ -297,12 +293,8 @@ export const depictObject: Depicter = (
export const depictNull: Depicter = () => ({ type: "null" });

export const depictDateIn: Depicter = ({}: DateInSchema, ctx) => {
if (ctx.isResponse) {
throw new DocumentationError({
message: "Please use ez.dateOut() for output.",
...ctx,
});
}
if (ctx.isResponse)
throw new DocumentationError("Please use ez.dateOut() for output.", ctx);
return {
description: "YYYY-MM-DDTHH:mm:ss.sssZ",
type: "string",
Expand All @@ -315,12 +307,8 @@ export const depictDateIn: Depicter = ({}: DateInSchema, ctx) => {
};

export const depictDateOut: Depicter = ({}: DateOutSchema, ctx) => {
if (!ctx.isResponse) {
throw new DocumentationError({
message: "Please use ez.dateIn() for input.",
...ctx,
});
}
if (!ctx.isResponse)
throw new DocumentationError("Please use ez.dateIn() for input.", ctx);
return {
description: "YYYY-MM-DDTHH:mm:ss.sssZ",
type: "string",
Expand All @@ -333,14 +321,14 @@ export const depictDateOut: Depicter = ({}: DateOutSchema, ctx) => {

/** @throws DocumentationError */
export const depictDate: Depicter = ({}: z.ZodDate, ctx) => {
throw new DocumentationError({
message: `Using z.date() within ${
throw new DocumentationError(
`Using z.date() within ${
ctx.isResponse ? "output" : "input"
} schema is forbidden. Please use ez.date${
ctx.isResponse ? "Out" : "In"
}() instead. Check out the documentation for details.`,
...ctx,
});
ctx,
);
};

export const depictBoolean: Depicter = () => ({ type: "boolean" });
Expand Down Expand Up @@ -747,10 +735,10 @@ export const onMissing: SchemaHandler<
OpenAPIContext,
"last"
> = (schema: z.ZodTypeAny, ctx) => {
throw new DocumentationError({
message: `Zod type ${schema.constructor.name} is unsupported.`,
...ctx,
});
throw new DocumentationError(
`Zod type ${schema.constructor.name} is unsupported.`,
ctx,
);
};

export const excludeParamsFromDepiction = (
Expand Down
3 changes: 1 addition & 2 deletions src/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ export class Documentation extends OpenApiBuilder {
return operationId;
}
if (userDefined) {
throw new DocumentationError({
message: `Duplicated operationId: "${userDefined}"`,
throw new DocumentationError(`Duplicated operationId: "${userDefined}"`, {
method,
isResponse: false,
path,
Expand Down
17 changes: 8 additions & 9 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ export class DocumentationError extends Error {
public override name = "DocumentationError";
public override readonly cause: string;

constructor({
message,
method,
path,
isResponse,
}: { message: string } & Pick<
OpenAPIContext,
"path" | "method" | "isResponse"
>) {
constructor(
message: string,
{
method,
path,
isResponse,
}: Pick<OpenAPIContext, "path" | "method" | "isResponse">,
) {
super(message);
this.cause = `${
isResponse ? "Response" : "Input"
Expand Down
28 changes: 16 additions & 12 deletions tests/unit/documentation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,14 @@ describe("Documentation", () => {
serverUrl: "https://example.com",
}),
).toThrow(
new DocumentationError({
method: "post",
path: "/v1/getSomething",
isResponse: false,
message: `Zod type ${zodType._def.typeName} is unsupported.`,
}),
new DocumentationError(
`Zod type ${zodType._def.typeName} is unsupported.`,
{
method: "post",
path: "/v1/getSomething",
isResponse: false,
},
),
);
});

Expand Down Expand Up @@ -732,12 +734,14 @@ describe("Documentation", () => {

test("should not be able to specify duplicated operation", () => {
const operationId = "coolOperationId";
const expectedError = new DocumentationError({
message: 'Duplicated operationId: "coolOperationId"',
isResponse: false,
method: "get",
path: "/v1/getSomeTwo/thing",
});
const expectedError = new DocumentationError(
'Duplicated operationId: "coolOperationId"',
{
isResponse: false,
method: "get",
path: "/v1/getSomeTwo/thing",
},
);
expect(
() =>
new Documentation({
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ describe("Errors", () => {
});

describe("DocumentationError", () => {
const error = new DocumentationError({
message: "test",
const error = new DocumentationError("test", {
path: "/v1/testPath",
method: "get",
isResponse: true,
Expand Down