Function fromURL

  • Asynchronously fetches and validates data from one or more URLs against a provided schema.

    This function is designed to handle fetching JSON data from specified URLs, which can either be a single URL or an array of URLs. It fetches all URLs in parallel, parses the JSON, merges the results into a single object, and then validates the merged object against the provided schema. The function leverages exception handling to manage errors during the fetch process.

    Example


    import { fromURL } from "@mrspartak/config";
    import * as z from "zod";

    const schema = z.object({
    name: z.string(),
    age: z.number(),
    isStudent: z.boolean(),
    });

    const data = await fromURL({
    url: "https://example.com/data.json",
    schema,
    });

    console.log(data); // { name: 'Alice', age: 25, isStudent: true }
    const name: string = data.name; // OK

    Type Parameters

    Parameters

    • params: {
          schema: $Validator;
          url: string | string[];
      }

      The parameters object.

      • schema: $Validator

        The schema against which the merged data will be validated.

      • url: string | string[]

        One or more URLs from which to fetch data.

    Returns Promise<inferValidator<$Validator>["out"]>

    A promise that resolves with the result of the validation.

    Throws

    • Captures and rethrows exceptions with context if the fetch or parsing fails.