@simtlix/simfinity-js-client 1.0.2 → 1.0.6
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
|
@@ -16,16 +16,29 @@ import SimfinityClient from '@simtlix/simfinity-js-client';
|
|
|
16
16
|
const client = new SimfinityClient('http://localhost:3000/graphql');
|
|
17
17
|
await client.init();
|
|
18
18
|
|
|
19
|
+
// Optional: attach auth headers (perform login in your app; this client does not include login)
|
|
20
|
+
const tokenStore = { accessToken: null };
|
|
21
|
+
const authedClient = new SimfinityClient('http://localhost:3000/graphql', {
|
|
22
|
+
prepareHeaders(headers) {
|
|
23
|
+
if (tokenStore.accessToken) {
|
|
24
|
+
headers['Authorization'] = `Bearer ${tokenStore.accessToken}`;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
await authedClient.init();
|
|
29
|
+
|
|
19
30
|
const series = await client.find('serie').exec();
|
|
20
31
|
console.log(series);
|
|
21
32
|
```
|
|
22
33
|
|
|
23
34
|
## API Reference
|
|
24
35
|
|
|
25
|
-
### `new SimfinityClient(endpoint)`
|
|
36
|
+
### `new SimfinityClient(endpoint [, options])`
|
|
26
37
|
|
|
27
38
|
Creates a new client instance pointing at the given GraphQL endpoint.
|
|
28
39
|
|
|
40
|
+
- **`options.prepareHeaders(headers)`** (optional): invoked before every GraphQL HTTP request. Mutate the plain `headers` object to add credentials (for example `Authorization`). The client does not implement login — call your auth API separately, then set tokens in a closure or store that `prepareHeaders` reads.
|
|
41
|
+
|
|
29
42
|
### `client.init()`
|
|
30
43
|
|
|
31
44
|
Runs an introspection query against the endpoint and builds internal registries of types, queries, and mutations — including Simfinity-specific field extensions (relation metadata, state machine flags, read-only markers). **Must be called before any other method.**
|
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/src/SimfinityClient.js
CHANGED
|
@@ -481,7 +481,14 @@ class AggregateBuilder {
|
|
|
481
481
|
// ---------------------------------------------------------------------------
|
|
482
482
|
|
|
483
483
|
export default class SimfinityClient {
|
|
484
|
-
|
|
484
|
+
/**
|
|
485
|
+
* @param {string} endpoint GraphQL HTTP endpoint URL
|
|
486
|
+
* @param {{ prepareHeaders?: (headers: Record<string, string>) => void }} [options]
|
|
487
|
+
* Optional `prepareHeaders` runs before each request; mutate `headers` to add auth
|
|
488
|
+
* (e.g. `headers['Authorization'] = 'Bearer …'`). Login is not part of this client —
|
|
489
|
+
* perform auth in your app and set tokens inside this callback or a closure it sees.
|
|
490
|
+
*/
|
|
491
|
+
constructor(endpoint, options = {}) {
|
|
485
492
|
this._endpoint = endpoint;
|
|
486
493
|
this._types = new Map();
|
|
487
494
|
this._queries = new Map();
|
|
@@ -491,6 +498,8 @@ export default class SimfinityClient {
|
|
|
491
498
|
this._typeNameToAggregate = new Map();
|
|
492
499
|
this._queryNameToType = new Map();
|
|
493
500
|
this._initialized = false;
|
|
501
|
+
this._prepareHeaders =
|
|
502
|
+
typeof options.prepareHeaders === 'function' ? options.prepareHeaders : null;
|
|
494
503
|
}
|
|
495
504
|
|
|
496
505
|
// -- Initialization ------------------------------------------------------
|
|
@@ -664,9 +673,13 @@ export default class SimfinityClient {
|
|
|
664
673
|
if (variables && Object.keys(variables).length > 0) {
|
|
665
674
|
body.variables = variables;
|
|
666
675
|
}
|
|
676
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
677
|
+
if (this._prepareHeaders) {
|
|
678
|
+
this._prepareHeaders(headers);
|
|
679
|
+
}
|
|
667
680
|
const response = await _fetch(this._endpoint, {
|
|
668
681
|
method: 'POST',
|
|
669
|
-
headers
|
|
682
|
+
headers,
|
|
670
683
|
body: JSON.stringify(body),
|
|
671
684
|
});
|
|
672
685
|
return response.json();
|