@ fastify/type-productor-json-schema-to-ts (英语)
类型提供者 json-schema-to-ts (日语)
安装
npm i @fastify/type-provider-json-schema-to-ts
类型脚本要求
它必须使用 [email protected] 或以上为
strict
模式已启用
noStrictGenericChecks
已禁用。 您可选择以下配置( E)tsconfig.json例如:
{
"compilerOptions": {
"strict": true,
"noStrictGenericChecks": false
}
}
插件定义
说明 当使用插件类型时,
withTypeProvider不需要注册插件.
const plugin: FastifyPluginAsyncJsonSchemaToTs = async function (
fastify,
_opts
) {
fastify.get(
"/",
{
schema: {
body: {
type: "object",
properties: {
x: { type: "string" },
y: { type: "number" },
z: { type: "boolean" },
},
required: ["x", "y", "z"],
} as const,
},
},
(req) => {
// The `x`, `y`, and `z` types are automatically inferred
const { x, y, z } = req.body;
}
);
};
为验证器和序列器设置 FromSchema
你可以设置 FromSchema 诸如此类的设置 references 财务报告和审定财务报表 deserialization 通过设置验证和序列化计划 ValidatorSchemaOptions 财务报告和审定财务报表 SerializerSchemaOptions 类型参数.
你可以用这个 deserialize 选项在 SerializerSchemaOptions 日期对象取代日期字符串或其他特殊序列规则 快速字符串。 。 。 .
const userSchema = {
type: "object",
additionalProperties: false,
properties: {
givenName: { type: "string" },
familyName: { type: "string" },
},
required: ["givenName", "familyName"],
} as const satisfies JSONSchema;
const sharedSchema = {
$id: "shared-schema",
definitions: {
user: userSchema,
},
} as const satisfies JSONSchema;
const userProfileSchema = {
$id: "userProfile",
type: "object",
additionalProperties: false,
properties: {
user: {
$ref: "shared-schema#/definitions/user",
},
joinedAt: { type: "string", format: "date-time" },
},
required: ["user", "joinedAt"],
} as const satisfies JSONSchema
type UserProfile = FromSchema<typeof userProfileSchema, {
references: [typeof sharedSchema]
deserialize: [{ pattern: { type: "string"; format: "date-time" }; output: Date }]
}>;
// Use JsonSchemaToTsProvider with shared schema references
const fastify = Fastify().withTypeProvider<
JsonSchemaToTsProvider<{
ValidatorSchemaOptions: { references: [typeof sharedSchema] }
SerializerSchemaOptions: {
references: [typeof userProfileSchema]
deserialize: [{ pattern: { type: "string"; format: "date-time" }; output: Date }]
}
}>
>()
fastify.get(
"/profile",
{
schema: {
body: {
type: "object",
properties: {
user: {
$ref: "shared-schema#/definitions/user",
},
},
required: ['user'],
},
response: {
200: { $ref: "userProfile#" },
},
} as const,
},
(req, reply) => {
// `givenName` and `familyName` are correctly typed as strings
const { givenName, familyName } = req.body.user;
// Construct a compatible response type
const profile: UserProfile = {
user: { givenName: "John", familyName: "Doe" },
joinedAt: new Date(), // Returning a Date object
};
// A type error is surfaced if profile doesn't match the serialization schema
reply.send(profile)
}
)
在插件定义中使用引用
在定义插件时,也可以使用共享的 schema 引用和去区域化选项 FastifyPluginAsyncJsonSchemaToTs 财务报告和审定财务报表 FastifyPluginCallbackJsonSchemaToTs。 。 。 .
示例
const schemaPerson = {
$id: "schema:person",
type: "object",
additionalProperties: false,
properties: {
givenName: { type: "string" },
familyName: { type: "string" },
joinedAt: { type: "string", format: "date-time" },
},
required: ["givenName", "familyName"],
} as const satisfies JSONSchema;
const plugin: FastifyPluginAsyncJsonSchemaToTs<{
ValidatorSchemaOptions: { references: [typeof schemaPerson] }
SerializerSchemaOptions: {
references: [typeof schemaPerson]
deserialize: [{ pattern: { type: "string"; format: "date-time" }; output: Date }]
};
}> = async function (fastify, _opts) {
fastify.addSchema(schemaPerson)
fastify.get(
"/profile",
{
schema: {
body: {
type: "object",
properties: {
user: {
$ref: "schema:person",
},
},
required: ['user'],
},
response: {
200: { $ref: "schema:person" },
},
}, // as const satisfies JSONSchema is not required thanks to FastifyPluginAsyncJsonSchemaToTs
},
(req, reply) => {
// `givenName`, `familyName`, and `joinedAt` are correctly typed as strings and validated for format.
const { givenName, familyName, joinedAt } = req.body.user;
// Send a serialized response
reply.send({
givenName: "John",
familyName: "Doe",
// Date objects form DB queries can be returned directly and transformed to string by fast-json-stringify
joinedAt: new Date(),
})
}
)
}
const callbackPlugin: FastifyPluginCallbackJsonSchemaToTs<{
ValidatorSchemaOptions: { references: [typeof schemaPerson] }
SerializerSchemaOptions: {
references: [typeof schemaPerson]
deserialize: [{ pattern: { type: "string"; format: "date-time" }; output: Date }]
};
}> = (fastify, options, done) => {
// Type check for custom options
expectType<string>(options.optionA)
// Schema is already added above
// fastify.addSchema(schemaPerson);
fastify.get(
"/callback-profile",
{
schema: {
body: {
type: "object",
properties: {
user: { $ref: "schema:person" },
},
required: ["user"],
},
response: {
200: { $ref: "schema:person" },
},
},
},
(req, reply) => {
const { givenName, familyName, joinedAt } = req.body.user
reply.send({
givenName,
familyName,
joinedAt: new Date(),
});
}
);
done()
};
许可证
许可证根据 麻省理工学院。 。 。 .