पाठ 11 / 32

Response Serialization

Response schemas घोषित करके JSON responses तेज़ करें, जिन्हें Fastify केवल सूचीबद्ध fields के लिए serialize करता है।

Schema-based serialization

Schema में response field Fastify को बताता है कि कौन-से fields भेजने हैं, केवल declared properties को शामिल करके JSON serialization को तेज़ करता है।

Response schema

Define करें कि response कैसा दिखता है। Fastify केवल schema में fields को serialize करेगा।

const getUserSchema = {
  response: {
    200: {
      type: 'object',
      properties: {
        id: { type: 'integer' },
        name: { type: 'string' },
        email: { type: 'string' },
      },
    },
  },
};

fastify.get('/users/:id', { schema: getUserSchema }, async (req) => {
  return {
    id: 1,
    name: 'Alice',
    email: 'alice@example.com',
    // password field will be stripped by serialization
  };
});