Skip to content

Commit 5e49f3e

Browse files
authored
fix: dist files not working correctly with node resolutions (#169)
1 parent bcadf64 commit 5e49f3e

9 files changed

Lines changed: 71 additions & 37 deletions

File tree

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
"version": "5.0.0",
44
"description": "Zod Type Provider for Fastify@5",
55
"type": "module",
6-
"main": "./dist/cjs/index.js",
6+
"main": "./dist/cjs/index.cjs",
77
"module": "./dist/esm/index.js",
8+
"types": "./dist/cjs/index.d.cts",
89
"exports": {
9-
"require": "./dist/cjs/index.js",
10-
"import": "./dist/esm/index.js"
10+
"require": {
11+
"types": "./dist/cjs/index.d.cts",
12+
"default": "./dist/cjs/index.cjs"
13+
},
14+
"default": {
15+
"types": "./dist/esm/index.d.ts",
16+
"default": "./dist/esm/index.js"
17+
}
1118
},
12-
"types": "./dist/cjs/index.d.ts",
1319
"files": ["README.md", "LICENSE", "dist"],
1420
"scripts": {
15-
"build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
21+
"build": "vite build",
1622
"test:coverage": "vitest --coverage",
1723
"test:ci": "npm run build && npm run typescript && npm run test:coverage",
1824
"lint": "biome check . && tsc --noEmit",
@@ -50,6 +56,7 @@
5056
"oas-validator": "^5.0.8",
5157
"tsd": "^0.32.0",
5258
"typescript": "^5.8.3",
59+
"unplugin-isolated-decl": "^0.14.3",
5360
"vitest": "^3.2.2",
5461
"zod": "^3.25.56"
5562
},

src/core.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const createJsonSchemaTransform = ({
9292
}
9393
}
9494

95-
export const jsonSchemaTransform = createJsonSchemaTransform({})
95+
export const jsonSchemaTransform: SwaggerTransform<Schema> = createJsonSchemaTransform({})
9696

9797
type CreateJsonSchemaTransformObjectOptions = {
9898
schemaRegistry?: z.core.$ZodRegistry<{ id?: string | undefined }>
@@ -132,7 +132,7 @@ export const createJsonSchemaTransformObject =
132132
} as ReturnType<SwaggerTransformObject>
133133
}
134134

135-
export const jsonSchemaTransformObject = createJsonSchemaTransformObject({})
135+
export const jsonSchemaTransformObject: SwaggerTransformObject = createJsonSchemaTransformObject({})
136136

137137
export const validatorCompiler: FastifySchemaCompiler<z.ZodTypeAny> =
138138
({ schema }) =>
@@ -177,7 +177,8 @@ export const createSerializerCompiler =
177177
return JSON.stringify(result.data, options?.replacer)
178178
}
179179

180-
export const serializerCompiler = createSerializerCompiler({})
180+
export const serializerCompiler: ReturnType<typeof createSerializerCompiler> =
181+
createSerializerCompiler({})
181182

182183
/**
183184
* FastifyPluginCallbackZod with Zod automatic type inference

src/errors.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1-
import createError from '@fastify/error'
1+
import createError, { type FastifyErrorConstructor } from '@fastify/error'
22
import type { FastifyError } from 'fastify'
33
import type { FastifySchemaValidationError } from 'fastify/types/schema'
44
import type { z } from 'zod/v4'
55

6-
export const InvalidSchemaError = createError<[string]>(
7-
'FST_ERR_INVALID_SCHEMA',
8-
'Invalid schema passed: %s',
9-
500,
10-
)
6+
export const InvalidSchemaError: FastifyErrorConstructor<
7+
{
8+
code: string
9+
},
10+
[string]
11+
> = createError<[string]>('FST_ERR_INVALID_SCHEMA', 'Invalid schema passed: %s', 500)
1112

12-
const ZodFastifySchemaValidationErrorSymbol = Symbol.for('ZodFastifySchemaValidationError')
13+
const ZodFastifySchemaValidationErrorSymbol: symbol = Symbol.for('ZodFastifySchemaValidationError')
1314

1415
export type ZodFastifySchemaValidationError = FastifySchemaValidationError & {
1516
[ZodFastifySchemaValidationErrorSymbol]: true
1617
}
1718

18-
export class ResponseSerializationError extends createError<[{ cause: z.ZodError }]>(
19+
const ResponseSerializationBase: FastifyErrorConstructor<
20+
{
21+
code: string
22+
},
23+
[
24+
{
25+
cause: z.ZodError
26+
},
27+
]
28+
> = createError<[{ cause: z.ZodError }]>(
1929
'FST_ERR_RESPONSE_SERIALIZATION',
2030
"Response doesn't match the schema",
2131
500,
22-
) {
32+
)
33+
34+
export class ResponseSerializationError extends ResponseSerializationBase {
2335
cause!: z.ZodError
2436

2537
constructor(

src/zod-to-json.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ const getOverride = (
2929
}
3030
}
3131

32-
const deleteInvalidProperties = (schema: z.core.JSONSchema.BaseSchema) => {
32+
const deleteInvalidProperties: (
33+
schema: z.core.JSONSchema.BaseSchema,
34+
) => Omit<z.core.JSONSchema.BaseSchema, 'id' | '$schema'> = (schema) => {
3335
const object = { ...schema }
3436

3537
delete object.id
@@ -38,11 +40,11 @@ const deleteInvalidProperties = (schema: z.core.JSONSchema.BaseSchema) => {
3840
return object
3941
}
4042

41-
export const zodSchemaToJson = (
43+
export const zodSchemaToJson: (
4244
zodSchema: z.ZodType,
4345
registry: z.core.$ZodRegistry<{ id?: string }>,
4446
io: 'input' | 'output',
45-
) => {
47+
) => ReturnType<typeof deleteInvalidProperties> = (zodSchema, registry, io) => {
4648
const result = z.toJSONSchema(zodSchema, {
4749
io,
4850
unrepresentable: 'any',
@@ -61,10 +63,10 @@ export const zodSchemaToJson = (
6163
return jsonSchema
6264
}
6365

64-
export const zodRegistryToJson = (
66+
export const zodRegistryToJson: (
6567
registry: z.core.$ZodRegistry<{ id?: string }>,
6668
io: 'input' | 'output',
67-
) => {
69+
) => Record<string, z.core.JSONSchema.BaseSchema> = (registry, io) => {
6870
const result = z.toJSONSchema(registry, {
6971
io,
7072
unrepresentable: 'any',

tsconfig.cjs.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

tsconfig.esm.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"esModuleInterop": true,
77
"declaration": true,
88
"strict": true,
9+
"isolatedDeclarations": true
910
},
10-
"exclude": ["./node_modules", "types/**/*.test-d.ts", "test", "dist", "vitest.config.mts"]
11+
"exclude": ["./node_modules", "types/**/*.test-d.ts", "test", "dist", "vite.config.ts", "vitest.config.ts"]
1112
}

vite.config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { defineConfig } from "vite";
2+
import UnpluginIsolatedDecl from 'unplugin-isolated-decl/vite'
3+
4+
export default defineConfig({
5+
build: {
6+
lib: { entry: "src/index.ts",},
7+
outDir: "dist",
8+
emptyOutDir: true,
9+
minify: false,
10+
sourcemap: true,
11+
rollupOptions: {
12+
external: ["fastify", "zod/v4", "@fastify/swagger", "@fastify/error"],
13+
output: [{
14+
preserveModules: true,
15+
entryFileNames: 'cjs/[name].cjs',
16+
format: 'commonjs'
17+
}, {
18+
preserveModules: true,
19+
entryFileNames: 'esm/[name].js',
20+
format: 'es'
21+
}],
22+
},
23+
},
24+
plugins: [UnpluginIsolatedDecl()],
25+
});
File renamed without changes.

0 commit comments

Comments
 (0)