@pipe2-ai/sdk 0.1.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.
@@ -0,0 +1,8 @@
1
+ import { getSdk } from '../generated/sdk.js';
2
+ export type Pipe2SDK = ReturnType<typeof getSdk>;
3
+ /**
4
+ * Create a Pipe2.ai SDK client with full support for queries, mutations,
5
+ * and real-time WebSocket subscriptions.
6
+ */
7
+ export declare function createClient(token: string, endpoint?: string): Pipe2SDK;
8
+ export * from '../generated/sdk.js';
@@ -0,0 +1,49 @@
1
+ import { GraphQLClient } from 'graphql-request';
2
+ import { createClient as createWsClient } from 'graphql-ws';
3
+ import { print } from 'graphql';
4
+ import WebSocket from 'ws';
5
+ import { getSdk } from '../generated/sdk.js';
6
+ /**
7
+ * Create a Pipe2.ai SDK client with full support for queries, mutations,
8
+ * and real-time WebSocket subscriptions.
9
+ */
10
+ export function createClient(token, endpoint = 'https://api.pipe2.ai/v1/graphql') {
11
+ const headers = {};
12
+ if (token) {
13
+ headers.Authorization = `Bearer ${token}`;
14
+ }
15
+ // HTTP client for queries and mutations
16
+ const httpClient = new GraphQLClient(endpoint, { headers });
17
+ // WebSocket client for subscriptions (lazy — connects on first subscription)
18
+ const wsEndpoint = endpoint.replace(/^http/, 'ws');
19
+ let wsClient = null;
20
+ const getWsClient = () => {
21
+ if (!wsClient) {
22
+ wsClient = createWsClient({
23
+ url: wsEndpoint,
24
+ connectionParams: { headers },
25
+ webSocketImpl: WebSocket,
26
+ });
27
+ }
28
+ return wsClient;
29
+ };
30
+ const requester = (doc, vars) => {
31
+ const query = print(doc);
32
+ const isSubscription = doc.definitions.some((d) => d.kind === 'OperationDefinition' && d.operation === 'subscription');
33
+ if (isSubscription) {
34
+ // Return an async iterable for subscriptions
35
+ const ws = getWsClient();
36
+ return (async function* () {
37
+ const iterator = ws.iterate({ query, variables: vars });
38
+ for await (const result of iterator) {
39
+ if (result.data)
40
+ yield result.data;
41
+ }
42
+ })();
43
+ }
44
+ // Regular HTTP request for queries and mutations
45
+ return httpClient.request({ document: doc, variables: vars });
46
+ };
47
+ return getSdk(requester);
48
+ }
49
+ export * from '../generated/sdk.js';
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@pipe2-ai/sdk",
3
+ "version": "0.1.0",
4
+ "main": "dist/src/index.js",
5
+ "types": "dist/src/index.d.ts",
6
+ "files": ["dist"],
7
+ "scripts": {
8
+ "codegen": "graphql-codegen --config codegen.ts",
9
+ "build": "bun run codegen && tsc"
10
+ },
11
+ "dependencies": {
12
+ "graphql": "^16.0.0",
13
+ "graphql-request": "^7.0.0",
14
+ "graphql-ws": "^6.0.0",
15
+ "ws": "^8.0.0"
16
+ },
17
+ "devDependencies": {
18
+ "@graphql-codegen/cli": "^5.0.0",
19
+ "@graphql-codegen/typescript": "^4.0.0",
20
+ "@graphql-codegen/typescript-generic-sdk": "^4.0.0",
21
+ "@graphql-codegen/typescript-operations": "^4.0.0",
22
+ "@types/ws": "^8.0.0",
23
+ "typescript": "^5.0.0"
24
+ }
25
+ }