पाठ 11 / 30

Response Serialization

प्रदर्शन के लिए responses को JSON में कैसे 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
  };
});