@stonecrop/graphql-client 0.8.9 → 0.8.11

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/client.js ADDED
@@ -0,0 +1,191 @@
1
+ /**
2
+ * Client for interacting with Stonecrop GraphQL API
3
+ * @public
4
+ */
5
+ export class StonecropClient {
6
+ endpoint;
7
+ headers;
8
+ metaCache = new Map();
9
+ constructor(options) {
10
+ this.endpoint = options.endpoint;
11
+ this.headers = {
12
+ 'Content-Type': 'application/json',
13
+ ...options.headers,
14
+ };
15
+ }
16
+ /**
17
+ * Execute a GraphQL query
18
+ * @param query - GraphQL query string
19
+ * @param variables - Query variables
20
+ */
21
+ async query(query, variables) {
22
+ const response = await fetch(this.endpoint, {
23
+ method: 'POST',
24
+ headers: this.headers,
25
+ body: JSON.stringify({ query, variables }),
26
+ });
27
+ const json = (await response.json());
28
+ if (json.errors?.length) {
29
+ throw new Error(json.errors[0].message);
30
+ }
31
+ return json.data;
32
+ }
33
+ /**
34
+ * Execute a GraphQL mutation
35
+ * @param mutation - GraphQL mutation string
36
+ * @param variables - Mutation variables
37
+ */
38
+ async mutate(mutation, variables) {
39
+ return this.query(mutation, variables);
40
+ }
41
+ /**
42
+ * Get doctype metadata
43
+ * @param context - Route context containing doctype name
44
+ */
45
+ async getMeta(context) {
46
+ const cached = this.metaCache.get(context.doctype);
47
+ if (cached)
48
+ return cached;
49
+ const result = await this.query(`
50
+ query GetMeta($doctype: String!) {
51
+ stonecropMeta(doctype: $doctype) {
52
+ name
53
+ slug
54
+ tableName
55
+ fields {
56
+ fieldname
57
+ fieldtype
58
+ component
59
+ label
60
+ required
61
+ readOnly
62
+ options
63
+ precision
64
+ scale
65
+ }
66
+ workflow {
67
+ states
68
+ actions
69
+ }
70
+ inherits
71
+ listDoctype
72
+ parentDoctype
73
+ }
74
+ }
75
+ `, { doctype: context.doctype });
76
+ if (result.stonecropMeta) {
77
+ this.metaCache.set(context.doctype, result.stonecropMeta);
78
+ }
79
+ return result.stonecropMeta;
80
+ }
81
+ /**
82
+ * Get all doctype metadata
83
+ */
84
+ async getAllMeta() {
85
+ const result = await this.query(`
86
+ query GetAllMeta {
87
+ stonecropAllMeta {
88
+ name
89
+ slug
90
+ tableName
91
+ fields {
92
+ fieldname
93
+ fieldtype
94
+ component
95
+ label
96
+ required
97
+ readOnly
98
+ options
99
+ precision
100
+ scale
101
+ }
102
+ workflow {
103
+ states
104
+ actions
105
+ }
106
+ inherits
107
+ listDoctype
108
+ parentDoctype
109
+ }
110
+ }
111
+ `);
112
+ for (const meta of result.stonecropAllMeta) {
113
+ this.metaCache.set(meta.name, meta);
114
+ }
115
+ return result.stonecropAllMeta;
116
+ }
117
+ /**
118
+ * Get a single record by ID
119
+ * @param doctype - Doctype metadata
120
+ * @param recordId - Record ID to fetch
121
+ */
122
+ async getRecord(doctype, recordId) {
123
+ const result = await this.query(`
124
+ query GetRecord($doctype: String!, $id: String!) {
125
+ stonecropRecord(doctype: $doctype, id: $id) {
126
+ data
127
+ }
128
+ }
129
+ `, { doctype: doctype.name, id: recordId });
130
+ return result.stonecropRecord.data;
131
+ }
132
+ /**
133
+ * Get multiple records with optional filtering and pagination
134
+ * @param doctype - Doctype metadata
135
+ * @param options - Query options (filters, orderBy, limit, offset)
136
+ */
137
+ async getRecords(doctype, options) {
138
+ const result = await this.query(`
139
+ query GetRecords(
140
+ $doctype: String!
141
+ $filters: JSON
142
+ $orderBy: String
143
+ $limit: Int
144
+ $offset: Int
145
+ ) {
146
+ stonecropRecords(
147
+ doctype: $doctype
148
+ filters: $filters
149
+ orderBy: $orderBy
150
+ limit: $limit
151
+ offset: $offset
152
+ ) {
153
+ data
154
+ count
155
+ }
156
+ }
157
+ `, {
158
+ doctype: doctype.name,
159
+ ...options,
160
+ });
161
+ return result.stonecropRecords.data;
162
+ }
163
+ /**
164
+ * Execute a doctype action
165
+ * @param doctype - Doctype metadata
166
+ * @param action - Action name to execute
167
+ * @param args - Action arguments
168
+ */
169
+ async runAction(doctype, action, args) {
170
+ const result = await this.query(`
171
+ mutation RunAction($doctype: String!, $action: String!, $args: JSON) {
172
+ stonecropAction(doctype: $doctype, action: $action, args: $args) {
173
+ success
174
+ data
175
+ error
176
+ }
177
+ }
178
+ `, {
179
+ doctype: doctype.name,
180
+ action,
181
+ args,
182
+ });
183
+ return result.stonecropAction;
184
+ }
185
+ /**
186
+ * Clear the cached doctype metadata
187
+ */
188
+ clearMetaCache() {
189
+ this.metaCache.clear();
190
+ }
191
+ }
@@ -1,3 +1,218 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Doctype metadata - complete definition of a doctype
5
+ * @public
6
+ */
7
+ export declare const DoctypeMeta: z.ZodObject<{
8
+ /** Display name of the doctype */
9
+ name: z.ZodString;
10
+ /** URL-friendly slug (kebab-case) */
11
+ slug: z.ZodOptional<z.ZodString>;
12
+ /** Database table name */
13
+ tableName: z.ZodOptional<z.ZodString>;
14
+ /** Field definitions */
15
+ fields: z.ZodArray<z.ZodObject<{
16
+ fieldname: z.ZodString;
17
+ fieldtype: z.ZodEnum<["Data", "Text", "Int", "Float", "Decimal", "Check", "Date", "Time", "Datetime", "Duration", "DateRange", "JSON", "Code", "Link", "Doctype", "Attach", "Currency", "Quantity", "Select"]>;
18
+ component: z.ZodOptional<z.ZodString>;
19
+ label: z.ZodOptional<z.ZodString>;
20
+ width: z.ZodOptional<z.ZodString>;
21
+ align: z.ZodOptional<z.ZodEnum<["left", "center", "right", "start", "end"]>>;
22
+ required: z.ZodOptional<z.ZodBoolean>;
23
+ readOnly: z.ZodOptional<z.ZodBoolean>;
24
+ edit: z.ZodOptional<z.ZodBoolean>;
25
+ hidden: z.ZodOptional<z.ZodBoolean>;
26
+ value: z.ZodOptional<z.ZodUnknown>;
27
+ default: z.ZodOptional<z.ZodUnknown>;
28
+ options: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">, z.ZodRecord<z.ZodString, z.ZodUnknown>]>>;
29
+ mask: z.ZodOptional<z.ZodString>;
30
+ validation: z.ZodOptional<z.ZodObject<{
31
+ errorMessage: z.ZodString;
32
+ }, "passthrough", z.ZodTypeAny, z.objectOutputType<{
33
+ errorMessage: z.ZodString;
34
+ }, z.ZodTypeAny, "passthrough">, z.objectInputType<{
35
+ errorMessage: z.ZodString;
36
+ }, z.ZodTypeAny, "passthrough">>>;
37
+ }, "strip", z.ZodTypeAny, {
38
+ fieldname: string;
39
+ fieldtype: "Data" | "Text" | "Int" | "Float" | "Decimal" | "Check" | "Date" | "Time" | "Datetime" | "Duration" | "DateRange" | "JSON" | "Code" | "Link" | "Doctype" | "Attach" | "Currency" | "Quantity" | "Select";
40
+ value?: unknown;
41
+ options?: string | string[] | Record<string, unknown> | undefined;
42
+ validation?: z.objectOutputType<{
43
+ errorMessage: z.ZodString;
44
+ }, z.ZodTypeAny, "passthrough"> | undefined;
45
+ component?: string | undefined;
46
+ label?: string | undefined;
47
+ width?: string | undefined;
48
+ align?: "left" | "center" | "right" | "start" | "end" | undefined;
49
+ required?: boolean | undefined;
50
+ readOnly?: boolean | undefined;
51
+ edit?: boolean | undefined;
52
+ hidden?: boolean | undefined;
53
+ default?: unknown;
54
+ mask?: string | undefined;
55
+ }, {
56
+ fieldname: string;
57
+ fieldtype: "Data" | "Text" | "Int" | "Float" | "Decimal" | "Check" | "Date" | "Time" | "Datetime" | "Duration" | "DateRange" | "JSON" | "Code" | "Link" | "Doctype" | "Attach" | "Currency" | "Quantity" | "Select";
58
+ value?: unknown;
59
+ options?: string | string[] | Record<string, unknown> | undefined;
60
+ validation?: z.objectInputType<{
61
+ errorMessage: z.ZodString;
62
+ }, z.ZodTypeAny, "passthrough"> | undefined;
63
+ component?: string | undefined;
64
+ label?: string | undefined;
65
+ width?: string | undefined;
66
+ align?: "left" | "center" | "right" | "start" | "end" | undefined;
67
+ required?: boolean | undefined;
68
+ readOnly?: boolean | undefined;
69
+ edit?: boolean | undefined;
70
+ hidden?: boolean | undefined;
71
+ default?: unknown;
72
+ mask?: string | undefined;
73
+ }>, "many">;
74
+ /** Workflow configuration */
75
+ workflow: z.ZodOptional<z.ZodObject<{
76
+ /** List of workflow states */
77
+ states: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
78
+ /** Actions available in this workflow */
79
+ actions: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
80
+ /** Display label for the action */
81
+ label: z.ZodString;
82
+ /** Handler function name or path */
83
+ handler: z.ZodString;
84
+ /** Fields that must have values before action can execute */
85
+ requiredFields: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
86
+ /** Workflow states where this action is available */
87
+ allowedStates: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
88
+ /** Whether to show a confirmation dialog */
89
+ confirm: z.ZodOptional<z.ZodBoolean>;
90
+ /** Additional arguments for the action */
91
+ args: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
92
+ }, "strip", z.ZodTypeAny, {
93
+ label: string;
94
+ handler: string;
95
+ requiredFields?: string[] | undefined;
96
+ allowedStates?: string[] | undefined;
97
+ confirm?: boolean | undefined;
98
+ args?: Record<string, unknown> | undefined;
99
+ }, {
100
+ label: string;
101
+ handler: string;
102
+ requiredFields?: string[] | undefined;
103
+ allowedStates?: string[] | undefined;
104
+ confirm?: boolean | undefined;
105
+ args?: Record<string, unknown> | undefined;
106
+ }>>>;
107
+ }, "strip", z.ZodTypeAny, {
108
+ states?: string[] | undefined;
109
+ actions?: Record<string, {
110
+ label: string;
111
+ handler: string;
112
+ requiredFields?: string[] | undefined;
113
+ allowedStates?: string[] | undefined;
114
+ confirm?: boolean | undefined;
115
+ args?: Record<string, unknown> | undefined;
116
+ }> | undefined;
117
+ }, {
118
+ states?: string[] | undefined;
119
+ actions?: Record<string, {
120
+ label: string;
121
+ handler: string;
122
+ requiredFields?: string[] | undefined;
123
+ allowedStates?: string[] | undefined;
124
+ confirm?: boolean | undefined;
125
+ args?: Record<string, unknown> | undefined;
126
+ }> | undefined;
127
+ }>>;
128
+ /** Parent doctype for inheritance */
129
+ inherits: z.ZodOptional<z.ZodString>;
130
+ /** Doctype to use for list views */
131
+ listDoctype: z.ZodOptional<z.ZodString>;
132
+ /** Parent doctype for child tables */
133
+ parentDoctype: z.ZodOptional<z.ZodString>;
134
+ }, "strip", z.ZodTypeAny, {
135
+ name: string;
136
+ fields: {
137
+ fieldname: string;
138
+ fieldtype: "Data" | "Text" | "Int" | "Float" | "Decimal" | "Check" | "Date" | "Time" | "Datetime" | "Duration" | "DateRange" | "JSON" | "Code" | "Link" | "Doctype" | "Attach" | "Currency" | "Quantity" | "Select";
139
+ value?: unknown;
140
+ options?: string | string[] | Record<string, unknown> | undefined;
141
+ validation?: z.objectOutputType<{
142
+ errorMessage: z.ZodString;
143
+ }, z.ZodTypeAny, "passthrough"> | undefined;
144
+ component?: string | undefined;
145
+ label?: string | undefined;
146
+ width?: string | undefined;
147
+ align?: "left" | "center" | "right" | "start" | "end" | undefined;
148
+ required?: boolean | undefined;
149
+ readOnly?: boolean | undefined;
150
+ edit?: boolean | undefined;
151
+ hidden?: boolean | undefined;
152
+ default?: unknown;
153
+ mask?: string | undefined;
154
+ }[];
155
+ slug?: string | undefined;
156
+ tableName?: string | undefined;
157
+ workflow?: {
158
+ states?: string[] | undefined;
159
+ actions?: Record<string, {
160
+ label: string;
161
+ handler: string;
162
+ requiredFields?: string[] | undefined;
163
+ allowedStates?: string[] | undefined;
164
+ confirm?: boolean | undefined;
165
+ args?: Record<string, unknown> | undefined;
166
+ }> | undefined;
167
+ } | undefined;
168
+ inherits?: string | undefined;
169
+ listDoctype?: string | undefined;
170
+ parentDoctype?: string | undefined;
171
+ }, {
172
+ name: string;
173
+ fields: {
174
+ fieldname: string;
175
+ fieldtype: "Data" | "Text" | "Int" | "Float" | "Decimal" | "Check" | "Date" | "Time" | "Datetime" | "Duration" | "DateRange" | "JSON" | "Code" | "Link" | "Doctype" | "Attach" | "Currency" | "Quantity" | "Select";
176
+ value?: unknown;
177
+ options?: string | string[] | Record<string, unknown> | undefined;
178
+ validation?: z.objectInputType<{
179
+ errorMessage: z.ZodString;
180
+ }, z.ZodTypeAny, "passthrough"> | undefined;
181
+ component?: string | undefined;
182
+ label?: string | undefined;
183
+ width?: string | undefined;
184
+ align?: "left" | "center" | "right" | "start" | "end" | undefined;
185
+ required?: boolean | undefined;
186
+ readOnly?: boolean | undefined;
187
+ edit?: boolean | undefined;
188
+ hidden?: boolean | undefined;
189
+ default?: unknown;
190
+ mask?: string | undefined;
191
+ }[];
192
+ slug?: string | undefined;
193
+ tableName?: string | undefined;
194
+ workflow?: {
195
+ states?: string[] | undefined;
196
+ actions?: Record<string, {
197
+ label: string;
198
+ handler: string;
199
+ requiredFields?: string[] | undefined;
200
+ allowedStates?: string[] | undefined;
201
+ confirm?: boolean | undefined;
202
+ args?: Record<string, unknown> | undefined;
203
+ }> | undefined;
204
+ } | undefined;
205
+ inherits?: string | undefined;
206
+ listDoctype?: string | undefined;
207
+ parentDoctype?: string | undefined;
208
+ }>;
209
+
210
+ /**
211
+ * Doctype metadata type inferred from Zod schema
212
+ * @public
213
+ */
214
+ export declare type DoctypeMeta = z.infer<typeof DoctypeMeta>;
215
+
1
216
  /**
2
217
  * @file This file contains all the types that are used in the application.
3
218
  * @public
@@ -64,6 +279,95 @@ export declare const queries: {
64
279
  getMeta: string;
65
280
  };
66
281
 
282
+ /**
283
+ * Route context for identifying what doctype/record we're working with.
284
+ * Used by graphql-middleware and graphql-client to resolve schema metadata.
285
+ * @public
286
+ */
287
+ export declare interface RouteContext {
288
+ /** Doctype name (e.g., 'Task', 'Customer') */
289
+ doctype: string;
290
+ /** Optional record ID for viewing/editing a specific record */
291
+ recordId?: string;
292
+ /** Additional context properties */
293
+ [key: string]: unknown;
294
+ }
295
+
296
+ /**
297
+ * Client for interacting with Stonecrop GraphQL API
298
+ * @public
299
+ */
300
+ export declare class StonecropClient {
301
+ private endpoint;
302
+ private headers;
303
+ private metaCache;
304
+ constructor(options: StonecropClientOptions);
305
+ /**
306
+ * Execute a GraphQL query
307
+ * @param query - GraphQL query string
308
+ * @param variables - Query variables
309
+ */
310
+ query<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T>;
311
+ /**
312
+ * Execute a GraphQL mutation
313
+ * @param mutation - GraphQL mutation string
314
+ * @param variables - Mutation variables
315
+ */
316
+ mutate<T = unknown>(mutation: string, variables?: Record<string, unknown>): Promise<T>;
317
+ /**
318
+ * Get doctype metadata
319
+ * @param context - Route context containing doctype name
320
+ */
321
+ getMeta(context: RouteContext): Promise<DoctypeMeta | null>;
322
+ /**
323
+ * Get all doctype metadata
324
+ */
325
+ getAllMeta(): Promise<DoctypeMeta[]>;
326
+ /**
327
+ * Get a single record by ID
328
+ * @param doctype - Doctype metadata
329
+ * @param recordId - Record ID to fetch
330
+ */
331
+ getRecord(doctype: DoctypeMeta, recordId: string): Promise<Record<string, unknown> | null>;
332
+ /**
333
+ * Get multiple records with optional filtering and pagination
334
+ * @param doctype - Doctype metadata
335
+ * @param options - Query options (filters, orderBy, limit, offset)
336
+ */
337
+ getRecords(doctype: DoctypeMeta, options?: {
338
+ filters?: Record<string, unknown>;
339
+ orderBy?: string;
340
+ limit?: number;
341
+ offset?: number;
342
+ }): Promise<Record<string, unknown>[]>;
343
+ /**
344
+ * Execute a doctype action
345
+ * @param doctype - Doctype metadata
346
+ * @param action - Action name to execute
347
+ * @param args - Action arguments
348
+ */
349
+ runAction(doctype: DoctypeMeta, action: string, args?: unknown[]): Promise<{
350
+ success: boolean;
351
+ data: unknown;
352
+ error: string | null;
353
+ }>;
354
+ /**
355
+ * Clear the cached doctype metadata
356
+ */
357
+ clearMetaCache(): void;
358
+ }
359
+
360
+ /**
361
+ * Options for creating a Stonecrop client
362
+ * @public
363
+ */
364
+ export declare interface StonecropClientOptions {
365
+ /** GraphQL endpoint URL */
366
+ endpoint: string;
367
+ /** Additional HTTP headers to include in requests */
368
+ headers?: Record<string, string>;
369
+ }
370
+
67
371
  /**
68
372
  * This is the schema for the GraphQL API.
69
373
  * @public