Skip to content

Commit

Permalink
Plural props for EndpointsFactory::build().
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed Feb 1, 2024
1 parent ddf42d7 commit 317cd2c
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 93 deletions.
4 changes: 2 additions & 2 deletions example/endpoints/accept-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ez } from "../../src";
import { taggedEndpointsFactory } from "../factories";

export const rawAcceptingEndpoint = taggedEndpointsFactory.build({
method: "post",
tag: "files",
methods: "post",
tags: "files",
input: ez
.raw() // requires to enable rawParser option in server config
.extend({}), // additional inputs, route params for example, if needed
Expand Down
4 changes: 2 additions & 2 deletions example/endpoints/create-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { statusDependingFactory } from "../factories";

/** @desc depending on the thrown error, the custom result handler of the factory responds slightly differently */
export const createUserEndpoint = statusDependingFactory.build({
method: "post",
tag: "users",
methods: "post",
tags: "users",
input: z.object({
name: z.string().min(1),
}),
Expand Down
4 changes: 2 additions & 2 deletions example/endpoints/list-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { arrayRespondingFactory } from "../factories";
* Avoid doing this in new projects. This feature is only for easier migration of legacy APIs.
* */
export const listUsersEndpoint = arrayRespondingFactory.build({
method: "get",
tag: "users",
methods: "get",
tags: "users",
input: z.object({}),
output: withMeta(
z.object({
Expand Down
4 changes: 2 additions & 2 deletions example/endpoints/retrieve-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const feature: z.ZodType<Feature> = baseFeature.extend({
export const retrieveUserEndpoint = taggedEndpointsFactory
.addMiddleware(methodProviderMiddleware)
.build({
method: "get",
tag: "users",
methods: "get",
tags: "users",
shortDescription: "Retrieves the user.",
description: "Example user retrieval endpoint.",
input: z.object({
Expand Down
2 changes: 1 addition & 1 deletion example/endpoints/send-avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fileSendingEndpointsFactory } from "../factories";
import { readFile } from "node:fs/promises";

export const sendAvatarEndpoint = fileSendingEndpointsFactory.build({
method: "get",
methods: "get",
shortDescription: "Sends a file content.",
tags: ["files", "users"],
input: z.object({
Expand Down
2 changes: 1 addition & 1 deletion example/endpoints/stream-avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from "zod";
import { fileStreamingEndpointsFactory } from "../factories";

export const streamAvatarEndpoint = fileStreamingEndpointsFactory.build({
method: "get",
methods: "get",
shortDescription: "Streams a file content.",
tags: ["users", "files"],
input: z.object({
Expand Down
4 changes: 2 additions & 2 deletions example/endpoints/update-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { keyAndTokenAuthenticatedEndpointsFactory } from "../factories";

export const updateUserEndpoint =
keyAndTokenAuthenticatedEndpointsFactory.build({
method: "patch",
tag: "users",
methods: "patch",
tags: "users",
description: "Changes the user record. Example user update endpoint.",
input: withMeta(
z.object({
Expand Down
4 changes: 2 additions & 2 deletions example/endpoints/upload-avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { createHash } from "node:crypto";
import { taggedEndpointsFactory } from "../factories";

export const uploadAvatarEndpoint = taggedEndpointsFactory.build({
method: "post",
tag: "files",
methods: "post",
tags: "files",
description: "Handles a file upload.",
input: z
.object({
Expand Down
26 changes: 10 additions & 16 deletions src/endpoints-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ type BuildProps<
description?: string;
shortDescription?: string;
operationId?: string | ((method: Method) => string);
} & ({ method: Method } | { methods: Method[] }) &
({ scopes?: SCO[] } | { scope?: SCO }) &
({ tags?: TAG[] } | { tag?: TAG });
methods: Method | Method[];
scopes?: SCO | SCO[];
tags?: TAG | TAG[];
};

export class EndpointsFactory<
IN extends IOSchema<"strip"> | null = null,
Expand Down Expand Up @@ -145,7 +146,9 @@ export class EndpointsFactory<
description,
shortDescription,
operationId,
...rest
methods,
scopes,
tags,
}: BuildProps<BIN, BOUT, IN, OUT, SCO, TAG>): Endpoint<
ProbableIntersection<IN, BIN>,
BOUT,
Expand All @@ -154,25 +157,16 @@ export class EndpointsFactory<
TAG
> {
const { middlewares, resultHandler } = this;
const methods = "methods" in rest ? rest.methods : [rest.method];
const getOperationId =
typeof operationId === "function" ? operationId : () => operationId;
const scopes =
"scopes" in rest
? rest.scopes
: "scope" in rest && rest.scope
? [rest.scope]
: [];
const tags =
"tags" in rest ? rest.tags : "tag" in rest && rest.tag ? [rest.tag] : [];
return new Endpoint({
handler,
middlewares,
outputSchema,
resultHandler,
scopes,
tags,
methods,
scopes: scopes ? (Array.isArray(scopes) ? scopes : [scopes]) : [],
tags: tags ? (Array.isArray(tags) ? tags : [tags]) : [],
methods: Array.isArray(methods) ? methods : [methods],
getOperationId,
description,
shortDescription,
Expand Down
4 changes: 2 additions & 2 deletions tests/system/system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("App", async () => {
},
)
.build({
method: "get",
methods: "get",
input: z.object({}),
output: z.object({ corsDone: z.boolean() }),
handler: async ({ options }) => ({
Expand Down Expand Up @@ -59,7 +59,7 @@ describe("App", async () => {
}),
)
.build({
method: "get",
methods: "get",
input: z.object({
epError: z
.any()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/depends-on-method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("DependsOnMethod", () => {
test("should accept an endpoint with a corresponding method", () => {
const instance = new DependsOnMethod({
post: new EndpointsFactory(defaultResultHandler).build({
method: "post",
methods: "post",
input: z.object({}),
output: z.object({}),
handler: async () => ({}),
Expand Down
Loading

0 comments on commit 317cd2c

Please sign in to comment.