express-zod-api
Advanced tools
Changelog
v2.6.0
z.record()
.// example
z.record(
z.enum(["option1", "option2"]), // keys
z.boolean(), // values
);
attachRouting()
now returns the logger
instance and notFoundHandler
. You can use it with your
custom express app for handling 404
(not found) errors:const { notFoundHandler } = attachRouting(config, routing);
app.use(notFoundHandler);
app.listen();
logger
instance with any ResultHandler
for the same purpose:const { logger } = attachRouting(config, routing);
app.use((request, response) => {
defaultResultHandler.handler({
request,
response,
logger,
error: createHttpError(404, `${request.path} not found`),
input: null,
output: null,
});
});
app.listen();
Changelog
v2.5.2
ResultHandler
.
LastResortHandler
comes into play.500
and sends out plain text with an error message.Changelog
v2.5.1
Changelog
v2.5.0
express-fileupload
which is based on busboy
.z.upload()
.const config = createConfig({
server: {
upload: true,
// or selected express-fileupload's options:
// @see https://github.com/richardgirges/express-fileupload#available-options
upload: {
uploadTimeout: 60000,
useTempFiles: true,
safeFileNames: true,
preserveExtension: 4,
tempFileDir: "/var/tmp",
},
},
});
Endpoint
:const fileUploadEndpoint = defaultEndpointsFactory.build({
method: "post",
type: "upload", // <- new option, required
input: z.object({
avatar: z.upload(),
}),
output: z.object({
/* ... */
}),
handler: async ({ input: { avatar } }) => {
// avatar: {name, mv(), mimetype, encoding, data, truncated, size, etc}
// avatar.truncated is true on failure
return {
/* ... */
};
},
});
multipart/form-data
content type.input
parameters, including arrays and objects.cors
option is enabled:# before
Access-Control-Allow-Methods: POST, OPTIONS, OPTIONS
# after
Access-Control-Allow-Methods: POST, OPTIONS
Changelog
v2.4.0
cuid
.z.preprocess()
.
Please avoid using it for Endpoint outputs.// example
z.object({
name: z.string().optional().default("John Wick"),
});
Changelog
v2.3.3
ZodObject
's method .nonstrict()
in the example and Readme since it's not required.Changelog
v2.3.2
^3.7.2
in the package.json
file in case of package manager issues.Changelog
v2.3.1
ConfigType
.
ConfigType
is now deprecated (will be removed in v3).createConfig()
.createServer()
and attachRouting()
.// before
const configBefore: ConfigType = {
server: {
listen: 8090,
},
cors: true,
logger: {
level: "debug",
color: true,
},
};
// after
export const configAfter = createConfig({
server: {
listen: 8090,
},
cors: true,
logger: {
level: "debug",
color: true,
},
});