{
  "version": 12,
  "name": "16_functions_and_libraries",
  "description": "Example: reusable code. A library (a plain .js file loaded before Functions) provides a tiny validation toolkit; the Functions panel defines flow helpers built on it; code blocks and run conditionals call both.\nNeeds the 'run code' permission.",
  "globals": [
    {
      "name": "$dummy",
      "value": "https://dummyjson.com"
    },
    {
      "name": "$minRating",
      "value": "4"
    }
  ],
  "globalFunctions": "// Fails the current step with a readable message, or logs a check mark.\nfunction assert(condition, message) {\n  if (!condition) throw new Error(\"Assertion failed: \" + message);\n  log(\"✔ \" + message);\n}\n\n// Flow helper built on the qa-kit library.\nfunction assertValidUser(u) {\n  assert(qaKit.isEmail(u.email), u.username + \" has a valid email\");\n  assert(u.age >= 18, u.username + \" is an adult\");\n}\n\n// Usable from run conditionals too.\nfunction isWellRated(rating) {\n  return rating >= $minRating;\n}",
  "libraries": [
    {
      "name": "qaKit",
      "fileName": "qa-kit.js",
      "code": "// qa-kit: a tiny example library (plain script, exposes a global).\nvar qaKit = {\n  isEmail: function (s) { return /^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$/.test(String(s)); },\n  isIsoDate: function (s) { return !isNaN(Date.parse(s)) && /^\\d{4}-\\d{2}-\\d{2}/.test(String(s)); },\n  average: function (xs) { return xs.length ? xs.reduce(function (a, b) { return a + b; }, 0) / xs.length : 0; }\n};",
      "kind": "globals",
      "members": []
    }
  ],
  "activeEnvironment": "",
  "auth": {
    "type": "none"
  },
  "settings": {},
  "data": {
    "file": ""
  },
  "cookies": [],
  "tags": [
    "example",
    "functions",
    "libraries",
    "code"
  ],
  "steps": [
    {
      "id": "users",
      "method": "GET",
      "name": "First 5 users",
      "params": [
        {
          "name": "limit",
          "value": "5"
        },
        {
          "name": "select",
          "value": "username,email,age"
        }
      ],
      "type": "request",
      "url": "$dummy/users"
    },
    {
      "code": "const { users } = JSON.parse(response.body);\nusers.forEach(assertValidUser);",
      "id": "validate-users",
      "name": "Validate users with Functions + library",
      "type": "code"
    },
    {
      "extract": [],
      "id": "reviews",
      "method": "GET",
      "name": "Product reviews",
      "params": [
        {
          "name": "select",
          "value": "title,reviews,rating"
        }
      ],
      "type": "request",
      "url": "$dummy/products/6",
      "checks": [
        {
          "op": "equals",
          "target": "status",
          "value": "200"
        },
        {
          "op": "type",
          "path": "$.reviews",
          "target": "json",
          "value": "array"
        }
      ]
    },
    {
      "code": "const p = JSON.parse(response.body);\nconst avg = qaKit.average(p.reviews.map((r) => r.rating));\nassert(p.reviews.every((r) => qaKit.isIsoDate(r.date)), \"every review has an ISO date\");\nlog(`${p.title}: ${p.reviews.length} reviews, average ${avg.toFixed(2)}`);\n$rating = p.rating;",
      "id": "review-stats",
      "name": "Average review score with the library",
      "type": "code"
    },
    {
      "conditional": "isWellRated($rating)",
      "id": "well-rated-only",
      "method": "GET",
      "name": "Only for well-rated products (Function in a conditional)",
      "params": [
        {
          "name": "rating",
          "value": "$rating"
        }
      ],
      "type": "request",
      "url": "$dummy/test"
    }
  ]
}
