@settlemint/sdk-hasura
Version:
Hasura and PostgreSQL integration module for SettleMint SDK, enabling database operations and GraphQL queries
129 lines (126 loc) • 4.57 kB
JavaScript
//#region rolldown:runtime
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
const __settlemint_sdk_utils_http = __toESM(require("@settlemint/sdk-utils/http"));
const __settlemint_sdk_utils_logging = __toESM(require("@settlemint/sdk-utils/logging"));
const __settlemint_sdk_utils_runtime = __toESM(require("@settlemint/sdk-utils/runtime"));
const __settlemint_sdk_utils_validation = __toESM(require("@settlemint/sdk-utils/validation"));
const gql_tada = __toESM(require("gql.tada"));
const graphql_request = __toESM(require("graphql-request"));
const zod_v4 = __toESM(require("zod/v4"));
//#region src/hasura.ts
/**
* Schema for validating client options for the Hasura client.
*/
const ClientOptionsSchema = zod_v4.z.object({
instance: __settlemint_sdk_utils_validation.UrlOrPathSchema,
accessToken: __settlemint_sdk_utils_validation.ApplicationAccessTokenSchema.optional(),
adminSecret: zod_v4.z.string(),
cache: zod_v4.z.enum([
"default",
"force-cache",
"no-cache",
"no-store",
"only-if-cached",
"reload"
]).optional()
});
/**
* Creates a Hasura GraphQL client with proper type safety using gql.tada
*
* @param options - Configuration options for the client
* @param clientOptions - Optional GraphQL client configuration options
* @param logger - Optional logger to use for logging the requests
* @returns An object containing:
* - client: The configured GraphQL client instance
* - graphql: The initialized gql.tada function for type-safe queries
* @throws Will throw an error if the options fail validation against ClientOptionsSchema
* @example
* import { createHasuraClient } from '@settlemint/sdk-hasura';
* import type { introspection } from "@schemas/hasura-env";
* import { createLogger, requestLogger } from "@settlemint/sdk-utils/logging";
*
* const logger = createLogger();
*
* const { client, graphql } = createHasuraClient<{
* introspection: introspection;
* disableMasking: true;
* scalars: {
* timestamp: string;
* timestampz: string;
* uuid: string;
* date: string;
* time: string;
* jsonb: string;
* numeric: string;
* interval: string;
* geometry: string;
* geography: string;
* };
* }>({
* instance: process.env.SETTLEMINT_HASURA_ENDPOINT,
* accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,
* adminSecret: process.env.SETTLEMINT_HASURA_ADMIN_SECRET,
* }, {
* fetch: requestLogger(logger, "hasura", fetch) as typeof fetch,
* });
*
* // Making GraphQL queries
* const query = graphql(`
* query GetUsers {
* users {
* id
* name
* email
* }
* }
* `);
*
* const result = await client.request(query);
*/
function createHasuraClient(options, clientOptions, logger) {
(0, __settlemint_sdk_utils_runtime.ensureServer)();
const validatedOptions = (0, __settlemint_sdk_utils_validation.validate)(ClientOptionsSchema, options);
const graphql = (0, gql_tada.initGraphQLTada)();
const fullUrl = new URL(validatedOptions.instance).toString();
return {
client: new graphql_request.GraphQLClient(fullUrl, {
...clientOptions,
headers: (0, __settlemint_sdk_utils_http.appendHeaders)(clientOptions?.headers, {
"x-auth-token": validatedOptions.accessToken,
"x-hasura-admin-secret": validatedOptions.adminSecret
}),
fetch: logger ? (0, __settlemint_sdk_utils_logging.requestLogger)(logger, "hasura", fetch) : fetch
}),
graphql
};
}
//#endregion
exports.ClientOptionsSchema = ClientOptionsSchema;
exports.createHasuraClient = createHasuraClient;
Object.defineProperty(exports, 'readFragment', {
enumerable: true,
get: function () {
return gql_tada.readFragment;
}
});
//# sourceMappingURL=hasura.cjs.map