infrahub-sdk 0.0.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.
- package/.eslintrc.js +18 -0
- package/.prettierrc +6 -0
- package/LICENSE +201 -0
- package/README.md +47 -0
- package/dist/branch.d.ts +36 -0
- package/dist/branch.js +136 -0
- package/dist/cjs/index.js +65 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.js +35 -0
- package/dist/constants.d.ts +0 -0
- package/dist/constants.js +1 -0
- package/dist/esm/index.js +32 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/graphql.d.ts +31 -0
- package/dist/graphql.js +174 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +115 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +62 -0
- package/dist/types/client.d.ts +7 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/constants.d.ts +1 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/utils/auth.d.ts +1 -0
- package/dist/types/utils/auth.d.ts.map +1 -0
- package/dist/types/utils/error-handling.d.ts +1 -0
- package/dist/types/utils/error-handling.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils/validation.d.ts +1 -0
- package/dist/types/utils/validation.d.ts.map +1 -0
- package/dist/types.d.ts +3144 -0
- package/dist/types.js +6 -0
- package/dist/utils/auth.d.ts +0 -0
- package/dist/utils/auth.js +1 -0
- package/dist/utils/error-handling.d.ts +0 -0
- package/dist/utils/error-handling.js +1 -0
- package/dist/utils/index.d.ts +0 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/validation.d.ts +0 -0
- package/dist/utils/validation.js +1 -0
- package/eslint.config.mjs +9 -0
- package/jest.config.js +6 -0
- package/package.json +33 -0
- package/src/branch.ts +161 -0
- package/src/graphql.ts +266 -0
- package/src/index.test.ts +74 -0
- package/src/index.ts +144 -0
- package/src/types.ts +3169 -0
- package/tsconfig.json +14 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Utility type for safe result pattern
|
|
2
|
+
export type SafeResult<T> =
|
|
3
|
+
| { data: T; error: undefined }
|
|
4
|
+
| { data: undefined; error: Error };
|
|
5
|
+
|
|
6
|
+
import createClient from 'openapi-fetch';
|
|
7
|
+
import { GraphQLClient, Variables } from 'graphql-request';
|
|
8
|
+
import type { paths } from './types';
|
|
9
|
+
import { InfrahubBranchManager } from './branch';
|
|
10
|
+
|
|
11
|
+
// Re-export gql and all exports from graphql-request
|
|
12
|
+
export * from 'graphql-request';
|
|
13
|
+
|
|
14
|
+
export * from './types';
|
|
15
|
+
|
|
16
|
+
export interface InfrahubClientOptions {
|
|
17
|
+
address: string;
|
|
18
|
+
token?: string;
|
|
19
|
+
branch?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DEFAULT_BRANCH_NAME = 'main';
|
|
23
|
+
|
|
24
|
+
export class InfrahubClient {
|
|
25
|
+
public rest: ReturnType<typeof createClient<paths>>;
|
|
26
|
+
private graphqlClient: GraphQLClient;
|
|
27
|
+
private baseUrl: string;
|
|
28
|
+
private token?: string;
|
|
29
|
+
public defaultBranch: string;
|
|
30
|
+
public branch: InfrahubBranchManager;
|
|
31
|
+
|
|
32
|
+
constructor(options: InfrahubClientOptions) {
|
|
33
|
+
this.baseUrl = options.address;
|
|
34
|
+
this.token = options.token;
|
|
35
|
+
this.defaultBranch = options.branch || DEFAULT_BRANCH_NAME;
|
|
36
|
+
this.branch = new InfrahubBranchManager(this);
|
|
37
|
+
|
|
38
|
+
// Initialize the openapi-fetch client
|
|
39
|
+
|
|
40
|
+
this.rest = createClient<paths>({
|
|
41
|
+
baseUrl: this.baseUrl
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Use middleware to dynamically add the auth token to every REST request.
|
|
45
|
+
this.rest.use({
|
|
46
|
+
onRequest: (req) => {
|
|
47
|
+
req.headers.set('content-type', 'application/json');
|
|
48
|
+
if (this.token) {
|
|
49
|
+
req.headers.set('X-INFRAHUB-KEY', this.token);
|
|
50
|
+
}
|
|
51
|
+
return req;
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Initialize the GraphQL client with the default branch endpoint
|
|
56
|
+
this.graphqlClient = new GraphQLClient(
|
|
57
|
+
this._graphqlUrl(this.defaultBranch)
|
|
58
|
+
);
|
|
59
|
+
this.graphqlClient.setHeaders({
|
|
60
|
+
'Content-Type': 'application/json'
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (this.token) {
|
|
64
|
+
this.setAuthToken(this.token);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Executes a GraphQL query or mutation and returns a discriminated union for ergonomic error handling.
|
|
70
|
+
* @param query The GraphQL query string.
|
|
71
|
+
* @param variables Optional variables for the query.
|
|
72
|
+
* @param branchName Optional branch name to target a specific branch.
|
|
73
|
+
*/
|
|
74
|
+
public async safeExecuteGraphQL<
|
|
75
|
+
TData = any,
|
|
76
|
+
TVariables extends Variables = Variables
|
|
77
|
+
>(
|
|
78
|
+
query: string,
|
|
79
|
+
variables?: TVariables,
|
|
80
|
+
branchName?: string
|
|
81
|
+
): Promise<SafeResult<TData>> {
|
|
82
|
+
try {
|
|
83
|
+
const data = await this.executeGraphQL<TData, TVariables>(
|
|
84
|
+
query,
|
|
85
|
+
variables,
|
|
86
|
+
branchName
|
|
87
|
+
);
|
|
88
|
+
return { data, error: undefined };
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error instanceof Error) {
|
|
91
|
+
return { data: undefined, error };
|
|
92
|
+
}
|
|
93
|
+
return { data: undefined, error: new Error('Unknown error') };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns the GraphQL endpoint URL, optionally for a specific branch.
|
|
99
|
+
* @param branchName Optional branch name. Defaults to this.defaultBranch if not provided.
|
|
100
|
+
*/
|
|
101
|
+
private _graphqlUrl(branchName?: string): string {
|
|
102
|
+
let url = `${this.baseUrl}/graphql`;
|
|
103
|
+
const branch = branchName || this.defaultBranch;
|
|
104
|
+
if (branch) {
|
|
105
|
+
url += `/${branch}`;
|
|
106
|
+
}
|
|
107
|
+
return url;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public setAuthToken(token: string) {
|
|
111
|
+
this.token = token;
|
|
112
|
+
this.graphqlClient.setHeaders({
|
|
113
|
+
'X-INFRAHUB-KEY': token,
|
|
114
|
+
'Content-Type': 'application/json'
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Executes a GraphQL query or mutation against the Infrahub API.
|
|
120
|
+
* @param query The GraphQL query string.
|
|
121
|
+
* @param variables Optional variables for the query.
|
|
122
|
+
* @param branchName Optional branch name to target a specific branch.
|
|
123
|
+
*/
|
|
124
|
+
public async executeGraphQL<
|
|
125
|
+
TData = any,
|
|
126
|
+
TVariables extends Variables = Variables
|
|
127
|
+
>(
|
|
128
|
+
query: string,
|
|
129
|
+
variables?: TVariables,
|
|
130
|
+
branchName?: string
|
|
131
|
+
): Promise<TData> {
|
|
132
|
+
try {
|
|
133
|
+
this.graphqlClient.setEndpoint(this._graphqlUrl(branchName));
|
|
134
|
+
this.graphqlClient.setHeaders({
|
|
135
|
+
'X-INFRAHUB-KEY': this.token || '',
|
|
136
|
+
'Content-Type': 'application/json'
|
|
137
|
+
});
|
|
138
|
+
return await (this.graphqlClient.request as any)(query, variables);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error('Error executing GraphQL query:', error);
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|