@settlemint/sdk-portal 1.2.5 → 2.0.0

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/README.md CHANGED
@@ -54,7 +54,7 @@ For detailed information about using the Smart Contract Portal Middleware, check
54
54
 
55
55
  > **createPortalClient**\<`Setup`\>(`options`, `clientOptions`?): `object`
56
56
 
57
- Defined in: [sdk/portal/src/portal.ts:110](https://github.com/settlemint/sdk/blob/v1.2.5/sdk/portal/src/portal.ts#L110)
57
+ Defined in: [sdk/portal/src/portal.ts:61](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/portal/src/portal.ts#L61)
58
58
 
59
59
  Creates a Portal GraphQL client with the provided configuration.
60
60
 
@@ -68,7 +68,10 @@ Creates a Portal GraphQL client with the provided configuration.
68
68
 
69
69
  | Parameter | Type | Description |
70
70
  | ------ | ------ | ------ |
71
- | `options` | `Omit`\<\{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instance`: `string`; `runtime`: `"server"`; \} \| \{ `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `runtime`: `"browser"`; \}, `"runtime"`\> & `Record`\<`string`, `unknown`\> | Configuration options for the Portal client |
71
+ | `options` | \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instance`: `string`; \} | Configuration options for the Portal client |
72
+ | `options.accessToken` | `string` | - |
73
+ | `options.cache`? | `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"` | - |
74
+ | `options.instance`? | `string` | - |
72
75
  | `clientOptions`? | `RequestConfig` | Additional GraphQL client configuration options |
73
76
 
74
77
  ##### Returns
@@ -79,8 +82,8 @@ An object containing the configured GraphQL client and graphql helper function
79
82
 
80
83
  | Name | Type | Defined in |
81
84
  | ------ | ------ | ------ |
82
- | `client` | `GraphQLClient` | [sdk/portal/src/portal.ts:114](https://github.com/settlemint/sdk/blob/v1.2.5/sdk/portal/src/portal.ts#L114) |
83
- | `graphql` | `initGraphQLTada`\<`Setup`\> | [sdk/portal/src/portal.ts:115](https://github.com/settlemint/sdk/blob/v1.2.5/sdk/portal/src/portal.ts#L115) |
85
+ | `client` | `GraphQLClient` | [sdk/portal/src/portal.ts:65](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/portal/src/portal.ts#L65) |
86
+ | `graphql` | `initGraphQLTada`\<`Setup`\> | [sdk/portal/src/portal.ts:66](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/portal/src/portal.ts#L66) |
84
87
 
85
88
  ##### Throws
86
89
 
@@ -92,7 +95,6 @@ If the provided options fail validation
92
95
  import { createPortalClient } from '@settlemint/sdk-portal';
93
96
  import type { introspection } from "@schemas/portal-env";
94
97
 
95
- // Server-side usage
96
98
  export const { client: portalClient, graphql: portalGraphql } = createPortalClient<{
97
99
  introspection: introspection;
98
100
  disableMasking: true;
@@ -102,20 +104,9 @@ export const { client: portalClient, graphql: portalGraphql } = createPortalClie
102
104
  };
103
105
  }>({
104
106
  instance: process.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT,
105
- runtime: "server",
106
107
  accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,
107
108
  });
108
109
 
109
- // Browser-side usage
110
- export const { client: portalBrowserClient, graphql: portalBrowserGraphql } = createPortalClient<{
111
- introspection: introspection;
112
- disableMasking: true;
113
- scalars: {
114
- // Change unknown to the type you are using to store metadata
115
- JSON: unknown;
116
- };
117
- }>({});
118
-
119
110
  // Making GraphQL queries
120
111
  const query = graphql(`
121
112
  query GetPendingTransactions {
@@ -134,7 +125,7 @@ const result = await client.request(query);
134
125
 
135
126
  > **ClientOptions** = `z.infer`\<*typeof* [`ClientOptionsSchema`](#clientoptionsschema)\>
136
127
 
137
- Defined in: [sdk/portal/src/portal.ts:32](https://github.com/settlemint/sdk/blob/v1.2.5/sdk/portal/src/portal.ts#L32)
128
+ Defined in: [sdk/portal/src/portal.ts:24](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/portal/src/portal.ts#L24)
138
129
 
139
130
  Type representing the validated client options.
140
131
 
@@ -144,7 +135,7 @@ Type representing the validated client options.
144
135
 
145
136
  > **RequestConfig** = `ConstructorParameters`\<*typeof* `GraphQLClient`\>\[`1`\]
146
137
 
147
- Defined in: [sdk/portal/src/portal.ts:10](https://github.com/settlemint/sdk/blob/v1.2.5/sdk/portal/src/portal.ts#L10)
138
+ Defined in: [sdk/portal/src/portal.ts:10](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/portal/src/portal.ts#L10)
148
139
 
149
140
  Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.
150
141
 
@@ -152,12 +143,11 @@ Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.
152
143
 
153
144
  #### ClientOptionsSchema
154
145
 
155
- > `const` **ClientOptionsSchema**: `ZodDiscriminatedUnion`\<`"runtime"`, \[`ZodObject`\<\{ `accessToken`: `ZodString`; `cache`: `ZodOptional`\<`ZodEnum`\<\[`"default"`, `"force-cache"`, `"no-cache"`, `"no-store"`, `"only-if-cached"`, `"reload"`\]\>\>; `instance`: `ZodUnion`\<\[`ZodString`, `ZodString`\]\>; `runtime`: `ZodLiteral`\<`"server"`\>; \}, `"strip"`, `ZodTypeAny`, \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instance`: `string`; `runtime`: `"server"`; \}, \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instance`: `string`; `runtime`: `"server"`; \}\>, `ZodObject`\<\{ `cache`: `ZodOptional`\<`ZodEnum`\<\[`"default"`, `"force-cache"`, `"no-cache"`, `"no-store"`, `"only-if-cached"`, `"reload"`\]\>\>; `runtime`: `ZodLiteral`\<`"browser"`\>; \}, `"strip"`, `ZodTypeAny`, \{ `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `runtime`: `"browser"`; \}, \{ `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `runtime`: `"browser"`; \}\>\]\>
146
+ > `const` **ClientOptionsSchema**: `ZodObject`\<\{ `accessToken`: `ZodString`; `cache`: `ZodOptional`\<`ZodEnum`\<\[`"default"`, `"force-cache"`, `"no-cache"`, `"no-store"`, `"only-if-cached"`, `"reload"`\]\>\>; `instance`: `ZodUnion`\<\[`ZodString`, `ZodString`\]\>; \}, `"strip"`, `ZodTypeAny`, \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instance`: `string`; \}, \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instance`: `string`; \}\>
156
147
 
157
- Defined in: [sdk/portal/src/portal.ts:16](https://github.com/settlemint/sdk/blob/v1.2.5/sdk/portal/src/portal.ts#L16)
148
+ Defined in: [sdk/portal/src/portal.ts:15](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/portal/src/portal.ts#L15)
158
149
 
159
150
  Schema for validating Portal client configuration options.
160
- Discriminates between server and browser runtime environments.
161
151
 
162
152
  ## Contributing
163
153
 
package/dist/portal.cjs CHANGED
@@ -31,42 +31,20 @@ 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
- instance: import_validation.UrlOrPathSchema,
37
- runtime: import_zod.z.literal("server"),
38
- accessToken: import_validation.ApplicationAccessTokenSchema,
39
- cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
40
- }),
41
- import_zod.z.object({
42
- runtime: import_zod.z.literal("browser"),
43
- cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
44
- })
45
- ]);
46
- function getFullUrl(options) {
47
- return options.runtime === "server" ? new URL(options.instance).toString() : new URL("/proxy/portal/graphql", window?.location?.origin ?? "http://localhost:3000").toString();
48
- }
49
- function getHeaders(options) {
50
- if (options.runtime === "server") {
51
- return {
52
- headers: {
53
- "x-auth-token": options.accessToken
54
- }
55
- };
56
- }
57
- return {};
58
- }
34
+ var ClientOptionsSchema = import_zod.z.object({
35
+ instance: import_validation.UrlOrPathSchema,
36
+ accessToken: import_validation.ApplicationAccessTokenSchema,
37
+ cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
38
+ });
59
39
  function createPortalClient(options, clientOptions) {
60
- const validatedOptions = (0, import_validation.validate)(ClientOptionsSchema, {
61
- ...options,
62
- runtime: import_runtime.runsOnServer ? "server" : "browser"
63
- });
40
+ (0, import_runtime.ensureServer)();
41
+ const validatedOptions = (0, import_validation.validate)(ClientOptionsSchema, options);
64
42
  const graphql = (0, import_gql.initGraphQLTada)();
65
- const fullUrl = getFullUrl(validatedOptions);
43
+ const fullUrl = new URL(validatedOptions.instance).toString();
66
44
  return {
67
45
  client: new import_graphql_request.GraphQLClient(fullUrl, {
68
46
  ...clientOptions,
69
- ...getHeaders(validatedOptions)
47
+ headers: { ...clientOptions?.headers ?? {}, "x-auth-token": validatedOptions.accessToken }
70
48
  }),
71
49
  graphql
72
50
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/portal.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 * Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating Portal client configuration options.\n * Discriminates between server and browser runtime environments.\n */\nexport const ClientOptionsSchema = z.discriminatedUnion(\"runtime\", [\n z.object({\n instance: UrlOrPathSchema,\n runtime: z.literal(\"server\"),\n accessToken: ApplicationAccessTokenSchema,\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 cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n }),\n]);\n\n/**\n * Type representing the validated client options.\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the Portal GraphQL endpoint URL based on runtime environment.\n *\n * @param options - The validated client options\n * @returns The complete GraphQL endpoint URL\n */\nfunction getFullUrl(options: ClientOptions): string {\n return options.runtime === \"server\"\n ? new URL(options.instance).toString()\n : new URL(\"/proxy/portal/graphql\", window?.location?.origin ?? \"http://localhost:3000\").toString();\n}\n\n/**\n * Gets the headers configuration based on runtime options\n *\n * @param options - The validated client options\n * @returns Headers configuration object or empty object\n */\nfunction getHeaders(options: ClientOptions): Record<string, unknown> {\n if (options.runtime === \"server\") {\n return {\n headers: {\n \"x-auth-token\": options.accessToken,\n },\n };\n }\n return {};\n}\n\n/**\n * Creates a Portal GraphQL client with the provided configuration.\n *\n * @param options - Configuration options for the Portal client\n * @param clientOptions - Additional GraphQL client configuration options\n * @returns An object containing the configured GraphQL client and graphql helper function\n * @throws If the provided options fail validation\n *\n * @example\n * import { createPortalClient } from '@settlemint/sdk-portal';\n * import type { introspection } from \"@schemas/portal-env\";\n *\n * // Server-side usage\n * export const { client: portalClient, graphql: portalGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>({\n * instance: process.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT,\n * runtime: \"server\",\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * });\n *\n * // Browser-side usage\n * export const { client: portalBrowserClient, graphql: portalBrowserGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>({});\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query GetPendingTransactions {\n * getPendingTransactions {\n * count\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createPortalClient<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 ...getHeaders(validatedOptions),\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;AAgIlB,IAAAA,cAA6B;AArHtB,IAAM,sBAAsB,aAAE,mBAAmB,WAAW;AAAA,EACjE,aAAE,OAAO;AAAA,IACP,UAAU;AAAA,IACV,SAAS,aAAE,QAAQ,QAAQ;AAAA,IAC3B,aAAa;AAAA,IACb,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,OAAO,aAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AAAA,EACzG,CAAC;AACH,CAAC;AAaD,SAAS,WAAW,SAAgC;AAClD,SAAO,QAAQ,YAAY,WACvB,IAAI,IAAI,QAAQ,QAAQ,EAAE,SAAS,IACnC,IAAI,IAAI,yBAAyB,QAAQ,UAAU,UAAU,uBAAuB,EAAE,SAAS;AACrG;AAQA,SAAS,WAAW,SAAiD;AACnE,MAAI,QAAQ,YAAY,UAAU;AAChC,WAAO;AAAA,MACL,SAAS;AAAA,QACP,gBAAgB,QAAQ;AAAA,MAC1B;AAAA,IACF;AAAA,EACF;AACA,SAAO,CAAC;AACV;AAiDO,SAAS,mBACd,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,GAAG,WAAW,gBAAgB;AAAA,IAChC,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["import_gql"]}
1
+ {"version":3,"sources":["../src/portal.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 * Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating Portal client configuration options.\n */\nexport const ClientOptionsSchema = z.object({\n instance: UrlOrPathSchema,\n accessToken: ApplicationAccessTokenSchema,\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type representing the validated client options.\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Creates a Portal GraphQL client with the provided configuration.\n *\n * @param options - Configuration options for the Portal client\n * @param clientOptions - Additional GraphQL client configuration options\n * @returns An object containing the configured GraphQL client and graphql helper function\n * @throws If the provided options fail validation\n *\n * @example\n * import { createPortalClient } from '@settlemint/sdk-portal';\n * import type { introspection } from \"@schemas/portal-env\";\n *\n * export const { client: portalClient, graphql: portalGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>({\n * instance: process.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT,\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query GetPendingTransactions {\n * getPendingTransactions {\n * count\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createPortalClient<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 = new URL(validatedOptions.instance).toString();\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: { ...(clientOptions?.headers ?? {}), \"x-auth-token\": validatedOptions.accessToken },\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;AA6ElB,IAAAA,cAA6B;AAnEtB,IAAM,sBAAsB,aAAE,OAAO;AAAA,EAC1C,UAAU;AAAA,EACV,aAAa;AAAA,EACb,OAAO,aAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AACzG,CAAC;AA0CM,SAAS,mBACd,SACA,eAIA;AACA,mCAAa;AACb,QAAM,uBAAmB,4BAAS,qBAAqB,OAAO;AAC9D,QAAM,cAAU,4BAAuB;AACvC,QAAM,UAAU,IAAI,IAAI,iBAAiB,QAAQ,EAAE,SAAS;AAE5D,SAAO;AAAA,IACL,QAAQ,IAAI,qCAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,SAAS,EAAE,GAAI,eAAe,WAAW,CAAC,GAAI,gBAAgB,iBAAiB,YAAY;AAAA,IAC7F,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["import_gql"]}
package/dist/portal.d.cts CHANGED
@@ -9,33 +9,20 @@ import { z } from 'zod';
9
9
  type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
10
10
  /**
11
11
  * Schema for validating Portal client configuration options.
12
- * Discriminates between server and browser runtime environments.
13
12
  */
14
- declare const ClientOptionsSchema: z.ZodDiscriminatedUnion<"runtime", [z.ZodObject<{
13
+ declare const ClientOptionsSchema: z.ZodObject<{
15
14
  instance: z.ZodUnion<[z.ZodString, z.ZodString]>;
16
- runtime: z.ZodLiteral<"server">;
17
15
  accessToken: z.ZodString;
18
16
  cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
19
17
  }, "strip", z.ZodTypeAny, {
20
- runtime: "server";
21
18
  instance: string;
22
19
  accessToken: string;
23
20
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
24
21
  }, {
25
- runtime: "server";
26
22
  instance: string;
27
23
  accessToken: string;
28
24
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
29
- }>, z.ZodObject<{
30
- runtime: z.ZodLiteral<"browser">;
31
- cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
32
- }, "strip", z.ZodTypeAny, {
33
- runtime: "browser";
34
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
35
- }, {
36
- runtime: "browser";
37
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
38
- }>]>;
25
+ }>;
39
26
  /**
40
27
  * Type representing the validated client options.
41
28
  */
@@ -52,7 +39,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
52
39
  * import { createPortalClient } from '@settlemint/sdk-portal';
53
40
  * import type { introspection } from "@schemas/portal-env";
54
41
  *
55
- * // Server-side usage
56
42
  * export const { client: portalClient, graphql: portalGraphql } = createPortalClient<{
57
43
  * introspection: introspection;
58
44
  * disableMasking: true;
@@ -62,20 +48,9 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
62
48
  * };
63
49
  * }>({
64
50
  * instance: process.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT,
65
- * runtime: "server",
66
51
  * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,
67
52
  * });
68
53
  *
69
- * // Browser-side usage
70
- * export const { client: portalBrowserClient, graphql: portalBrowserGraphql } = createPortalClient<{
71
- * introspection: introspection;
72
- * disableMasking: true;
73
- * scalars: {
74
- * // Change unknown to the type you are using to store metadata
75
- * JSON: unknown;
76
- * };
77
- * }>({});
78
- *
79
54
  * // Making GraphQL queries
80
55
  * const query = graphql(`
81
56
  * query GetPendingTransactions {
@@ -87,7 +62,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
87
62
  *
88
63
  * const result = await client.request(query);
89
64
  */
90
- declare function createPortalClient<const Setup extends AbstractSetupSchema>(options: Omit<ClientOptions, "runtime"> & Record<string, unknown>, clientOptions?: RequestConfig): {
65
+ declare function createPortalClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
91
66
  client: GraphQLClient;
92
67
  graphql: initGraphQLTada<Setup>;
93
68
  };
package/dist/portal.d.ts CHANGED
@@ -9,33 +9,20 @@ import { z } from 'zod';
9
9
  type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
10
10
  /**
11
11
  * Schema for validating Portal client configuration options.
12
- * Discriminates between server and browser runtime environments.
13
12
  */
14
- declare const ClientOptionsSchema: z.ZodDiscriminatedUnion<"runtime", [z.ZodObject<{
13
+ declare const ClientOptionsSchema: z.ZodObject<{
15
14
  instance: z.ZodUnion<[z.ZodString, z.ZodString]>;
16
- runtime: z.ZodLiteral<"server">;
17
15
  accessToken: z.ZodString;
18
16
  cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
19
17
  }, "strip", z.ZodTypeAny, {
20
- runtime: "server";
21
18
  instance: string;
22
19
  accessToken: string;
23
20
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
24
21
  }, {
25
- runtime: "server";
26
22
  instance: string;
27
23
  accessToken: string;
28
24
  cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
29
- }>, z.ZodObject<{
30
- runtime: z.ZodLiteral<"browser">;
31
- cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
32
- }, "strip", z.ZodTypeAny, {
33
- runtime: "browser";
34
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
35
- }, {
36
- runtime: "browser";
37
- cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
38
- }>]>;
25
+ }>;
39
26
  /**
40
27
  * Type representing the validated client options.
41
28
  */
@@ -52,7 +39,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
52
39
  * import { createPortalClient } from '@settlemint/sdk-portal';
53
40
  * import type { introspection } from "@schemas/portal-env";
54
41
  *
55
- * // Server-side usage
56
42
  * export const { client: portalClient, graphql: portalGraphql } = createPortalClient<{
57
43
  * introspection: introspection;
58
44
  * disableMasking: true;
@@ -62,20 +48,9 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
62
48
  * };
63
49
  * }>({
64
50
  * instance: process.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT,
65
- * runtime: "server",
66
51
  * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,
67
52
  * });
68
53
  *
69
- * // Browser-side usage
70
- * export const { client: portalBrowserClient, graphql: portalBrowserGraphql } = createPortalClient<{
71
- * introspection: introspection;
72
- * disableMasking: true;
73
- * scalars: {
74
- * // Change unknown to the type you are using to store metadata
75
- * JSON: unknown;
76
- * };
77
- * }>({});
78
- *
79
54
  * // Making GraphQL queries
80
55
  * const query = graphql(`
81
56
  * query GetPendingTransactions {
@@ -87,7 +62,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
87
62
  *
88
63
  * const result = await client.request(query);
89
64
  */
90
- declare function createPortalClient<const Setup extends AbstractSetupSchema>(options: Omit<ClientOptions, "runtime"> & Record<string, unknown>, clientOptions?: RequestConfig): {
65
+ declare function createPortalClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
91
66
  client: GraphQLClient;
92
67
  graphql: initGraphQLTada<Setup>;
93
68
  };
package/dist/portal.mjs CHANGED
@@ -1,46 +1,24 @@
1
1
  // src/portal.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
- instance: UrlOrPathSchema,
11
- runtime: z.literal("server"),
12
- accessToken: ApplicationAccessTokenSchema,
13
- cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
14
- }),
15
- z.object({
16
- runtime: z.literal("browser"),
17
- cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
18
- })
19
- ]);
20
- function getFullUrl(options) {
21
- return options.runtime === "server" ? new URL(options.instance).toString() : new URL("/proxy/portal/graphql", window?.location?.origin ?? "http://localhost:3000").toString();
22
- }
23
- function getHeaders(options) {
24
- if (options.runtime === "server") {
25
- return {
26
- headers: {
27
- "x-auth-token": options.accessToken
28
- }
29
- };
30
- }
31
- return {};
32
- }
8
+ var ClientOptionsSchema = z.object({
9
+ instance: UrlOrPathSchema,
10
+ accessToken: ApplicationAccessTokenSchema,
11
+ cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
12
+ });
33
13
  function createPortalClient(options, clientOptions) {
34
- const validatedOptions = validate(ClientOptionsSchema, {
35
- ...options,
36
- runtime: runsOnServer ? "server" : "browser"
37
- });
14
+ ensureServer();
15
+ const validatedOptions = validate(ClientOptionsSchema, options);
38
16
  const graphql = initGraphQLTada();
39
- const fullUrl = getFullUrl(validatedOptions);
17
+ const fullUrl = new URL(validatedOptions.instance).toString();
40
18
  return {
41
19
  client: new GraphQLClient(fullUrl, {
42
20
  ...clientOptions,
43
- ...getHeaders(validatedOptions)
21
+ headers: { ...clientOptions?.headers ?? {}, "x-auth-token": validatedOptions.accessToken }
44
22
  }),
45
23
  graphql
46
24
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/portal.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 * Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating Portal client configuration options.\n * Discriminates between server and browser runtime environments.\n */\nexport const ClientOptionsSchema = z.discriminatedUnion(\"runtime\", [\n z.object({\n instance: UrlOrPathSchema,\n runtime: z.literal(\"server\"),\n accessToken: ApplicationAccessTokenSchema,\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 cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n }),\n]);\n\n/**\n * Type representing the validated client options.\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the Portal GraphQL endpoint URL based on runtime environment.\n *\n * @param options - The validated client options\n * @returns The complete GraphQL endpoint URL\n */\nfunction getFullUrl(options: ClientOptions): string {\n return options.runtime === \"server\"\n ? new URL(options.instance).toString()\n : new URL(\"/proxy/portal/graphql\", window?.location?.origin ?? \"http://localhost:3000\").toString();\n}\n\n/**\n * Gets the headers configuration based on runtime options\n *\n * @param options - The validated client options\n * @returns Headers configuration object or empty object\n */\nfunction getHeaders(options: ClientOptions): Record<string, unknown> {\n if (options.runtime === \"server\") {\n return {\n headers: {\n \"x-auth-token\": options.accessToken,\n },\n };\n }\n return {};\n}\n\n/**\n * Creates a Portal GraphQL client with the provided configuration.\n *\n * @param options - Configuration options for the Portal client\n * @param clientOptions - Additional GraphQL client configuration options\n * @returns An object containing the configured GraphQL client and graphql helper function\n * @throws If the provided options fail validation\n *\n * @example\n * import { createPortalClient } from '@settlemint/sdk-portal';\n * import type { introspection } from \"@schemas/portal-env\";\n *\n * // Server-side usage\n * export const { client: portalClient, graphql: portalGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>({\n * instance: process.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT,\n * runtime: \"server\",\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * });\n *\n * // Browser-side usage\n * export const { client: portalBrowserClient, graphql: portalBrowserGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>({});\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query GetPendingTransactions {\n * getPendingTransactions {\n * count\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createPortalClient<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 ...getHeaders(validatedOptions),\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;AAgIlB,SAAS,oBAAoB;AArHtB,IAAM,sBAAsB,EAAE,mBAAmB,WAAW;AAAA,EACjE,EAAE,OAAO;AAAA,IACP,UAAU;AAAA,IACV,SAAS,EAAE,QAAQ,QAAQ;AAAA,IAC3B,aAAa;AAAA,IACb,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,OAAO,EAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AAAA,EACzG,CAAC;AACH,CAAC;AAaD,SAAS,WAAW,SAAgC;AAClD,SAAO,QAAQ,YAAY,WACvB,IAAI,IAAI,QAAQ,QAAQ,EAAE,SAAS,IACnC,IAAI,IAAI,yBAAyB,QAAQ,UAAU,UAAU,uBAAuB,EAAE,SAAS;AACrG;AAQA,SAAS,WAAW,SAAiD;AACnE,MAAI,QAAQ,YAAY,UAAU;AAChC,WAAO;AAAA,MACL,SAAS;AAAA,QACP,gBAAgB,QAAQ;AAAA,MAC1B;AAAA,IACF;AAAA,EACF;AACA,SAAO,CAAC;AACV;AAiDO,SAAS,mBACd,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,GAAG,WAAW,gBAAgB;AAAA,IAChC,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/portal.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 * Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating Portal client configuration options.\n */\nexport const ClientOptionsSchema = z.object({\n instance: UrlOrPathSchema,\n accessToken: ApplicationAccessTokenSchema,\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type representing the validated client options.\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Creates a Portal GraphQL client with the provided configuration.\n *\n * @param options - Configuration options for the Portal client\n * @param clientOptions - Additional GraphQL client configuration options\n * @returns An object containing the configured GraphQL client and graphql helper function\n * @throws If the provided options fail validation\n *\n * @example\n * import { createPortalClient } from '@settlemint/sdk-portal';\n * import type { introspection } from \"@schemas/portal-env\";\n *\n * export const { client: portalClient, graphql: portalGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>({\n * instance: process.env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT,\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN,\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query GetPendingTransactions {\n * getPendingTransactions {\n * count\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createPortalClient<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 = new URL(validatedOptions.instance).toString();\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: { ...(clientOptions?.headers ?? {}), \"x-auth-token\": validatedOptions.accessToken },\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;AA6ElB,SAAS,oBAAoB;AAnEtB,IAAM,sBAAsB,EAAE,OAAO;AAAA,EAC1C,UAAU;AAAA,EACV,aAAa;AAAA,EACb,OAAO,EAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AACzG,CAAC;AA0CM,SAAS,mBACd,SACA,eAIA;AACA,eAAa;AACb,QAAM,mBAAmB,SAAS,qBAAqB,OAAO;AAC9D,QAAM,UAAU,gBAAuB;AACvC,QAAM,UAAU,IAAI,IAAI,iBAAiB,QAAQ,EAAE,SAAS;AAE5D,SAAO;AAAA,IACL,QAAQ,IAAI,cAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,SAAS,EAAE,GAAI,eAAe,WAAW,CAAC,GAAI,gBAAgB,iBAAiB,YAAY;AAAA,IAC7F,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@settlemint/sdk-portal",
3
3
  "description": "Portal API client module for SettleMint SDK, providing access to smart contract portal services and APIs",
4
- "version": "1.2.5",
4
+ "version": "2.0.0",
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",
55
+ "@settlemint/sdk-utils": "2.0.0",
56
56
  "graphql-request": "^7",
57
57
  "zod": "^3"
58
58
  },