@stigmer/rpc-client 0.0.34
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/LICENSE +190 -0
- package/README.md +66 -0
- package/context.d.ts +13 -0
- package/context.d.ts.map +1 -0
- package/context.js +14 -0
- package/context.js.map +1 -0
- package/hooks.d.ts +27 -0
- package/hooks.d.ts.map +1 -0
- package/hooks.js +38 -0
- package/hooks.js.map +1 -0
- package/index.d.ts +11 -0
- package/index.d.ts.map +1 -0
- package/index.js +12 -0
- package/index.js.map +1 -0
- package/interceptors.d.ts +19 -0
- package/interceptors.d.ts.map +1 -0
- package/interceptors.js +37 -0
- package/interceptors.js.map +1 -0
- package/package.json +38 -0
- package/provider.d.ts +29 -0
- package/provider.d.ts.map +1 -0
- package/provider.js +36 -0
- package/provider.js.map +1 -0
- package/src/context.ts +16 -0
- package/src/hooks.ts +45 -0
- package/src/index.ts +21 -0
- package/src/interceptors.ts +42 -0
- package/src/provider.tsx +55 -0
- package/src/transport.ts +31 -0
- package/src/types.ts +37 -0
- package/transport.d.ts +19 -0
- package/transport.d.ts.map +1 -0
- package/transport.js +30 -0
- package/transport.js.map +1 -0
- package/types.d.ts +34 -0
- package/types.d.ts.map +1 -0
- package/types.js +2 -0
- package/types.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Stigmer Contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# @stigmer/rpc-client
|
|
2
|
+
|
|
3
|
+
Connect-RPC transport, auth interceptor, and typed service client factory for Stigmer APIs.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @stigmer/rpc-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Peer dependencies: `react`, `@bufbuild/protobuf`, `@connectrpc/connect`, `@connectrpc/connect-web`
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### 1. Wrap your app with the transport provider
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { StigmerTransportProvider } from "@stigmer/rpc-client";
|
|
19
|
+
|
|
20
|
+
function App() {
|
|
21
|
+
return (
|
|
22
|
+
<StigmerTransportProvider
|
|
23
|
+
serverUrl="http://localhost:8090"
|
|
24
|
+
getAccessToken={() => Promise.resolve(myToken)}
|
|
25
|
+
>
|
|
26
|
+
<MyApp />
|
|
27
|
+
</StigmerTransportProvider>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 2. Use typed service clients in components
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import { useServiceClient } from "@stigmer/rpc-client";
|
|
36
|
+
import { AgentExecutionQueryService } from "@stigmer/protos/ai/stigmer/agentic/agentexecution/v1/query_connect";
|
|
37
|
+
|
|
38
|
+
function MyComponent() {
|
|
39
|
+
const client = useServiceClient(AgentExecutionQueryService);
|
|
40
|
+
// client is fully typed — call any RPC method directly
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Non-React usage
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { createStigmerTransport } from "@stigmer/rpc-client";
|
|
48
|
+
import { createClient } from "@connectrpc/connect";
|
|
49
|
+
|
|
50
|
+
const transport = createStigmerTransport({
|
|
51
|
+
serverUrl: "http://localhost:8090",
|
|
52
|
+
});
|
|
53
|
+
const client = createClient(SomeService, transport);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Exports
|
|
57
|
+
|
|
58
|
+
- `StigmerTransportProvider` — React context provider
|
|
59
|
+
- `useStigmerTransport()` — access the transport from context
|
|
60
|
+
- `useServiceClient(service)` — typed client factory hook
|
|
61
|
+
- `createStigmerTransport(config)` — imperative factory (no React)
|
|
62
|
+
- `createAuthInterceptor`, `errorStripInterceptor` — interceptors
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
Apache-2.0
|
package/context.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Transport } from "@connectrpc/connect";
|
|
2
|
+
/**
|
|
3
|
+
* React context for the Stigmer Connect-RPC transport.
|
|
4
|
+
*
|
|
5
|
+
* Separated from the provider component to prevent circular imports:
|
|
6
|
+
* both the provider (`provider.tsx`) and the consumer hooks (`hooks.ts`)
|
|
7
|
+
* import from this file, but neither imports the other.
|
|
8
|
+
*
|
|
9
|
+
* The context value is `null` when no provider is mounted. Consumer hooks
|
|
10
|
+
* throw in this case to surface wiring mistakes during development.
|
|
11
|
+
*/
|
|
12
|
+
export declare const StigmerTransportContext: import("react").Context<Transport | null>;
|
|
13
|
+
//# sourceMappingURL=context.d.ts.map
|
package/context.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;;;;;;;GASG;AACH,eAAO,MAAM,uBAAuB,2CAAwC,CAAC"}
|
package/context.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* React context for the Stigmer Connect-RPC transport.
|
|
5
|
+
*
|
|
6
|
+
* Separated from the provider component to prevent circular imports:
|
|
7
|
+
* both the provider (`provider.tsx`) and the consumer hooks (`hooks.ts`)
|
|
8
|
+
* import from this file, but neither imports the other.
|
|
9
|
+
*
|
|
10
|
+
* The context value is `null` when no provider is mounted. Consumer hooks
|
|
11
|
+
* throw in this case to surface wiring mistakes during development.
|
|
12
|
+
*/
|
|
13
|
+
export const StigmerTransportContext = createContext(null);
|
|
14
|
+
//# sourceMappingURL=context.js.map
|
package/context.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAGtC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,aAAa,CAAmB,IAAI,CAAC,CAAC"}
|
package/hooks.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Client } from "@connectrpc/connect";
|
|
2
|
+
import type { DescService } from "@bufbuild/protobuf";
|
|
3
|
+
/**
|
|
4
|
+
* Access the Stigmer RPC transport from the nearest
|
|
5
|
+
* {@link StigmerTransportProvider}.
|
|
6
|
+
*
|
|
7
|
+
* Throws if called outside a provider — this surfaces wiring mistakes
|
|
8
|
+
* immediately during development rather than producing silent `null`
|
|
9
|
+
* failures at runtime.
|
|
10
|
+
*/
|
|
11
|
+
export declare function useStigmerTransport(): import("@connectrpc/connect").Transport;
|
|
12
|
+
/**
|
|
13
|
+
* Create a typed Connect-RPC service client from the nearest transport
|
|
14
|
+
* provider.
|
|
15
|
+
*
|
|
16
|
+
* The client is memoized on the `(service, transport)` tuple — it is
|
|
17
|
+
* stable across re-renders as long as the transport does not change
|
|
18
|
+
* (which only happens when `serverUrl` changes).
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const client = useServiceClient(AgentExecutionQueryController);
|
|
23
|
+
* const execution = await client.get(request);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function useServiceClient<T extends DescService>(service: T): Client<T>;
|
|
27
|
+
//# sourceMappingURL=hooks.d.ts.map
|
package/hooks.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGtD;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,4CAQlC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,WAAW,EACpD,OAAO,EAAE,CAAC,GACT,MAAM,CAAC,CAAC,CAAC,CAGX"}
|
package/hooks.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useContext, useMemo } from "react";
|
|
3
|
+
import { createClient } from "@connectrpc/connect";
|
|
4
|
+
import { StigmerTransportContext } from "./context";
|
|
5
|
+
/**
|
|
6
|
+
* Access the Stigmer RPC transport from the nearest
|
|
7
|
+
* {@link StigmerTransportProvider}.
|
|
8
|
+
*
|
|
9
|
+
* Throws if called outside a provider — this surfaces wiring mistakes
|
|
10
|
+
* immediately during development rather than producing silent `null`
|
|
11
|
+
* failures at runtime.
|
|
12
|
+
*/
|
|
13
|
+
export function useStigmerTransport() {
|
|
14
|
+
const transport = useContext(StigmerTransportContext);
|
|
15
|
+
if (!transport) {
|
|
16
|
+
throw new Error("useStigmerTransport must be used within <StigmerTransportProvider>");
|
|
17
|
+
}
|
|
18
|
+
return transport;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create a typed Connect-RPC service client from the nearest transport
|
|
22
|
+
* provider.
|
|
23
|
+
*
|
|
24
|
+
* The client is memoized on the `(service, transport)` tuple — it is
|
|
25
|
+
* stable across re-renders as long as the transport does not change
|
|
26
|
+
* (which only happens when `serverUrl` changes).
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const client = useServiceClient(AgentExecutionQueryController);
|
|
31
|
+
* const execution = await client.get(request);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function useServiceClient(service) {
|
|
35
|
+
const transport = useStigmerTransport();
|
|
36
|
+
return useMemo(() => createClient(service, transport), [service, transport]);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=hooks.js.map
|
package/hooks.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAe,MAAM,qBAAqB,CAAC;AAEhE,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAEpD;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB;IACjC,MAAM,SAAS,GAAG,UAAU,CAAC,uBAAuB,CAAC,CAAC;IACtD,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,oEAAoE,CACrE,CAAC;IACJ,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,gBAAgB,CAC9B,OAAU;IAEV,MAAM,SAAS,GAAG,mBAAmB,EAAE,CAAC;IACxC,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC;AAC/E,CAAC"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type { StigmerRpcConfig, TokenProvider } from "./types";
|
|
2
|
+
export { createStigmerTransport } from "./transport";
|
|
3
|
+
export { createAuthInterceptor, errorStripInterceptor } from "./interceptors";
|
|
4
|
+
export { StigmerTransportContext } from "./context";
|
|
5
|
+
export { StigmerTransportProvider } from "./provider";
|
|
6
|
+
export type { StigmerTransportProviderProps } from "./provider";
|
|
7
|
+
export { useStigmerTransport, useServiceClient } from "./hooks";
|
|
8
|
+
export { createClient } from "@connectrpc/connect";
|
|
9
|
+
export type { Client, Transport, Interceptor } from "@connectrpc/connect";
|
|
10
|
+
export type { DescService } from "@bufbuild/protobuf";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG/D,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAGrD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAG9E,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACtD,YAAY,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAC;AAGhE,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGhE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC1E,YAAY,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC"}
|
package/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Transport factory (non-React, usable in tests and scripts)
|
|
2
|
+
export { createStigmerTransport } from "./transport";
|
|
3
|
+
// Interceptors (for custom transport compositions)
|
|
4
|
+
export { createAuthInterceptor, errorStripInterceptor } from "./interceptors";
|
|
5
|
+
// React context and provider
|
|
6
|
+
export { StigmerTransportContext } from "./context";
|
|
7
|
+
export { StigmerTransportProvider } from "./provider";
|
|
8
|
+
// React hooks
|
|
9
|
+
export { useStigmerTransport, useServiceClient } from "./hooks";
|
|
10
|
+
// Re-exports for consumer convenience
|
|
11
|
+
export { createClient } from "@connectrpc/connect";
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,6DAA6D;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAErD,mDAAmD;AACnD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAE9E,6BAA6B;AAC7B,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AAGtD,cAAc;AACd,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhE,sCAAsC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Interceptor } from "@connectrpc/connect";
|
|
2
|
+
import type { TokenProvider } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Create an interceptor that attaches an `Authorization: Bearer <token>`
|
|
5
|
+
* header to every outgoing request.
|
|
6
|
+
*
|
|
7
|
+
* The `getAccessToken` callback is invoked per-request, so token refresh
|
|
8
|
+
* and auth state changes are picked up automatically. When the callback is
|
|
9
|
+
* `undefined` (disabled auth mode) or returns `null`, no header is set.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createAuthInterceptor(getAccessToken?: TokenProvider): Interceptor;
|
|
12
|
+
/**
|
|
13
|
+
* Interceptor that strips gRPC status-code prefixes (e.g., `[internal]`)
|
|
14
|
+
* from error messages before they reach application code. This produces
|
|
15
|
+
* cleaner user-facing error text without losing the structured error code
|
|
16
|
+
* (which remains on the `ConnectError.code` property).
|
|
17
|
+
*/
|
|
18
|
+
export declare const errorStripInterceptor: Interceptor;
|
|
19
|
+
//# sourceMappingURL=interceptors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interceptors.d.ts","sourceRoot":"","sources":["../src/interceptors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,cAAc,CAAC,EAAE,aAAa,GAC7B,WAAW,CAUb;AAED;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,EAAE,WAUjC,CAAC"}
|
package/interceptors.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create an interceptor that attaches an `Authorization: Bearer <token>`
|
|
3
|
+
* header to every outgoing request.
|
|
4
|
+
*
|
|
5
|
+
* The `getAccessToken` callback is invoked per-request, so token refresh
|
|
6
|
+
* and auth state changes are picked up automatically. When the callback is
|
|
7
|
+
* `undefined` (disabled auth mode) or returns `null`, no header is set.
|
|
8
|
+
*/
|
|
9
|
+
export function createAuthInterceptor(getAccessToken) {
|
|
10
|
+
return (next) => async (request) => {
|
|
11
|
+
if (getAccessToken) {
|
|
12
|
+
const token = await getAccessToken();
|
|
13
|
+
if (token) {
|
|
14
|
+
request.header.set("Authorization", `Bearer ${token}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return next(request);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Interceptor that strips gRPC status-code prefixes (e.g., `[internal]`)
|
|
22
|
+
* from error messages before they reach application code. This produces
|
|
23
|
+
* cleaner user-facing error text without losing the structured error code
|
|
24
|
+
* (which remains on the `ConnectError.code` property).
|
|
25
|
+
*/
|
|
26
|
+
export const errorStripInterceptor = (next) => async (request) => {
|
|
27
|
+
try {
|
|
28
|
+
return await next(request);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (error instanceof Error && error.message) {
|
|
32
|
+
error.message = error.message.replace(/^\[.*?]\s*/, "");
|
|
33
|
+
}
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=interceptors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interceptors.js","sourceRoot":"","sources":["../src/interceptors.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CACnC,cAA8B;IAE9B,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;QACjC,IAAI,cAAc,EAAE,CAAC;YACnB,MAAM,KAAK,GAAG,MAAM,cAAc,EAAE,CAAC;YACrC,IAAI,KAAK,EAAE,CAAC;gBACV,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,eAAe,EAAE,UAAU,KAAK,EAAE,CAAC,CAAC;YACzD,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAChC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;IAC1B,IAAI,CAAC;QACH,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;YAC5C,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QAC1D,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stigmer/rpc-client",
|
|
3
|
+
"version": "0.0.34",
|
|
4
|
+
"description": "Connect-RPC transport, auth interceptor, and service client factory for Stigmer APIs",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/stigmer/stigmer.git",
|
|
11
|
+
"directory": "client-apps/web/_libs/infra/rpc-client"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=18"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"stigmer",
|
|
18
|
+
"rpc",
|
|
19
|
+
"connect-rpc",
|
|
20
|
+
"grpc-web",
|
|
21
|
+
"react",
|
|
22
|
+
"ai-agents"
|
|
23
|
+
],
|
|
24
|
+
"main": "./index.js",
|
|
25
|
+
"types": "./index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./index.d.ts",
|
|
29
|
+
"import": "./index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@bufbuild/protobuf": "^2.0.0",
|
|
34
|
+
"@connectrpc/connect": "^2.0.0",
|
|
35
|
+
"@connectrpc/connect-web": "^2.0.0",
|
|
36
|
+
"react": "^19.0.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/provider.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { StigmerRpcConfig } from "./types";
|
|
3
|
+
export interface StigmerTransportProviderProps extends StigmerRpcConfig {
|
|
4
|
+
readonly children: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* React provider that creates a Stigmer RPC transport and distributes it
|
|
8
|
+
* to descendant components via {@link StigmerTransportContext}.
|
|
9
|
+
*
|
|
10
|
+
* The transport is memoized on `(serverUrl, getAccessToken, interceptors)`.
|
|
11
|
+
* To avoid unnecessary transport recreation on re-renders, stabilize the
|
|
12
|
+
* callback and interceptor references in the parent component:
|
|
13
|
+
*
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const getToken = useCallback(() => auth.accessToken, [auth.accessToken]);
|
|
16
|
+
*
|
|
17
|
+
* <StigmerTransportProvider
|
|
18
|
+
* serverUrl="http://localhost:7234"
|
|
19
|
+
* getAccessToken={getToken}
|
|
20
|
+
* >
|
|
21
|
+
* <App />
|
|
22
|
+
* </StigmerTransportProvider>
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* When `getAccessToken` is omitted (disabled auth mode), the transport is
|
|
26
|
+
* recreated only when `serverUrl` changes.
|
|
27
|
+
*/
|
|
28
|
+
export declare function StigmerTransportProvider({ serverUrl, getAccessToken, interceptors, children, }: StigmerTransportProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
29
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAW,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAChD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAIhD,MAAM,WAAW,6BAA8B,SAAQ,gBAAgB;IACrE,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,wBAAwB,CAAC,EACvC,SAAS,EACT,cAAc,EACd,YAAY,EACZ,QAAQ,GACT,EAAE,6BAA6B,2CAgB/B"}
|
package/provider.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { createStigmerTransport } from "./transport";
|
|
5
|
+
import { StigmerTransportContext } from "./context";
|
|
6
|
+
/**
|
|
7
|
+
* React provider that creates a Stigmer RPC transport and distributes it
|
|
8
|
+
* to descendant components via {@link StigmerTransportContext}.
|
|
9
|
+
*
|
|
10
|
+
* The transport is memoized on `(serverUrl, getAccessToken, interceptors)`.
|
|
11
|
+
* To avoid unnecessary transport recreation on re-renders, stabilize the
|
|
12
|
+
* callback and interceptor references in the parent component:
|
|
13
|
+
*
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const getToken = useCallback(() => auth.accessToken, [auth.accessToken]);
|
|
16
|
+
*
|
|
17
|
+
* <StigmerTransportProvider
|
|
18
|
+
* serverUrl="http://localhost:7234"
|
|
19
|
+
* getAccessToken={getToken}
|
|
20
|
+
* >
|
|
21
|
+
* <App />
|
|
22
|
+
* </StigmerTransportProvider>
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* When `getAccessToken` is omitted (disabled auth mode), the transport is
|
|
26
|
+
* recreated only when `serverUrl` changes.
|
|
27
|
+
*/
|
|
28
|
+
export function StigmerTransportProvider({ serverUrl, getAccessToken, interceptors, children, }) {
|
|
29
|
+
const transport = useMemo(() => createStigmerTransport({
|
|
30
|
+
serverUrl,
|
|
31
|
+
getAccessToken,
|
|
32
|
+
interceptors,
|
|
33
|
+
}), [serverUrl, getAccessToken, interceptors]);
|
|
34
|
+
return (_jsx(StigmerTransportContext.Provider, { value: transport, children: children }));
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=provider.js.map
|
package/provider.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,OAAO,EAAkB,MAAM,OAAO,CAAC;AAEhD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAMpD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,wBAAwB,CAAC,EACvC,SAAS,EACT,cAAc,EACd,YAAY,EACZ,QAAQ,GACsB;IAC9B,MAAM,SAAS,GAAG,OAAO,CACvB,GAAG,EAAE,CACH,sBAAsB,CAAC;QACrB,SAAS;QACT,cAAc;QACd,YAAY;KACb,CAAC,EACJ,CAAC,SAAS,EAAE,cAAc,EAAE,YAAY,CAAC,CAC1C,CAAC;IAEF,OAAO,CACL,KAAC,uBAAuB,CAAC,QAAQ,IAAC,KAAK,EAAE,SAAS,YAC/C,QAAQ,GACwB,CACpC,CAAC;AACJ,CAAC"}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { createContext } from "react";
|
|
4
|
+
import type { Transport } from "@connectrpc/connect";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* React context for the Stigmer Connect-RPC transport.
|
|
8
|
+
*
|
|
9
|
+
* Separated from the provider component to prevent circular imports:
|
|
10
|
+
* both the provider (`provider.tsx`) and the consumer hooks (`hooks.ts`)
|
|
11
|
+
* import from this file, but neither imports the other.
|
|
12
|
+
*
|
|
13
|
+
* The context value is `null` when no provider is mounted. Consumer hooks
|
|
14
|
+
* throw in this case to surface wiring mistakes during development.
|
|
15
|
+
*/
|
|
16
|
+
export const StigmerTransportContext = createContext<Transport | null>(null);
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useContext, useMemo } from "react";
|
|
4
|
+
import { createClient, type Client } from "@connectrpc/connect";
|
|
5
|
+
import type { DescService } from "@bufbuild/protobuf";
|
|
6
|
+
import { StigmerTransportContext } from "./context";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Access the Stigmer RPC transport from the nearest
|
|
10
|
+
* {@link StigmerTransportProvider}.
|
|
11
|
+
*
|
|
12
|
+
* Throws if called outside a provider — this surfaces wiring mistakes
|
|
13
|
+
* immediately during development rather than producing silent `null`
|
|
14
|
+
* failures at runtime.
|
|
15
|
+
*/
|
|
16
|
+
export function useStigmerTransport() {
|
|
17
|
+
const transport = useContext(StigmerTransportContext);
|
|
18
|
+
if (!transport) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"useStigmerTransport must be used within <StigmerTransportProvider>",
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return transport;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Create a typed Connect-RPC service client from the nearest transport
|
|
28
|
+
* provider.
|
|
29
|
+
*
|
|
30
|
+
* The client is memoized on the `(service, transport)` tuple — it is
|
|
31
|
+
* stable across re-renders as long as the transport does not change
|
|
32
|
+
* (which only happens when `serverUrl` changes).
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const client = useServiceClient(AgentExecutionQueryController);
|
|
37
|
+
* const execution = await client.get(request);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function useServiceClient<T extends DescService>(
|
|
41
|
+
service: T,
|
|
42
|
+
): Client<T> {
|
|
43
|
+
const transport = useStigmerTransport();
|
|
44
|
+
return useMemo(() => createClient(service, transport), [service, transport]);
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Configuration types
|
|
2
|
+
export type { StigmerRpcConfig, TokenProvider } from "./types";
|
|
3
|
+
|
|
4
|
+
// Transport factory (non-React, usable in tests and scripts)
|
|
5
|
+
export { createStigmerTransport } from "./transport";
|
|
6
|
+
|
|
7
|
+
// Interceptors (for custom transport compositions)
|
|
8
|
+
export { createAuthInterceptor, errorStripInterceptor } from "./interceptors";
|
|
9
|
+
|
|
10
|
+
// React context and provider
|
|
11
|
+
export { StigmerTransportContext } from "./context";
|
|
12
|
+
export { StigmerTransportProvider } from "./provider";
|
|
13
|
+
export type { StigmerTransportProviderProps } from "./provider";
|
|
14
|
+
|
|
15
|
+
// React hooks
|
|
16
|
+
export { useStigmerTransport, useServiceClient } from "./hooks";
|
|
17
|
+
|
|
18
|
+
// Re-exports for consumer convenience
|
|
19
|
+
export { createClient } from "@connectrpc/connect";
|
|
20
|
+
export type { Client, Transport, Interceptor } from "@connectrpc/connect";
|
|
21
|
+
export type { DescService } from "@bufbuild/protobuf";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Interceptor } from "@connectrpc/connect";
|
|
2
|
+
import type { TokenProvider } from "./types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Create an interceptor that attaches an `Authorization: Bearer <token>`
|
|
6
|
+
* header to every outgoing request.
|
|
7
|
+
*
|
|
8
|
+
* The `getAccessToken` callback is invoked per-request, so token refresh
|
|
9
|
+
* and auth state changes are picked up automatically. When the callback is
|
|
10
|
+
* `undefined` (disabled auth mode) or returns `null`, no header is set.
|
|
11
|
+
*/
|
|
12
|
+
export function createAuthInterceptor(
|
|
13
|
+
getAccessToken?: TokenProvider,
|
|
14
|
+
): Interceptor {
|
|
15
|
+
return (next) => async (request) => {
|
|
16
|
+
if (getAccessToken) {
|
|
17
|
+
const token = await getAccessToken();
|
|
18
|
+
if (token) {
|
|
19
|
+
request.header.set("Authorization", `Bearer ${token}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return next(request);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Interceptor that strips gRPC status-code prefixes (e.g., `[internal]`)
|
|
28
|
+
* from error messages before they reach application code. This produces
|
|
29
|
+
* cleaner user-facing error text without losing the structured error code
|
|
30
|
+
* (which remains on the `ConnectError.code` property).
|
|
31
|
+
*/
|
|
32
|
+
export const errorStripInterceptor: Interceptor =
|
|
33
|
+
(next) => async (request) => {
|
|
34
|
+
try {
|
|
35
|
+
return await next(request);
|
|
36
|
+
} catch (error: unknown) {
|
|
37
|
+
if (error instanceof Error && error.message) {
|
|
38
|
+
error.message = error.message.replace(/^\[.*?]\s*/, "");
|
|
39
|
+
}
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
};
|
package/src/provider.tsx
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useMemo, type ReactNode } from "react";
|
|
4
|
+
import type { StigmerRpcConfig } from "./types";
|
|
5
|
+
import { createStigmerTransport } from "./transport";
|
|
6
|
+
import { StigmerTransportContext } from "./context";
|
|
7
|
+
|
|
8
|
+
export interface StigmerTransportProviderProps extends StigmerRpcConfig {
|
|
9
|
+
readonly children: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* React provider that creates a Stigmer RPC transport and distributes it
|
|
14
|
+
* to descendant components via {@link StigmerTransportContext}.
|
|
15
|
+
*
|
|
16
|
+
* The transport is memoized on `(serverUrl, getAccessToken, interceptors)`.
|
|
17
|
+
* To avoid unnecessary transport recreation on re-renders, stabilize the
|
|
18
|
+
* callback and interceptor references in the parent component:
|
|
19
|
+
*
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const getToken = useCallback(() => auth.accessToken, [auth.accessToken]);
|
|
22
|
+
*
|
|
23
|
+
* <StigmerTransportProvider
|
|
24
|
+
* serverUrl="http://localhost:7234"
|
|
25
|
+
* getAccessToken={getToken}
|
|
26
|
+
* >
|
|
27
|
+
* <App />
|
|
28
|
+
* </StigmerTransportProvider>
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* When `getAccessToken` is omitted (disabled auth mode), the transport is
|
|
32
|
+
* recreated only when `serverUrl` changes.
|
|
33
|
+
*/
|
|
34
|
+
export function StigmerTransportProvider({
|
|
35
|
+
serverUrl,
|
|
36
|
+
getAccessToken,
|
|
37
|
+
interceptors,
|
|
38
|
+
children,
|
|
39
|
+
}: StigmerTransportProviderProps) {
|
|
40
|
+
const transport = useMemo(
|
|
41
|
+
() =>
|
|
42
|
+
createStigmerTransport({
|
|
43
|
+
serverUrl,
|
|
44
|
+
getAccessToken,
|
|
45
|
+
interceptors,
|
|
46
|
+
}),
|
|
47
|
+
[serverUrl, getAccessToken, interceptors],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<StigmerTransportContext.Provider value={transport}>
|
|
52
|
+
{children}
|
|
53
|
+
</StigmerTransportContext.Provider>
|
|
54
|
+
);
|
|
55
|
+
}
|
package/src/transport.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createGrpcWebTransport } from "@connectrpc/connect-web";
|
|
2
|
+
import type { Transport } from "@connectrpc/connect";
|
|
3
|
+
import type { StigmerRpcConfig } from "./types";
|
|
4
|
+
import { createAuthInterceptor, errorStripInterceptor } from "./interceptors";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a Connect-RPC gRPC-Web transport configured for a Stigmer server.
|
|
8
|
+
*
|
|
9
|
+
* This is the imperative (non-React) API. It produces a standard Connect-RPC
|
|
10
|
+
* `Transport` that can be used directly with `createClient(service, transport)`
|
|
11
|
+
* in tests, scripts, or any context where React hooks are unavailable.
|
|
12
|
+
*
|
|
13
|
+
* For React component trees, prefer {@link StigmerTransportProvider} which
|
|
14
|
+
* wraps this factory and distributes the transport via context.
|
|
15
|
+
*
|
|
16
|
+
* Built-in interceptors (applied in order):
|
|
17
|
+
* 1. Auth interceptor — attaches `Authorization: Bearer <token>` header
|
|
18
|
+
* 2. Error-strip interceptor — removes gRPC status-code prefixes from messages
|
|
19
|
+
* 3. Any additional interceptors provided via `config.interceptors`
|
|
20
|
+
*/
|
|
21
|
+
export function createStigmerTransport(config: StigmerRpcConfig): Transport {
|
|
22
|
+
return createGrpcWebTransport({
|
|
23
|
+
baseUrl: config.serverUrl,
|
|
24
|
+
useBinaryFormat: true,
|
|
25
|
+
interceptors: [
|
|
26
|
+
createAuthInterceptor(config.getAccessToken),
|
|
27
|
+
errorStripInterceptor,
|
|
28
|
+
...(config.interceptors ?? []),
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Interceptor } from "@connectrpc/connect";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Callback that returns the current access token for authenticating RPC
|
|
5
|
+
* requests. Called by the auth interceptor on every outgoing request.
|
|
6
|
+
*
|
|
7
|
+
* Return `null` to send unauthenticated requests (e.g., disabled auth mode).
|
|
8
|
+
* May be async to support token refresh flows.
|
|
9
|
+
*/
|
|
10
|
+
export type TokenProvider = () => Promise<string | null> | string | null;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for a Stigmer RPC transport.
|
|
14
|
+
*
|
|
15
|
+
* Passed to {@link createStigmerTransport} (imperative) or
|
|
16
|
+
* {@link StigmerTransportProvider} (React context). The consumer — whether
|
|
17
|
+
* the Stigmer web console or an external platform owner's app — provides
|
|
18
|
+
* these values; the library never reads environment variables or auth state
|
|
19
|
+
* directly.
|
|
20
|
+
*/
|
|
21
|
+
export interface StigmerRpcConfig {
|
|
22
|
+
/** Base URL of the Stigmer server (e.g., `"http://localhost:7234"`). */
|
|
23
|
+
readonly serverUrl: string;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Token provider invoked on every RPC request to obtain the current
|
|
27
|
+
* access token. Omit for unauthenticated usage (disabled auth mode).
|
|
28
|
+
*/
|
|
29
|
+
readonly getAccessToken?: TokenProvider;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Additional Connect-RPC interceptors appended after the built-in auth
|
|
33
|
+
* and error-strip interceptors. Use this for application-specific
|
|
34
|
+
* concerns like tracing or custom error handling.
|
|
35
|
+
*/
|
|
36
|
+
readonly interceptors?: Interceptor[];
|
|
37
|
+
}
|
package/transport.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Transport } from "@connectrpc/connect";
|
|
2
|
+
import type { StigmerRpcConfig } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Create a Connect-RPC gRPC-Web transport configured for a Stigmer server.
|
|
5
|
+
*
|
|
6
|
+
* This is the imperative (non-React) API. It produces a standard Connect-RPC
|
|
7
|
+
* `Transport` that can be used directly with `createClient(service, transport)`
|
|
8
|
+
* in tests, scripts, or any context where React hooks are unavailable.
|
|
9
|
+
*
|
|
10
|
+
* For React component trees, prefer {@link StigmerTransportProvider} which
|
|
11
|
+
* wraps this factory and distributes the transport via context.
|
|
12
|
+
*
|
|
13
|
+
* Built-in interceptors (applied in order):
|
|
14
|
+
* 1. Auth interceptor — attaches `Authorization: Bearer <token>` header
|
|
15
|
+
* 2. Error-strip interceptor — removes gRPC status-code prefixes from messages
|
|
16
|
+
* 3. Any additional interceptors provided via `config.interceptors`
|
|
17
|
+
*/
|
|
18
|
+
export declare function createStigmerTransport(config: StigmerRpcConfig): Transport;
|
|
19
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGhD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,gBAAgB,GAAG,SAAS,CAU1E"}
|
package/transport.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createGrpcWebTransport } from "@connectrpc/connect-web";
|
|
2
|
+
import { createAuthInterceptor, errorStripInterceptor } from "./interceptors";
|
|
3
|
+
/**
|
|
4
|
+
* Create a Connect-RPC gRPC-Web transport configured for a Stigmer server.
|
|
5
|
+
*
|
|
6
|
+
* This is the imperative (non-React) API. It produces a standard Connect-RPC
|
|
7
|
+
* `Transport` that can be used directly with `createClient(service, transport)`
|
|
8
|
+
* in tests, scripts, or any context where React hooks are unavailable.
|
|
9
|
+
*
|
|
10
|
+
* For React component trees, prefer {@link StigmerTransportProvider} which
|
|
11
|
+
* wraps this factory and distributes the transport via context.
|
|
12
|
+
*
|
|
13
|
+
* Built-in interceptors (applied in order):
|
|
14
|
+
* 1. Auth interceptor — attaches `Authorization: Bearer <token>` header
|
|
15
|
+
* 2. Error-strip interceptor — removes gRPC status-code prefixes from messages
|
|
16
|
+
* 3. Any additional interceptors provided via `config.interceptors`
|
|
17
|
+
*/
|
|
18
|
+
export function createStigmerTransport(config) {
|
|
19
|
+
var _a;
|
|
20
|
+
return createGrpcWebTransport({
|
|
21
|
+
baseUrl: config.serverUrl,
|
|
22
|
+
useBinaryFormat: true,
|
|
23
|
+
interceptors: [
|
|
24
|
+
createAuthInterceptor(config.getAccessToken),
|
|
25
|
+
errorStripInterceptor,
|
|
26
|
+
...((_a = config.interceptors) !== null && _a !== void 0 ? _a : []),
|
|
27
|
+
],
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=transport.js.map
|
package/transport.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AAGjE,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAE9E;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAwB;;IAC7D,OAAO,sBAAsB,CAAC;QAC5B,OAAO,EAAE,MAAM,CAAC,SAAS;QACzB,eAAe,EAAE,IAAI;QACrB,YAAY,EAAE;YACZ,qBAAqB,CAAC,MAAM,CAAC,cAAc,CAAC;YAC5C,qBAAqB;YACrB,GAAG,CAAC,MAAA,MAAM,CAAC,YAAY,mCAAI,EAAE,CAAC;SAC/B;KACF,CAAC,CAAC;AACL,CAAC"}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Interceptor } from "@connectrpc/connect";
|
|
2
|
+
/**
|
|
3
|
+
* Callback that returns the current access token for authenticating RPC
|
|
4
|
+
* requests. Called by the auth interceptor on every outgoing request.
|
|
5
|
+
*
|
|
6
|
+
* Return `null` to send unauthenticated requests (e.g., disabled auth mode).
|
|
7
|
+
* May be async to support token refresh flows.
|
|
8
|
+
*/
|
|
9
|
+
export type TokenProvider = () => Promise<string | null> | string | null;
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for a Stigmer RPC transport.
|
|
12
|
+
*
|
|
13
|
+
* Passed to {@link createStigmerTransport} (imperative) or
|
|
14
|
+
* {@link StigmerTransportProvider} (React context). The consumer — whether
|
|
15
|
+
* the Stigmer web console or an external platform owner's app — provides
|
|
16
|
+
* these values; the library never reads environment variables or auth state
|
|
17
|
+
* directly.
|
|
18
|
+
*/
|
|
19
|
+
export interface StigmerRpcConfig {
|
|
20
|
+
/** Base URL of the Stigmer server (e.g., `"http://localhost:7234"`). */
|
|
21
|
+
readonly serverUrl: string;
|
|
22
|
+
/**
|
|
23
|
+
* Token provider invoked on every RPC request to obtain the current
|
|
24
|
+
* access token. Omit for unauthenticated usage (disabled auth mode).
|
|
25
|
+
*/
|
|
26
|
+
readonly getAccessToken?: TokenProvider;
|
|
27
|
+
/**
|
|
28
|
+
* Additional Connect-RPC interceptors appended after the built-in auth
|
|
29
|
+
* and error-strip interceptors. Use this for application-specific
|
|
30
|
+
* concerns like tracing or custom error handling.
|
|
31
|
+
*/
|
|
32
|
+
readonly interceptors?: Interceptor[];
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=types.d.ts.map
|
package/types.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,MAAM,GAAG,IAAI,CAAC;AAEzE;;;;;;;;GAQG;AACH,MAAM,WAAW,gBAAgB;IAC/B,wEAAwE;IACxE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,aAAa,CAAC;IAExC;;;;OAIG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,WAAW,EAAE,CAAC;CACvC"}
|
package/types.js
ADDED
package/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|