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

Feat: Support async functions by .addOptions() #1753

Merged
merged 3 commits into from
May 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

## Version 18

### v18.6.0

- Feat: Supporting async functon as an argument for `EndpointsFactory::addOptions()`:
- I realized that it does not make sense for `.addOptions` just to proxy the static data;
- In case your options are static you can just `import` the corresponding `const` instead;
- Static options are deprecated and its support will be removed in v19.

```ts
import { readFile } from "node:fs/promises";
import { defaultEndpointsFactory } from "express-zod-api";

const endpointsFactory = defaultEndpointsFactory.addOptions(async () => {
const db = mongoose.connect("mongodb://connection.string");
const privateKey = await readFile("private-key.pem", "utf-8");
return { db, privateKey };
});
```

### v18.5.2

- Muted uploader logs related to non-eligible requests;
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,13 @@ In case you'd like to provide your endpoints with options that do not depend on
instance, consider shorthand method `addOptions`.

```typescript
import { readFile } from "node:fs/promises";
import { defaultEndpointsFactory } from "express-zod-api";

const endpointsFactory = defaultEndpointsFactory.addOptions({
db: mongoose.connect("mongodb://connection.string"),
privateKey: fs.readFileSync("private-key.pem", "utf-8"),
const endpointsFactory = defaultEndpointsFactory.addOptions(async () => {
const db = mongoose.connect("mongodb://connection.string");
const privateKey = await readFile("private-key.pem", "utf-8");
return { db, privateKey };
});
```

Expand Down
16 changes: 14 additions & 2 deletions src/endpoints-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,24 @@ export class EndpointsFactory<
);
}

public addOptions<AOUT extends FlatObject>(options: AOUT) {
/** @todo remove the static options in v19 - it makes no sense */
public addOptions<AOUT extends FlatObject>(
options: AOUT | (() => Promise<AOUT>),
) {
return EndpointsFactory.#create<IN, OUT & AOUT, SCO, TAG>(
this.middlewares.concat(
createMiddleware({
input: z.object({}),
middleware: async () => options,
middleware:
typeof options === "function"
? options
: async ({ logger }) => {
logger.warn(
"addOptions: Static options are deprecated. " +
"Replace with async function or just import the const.",
);
return options;
},
}),
),
this.resultHandler,
Expand Down
66 changes: 38 additions & 28 deletions tests/unit/endpoints-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { Endpoint } from "../../src/endpoint";
import { expectType } from "tsd";
import { AbstractLogger } from "../../src/logger";
import { makeLoggerMock } from "../../src/testing";
import { serializeSchemaForTest } from "../helpers";
import { z } from "zod";
import { describe, expect, test, vi } from "vitest";
Expand Down Expand Up @@ -91,38 +92,47 @@ describe("EndpointsFactory", () => {
});

describe(".addOptions()", () => {
test("Should create a new factory with an empty-input middleware and the same result handler", async () => {
const resultHandlerMock = createResultHandler({
getPositiveResponse: () => z.string(),
getNegativeResponse: () => z.string(),
handler: vi.fn(),
});
const factory = new EndpointsFactory(resultHandlerMock);
const newFactory = factory.addOptions({
test.each([
{
option1: "some value",
option2: "other value",
});
expect(factory["middlewares"]).toStrictEqual([]);
expect(factory["resultHandler"]).toStrictEqual(resultHandlerMock);
expect(newFactory["middlewares"].length).toBe(1);
expect(newFactory["middlewares"][0].input).toBeInstanceOf(z.ZodObject);
expect(
(newFactory["middlewares"][0].input as z.AnyZodObject).shape,
).toEqual({});
expect(
await newFactory["middlewares"][0].middleware({
input: {},
options: {},
request: {} as Request,
response: {} as Response,
logger: {} as AbstractLogger,
}),
).toEqual({
},
async () => ({
option1: "some value",
option2: "other value",
});
expect(newFactory["resultHandler"]).toStrictEqual(resultHandlerMock);
});
}),
])(
"Should create a new factory with an empty-input middleware and the same result handler",
async (options) => {
const resultHandlerMock = createResultHandler({
getPositiveResponse: () => z.string(),
getNegativeResponse: () => z.string(),
handler: vi.fn(),
});
const factory = new EndpointsFactory(resultHandlerMock);
const newFactory = factory.addOptions(options);
expect(factory["middlewares"]).toStrictEqual([]);
expect(factory["resultHandler"]).toStrictEqual(resultHandlerMock);
expect(newFactory["middlewares"].length).toBe(1);
expect(newFactory["middlewares"][0].input).toBeInstanceOf(z.ZodObject);
expect(
(newFactory["middlewares"][0].input as z.AnyZodObject).shape,
).toEqual({});
expect(
await newFactory["middlewares"][0].middleware({
input: {},
options: {},
request: {} as Request,
response: {} as Response,
logger: makeLoggerMock({ fnMethod: vi.fn }),
}),
).toEqual({
option1: "some value",
option2: "other value",
});
expect(newFactory["resultHandler"]).toStrictEqual(resultHandlerMock);
},
);
});

describe.each(["addExpressMiddleware" as const, "use" as const])(
Expand Down