infrahub-sdk 0.0.1 → 0.0.4

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,158 @@
1
+ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ import { parse } from 'graphql';
3
+ import { gql } from 'graphql-request';
4
+ import { InfrahubClient } from '..';
5
+
6
+ const InfrahubInfoDocument = parse(gql`
7
+ query InfrahubInfo {
8
+ InfrahubInfo {
9
+ version
10
+ }
11
+ }
12
+ `);
13
+
14
+ export interface InfrahubInfoResponse {
15
+ InfrahubInfo: {
16
+ version: string;
17
+ };
18
+ }
19
+
20
+ export const InfrahubInfoQuery: TypedDocumentNode<
21
+ InfrahubInfoResponse,
22
+ Record<any, never>
23
+ > = InfrahubInfoDocument;
24
+
25
+ const InfrahubUserDocument = parse(gql`
26
+ query GET_PROFILE_DETAILS {
27
+ AccountProfile {
28
+ id
29
+ display_label
30
+ account_type {
31
+ value
32
+ __typename
33
+ updated_at
34
+ }
35
+ status {
36
+ label
37
+ value
38
+ updated_at
39
+ __typename
40
+ }
41
+ description {
42
+ value
43
+ updated_at
44
+ __typename
45
+ }
46
+ label {
47
+ value
48
+ updated_at
49
+ __typename
50
+ }
51
+ member_of_groups {
52
+ count
53
+ edges {
54
+ node {
55
+ display_label
56
+ group_type {
57
+ value
58
+ }
59
+ ... on CoreAccountGroup {
60
+ id
61
+ roles {
62
+ count
63
+ edges {
64
+ node {
65
+ permissions {
66
+ count
67
+ edges {
68
+ node {
69
+ display_label
70
+ identifier {
71
+ value
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
79
+ display_label
80
+ }
81
+ }
82
+ }
83
+ }
84
+ __typename
85
+ name {
86
+ value
87
+ updated_at
88
+ __typename
89
+ }
90
+ }
91
+ }
92
+ `);
93
+
94
+ export interface InfrahubUserResponse {
95
+ AccountProfile: {
96
+ id: string;
97
+ display_label: string;
98
+ account_type: {
99
+ value: string;
100
+ __typename: string;
101
+ updated_at: string;
102
+ };
103
+ status: {
104
+ label: string;
105
+ value: string;
106
+ updated_at: string;
107
+ __typename: string;
108
+ };
109
+ description: {
110
+ value: string;
111
+ updated_at: string;
112
+ __typename: string;
113
+ };
114
+ label: {
115
+ value: string;
116
+ updated_at: string;
117
+ __typename: string;
118
+ };
119
+ member_of_groups: {
120
+ count: number;
121
+ edges: Array<{
122
+ node: {
123
+ display_label: string;
124
+ group_type: { value: string };
125
+ id?: string; // Optional for CoreAccountGroup
126
+ roles?: {
127
+ count: number;
128
+ edges: Array<{
129
+ node: {
130
+ permissions?: {
131
+ count: number;
132
+ edges: Array<{
133
+ node: {
134
+ display_label: string;
135
+ identifier?: { value: string };
136
+ };
137
+ }>;
138
+ };
139
+ };
140
+ }>;
141
+ };
142
+ };
143
+ }>;
144
+ __typename?: string; // Optional, depending on the API response
145
+ };
146
+ name: {
147
+ value: string;
148
+ updated_at: string;
149
+ __typename?: string; // Optional, depending on the API response
150
+ };
151
+ };
152
+ }
153
+
154
+ export const InfrahubUserQuery: TypedDocumentNode<
155
+ InfrahubUserResponse,
156
+ Record<any, never>
157
+ > = InfrahubUserDocument;
158
+
package/src/index.ts CHANGED
@@ -3,10 +3,18 @@ export type SafeResult<T> =
3
3
  | { data: T; error: undefined }
4
4
  | { data: undefined; error: Error };
5
5
 
6
+ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
7
+
6
8
  import createClient from 'openapi-fetch';
7
- import { GraphQLClient, Variables } from 'graphql-request';
9
+ import { GraphQLClient, Variables, gql } from 'graphql-request';
8
10
  import type { paths } from './types';
9
11
  import { InfrahubBranchManager } from './branch';
12
+ import {
13
+ InfrahubInfoQuery,
14
+ InfrahubInfoResponse,
15
+ InfrahubUserResponse,
16
+ InfrahubUserQuery
17
+ } from './graphql/client';
10
18
 
11
19
  // Re-export gql and all exports from graphql-request
12
20
  export * from 'graphql-request';
@@ -125,7 +133,7 @@ export class InfrahubClient {
125
133
  TData = any,
126
134
  TVariables extends Variables = Variables
127
135
  >(
128
- query: string,
136
+ query: string | TypedDocumentNode<TData, TVariables>,
129
137
  variables?: TVariables,
130
138
  branchName?: string
131
139
  ): Promise<TData> {
@@ -141,4 +149,27 @@ export class InfrahubClient {
141
149
  throw error;
142
150
  }
143
151
  }
152
+
153
+ public async getVersion(): Promise<string> {
154
+ const data = await this.executeGraphQL<
155
+ InfrahubInfoResponse,
156
+ Record<any, never>
157
+ >(InfrahubInfoQuery);
158
+ return data.InfrahubInfo.version;
159
+ }
160
+
161
+ public async getUser(): Promise<InfrahubUserResponse> {
162
+ const data = await this.executeGraphQL<
163
+ InfrahubUserResponse,
164
+ Record<any, never>
165
+ >(InfrahubUserQuery);
166
+ return data;
167
+ }
168
+
169
+ public async getUserPermissions(): Promise<
170
+ InfrahubUserResponse['AccountProfile']['member_of_groups']['edges']
171
+ > {
172
+ const user = await this.getUser();
173
+ return user.AccountProfile.member_of_groups.edges;
174
+ }
144
175
  }
package/test/main.js ADDED
@@ -0,0 +1,11 @@
1
+ import { InfrahubClient } from 'infrahub-sdk';
2
+
3
+ const options = {
4
+ address: "https://demo.infrahub.app",
5
+ token: "your-api-token"
6
+ }
7
+
8
+ console.log('Executing GraphQL query...');
9
+
10
+ const client = new InfrahubClient(options);
11
+ await client.getVersion();