"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/thegraph.ts var thegraph_exports = {}; __export(thegraph_exports, { ClientOptionsSchema: () => ClientOptionsSchema, createTheGraphClient: () => createTheGraphClient, readFragment: () => import_gql2.readFragment }); module.exports = __toCommonJS(thegraph_exports); var import_runtime = require("@settlemint/sdk-utils/runtime"); var import_validation = require("@settlemint/sdk-utils/validation"); var import_gql = require("gql.tada"); var import_graphql_request = require("graphql-request"); var import_zod = require("zod"); var import_gql2 = require("gql.tada"); var ClientOptionsSchema = import_zod.z.discriminatedUnion("runtime", [ import_zod.z.object({ instances: import_zod.z.array(import_validation.UrlOrPathSchema), runtime: import_zod.z.literal("server"), accessToken: import_validation.ApplicationAccessTokenSchema, subgraphName: import_zod.z.string() }), import_zod.z.object({ runtime: import_zod.z.literal("browser"), subgraphName: import_zod.z.string() }) ]); function getFullUrl(options) { if (options.runtime === "server") { const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`)); if (!instance) { throw new Error(`Instance for subgraph ${options.subgraphName} not found`); } return new URL(instance).toString(); } return new URL( `/proxy/thegraph/graphql/${encodeURIComponent(options.subgraphName)}`, window?.location?.origin ?? "http://localhost:3000" ).toString(); } function createTheGraphClient(options, clientOptions) { const validatedOptions = (0, import_validation.validate)(ClientOptionsSchema, { ...options, runtime: import_runtime.runsOnServer ? "server" : "browser" }); const graphql = (0, import_gql.initGraphQLTada)(); const fullUrl = getFullUrl(validatedOptions); return { client: new import_graphql_request.GraphQLClient(fullUrl, { ...clientOptions, ...validatedOptions.runtime === "server" && { headers: { "x-auth-token": validatedOptions.accessToken } } }), graphql }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ClientOptionsSchema, createTheGraphClient, readFragment }); //# sourceMappingURL=thegraph.cjs.map