@stonecrop/graphql-client 0.11.0 → 0.11.1

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.
@@ -1,221 +0,0 @@
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 - Doctype 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
- width
61
- align
62
- required
63
- readOnly
64
- edit
65
- hidden
66
- default
67
- options
68
- mask
69
- precision
70
- scale
71
- mode
72
- validation
73
- }
74
- workflow {
75
- states
76
- actions {
77
- label
78
- handler
79
- requiredFields
80
- allowedStates
81
- confirm
82
- args
83
- }
84
- }
85
- inherits
86
- listDoctype
87
- parentDoctype
88
- }
89
- }
90
- `, { doctype: context.doctype });
91
- if (result.stonecropMeta) {
92
- this.metaCache.set(context.doctype, result.stonecropMeta);
93
- }
94
- return result.stonecropMeta;
95
- }
96
- /**
97
- * Get all doctype metadata
98
- */
99
- async getAllMeta() {
100
- const result = await this.query(`
101
- query GetAllMeta {
102
- stonecropAllMeta {
103
- name
104
- slug
105
- tableName
106
- fields {
107
- fieldname
108
- fieldtype
109
- component
110
- label
111
- width
112
- align
113
- required
114
- readOnly
115
- edit
116
- hidden
117
- default
118
- options
119
- mask
120
- precision
121
- scale
122
- mode
123
- validation
124
- }
125
- workflow {
126
- states
127
- actions {
128
- label
129
- handler
130
- requiredFields
131
- allowedStates
132
- confirm
133
- args
134
- }
135
- }
136
- inherits
137
- listDoctype
138
- parentDoctype
139
- }
140
- }
141
- `);
142
- for (const meta of result.stonecropAllMeta) {
143
- this.metaCache.set(meta.name, meta);
144
- }
145
- return result.stonecropAllMeta;
146
- }
147
- /**
148
- * Get a single record by ID
149
- * @param doctype - Doctype reference (name and optional slug)
150
- * @param recordId - Record ID to fetch
151
- */
152
- async getRecord(doctype, recordId) {
153
- const result = await this.query(`
154
- query GetRecord($doctype: String!, $id: String!) {
155
- stonecropRecord(doctype: $doctype, id: $id) {
156
- data
157
- }
158
- }
159
- `, { doctype: doctype.name, id: recordId });
160
- return result.stonecropRecord.data;
161
- }
162
- /**
163
- * Get multiple records with optional filtering and pagination
164
- * @param doctype - Doctype reference (name and optional slug)
165
- * @param options - Query options (filters, orderBy, limit, offset)
166
- */
167
- async getRecords(doctype, options) {
168
- const result = await this.query(`
169
- query GetRecords(
170
- $doctype: String!
171
- $filters: JSON
172
- $orderBy: String
173
- $limit: Int
174
- $offset: Int
175
- ) {
176
- stonecropRecords(
177
- doctype: $doctype
178
- filters: $filters
179
- orderBy: $orderBy
180
- limit: $limit
181
- offset: $offset
182
- ) {
183
- data
184
- count
185
- }
186
- }
187
- `, {
188
- doctype: doctype.name,
189
- ...options,
190
- });
191
- return result.stonecropRecords.data;
192
- }
193
- /**
194
- * Execute a doctype action
195
- * @param doctype - Doctype reference (name and optional slug)
196
- * @param action - Action name to execute
197
- * @param args - Action arguments
198
- */
199
- async runAction(doctype, action, args) {
200
- const result = await this.query(`
201
- mutation RunAction($doctype: String!, $action: String!, $args: JSON) {
202
- stonecropAction(doctype: $doctype, action: $action, args: $args) {
203
- success
204
- data
205
- error
206
- }
207
- }
208
- `, {
209
- doctype: doctype.name,
210
- action,
211
- args,
212
- });
213
- return result.stonecropAction;
214
- }
215
- /**
216
- * Clear the cached doctype metadata
217
- */
218
- clearMetaCache() {
219
- this.metaCache.clear();
220
- }
221
- }
@@ -1,53 +0,0 @@
1
- import { gql } from 'graphql-request';
2
- /**
3
- * This is the schema for the GraphQL API.
4
- * @public
5
- */
6
- const typeDefs = gql `
7
- type Doctype {
8
- id: ID!
9
- name: String!
10
- workflow: String!
11
- schema: String!
12
- actions: String!
13
- }
14
-
15
- type DoctypeField {
16
- id: ID!
17
- label: String!
18
- fieldtype: String
19
- component: String
20
- required: Boolean
21
- readonly: Boolean
22
- }
23
-
24
- type DoctypeWorkflow {
25
- name: String!
26
- machine: StateMachine!
27
- }
28
-
29
- type StateMachine {
30
- id: ID!
31
- }
32
-
33
- type DoctypeAction {
34
- eventName: String!
35
- callback: String
36
- }
37
-
38
- type Query {
39
- getMeta(doctype: String!): Doctype # ∪ error
40
- getRecords(doctype: String!, filters: [String]): [String] # ∪ error
41
- getRecord(doctype: String!, id: ID!): String # ∪ error
42
- }
43
-
44
- type Mutation {
45
- runAction(doctype: String!, id: [ID!]!, functionName: String!): [String!]! # ∪ error
46
- }
47
-
48
- schema {
49
- query: Query
50
- mutation: Mutation
51
- }
52
- `;
53
- export default typeDefs;
package/dist/src/index.js DELETED
@@ -1,61 +0,0 @@
1
- import { Decimal } from 'decimal.js';
2
- import { GraphQLClient } from 'graphql-request';
3
- import { queries } from './queries';
4
- import typeDefs from './gql/schema';
5
- /**
6
- * Parse the response from the GraphQL server. Converts the stringified JSON to JSON and converts the stringified numbers to Decimal.
7
- * @param obj - The response from the GraphQL server
8
- * @returns The parsed response
9
- * @example
10
- * const response = '{"data":{"getMeta":{"id":"Issue","name":"Issue","workflow":"{\"machineId\":null,\"name\":\"save\",\"id\":\"1\"}","schema":"[{\"label\":\"Subject\",\"id\":\"1\"}]","actions":"[{\"eventName\":\"save\",\"id\":\"1\"}]"}}}'
11
- * const parsedResponse = metaParser(response)
12
- * console.log(parsedResponse)
13
- * /* Output: {"id": "Issue", "name": "Issue", "workflow": { "machineId": null, "name": "save", "id": "1" }, "schema": [{ "label": "Subject", "id": "1" }], "actions": [{ "eventName": "save", "id": "1" }]}
14
- */
15
- const metaParser = (obj) => {
16
- return JSON.parse(obj, (key, value) => {
17
- if (typeof value === 'string') {
18
- try {
19
- return JSON.parse(value, (_key, value) => {
20
- if (typeof value === 'string' && !isNaN(Number(value))) {
21
- return new Decimal(value);
22
- }
23
- return value;
24
- });
25
- }
26
- catch {
27
- // if the value is not a stringified JSON, return as it is
28
- return value;
29
- }
30
- }
31
- else if (!isNaN(Number(value))) {
32
- return new Decimal(value);
33
- }
34
- return value;
35
- });
36
- };
37
- /**
38
- * Get meta information for a doctype
39
- * @param doctype - The doctype to get meta information for
40
- * @param url - The URL to send the request to
41
- * @returns The meta information for the doctype
42
- * @public
43
- */
44
- const methods = {
45
- getMeta: async (doctype, url) => {
46
- const client = new GraphQLClient(url || '/graphql', {
47
- fetch: window.fetch,
48
- jsonSerializer: {
49
- stringify: obj => JSON.stringify(obj), // process the request object before sending; leave as default JSON
50
- parse: metaParser, // process the response meta object
51
- },
52
- });
53
- const { getMeta } = await client.request({
54
- document: queries.getMeta,
55
- variables: { doctype },
56
- });
57
- return getMeta;
58
- },
59
- };
60
- export { queries, typeDefs, methods };
61
- export { StonecropClient } from './client';
@@ -1,19 +0,0 @@
1
- import { gql } from 'graphql-request';
2
- /**
3
- * Queries for the GraphQL API.
4
- * @public
5
- */
6
- const queries = {
7
- getMeta: gql `
8
- query getDoctype($doctype: String!) {
9
- getMeta(doctype: $doctype) {
10
- id
11
- name
12
- workflow
13
- schema
14
- actions
15
- }
16
- }
17
- `,
18
- };
19
- export { queries };
@@ -1,87 +0,0 @@
1
- import type { DoctypeMeta as DoctypeMetaType } from '@stonecrop/schema';
2
- /**
3
- * Route context for identifying what doctype/record we're working with
4
- * @public
5
- */
6
- export interface RouteContext {
7
- /** Doctype name (e.g., 'Task', 'Customer') */
8
- doctype: string;
9
- /** Optional record ID for viewing/editing a specific record */
10
- recordId?: string;
11
- /** Additional context properties */
12
- [key: string]: unknown;
13
- }
14
- /**
15
- * Options for creating a Stonecrop client
16
- * @public
17
- */
18
- export interface StonecropClientOptions {
19
- /** GraphQL endpoint URL */
20
- endpoint: string;
21
- /** Additional HTTP headers to include in requests */
22
- headers?: Record<string, string>;
23
- }
24
- /**
25
- * Client for interacting with Stonecrop GraphQL API
26
- * @public
27
- */
28
- export declare class StonecropClient {
29
- private endpoint;
30
- private headers;
31
- private metaCache;
32
- constructor(options: StonecropClientOptions);
33
- /**
34
- * Execute a GraphQL query
35
- * @param query - GraphQL query string
36
- * @param variables - Query variables
37
- */
38
- query<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T>;
39
- /**
40
- * Execute a GraphQL mutation
41
- * @param mutation - GraphQL mutation string
42
- * @param variables - Mutation variables
43
- */
44
- mutate<T = unknown>(mutation: string, variables?: Record<string, unknown>): Promise<T>;
45
- /**
46
- * Get doctype metadata
47
- * @param context - Route context containing doctype name
48
- */
49
- getMeta(context: RouteContext): Promise<DoctypeMetaType | null>;
50
- /**
51
- * Get all doctype metadata
52
- */
53
- getAllMeta(): Promise<DoctypeMetaType[]>;
54
- /**
55
- * Get a single record by ID
56
- * @param doctype - Doctype metadata
57
- * @param recordId - Record ID to fetch
58
- */
59
- getRecord(doctype: DoctypeMetaType, recordId: string): Promise<Record<string, unknown> | null>;
60
- /**
61
- * Get multiple records with optional filtering and pagination
62
- * @param doctype - Doctype metadata
63
- * @param options - Query options (filters, orderBy, limit, offset)
64
- */
65
- getRecords(doctype: DoctypeMetaType, options?: {
66
- filters?: Record<string, unknown>;
67
- orderBy?: string;
68
- limit?: number;
69
- offset?: number;
70
- }): Promise<Record<string, unknown>[]>;
71
- /**
72
- * Execute a doctype action
73
- * @param doctype - Doctype metadata
74
- * @param action - Action name to execute
75
- * @param args - Action arguments
76
- */
77
- runAction(doctype: DoctypeMetaType, action: string, args?: unknown[]): Promise<{
78
- success: boolean;
79
- data: unknown;
80
- error: string | null;
81
- }>;
82
- /**
83
- * Clear the cached doctype metadata
84
- */
85
- clearMetaCache(): void;
86
- }
87
- //# sourceMappingURL=stonecrop-client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stonecrop-client.d.ts","sourceRoot":"","sources":["../../src/stonecrop-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,IAAI,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAEvE;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC5B,8CAA8C;IAC9C,OAAO,EAAE,MAAM,CAAA;IACf,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,oCAAoC;IACpC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACtB;AAED;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACtC,2BAA2B;IAC3B,QAAQ,EAAE,MAAM,CAAA;IAChB,qDAAqD;IACrD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAChC;AAED;;;GAGG;AACH,qBAAa,eAAe;IAC3B,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,OAAO,CAAwB;IACvC,OAAO,CAAC,SAAS,CAA0C;gBAE/C,OAAO,EAAE,sBAAsB;IAQ3C;;;;OAIG;IACG,KAAK,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAmBxF;;;;OAIG;IACG,MAAM,CAAC,CAAC,GAAG,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAI5F;;;OAGG;IACG,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;IA0CrE;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;IAsC9C;;;;OAIG;IACG,SAAS,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAiBpG;;;;OAIG;IACG,UAAU,CACf,OAAO,EAAE,eAAe,EACxB,OAAO,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QACjC,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,MAAM,CAAC,EAAE,MAAM,CAAA;KACf,GACC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IAiCrC;;;;;OAKG;IACG,SAAS,CACd,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,OAAO,EAAE,GACd,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IAuBrE;;OAEG;IACH,cAAc,IAAI,IAAI;CAGtB"}
@@ -1,11 +0,0 @@
1
- // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
- // It should be published with your NPM package. It should not be tracked by Git.
3
- {
4
- "tsdocVersion": "0.12",
5
- "toolPackages": [
6
- {
7
- "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.57.0"
9
- }
10
- ]
11
- }
@@ -1,4 +0,0 @@
1
- /**
2
- * @file This file contains all the types that are used in the application.
3
- * @public
4
- */
@@ -1,191 +0,0 @@
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
- }