Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-type-provider-zod

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-type-provider-zod - npm Package Compare versions

Comparing version 1.1.9 to 1.2.0

37

dist/index.d.ts

@@ -1,7 +0,7 @@

import type { FastifySchema, FastifySchemaCompiler, FastifyTypeProvider } from 'fastify';
import type { FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifySchema, FastifySchemaCompiler, FastifyTypeProvider, RawServerBase, RawServerDefault } from 'fastify';
import type { FastifySerializerCompiler } from 'fastify/types/schema';
import type { z, ZodAny, ZodTypeAny } from 'zod';
import type { ZodAny, ZodTypeAny, z } from 'zod';
type FreeformRecord = Record<string, any>;
export interface ZodTypeProvider extends FastifyTypeProvider {
output: this['input'] extends ZodTypeAny ? z.infer<this['input']> : never;
output: this['input'] extends ZodTypeAny ? z.infer<this['input']> : unknown;
}

@@ -17,2 +17,5 @@ interface Schema extends FastifySchema {

}) => {
schema: Schema;
url: string;
} | {
schema: FreeformRecord;

@@ -25,2 +28,5 @@ url: string;

}) => {
schema: Schema;
url: string;
} | {
schema: FreeformRecord;

@@ -37,2 +43,27 @@ url: string;

}>;
/**
* FastifyPluginCallbackZod with Zod automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginCallbackZod } from "fastify-type-provider-zod"
*
* const plugin: FastifyPluginCallbackZod = (fastify, options, done) => {
* done()
* }
* ```
*/
export type FastifyPluginCallbackZod<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = FastifyPluginCallback<Options, Server, ZodTypeProvider>;
/**
* FastifyPluginAsyncZod with Zod automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginAsyncZod } from "fastify-type-provider-zod"
*
* const plugin: FastifyPluginAsyncZod = async (fastify, options) => {
* }
* ```
*/
export type FastifyPluginAsyncZod<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = FastifyPluginAsync<Options, Server, ZodTypeProvider>;
export {};

128

dist/index.js
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
exports.__esModule = true;
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializerCompiler = exports.ResponseValidationError = exports.validatorCompiler = exports.jsonSchemaTransform = exports.createJsonSchemaTransform = void 0;
var zod_to_json_schema_1 = require("zod-to-json-schema");
var defaultSkipList = [
const zod_to_json_schema_1 = require("zod-to-json-schema");
const defaultSkipList = [
'/documentation/',

@@ -40,25 +14,23 @@ '/documentation/initOAuth',

];
var zodToJsonSchemaOptions = {
const zodToJsonSchemaOptions = {
target: 'openApi3',
$refStrategy: 'none'
$refStrategy: 'none',
};
var createJsonSchemaTransform = function (_a) {
var skipList = _a.skipList;
return function (_a) {
var schema = _a.schema, url = _a.url;
const createJsonSchemaTransform = ({ skipList }) => {
return ({ schema, url }) => {
if (!schema) {
return {
schema: schema,
url: url
schema,
url,
};
}
var response = schema.response, headers = schema.headers, querystring = schema.querystring, body = schema.body, params = schema.params, hide = schema.hide, rest = __rest(schema, ["response", "headers", "querystring", "body", "params", "hide"]);
var transformed = {};
const { response, headers, querystring, body, params, hide, ...rest } = schema;
const transformed = {};
if (skipList.includes(url) || hide) {
transformed.hide = true;
return { schema: transformed, url: url };
return { schema: transformed, url };
}
var zodSchemas = { headers: headers, querystring: querystring, body: body, params: params };
for (var prop in zodSchemas) {
var zodSchema = zodSchemas[prop];
const zodSchemas = { headers, querystring, body, params };
for (const prop in zodSchemas) {
const zodSchema = zodSchemas[prop];
if (zodSchema) {

@@ -71,13 +43,13 @@ transformed[prop] = (0, zod_to_json_schema_1.zodToJsonSchema)(zodSchema, zodToJsonSchemaOptions);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (var prop in response) {
for (const prop in response) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var schema_1 = resolveSchema(response[prop]);
var transformedResponse = (0, zod_to_json_schema_1.zodToJsonSchema)(
const schema = resolveSchema(response[prop]);
const transformedResponse = (0, zod_to_json_schema_1.zodToJsonSchema)(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
schema_1, zodToJsonSchemaOptions);
schema, zodToJsonSchemaOptions);
transformed.response[prop] = transformedResponse;
}
}
for (var prop in rest) {
var meta = rest[prop];
for (const prop in rest) {
const meta = rest[prop];
if (meta) {

@@ -87,3 +59,3 @@ transformed[prop] = meta;

}
return { schema: transformed, url: url };
return { schema: transformed, url };
};

@@ -93,15 +65,13 @@ };

exports.jsonSchemaTransform = (0, exports.createJsonSchemaTransform)({
skipList: defaultSkipList
skipList: defaultSkipList,
});
var validatorCompiler = function (_a) {
var schema = _a.schema;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function (data) {
try {
return { value: schema.parse(data) };
}
catch (error) {
return { error: error };
}
};
const validatorCompiler = ({ schema }) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(data) => {
try {
return { value: schema.parse(data) };
}
catch (error) {
return { error };
}
};

@@ -120,26 +90,20 @@ exports.validatorCompiler = validatorCompiler;

}
throw new Error("Invalid schema passed: ".concat(JSON.stringify(maybeSchema)));
throw new Error(`Invalid schema passed: ${JSON.stringify(maybeSchema)}`);
}
var ResponseValidationError = /** @class */ (function (_super) {
__extends(ResponseValidationError, _super);
function ResponseValidationError(validationResult) {
var _this = _super.call(this, "Response doesn't match the schema") || this;
_this.name = 'ResponseValidationError';
_this.details = validationResult.error;
return _this;
class ResponseValidationError extends Error {
constructor(validationResult) {
super("Response doesn't match the schema");
this.name = 'ResponseValidationError';
this.details = validationResult.error;
}
return ResponseValidationError;
}(Error));
}
exports.ResponseValidationError = ResponseValidationError;
var serializerCompiler = function (_a) {
var maybeSchema = _a.schema;
return function (data) {
var schema = resolveSchema(maybeSchema);
var result = schema.safeParse(data);
if (result.success) {
return JSON.stringify(result.data);
}
throw new ResponseValidationError(result);
};
const serializerCompiler = ({ schema: maybeSchema }) => (data) => {
const schema = resolveSchema(maybeSchema);
const result = schema.safeParse(data);
if (result.success) {
return JSON.stringify(result.data);
}
throw new ResponseValidationError(result);
};
exports.serializerCompiler = serializerCompiler;
{
"name": "fastify-type-provider-zod",
"version": "1.1.9",
"version": "1.2.0",
"description": "Zod Type Provider for Fastify@4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"README.md",
"LICENSE",
"dist"
],
"scripts": {

@@ -11,5 +16,4 @@ "build": "tsc",

"test:coverage": "jest --coverage",
"format": "prettier --write .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"lint:fix": "eslint --fix . && prettier --write .",
"typescript": "tsd",

@@ -38,23 +42,22 @@ "prepublishOnly": "npm run build"

"dependencies": {
"zod-to-json-schema": "^3.17.1"
"zod-to-json-schema": "^3.23.0"
},
"devDependencies": {
"@fastify/swagger": "^8.2.0",
"@fastify/swagger-ui": "^1.3.0",
"@types/jest": "^29.2.0",
"@types/node": "^18.11.7",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.1",
"fastify": "^4.10.2",
"jest": "^29.2.2",
"@fastify/swagger": "^8.14.0",
"@fastify/swagger-ui": "^3.0.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"fastify": "^4.27.0",
"fastify-plugin": "^4.5.1",
"jest": "^29.7.0",
"oas-validator": "^5.0.8",
"prettier": "^2.7.1",
"ts-jest": "^29.0.3",
"tsd": "^0.25.0",
"typescript": "^4.8.4",
"zod": "^3.19.1"
"prettier": "^3.2.5",
"ts-jest": "^29.1.2",
"tsd": "^0.31.0",
"typescript": "^5.4.5",
"zod": "^3.23.8"
},

@@ -61,0 +64,0 @@ "tsd": {

@@ -109,1 +109,27 @@ # Fastify Type Provider Zod

```
## How to create a plugin?
```ts
import { z } from 'zod';
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod';
const plugin: FastifyPluginAsyncZod = async function (fastify, _opts) {
fastify.route({
method: 'GET',
url: '/',
// Define your schema
schema: {
querystring: z.object({
name: z.string().min(4),
}),
response: {
200: z.string(),
},
},
handler: (req, res) => {
res.send(req.query.name);
},
});
};
```

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc