Skip to content

Commit c6434fe

Browse files
authored
Async loading TypeScript by Integration (#3149)
The better implementation of #3139 using the dynamic `import()` instead of `createRequire()`. The new implementation requires either: - to import and provide the `typescript` to `Integration::constructor()` - or to use the new async method `Integration::create()` This is a breaking change. `typescript` becomes an optional peer dependency. Documentation adjusted accordingly. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated README installation section to clarify TypeScript handling. * Enhanced frontend client generation examples with explicit TypeScript configuration instructions and usage patterns. * **New Features** * Added async factory method for integration setup, offering alternative to direct instantiation. * Made TypeScript an optional peer dependency instead of required, reducing installation footprint. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 3d369bb commit c6434fe

13 files changed

Lines changed: 282 additions & 150 deletions

File tree

CHANGELOG.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@
44

55
### v27.0.0
66

7-
- to be announced.
7+
- Breaking change to the `Integration` class:
8+
- Either import and assign the `typescript` property to constructor argument;
9+
- Or use the new static async method `create()` to delegate the import;
10+
11+
```diff
12+
/** Option 1: import and assign */
13+
import { Integration } from "express-zod-api";
14+
+ import typescript from "typescript";
15+
16+
const client = new Integration({
17+
routing,
18+
config,
19+
+ typescript,
20+
});
21+
```
22+
23+
```diff
24+
/** Option 2: delegate asynchronously */
25+
import { Integration } from "express-zod-api";
26+
27+
- const client = new Integration({
28+
+ const client = await Integration.create({
29+
routing,
30+
config,
31+
});
32+
```
833

934
## Version 26
1035

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Install the framework, its peer dependencies and type assistance packages using
180180

181181
```shell
182182
# example for pnpm:
183-
pnpm add express-zod-api express zod typescript http-errors
183+
pnpm add express-zod-api express zod http-errors
184184
pnpm add -D @types/express @types/node @types/http-errors
185185
```
186186

@@ -1074,13 +1074,15 @@ adding the runtime helpers the framework relies on.
10741074

10751075
## Generating a Frontend Client
10761076

1077-
You can generate a Typescript file containing the IO types of your API and a client for it.
1078-
Consider installing `prettier` and using the async `printFormatted()` method.
1077+
You can generate a Typescript file containing the IO types of your API and a client for it. Make sure you have
1078+
`typescript` installed. Consider also installing `prettier` and using the async `printFormatted()` method.
10791079

10801080
```ts
1081+
import typescript from "typescript";
10811082
import { Integration } from "express-zod-api";
10821083

10831084
const client = new Integration({
1085+
typescript, // or await Integration.create() to delegate importing
10841086
routing,
10851087
config,
10861088
variant: "client", // <— optional, see also "types" for a DIY solution

eslint.config.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ const tsFactoryConcerns = [
176176
selector: "Identifier[name='createUnionTypeNode']",
177177
message: "use makeUnion() helper",
178178
},
179+
{
180+
selector: "Identifier[name='createIdentifier']",
181+
message: "use makeId() helper",
182+
},
179183
];
180184

181185
export default tsPlugin.config(
@@ -238,11 +242,7 @@ export default tsPlugin.config(
238242
files: ["migration/index.ts"],
239243
rules: {
240244
"allowed/dependencies": ["error", { packageDir: migrationDir }],
241-
"no-restricted-syntax": [
242-
"warn",
243-
...importConcerns,
244-
...performanceConcerns,
245-
],
245+
"no-restricted-syntax": ["warn", ...importConcerns],
246246
},
247247
},
248248
{

example/generate-client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { writeFile } from "node:fs/promises";
22
import { Integration } from "express-zod-api";
33
import { routing } from "./routing";
44
import { config } from "./config";
5+
import typescript from "typescript";
56

67
await writeFile(
78
"example.client.ts",
89
await new Integration({
10+
typescript,
911
routing,
1012
config,
1113
serverUrl: `http://localhost:${config.http!.listen}`,

express-zod-api/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
},
7979
"express-fileupload": {
8080
"optional": true
81+
},
82+
"typescript": {
83+
"optional": true
8184
}
8285
},
8386
"devDependencies": {

0 commit comments

Comments
 (0)