@settlemint/sdk-thegraph 1.2.5-pr252cf55e → 1.2.5-pr67bd8eac

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/thegraph.cjs CHANGED
@@ -31,47 +31,30 @@ var import_gql = require("gql.tada");
31
31
  var import_graphql_request = require("graphql-request");
32
32
  var import_zod = require("zod");
33
33
  var import_gql2 = require("gql.tada");
34
- var ClientOptionsSchema = import_zod.z.discriminatedUnion("runtime", [
35
- import_zod.z.object({
36
- instances: import_zod.z.array(import_validation.UrlOrPathSchema),
37
- runtime: import_zod.z.literal("server"),
38
- accessToken: import_validation.ApplicationAccessTokenSchema,
39
- subgraphName: import_zod.z.string(),
40
- cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
41
- }),
42
- import_zod.z.object({
43
- runtime: import_zod.z.literal("browser"),
44
- subgraphName: import_zod.z.string(),
45
- cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
46
- })
47
- ]);
34
+ var ClientOptionsSchema = import_zod.z.object({
35
+ instances: import_zod.z.array(import_validation.UrlOrPathSchema),
36
+ accessToken: import_validation.ApplicationAccessTokenSchema,
37
+ subgraphName: import_zod.z.string(),
38
+ cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
39
+ });
48
40
  function getFullUrl(options) {
49
- if (options.runtime === "server") {
50
- const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`));
51
- if (!instance) {
52
- throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
53
- }
54
- return new URL(instance).toString();
41
+ const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`));
42
+ if (!instance) {
43
+ throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
55
44
  }
56
- return new URL(
57
- `/proxy/thegraph/graphql/${encodeURIComponent(options.subgraphName)}`,
58
- window?.location?.origin ?? "http://localhost:3000"
59
- ).toString();
45
+ return new URL(instance).toString();
60
46
  }
61
47
  function createTheGraphClient(options, clientOptions) {
62
- const validatedOptions = (0, import_validation.validate)(ClientOptionsSchema, {
63
- ...options,
64
- runtime: import_runtime.runsOnServer ? "server" : "browser"
65
- });
48
+ (0, import_runtime.ensureServer)();
49
+ const validatedOptions = (0, import_validation.validate)(ClientOptionsSchema, options);
66
50
  const graphql = (0, import_gql.initGraphQLTada)();
67
51
  const fullUrl = getFullUrl(validatedOptions);
68
52
  return {
69
53
  client: new import_graphql_request.GraphQLClient(fullUrl, {
70
54
  ...clientOptions,
71
- ...validatedOptions.runtime === "server" && {
72
- headers: {
73
- "x-auth-token": validatedOptions.accessToken
74
- }
55
+ headers: {
56
+ ...clientOptions?.headers ?? {},
57
+ "x-auth-token": validatedOptions.accessToken
75
58
  }
76
59
  }),
77
60
  graphql
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import { runsOnServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n * Defines two possible runtime configurations:\n * 1. Server-side with instance URLs, access token and subgraph name\n * 2. Browser-side with just subgraph name\n */\nexport const ClientOptionsSchema = z.discriminatedUnion(\"runtime\", [\n z.object({\n instances: z.array(UrlOrPathSchema),\n runtime: z.literal(\"server\"),\n accessToken: ApplicationAccessTokenSchema,\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n }),\n z.object({\n runtime: z.literal(\"browser\"),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n }),\n]);\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n if (options.runtime === \"server\") {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n }\n return new URL(\n `/proxy/thegraph/graphql/${encodeURIComponent(options.subgraphName)}`,\n window?.location?.origin ?? \"http://localhost:3000\",\n ).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client:\n * - For server-side: instance URLs, access token and subgraph name\n * - For browser-side: just subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n *\n * // Server-side usage\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,\n * subgraphName: 'kits'\n * });\n *\n * // Browser-side usage\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * subgraphName: 'kits'\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: Omit<ClientOptions, \"runtime\"> & Record<string, unknown>,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n const validatedOptions = validate(ClientOptionsSchema, {\n ...options,\n runtime: runsOnServer ? \"server\" : \"browser\",\n });\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n ...(validatedOptions.runtime === \"server\" && {\n headers: {\n \"x-auth-token\": validatedOptions.accessToken,\n },\n }),\n }),\n graphql,\n };\n}\n\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA6B;AAC7B,wBAAwE;AACxE,iBAA0D;AAC1D,6BAA8B;AAC9B,iBAAkB;AA6IlB,IAAAA,cAA6B;AAhItB,IAAM,sBAAsB,aAAE,mBAAmB,WAAW;AAAA,EACjE,aAAE,OAAO;AAAA,IACP,WAAW,aAAE,MAAM,iCAAe;AAAA,IAClC,SAAS,aAAE,QAAQ,QAAQ;AAAA,IAC3B,aAAa;AAAA,IACb,cAAc,aAAE,OAAO;AAAA,IACvB,OAAO,aAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AAAA,EACzG,CAAC;AAAA,EACD,aAAE,OAAO;AAAA,IACP,SAAS,aAAE,QAAQ,SAAS;AAAA,IAC5B,cAAc,aAAE,OAAO;AAAA,IACvB,OAAO,aAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AAAA,EACzG,CAAC;AACH,CAAC;AAcD,SAAS,WAAW,SAAgC;AAClD,MAAI,QAAQ,YAAY,UAAU;AAChC,UAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,cAAaA,UAAS,SAAS,IAAI,QAAQ,YAAY,EAAE,CAAC;AACnG,QAAI,CAAC,UAAU;AACb,YAAM,IAAI,MAAM,yBAAyB,QAAQ,YAAY,YAAY;AAAA,IAC3E;AACA,WAAO,IAAI,IAAI,QAAQ,EAAE,SAAS;AAAA,EACpC;AACA,SAAO,IAAI;AAAA,IACT,2BAA2B,mBAAmB,QAAQ,YAAY,CAAC;AAAA,IACnE,QAAQ,UAAU,UAAU;AAAA,EAC9B,EAAE,SAAS;AACb;AA8DO,SAAS,qBACd,SACA,eAIA;AACA,QAAM,uBAAmB,4BAAS,qBAAqB;AAAA,IACrD,GAAG;AAAA,IACH,SAAS,8BAAe,WAAW;AAAA,EACrC,CAAC;AACD,QAAM,cAAU,4BAAuB;AACvC,QAAM,UAAU,WAAW,gBAAgB;AAE3C,SAAO;AAAA,IACL,QAAQ,IAAI,qCAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,GAAI,iBAAiB,YAAY,YAAY;AAAA,QAC3C,SAAS;AAAA,UACP,gBAAgB,iBAAiB;AAAA,QACnC;AAAA,MACF;AAAA,IACF,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["import_gql","instance"]}
1
+ {"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema,\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,\n * subgraphName: 'kits'\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: {\n ...(clientOptions?.headers ?? {}),\n \"x-auth-token\": validatedOptions.accessToken,\n },\n }),\n graphql,\n };\n}\n\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA6B;AAC7B,wBAAwE;AACxE,iBAA0D;AAC1D,6BAA8B;AAC9B,iBAAkB;AAwGlB,IAAAA,cAA6B;AA9FtB,IAAM,sBAAsB,aAAE,OAAO;AAAA,EAC1C,WAAW,aAAE,MAAM,iCAAe;AAAA,EAClC,aAAa;AAAA,EACb,cAAc,aAAE,OAAO;AAAA,EACvB,OAAO,aAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AACzG,CAAC;AAcD,SAAS,WAAW,SAAgC;AAClD,QAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,cAAaA,UAAS,SAAS,IAAI,QAAQ,YAAY,EAAE,CAAC;AACnG,MAAI,CAAC,UAAU;AACb,UAAM,IAAI,MAAM,yBAAyB,QAAQ,YAAY,YAAY;AAAA,EAC3E;AACA,SAAO,IAAI,IAAI,QAAQ,EAAE,SAAS;AACpC;AA6CO,SAAS,qBACd,SACA,eAIA;AACA,mCAAa;AACb,QAAM,uBAAmB,4BAAS,qBAAqB,OAAO;AAC9D,QAAM,cAAU,4BAAuB;AACvC,QAAM,UAAU,WAAW,gBAAgB;AAE3C,SAAO;AAAA,IACL,QAAQ,IAAI,qCAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,SAAS;AAAA,QACP,GAAI,eAAe,WAAW,CAAC;AAAA,QAC/B,gBAAgB,iBAAiB;AAAA,MACnC;AAAA,IACF,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["import_gql","instance"]}
@@ -9,41 +9,23 @@ import { z } from 'zod';
9
9
  type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
10
10
  /**
11
11
  * Schema for validating client options for the TheGraph client.
12
- * Defines two possible runtime configurations:
13
- * 1. Server-side with instance URLs, access token and subgraph name
14
- * 2. Browser-side with just subgraph name
15
12
  */
16
- declare const ClientOptionsSchema: z.ZodDiscriminatedUnion<"runtime", [z.ZodObject<{
13
+ declare const ClientOptionsSchema: z.ZodObject<{
17
14
  instances: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
18
- runtime: z.ZodLiteral<"server">;
19
15
  accessToken: z.ZodString;
20
16
  subgraphName: z.ZodString;
21
17
  cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
22
18
  }, "strip", z.ZodTypeAny, {
23
- runtime: "server";
24
19
  instances: string[];
25
20
  accessToken: string;
26
21
  subgraphName: string;
27
22
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
28
23
  }, {
29
- runtime: "server";
30
24
  instances: string[];
31
25
  accessToken: string;
32
26
  subgraphName: string;
33
27
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
34
- }>, z.ZodObject<{
35
- runtime: z.ZodLiteral<"browser">;
36
- subgraphName: z.ZodString;
37
- cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
38
- }, "strip", z.ZodTypeAny, {
39
- runtime: "browser";
40
- subgraphName: string;
41
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
42
- }, {
43
- runtime: "browser";
44
- subgraphName: string;
45
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
46
- }>]>;
28
+ }>;
47
29
  /**
48
30
  * Type definition for client options derived from the ClientOptionsSchema
49
31
  */
@@ -51,9 +33,8 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
51
33
  /**
52
34
  * Creates a TheGraph GraphQL client with proper type safety using gql.tada
53
35
  *
54
- * @param options - Configuration options for the client:
55
- * - For server-side: instance URLs, access token and subgraph name
56
- * - For browser-side: just subgraph name
36
+ * @param options - Configuration options for the client including instance URLs,
37
+ * access token and subgraph name
57
38
  * @param clientOptions - Optional GraphQL client configuration options
58
39
  * @returns An object containing:
59
40
  * - client: The configured GraphQL client instance
@@ -63,7 +44,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
63
44
  * import { createTheGraphClient } from '@settlemint/sdk-thegraph';
64
45
  * import type { introspection } from '@schemas/the-graph-env-kits';
65
46
  *
66
- * // Server-side usage
67
47
  * const { client, graphql } = createTheGraphClient<{
68
48
  * introspection: introspection;
69
49
  * disableMasking: true;
@@ -80,21 +60,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
80
60
  * subgraphName: 'kits'
81
61
  * });
82
62
  *
83
- * // Browser-side usage
84
- * const { client, graphql } = createTheGraphClient<{
85
- * introspection: introspection;
86
- * disableMasking: true;
87
- * scalars: {
88
- * Bytes: string;
89
- * Int8: string;
90
- * BigInt: string;
91
- * BigDecimal: string;
92
- * Timestamp: string;
93
- * };
94
- * }>({
95
- * subgraphName: 'kits'
96
- * });
97
- *
98
63
  * // Making GraphQL queries
99
64
  * const query = graphql(`
100
65
  * query SearchAssets {
@@ -108,7 +73,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
108
73
  *
109
74
  * const result = await client.request(query);
110
75
  */
111
- declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options: Omit<ClientOptions, "runtime"> & Record<string, unknown>, clientOptions?: RequestConfig): {
76
+ declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
112
77
  client: GraphQLClient;
113
78
  graphql: initGraphQLTada<Setup>;
114
79
  };
@@ -9,41 +9,23 @@ import { z } from 'zod';
9
9
  type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
10
10
  /**
11
11
  * Schema for validating client options for the TheGraph client.
12
- * Defines two possible runtime configurations:
13
- * 1. Server-side with instance URLs, access token and subgraph name
14
- * 2. Browser-side with just subgraph name
15
12
  */
16
- declare const ClientOptionsSchema: z.ZodDiscriminatedUnion<"runtime", [z.ZodObject<{
13
+ declare const ClientOptionsSchema: z.ZodObject<{
17
14
  instances: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
18
- runtime: z.ZodLiteral<"server">;
19
15
  accessToken: z.ZodString;
20
16
  subgraphName: z.ZodString;
21
17
  cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
22
18
  }, "strip", z.ZodTypeAny, {
23
- runtime: "server";
24
19
  instances: string[];
25
20
  accessToken: string;
26
21
  subgraphName: string;
27
22
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
28
23
  }, {
29
- runtime: "server";
30
24
  instances: string[];
31
25
  accessToken: string;
32
26
  subgraphName: string;
33
27
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
34
- }>, z.ZodObject<{
35
- runtime: z.ZodLiteral<"browser">;
36
- subgraphName: z.ZodString;
37
- cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
38
- }, "strip", z.ZodTypeAny, {
39
- runtime: "browser";
40
- subgraphName: string;
41
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
42
- }, {
43
- runtime: "browser";
44
- subgraphName: string;
45
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
46
- }>]>;
28
+ }>;
47
29
  /**
48
30
  * Type definition for client options derived from the ClientOptionsSchema
49
31
  */
@@ -51,9 +33,8 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
51
33
  /**
52
34
  * Creates a TheGraph GraphQL client with proper type safety using gql.tada
53
35
  *
54
- * @param options - Configuration options for the client:
55
- * - For server-side: instance URLs, access token and subgraph name
56
- * - For browser-side: just subgraph name
36
+ * @param options - Configuration options for the client including instance URLs,
37
+ * access token and subgraph name
57
38
  * @param clientOptions - Optional GraphQL client configuration options
58
39
  * @returns An object containing:
59
40
  * - client: The configured GraphQL client instance
@@ -63,7 +44,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
63
44
  * import { createTheGraphClient } from '@settlemint/sdk-thegraph';
64
45
  * import type { introspection } from '@schemas/the-graph-env-kits';
65
46
  *
66
- * // Server-side usage
67
47
  * const { client, graphql } = createTheGraphClient<{
68
48
  * introspection: introspection;
69
49
  * disableMasking: true;
@@ -80,21 +60,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
80
60
  * subgraphName: 'kits'
81
61
  * });
82
62
  *
83
- * // Browser-side usage
84
- * const { client, graphql } = createTheGraphClient<{
85
- * introspection: introspection;
86
- * disableMasking: true;
87
- * scalars: {
88
- * Bytes: string;
89
- * Int8: string;
90
- * BigInt: string;
91
- * BigDecimal: string;
92
- * Timestamp: string;
93
- * };
94
- * }>({
95
- * subgraphName: 'kits'
96
- * });
97
- *
98
63
  * // Making GraphQL queries
99
64
  * const query = graphql(`
100
65
  * query SearchAssets {
@@ -108,7 +73,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
108
73
  *
109
74
  * const result = await client.request(query);
110
75
  */
111
- declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options: Omit<ClientOptions, "runtime"> & Record<string, unknown>, clientOptions?: RequestConfig): {
76
+ declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
112
77
  client: GraphQLClient;
113
78
  graphql: initGraphQLTada<Setup>;
114
79
  };
package/dist/thegraph.mjs CHANGED
@@ -1,51 +1,34 @@
1
1
  // src/thegraph.ts
2
- import { runsOnServer } from "@settlemint/sdk-utils/runtime";
2
+ import { ensureServer } from "@settlemint/sdk-utils/runtime";
3
3
  import { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from "@settlemint/sdk-utils/validation";
4
4
  import { initGraphQLTada } from "gql.tada";
5
5
  import { GraphQLClient } from "graphql-request";
6
6
  import { z } from "zod";
7
7
  import { readFragment } from "gql.tada";
8
- var ClientOptionsSchema = z.discriminatedUnion("runtime", [
9
- z.object({
10
- instances: z.array(UrlOrPathSchema),
11
- runtime: z.literal("server"),
12
- accessToken: ApplicationAccessTokenSchema,
13
- subgraphName: z.string(),
14
- cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
15
- }),
16
- z.object({
17
- runtime: z.literal("browser"),
18
- subgraphName: z.string(),
19
- cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
20
- })
21
- ]);
8
+ var ClientOptionsSchema = z.object({
9
+ instances: z.array(UrlOrPathSchema),
10
+ accessToken: ApplicationAccessTokenSchema,
11
+ subgraphName: z.string(),
12
+ cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
13
+ });
22
14
  function getFullUrl(options) {
23
- if (options.runtime === "server") {
24
- const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`));
25
- if (!instance) {
26
- throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
27
- }
28
- return new URL(instance).toString();
15
+ const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`));
16
+ if (!instance) {
17
+ throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
29
18
  }
30
- return new URL(
31
- `/proxy/thegraph/graphql/${encodeURIComponent(options.subgraphName)}`,
32
- window?.location?.origin ?? "http://localhost:3000"
33
- ).toString();
19
+ return new URL(instance).toString();
34
20
  }
35
21
  function createTheGraphClient(options, clientOptions) {
36
- const validatedOptions = validate(ClientOptionsSchema, {
37
- ...options,
38
- runtime: runsOnServer ? "server" : "browser"
39
- });
22
+ ensureServer();
23
+ const validatedOptions = validate(ClientOptionsSchema, options);
40
24
  const graphql = initGraphQLTada();
41
25
  const fullUrl = getFullUrl(validatedOptions);
42
26
  return {
43
27
  client: new GraphQLClient(fullUrl, {
44
28
  ...clientOptions,
45
- ...validatedOptions.runtime === "server" && {
46
- headers: {
47
- "x-auth-token": validatedOptions.accessToken
48
- }
29
+ headers: {
30
+ ...clientOptions?.headers ?? {},
31
+ "x-auth-token": validatedOptions.accessToken
49
32
  }
50
33
  }),
51
34
  graphql
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import { runsOnServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n * Defines two possible runtime configurations:\n * 1. Server-side with instance URLs, access token and subgraph name\n * 2. Browser-side with just subgraph name\n */\nexport const ClientOptionsSchema = z.discriminatedUnion(\"runtime\", [\n z.object({\n instances: z.array(UrlOrPathSchema),\n runtime: z.literal(\"server\"),\n accessToken: ApplicationAccessTokenSchema,\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n }),\n z.object({\n runtime: z.literal(\"browser\"),\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n }),\n]);\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n if (options.runtime === \"server\") {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n }\n return new URL(\n `/proxy/thegraph/graphql/${encodeURIComponent(options.subgraphName)}`,\n window?.location?.origin ?? \"http://localhost:3000\",\n ).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client:\n * - For server-side: instance URLs, access token and subgraph name\n * - For browser-side: just subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n *\n * // Server-side usage\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,\n * subgraphName: 'kits'\n * });\n *\n * // Browser-side usage\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * subgraphName: 'kits'\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: Omit<ClientOptions, \"runtime\"> & Record<string, unknown>,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n const validatedOptions = validate(ClientOptionsSchema, {\n ...options,\n runtime: runsOnServer ? \"server\" : \"browser\",\n });\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n ...(validatedOptions.runtime === \"server\" && {\n headers: {\n \"x-auth-token\": validatedOptions.accessToken,\n },\n }),\n }),\n graphql,\n };\n}\n\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,8BAA8B,iBAAiB,gBAAgB;AACxE,SAAmC,uBAAuB;AAC1D,SAAS,qBAAqB;AAC9B,SAAS,SAAS;AA6IlB,SAAS,oBAAoB;AAhItB,IAAM,sBAAsB,EAAE,mBAAmB,WAAW;AAAA,EACjE,EAAE,OAAO;AAAA,IACP,WAAW,EAAE,MAAM,eAAe;AAAA,IAClC,SAAS,EAAE,QAAQ,QAAQ;AAAA,IAC3B,aAAa;AAAA,IACb,cAAc,EAAE,OAAO;AAAA,IACvB,OAAO,EAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AAAA,EACzG,CAAC;AAAA,EACD,EAAE,OAAO;AAAA,IACP,SAAS,EAAE,QAAQ,SAAS;AAAA,IAC5B,cAAc,EAAE,OAAO;AAAA,IACvB,OAAO,EAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AAAA,EACzG,CAAC;AACH,CAAC;AAcD,SAAS,WAAW,SAAgC;AAClD,MAAI,QAAQ,YAAY,UAAU;AAChC,UAAM,WAAW,QAAQ,UAAU,KAAK,CAACA,cAAaA,UAAS,SAAS,IAAI,QAAQ,YAAY,EAAE,CAAC;AACnG,QAAI,CAAC,UAAU;AACb,YAAM,IAAI,MAAM,yBAAyB,QAAQ,YAAY,YAAY;AAAA,IAC3E;AACA,WAAO,IAAI,IAAI,QAAQ,EAAE,SAAS;AAAA,EACpC;AACA,SAAO,IAAI;AAAA,IACT,2BAA2B,mBAAmB,QAAQ,YAAY,CAAC;AAAA,IACnE,QAAQ,UAAU,UAAU;AAAA,EAC9B,EAAE,SAAS;AACb;AA8DO,SAAS,qBACd,SACA,eAIA;AACA,QAAM,mBAAmB,SAAS,qBAAqB;AAAA,IACrD,GAAG;AAAA,IACH,SAAS,eAAe,WAAW;AAAA,EACrC,CAAC;AACD,QAAM,UAAU,gBAAuB;AACvC,QAAM,UAAU,WAAW,gBAAgB;AAE3C,SAAO;AAAA,IACL,QAAQ,IAAI,cAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,GAAI,iBAAiB,YAAY,YAAY;AAAA,QAC3C,SAAS;AAAA,UACP,gBAAgB,iBAAiB;AAAA,QACnC;AAAA,MACF;AAAA,IACF,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["instance"]}
1
+ {"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema,\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,\n * subgraphName: 'kits'\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: {\n ...(clientOptions?.headers ?? {}),\n \"x-auth-token\": validatedOptions.accessToken,\n },\n }),\n graphql,\n };\n}\n\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,8BAA8B,iBAAiB,gBAAgB;AACxE,SAAmC,uBAAuB;AAC1D,SAAS,qBAAqB;AAC9B,SAAS,SAAS;AAwGlB,SAAS,oBAAoB;AA9FtB,IAAM,sBAAsB,EAAE,OAAO;AAAA,EAC1C,WAAW,EAAE,MAAM,eAAe;AAAA,EAClC,aAAa;AAAA,EACb,cAAc,EAAE,OAAO;AAAA,EACvB,OAAO,EAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AACzG,CAAC;AAcD,SAAS,WAAW,SAAgC;AAClD,QAAM,WAAW,QAAQ,UAAU,KAAK,CAACA,cAAaA,UAAS,SAAS,IAAI,QAAQ,YAAY,EAAE,CAAC;AACnG,MAAI,CAAC,UAAU;AACb,UAAM,IAAI,MAAM,yBAAyB,QAAQ,YAAY,YAAY;AAAA,EAC3E;AACA,SAAO,IAAI,IAAI,QAAQ,EAAE,SAAS;AACpC;AA6CO,SAAS,qBACd,SACA,eAIA;AACA,eAAa;AACb,QAAM,mBAAmB,SAAS,qBAAqB,OAAO;AAC9D,QAAM,UAAU,gBAAuB;AACvC,QAAM,UAAU,WAAW,gBAAgB;AAE3C,SAAO;AAAA,IACL,QAAQ,IAAI,cAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,SAAS;AAAA,QACP,GAAI,eAAe,WAAW,CAAC;AAAA,QAC/B,gBAAgB,iBAAiB;AAAA,MACnC;AAAA,IACF,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["instance"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@settlemint/sdk-thegraph",
3
3
  "description": "TheGraph integration module for SettleMint SDK, enabling querying and indexing of blockchain data through subgraphs",
4
- "version": "1.2.5-pr252cf55e",
4
+ "version": "1.2.5-pr67bd8eac",
5
5
  "type": "module",
6
6
  "private": false,
7
7
  "license": "FSL-1.1-MIT",
@@ -52,7 +52,7 @@
52
52
  "devDependencies": {},
53
53
  "dependencies": {
54
54
  "gql.tada": "^1",
55
- "@settlemint/sdk-utils": "1.2.5-pr252cf55e",
55
+ "@settlemint/sdk-utils": "1.2.5-pr67bd8eac",
56
56
  "graphql-request": "^7",
57
57
  "zod": "^3"
58
58
  },