infrahub-sdk 0.0.1 → 0.0.2
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/.github/workflows/publish.yml +21 -0
- package/package.json +1 -1
- package/src/index.ts +0 -3
- package/dist/branch.d.ts +0 -36
- package/dist/branch.js +0 -136
- package/dist/cjs/index.js +0 -65
- package/dist/cjs/index.js.map +0 -7
- package/dist/client.d.ts +0 -6
- package/dist/client.js +0 -35
- package/dist/constants.d.ts +0 -0
- package/dist/constants.js +0 -1
- package/dist/esm/index.js +0 -32
- package/dist/esm/index.js.map +0 -7
- package/dist/graphql.d.ts +0 -31
- package/dist/graphql.js +0 -174
- package/dist/index.d.ts +0 -47
- package/dist/index.js +0 -115
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -62
- package/dist/types/client.d.ts +0 -7
- package/dist/types/client.d.ts.map +0 -1
- package/dist/types/constants.d.ts +0 -1
- package/dist/types/constants.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/utils/auth.d.ts +0 -1
- package/dist/types/utils/auth.d.ts.map +0 -1
- package/dist/types/utils/error-handling.d.ts +0 -1
- package/dist/types/utils/error-handling.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts +0 -1
- package/dist/types/utils/index.d.ts.map +0 -1
- package/dist/types/utils/validation.d.ts +0 -1
- package/dist/types/utils/validation.d.ts.map +0 -1
- package/dist/types.d.ts +0 -3144
- package/dist/types.js +0 -6
- package/dist/utils/auth.d.ts +0 -0
- package/dist/utils/auth.js +0 -1
- package/dist/utils/error-handling.d.ts +0 -0
- package/dist/utils/error-handling.js +0 -1
- package/dist/utils/index.d.ts +0 -0
- package/dist/utils/index.js +0 -1
- package/dist/utils/validation.d.ts +0 -0
- package/dist/utils/validation.js +0 -1
- package/src/branch.ts +0 -161
- package/src/graphql.ts +0 -266
package/dist/index.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.InfrahubClient = void 0;
|
|
21
|
-
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
|
|
22
|
-
const graphql_request_1 = require("graphql-request");
|
|
23
|
-
const branch_1 = require("./branch");
|
|
24
|
-
// Re-export gql and all exports from graphql-request
|
|
25
|
-
__exportStar(require("graphql-request"), exports);
|
|
26
|
-
__exportStar(require("./types"), exports);
|
|
27
|
-
const DEFAULT_BRANCH_NAME = 'main';
|
|
28
|
-
class InfrahubClient {
|
|
29
|
-
constructor(options) {
|
|
30
|
-
this.baseUrl = options.address;
|
|
31
|
-
this.token = options.token;
|
|
32
|
-
this.defaultBranch = options.branch || DEFAULT_BRANCH_NAME;
|
|
33
|
-
this.branch = new branch_1.InfrahubBranchManager(this);
|
|
34
|
-
// Initialize the openapi-fetch client
|
|
35
|
-
this.rest = (0, openapi_fetch_1.default)({
|
|
36
|
-
baseUrl: this.baseUrl
|
|
37
|
-
});
|
|
38
|
-
// Use middleware to dynamically add the auth token to every REST request.
|
|
39
|
-
this.rest.use({
|
|
40
|
-
onRequest: (req) => {
|
|
41
|
-
req.headers.set('content-type', 'application/json');
|
|
42
|
-
if (this.token) {
|
|
43
|
-
req.headers.set('X-INFRAHUB-KEY', this.token);
|
|
44
|
-
}
|
|
45
|
-
return req;
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
// Initialize the GraphQL client with the default branch endpoint
|
|
49
|
-
this.graphqlClient = new graphql_request_1.GraphQLClient(this._graphqlUrl(this.defaultBranch));
|
|
50
|
-
this.graphqlClient.setHeaders({
|
|
51
|
-
'Content-Type': 'application/json'
|
|
52
|
-
});
|
|
53
|
-
if (this.token) {
|
|
54
|
-
this.setAuthToken(this.token);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Executes a GraphQL query or mutation and returns a discriminated union for ergonomic error handling.
|
|
59
|
-
* @param query The GraphQL query string.
|
|
60
|
-
* @param variables Optional variables for the query.
|
|
61
|
-
* @param branchName Optional branch name to target a specific branch.
|
|
62
|
-
*/
|
|
63
|
-
async safeExecuteGraphQL(query, variables, branchName) {
|
|
64
|
-
try {
|
|
65
|
-
const data = await this.executeGraphQL(query, variables, branchName);
|
|
66
|
-
return { data, error: undefined };
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
if (error instanceof Error) {
|
|
70
|
-
return { data: undefined, error };
|
|
71
|
-
}
|
|
72
|
-
return { data: undefined, error: new Error('Unknown error') };
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Returns the GraphQL endpoint URL, optionally for a specific branch.
|
|
77
|
-
* @param branchName Optional branch name. Defaults to this.defaultBranch if not provided.
|
|
78
|
-
*/
|
|
79
|
-
_graphqlUrl(branchName) {
|
|
80
|
-
let url = `${this.baseUrl}/graphql`;
|
|
81
|
-
const branch = branchName || this.defaultBranch;
|
|
82
|
-
if (branch) {
|
|
83
|
-
url += `/${branch}`;
|
|
84
|
-
}
|
|
85
|
-
return url;
|
|
86
|
-
}
|
|
87
|
-
setAuthToken(token) {
|
|
88
|
-
this.token = token;
|
|
89
|
-
this.graphqlClient.setHeaders({
|
|
90
|
-
'X-INFRAHUB-KEY': token,
|
|
91
|
-
'Content-Type': 'application/json'
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Executes a GraphQL query or mutation against the Infrahub API.
|
|
96
|
-
* @param query The GraphQL query string.
|
|
97
|
-
* @param variables Optional variables for the query.
|
|
98
|
-
* @param branchName Optional branch name to target a specific branch.
|
|
99
|
-
*/
|
|
100
|
-
async executeGraphQL(query, variables, branchName) {
|
|
101
|
-
try {
|
|
102
|
-
this.graphqlClient.setEndpoint(this._graphqlUrl(branchName));
|
|
103
|
-
this.graphqlClient.setHeaders({
|
|
104
|
-
'X-INFRAHUB-KEY': this.token || '',
|
|
105
|
-
'Content-Type': 'application/json'
|
|
106
|
-
});
|
|
107
|
-
return await this.graphqlClient.request(query, variables);
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
console.error('Error executing GraphQL query:', error);
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
exports.InfrahubClient = InfrahubClient;
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const index_1 = require("./index");
|
|
4
|
-
const graphql_request_1 = require("graphql-request");
|
|
5
|
-
// Import expect for type checking and editor support
|
|
6
|
-
const globals_1 = require("@jest/globals");
|
|
7
|
-
(0, globals_1.describe)('InfrahubClient', () => {
|
|
8
|
-
const baseURL = 'https://example.com';
|
|
9
|
-
const token = 'test-token';
|
|
10
|
-
let client;
|
|
11
|
-
// Initialize the client before each test
|
|
12
|
-
(0, globals_1.beforeEach)(() => {
|
|
13
|
-
const options = {
|
|
14
|
-
address: baseURL,
|
|
15
|
-
token: token
|
|
16
|
-
};
|
|
17
|
-
client = new index_1.InfrahubClient(options);
|
|
18
|
-
});
|
|
19
|
-
(0, globals_1.it)('should initialize with baseURL and token', () => {
|
|
20
|
-
(0, globals_1.expect)(client.baseUrl).toBe(baseURL);
|
|
21
|
-
(0, globals_1.expect)(client.token).toBe(token);
|
|
22
|
-
});
|
|
23
|
-
(0, globals_1.it)('should set auth token and update GraphQL headers', () => {
|
|
24
|
-
const newToken = 'new-token';
|
|
25
|
-
client.setAuthToken(newToken);
|
|
26
|
-
(0, globals_1.expect)(client.token).toBe(newToken);
|
|
27
|
-
// GraphQLClient headers are private, so we check no error is thrown
|
|
28
|
-
(0, globals_1.expect)(() => client.executeGraphQL('{ __typename }')).not.toThrow();
|
|
29
|
-
});
|
|
30
|
-
(0, globals_1.it)('should call graphqlQuery and handle errors', async () => {
|
|
31
|
-
const mockRequest = globals_1.jest
|
|
32
|
-
.fn()
|
|
33
|
-
.mockImplementationOnce(() => Promise.reject(new Error('GraphQL error')));
|
|
34
|
-
client.graphqlClient.request = mockRequest;
|
|
35
|
-
const consoleErrorSpy = globals_1.jest
|
|
36
|
-
.spyOn(console, 'error')
|
|
37
|
-
.mockImplementation(() => { });
|
|
38
|
-
await (0, globals_1.expect)(client.executeGraphQL('{ test }')).rejects.toThrow('GraphQL error');
|
|
39
|
-
consoleErrorSpy.mockRestore();
|
|
40
|
-
});
|
|
41
|
-
(0, globals_1.it)('should initialize rest client with baseUrl', () => {
|
|
42
|
-
(0, globals_1.expect)(client.rest).toBeDefined();
|
|
43
|
-
});
|
|
44
|
-
(0, globals_1.it)('should have a GraphQL client initialized', () => {
|
|
45
|
-
(0, globals_1.expect)(client.graphqlClient).toBeInstanceOf(graphql_request_1.GraphQLClient);
|
|
46
|
-
});
|
|
47
|
-
(0, globals_1.it)('should have a rest client initialized', () => {
|
|
48
|
-
(0, globals_1.expect)(client.rest).toBeDefined();
|
|
49
|
-
});
|
|
50
|
-
(0, globals_1.it)('should have a default branch set', () => {
|
|
51
|
-
(0, globals_1.expect)(client.defaultBranch).toBe('main');
|
|
52
|
-
});
|
|
53
|
-
(0, globals_1.it)('should have a graphqlUrl method that returns the correct URL', () => {
|
|
54
|
-
const url = client._graphqlUrl();
|
|
55
|
-
(0, globals_1.expect)(url).toBe(`${baseURL}/graphql/main`);
|
|
56
|
-
});
|
|
57
|
-
(0, globals_1.it)('graphqlUrl should return the correct URL for a specific branch', () => {
|
|
58
|
-
const branchName = 'test-branch';
|
|
59
|
-
const url = client._graphqlUrl(branchName);
|
|
60
|
-
(0, globals_1.expect)(url).toBe(`${baseURL}/graphql/${branchName}`);
|
|
61
|
-
});
|
|
62
|
-
});
|
package/dist/types/client.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAEA,qBAAa,cAAc;IACvB,OAAO,CAAC,aAAa,CAAgB;gBAEzB,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM;IAapC,YAAY,CAAC,KAAK,EAAE,MAAM;IAKpB,OAAO;CASvB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":""}
|
package/dist/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../src/utils/auth.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=error-handling.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error-handling.d.ts","sourceRoot":"","sources":["../../../src/utils/error-handling.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/utils/validation.ts"],"names":[],"mappings":""}
|