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,22 @@
1
+ name: Publish Package to npmjs
2
+ on:
3
+ release:
4
+ types: [published]
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ permissions:
9
+ contents: read
10
+ id-token: write
11
+ steps:
12
+ - uses: actions/checkout@v4
13
+ # Setup .npmrc file to publish to npm
14
+ - uses: actions/setup-node@v4
15
+ with:
16
+ node-version: '20.x'
17
+ registry-url: 'https://registry.npmjs.org'
18
+ - run: npm ci
19
+ - run: npm run build
20
+ - run: npm publish
21
+ env:
22
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
package/dist/branch.d.ts CHANGED
@@ -1,20 +1,5 @@
1
1
  import { InfrahubClient } from './index';
2
- interface Branch {
3
- id: string;
4
- name: string;
5
- description: string;
6
- origin_branch: string;
7
- branched_from: string;
8
- is_default: boolean;
9
- sync_with_git: boolean;
10
- has_schema_changes: boolean;
11
- }
12
- export interface BranchCreateInput {
13
- name: string;
14
- description?: string;
15
- sync_with_git?: boolean;
16
- wait_until_completion?: boolean;
17
- }
2
+ import { BranchResponse, BranchCreateInput } from './graphql/branch';
18
3
  export declare class BranchNotFoundError extends Error {
19
4
  constructor(identifier: string);
20
5
  }
@@ -24,13 +9,12 @@ export declare class InfrahubBranchManager {
24
9
  /**
25
10
  * Fetches all branches using GraphQL and returns an object mapping branch names to branch data.
26
11
  */
27
- all(): Promise<Record<string, Branch>>;
12
+ all(): Promise<Record<string, BranchResponse>>;
28
13
  /**
29
14
  * Fetches a specific branch by name.
30
15
  * @param branchName The name of the branch to fetch.
31
16
  */
32
- get(branchName: string): Promise<Branch | null>;
33
- create(input: BranchCreateInput): Promise<Branch>;
17
+ get(branchName: string): Promise<BranchResponse | null>;
18
+ create(input: BranchCreateInput): Promise<BranchResponse>;
34
19
  delete(branchName: string): Promise<boolean>;
35
20
  }
36
- export {};
package/dist/branch.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InfrahubBranchManager = exports.BranchNotFoundError = void 0;
4
- const index_1 = require("./index");
5
4
  const graphql_1 = require("./graphql");
5
+ const branch_1 = require("./graphql/branch");
6
6
  class BranchNotFoundError extends Error {
7
7
  constructor(identifier) {
8
8
  super(`Branch not found: ${identifier}`);
@@ -33,26 +33,12 @@ class InfrahubBranchManager {
33
33
  * Fetches all branches using GraphQL and returns an object mapping branch names to branch data.
34
34
  */
35
35
  async all() {
36
- const query = (0, index_1.gql) `
37
- query BranchQuery {
38
- Branch {
39
- id
40
- name
41
- description
42
- origin_branch
43
- branched_from
44
- is_default
45
- sync_with_git
46
- has_schema_changes
47
- }
48
- }
49
- `;
50
- const response = await this.client.executeGraphQL(query);
36
+ const response = await this.client.executeGraphQL(branch_1.BranchAllQuery);
51
37
  const branches = response.Branch || [];
52
38
  const result = {};
53
- for (const branch of branches) {
39
+ branches.forEach((branch) => {
54
40
  result[branch.name] = branch;
55
- }
41
+ });
56
42
  return result;
57
43
  }
58
44
  /**
@@ -60,21 +46,7 @@ class InfrahubBranchManager {
60
46
  * @param branchName The name of the branch to fetch.
61
47
  */
62
48
  async get(branchName) {
63
- const query = (0, index_1.gql) `
64
- query BranchQuery($name: String!) {
65
- Branch(name: $name) {
66
- id
67
- name
68
- description
69
- origin_branch
70
- branched_from
71
- is_default
72
- sync_with_git
73
- has_schema_changes
74
- }
75
- }
76
- `;
77
- const response = await this.client.executeGraphQL(query, {
49
+ const response = await this.client.executeGraphQL(branch_1.BranchGetQuery, {
78
50
  name: branchName
79
51
  });
80
52
  const branchArr = response.Branch || [];
@@ -102,11 +74,8 @@ class InfrahubBranchManager {
102
74
  query: mutationQuery,
103
75
  name: 'CreateBranch'
104
76
  });
105
- // You may want to remove this log in production
106
77
  console.log(mutation.render());
107
- const response = await this.client.executeGraphQL(mutation.render(), {
108
- input: inputData
109
- });
78
+ const response = await this.client.executeGraphQL(mutation.render());
110
79
  // Adjust the response path as needed based on your API
111
80
  const branchData = response.BranchCreate?.object || response.BranchCreate?.task || null;
112
81
  if (!branchData) {
@@ -0,0 +1,23 @@
1
+ import { TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ export interface BranchCreateInput {
3
+ name: string;
4
+ description?: string;
5
+ sync_with_git?: boolean;
6
+ wait_until_completion?: boolean;
7
+ }
8
+ export interface BranchResponse {
9
+ id: string;
10
+ name: string;
11
+ description: string;
12
+ origin_branch: string;
13
+ branched_from: string;
14
+ is_default: boolean;
15
+ sync_with_git: boolean;
16
+ has_schema_changes: boolean;
17
+ }
18
+ export declare const BranchAllQuery: TypedDocumentNode<{
19
+ Branch: BranchResponse[];
20
+ }, Record<any, never>>;
21
+ export declare const BranchGetQuery: TypedDocumentNode<{
22
+ Branch: BranchResponse[];
23
+ }, Record<any, any>>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BranchGetQuery = exports.BranchAllQuery = void 0;
4
+ const graphql_request_1 = require("graphql-request");
5
+ const graphql_1 = require("graphql");
6
+ const BranchAllQueryDocument = (0, graphql_1.parse)((0, graphql_request_1.gql) `
7
+ query BranchAllQuery {
8
+ Branch {
9
+ id
10
+ name
11
+ description
12
+ origin_branch
13
+ branched_from
14
+ is_default
15
+ sync_with_git
16
+ has_schema_changes
17
+ }
18
+ }
19
+ `);
20
+ const BranchGetQueryDocument = (0, graphql_1.parse)((0, graphql_request_1.gql) `
21
+ query BranchGetQuery($name: String!) {
22
+ Branch(name: $name) {
23
+ id
24
+ name
25
+ description
26
+ origin_branch
27
+ branched_from
28
+ is_default
29
+ sync_with_git
30
+ has_schema_changes
31
+ }
32
+ }
33
+ `);
34
+ exports.BranchAllQuery = BranchAllQueryDocument;
35
+ exports.BranchGetQuery = BranchGetQueryDocument;
@@ -0,0 +1,71 @@
1
+ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
2
+ export interface InfrahubInfoResponse {
3
+ InfrahubInfo: {
4
+ version: string;
5
+ };
6
+ }
7
+ export declare const InfrahubInfoQuery: TypedDocumentNode<InfrahubInfoResponse, Record<any, never>>;
8
+ export interface InfrahubUserResponse {
9
+ AccountProfile: {
10
+ id: string;
11
+ display_label: string;
12
+ account_type: {
13
+ value: string;
14
+ __typename: string;
15
+ updated_at: string;
16
+ };
17
+ status: {
18
+ label: string;
19
+ value: string;
20
+ updated_at: string;
21
+ __typename: string;
22
+ };
23
+ description: {
24
+ value: string;
25
+ updated_at: string;
26
+ __typename: string;
27
+ };
28
+ label: {
29
+ value: string;
30
+ updated_at: string;
31
+ __typename: string;
32
+ };
33
+ member_of_groups: {
34
+ count: number;
35
+ edges: Array<{
36
+ node: {
37
+ display_label: string;
38
+ group_type: {
39
+ value: string;
40
+ };
41
+ id?: string;
42
+ roles?: {
43
+ count: number;
44
+ edges: Array<{
45
+ node: {
46
+ permissions?: {
47
+ count: number;
48
+ edges: Array<{
49
+ node: {
50
+ display_label: string;
51
+ identifier?: {
52
+ value: string;
53
+ };
54
+ };
55
+ }>;
56
+ };
57
+ };
58
+ }>;
59
+ };
60
+ };
61
+ }>;
62
+ __typename?: string;
63
+ };
64
+ name: {
65
+ value: string;
66
+ updated_at: string;
67
+ __typename?: string;
68
+ };
69
+ };
70
+ }
71
+ export declare const InfrahubUserQuery: TypedDocumentNode<InfrahubUserResponse, Record<any, never>>;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InfrahubUserQuery = exports.InfrahubInfoQuery = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const graphql_request_1 = require("graphql-request");
6
+ const InfrahubInfoDocument = (0, graphql_1.parse)((0, graphql_request_1.gql) `
7
+ query InfrahubInfo {
8
+ InfrahubInfo {
9
+ version
10
+ }
11
+ }
12
+ `);
13
+ exports.InfrahubInfoQuery = InfrahubInfoDocument;
14
+ const InfrahubUserDocument = (0, graphql_1.parse)((0, graphql_request_1.gql) `
15
+ query GET_PROFILE_DETAILS {
16
+ AccountProfile {
17
+ id
18
+ display_label
19
+ account_type {
20
+ value
21
+ __typename
22
+ updated_at
23
+ }
24
+ status {
25
+ label
26
+ value
27
+ updated_at
28
+ __typename
29
+ }
30
+ description {
31
+ value
32
+ updated_at
33
+ __typename
34
+ }
35
+ label {
36
+ value
37
+ updated_at
38
+ __typename
39
+ }
40
+ member_of_groups {
41
+ count
42
+ edges {
43
+ node {
44
+ display_label
45
+ group_type {
46
+ value
47
+ }
48
+ ... on CoreAccountGroup {
49
+ id
50
+ roles {
51
+ count
52
+ edges {
53
+ node {
54
+ permissions {
55
+ count
56
+ edges {
57
+ node {
58
+ display_label
59
+ identifier {
60
+ value
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+ display_label
69
+ }
70
+ }
71
+ }
72
+ }
73
+ __typename
74
+ name {
75
+ value
76
+ updated_at
77
+ __typename
78
+ }
79
+ }
80
+ }
81
+ `);
82
+ exports.InfrahubUserQuery = InfrahubUserDocument;
package/dist/index.d.ts CHANGED
@@ -5,10 +5,12 @@ export type SafeResult<T> = {
5
5
  data: undefined;
6
6
  error: Error;
7
7
  };
8
+ import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
8
9
  import createClient from 'openapi-fetch';
9
10
  import { Variables } from 'graphql-request';
10
11
  import type { paths } from './types';
11
12
  import { InfrahubBranchManager } from './branch';
13
+ import { InfrahubUserResponse } from './graphql/client';
12
14
  export * from 'graphql-request';
13
15
  export * from './types';
14
16
  export interface InfrahubClientOptions {
@@ -43,5 +45,8 @@ export declare class InfrahubClient {
43
45
  * @param variables Optional variables for the query.
44
46
  * @param branchName Optional branch name to target a specific branch.
45
47
  */
46
- executeGraphQL<TData = any, TVariables extends Variables = Variables>(query: string, variables?: TVariables, branchName?: string): Promise<TData>;
48
+ executeGraphQL<TData = any, TVariables extends Variables = Variables>(query: string | TypedDocumentNode<TData, TVariables>, variables?: TVariables, branchName?: string): Promise<TData>;
49
+ getVersion(): Promise<string>;
50
+ getUser(): Promise<InfrahubUserResponse>;
51
+ getUserPermissions(): Promise<InfrahubUserResponse['AccountProfile']['member_of_groups']['edges']>;
47
52
  }
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ exports.InfrahubClient = void 0;
21
21
  const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
22
22
  const graphql_request_1 = require("graphql-request");
23
23
  const branch_1 = require("./branch");
24
+ const client_1 = require("./graphql/client");
24
25
  // Re-export gql and all exports from graphql-request
25
26
  __exportStar(require("graphql-request"), exports);
26
27
  __exportStar(require("./types"), exports);
@@ -111,5 +112,17 @@ class InfrahubClient {
111
112
  throw error;
112
113
  }
113
114
  }
115
+ async getVersion() {
116
+ const data = await this.executeGraphQL(client_1.InfrahubInfoQuery);
117
+ return data.InfrahubInfo.version;
118
+ }
119
+ async getUser() {
120
+ const data = await this.executeGraphQL(client_1.InfrahubUserQuery);
121
+ return data;
122
+ }
123
+ async getUserPermissions() {
124
+ const user = await this.getUser();
125
+ return user.AccountProfile.member_of_groups.edges;
126
+ }
114
127
  }
115
128
  exports.InfrahubClient = InfrahubClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "infrahub-sdk",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "A client SDK for the Infrahub API.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -17,6 +17,7 @@
17
17
  "author": "",
18
18
  "license": "Apache-2.0",
19
19
  "dependencies": {
20
+ "@graphql-codegen/typed-document-node": "^5.1.2",
20
21
  "graphql-request": "^6.1.0",
21
22
  "openapi-fetch": "^0.9.4"
22
23
  },
package/src/branch.ts CHANGED
@@ -1,23 +1,11 @@
1
1
  import { InfrahubClient, gql } from './index';
2
2
  import { Mutation } from './graphql';
3
-
4
- interface Branch {
5
- id: string;
6
- name: string;
7
- description: string;
8
- origin_branch: string;
9
- branched_from: string;
10
- is_default: boolean;
11
- sync_with_git: boolean;
12
- has_schema_changes: boolean;
13
- }
14
-
15
- export interface BranchCreateInput {
16
- name: string;
17
- description?: string;
18
- sync_with_git?: boolean;
19
- wait_until_completion?: boolean;
20
- }
3
+ import {
4
+ BranchAllQuery,
5
+ BranchResponse,
6
+ BranchGetQuery,
7
+ BranchCreateInput
8
+ } from './graphql/branch';
21
9
 
22
10
  export class BranchNotFoundError extends Error {
23
11
  constructor(identifier: string) {
@@ -52,27 +40,18 @@ export class InfrahubBranchManager {
52
40
  /**
53
41
  * Fetches all branches using GraphQL and returns an object mapping branch names to branch data.
54
42
  */
55
- async all(): Promise<Record<string, Branch>> {
56
- const query = gql`
57
- query BranchQuery {
58
- Branch {
59
- id
60
- name
61
- description
62
- origin_branch
63
- branched_from
64
- is_default
65
- sync_with_git
66
- has_schema_changes
67
- }
68
- }
69
- `;
70
- const response = await this.client.executeGraphQL(query);
71
- const branches: Branch[] = response.Branch || [];
72
- const result: Record<string, Branch> = {};
73
- for (const branch of branches) {
43
+ async all(): Promise<Record<string, BranchResponse>> {
44
+ const response = await this.client.executeGraphQL<
45
+ {
46
+ Branch: BranchResponse[];
47
+ },
48
+ Record<string, never>
49
+ >(BranchAllQuery);
50
+ const branches: BranchResponse[] = response.Branch || [];
51
+ const result: Record<string, BranchResponse> = {};
52
+ branches.forEach((branch) => {
74
53
  result[branch.name] = branch;
75
- }
54
+ });
76
55
  return result;
77
56
  }
78
57
 
@@ -80,32 +59,23 @@ export class InfrahubBranchManager {
80
59
  * Fetches a specific branch by name.
81
60
  * @param branchName The name of the branch to fetch.
82
61
  */
83
- async get(branchName: string): Promise<Branch | null> {
84
- const query = gql`
85
- query BranchQuery($name: String!) {
86
- Branch(name: $name) {
87
- id
88
- name
89
- description
90
- origin_branch
91
- branched_from
92
- is_default
93
- sync_with_git
94
- has_schema_changes
95
- }
96
- }
97
- `;
98
- const response = await this.client.executeGraphQL(query, {
62
+ async get(branchName: string): Promise<BranchResponse | null> {
63
+ const response = await this.client.executeGraphQL<
64
+ {
65
+ Branch: BranchResponse[];
66
+ },
67
+ Record<string, any>
68
+ >(BranchGetQuery, {
99
69
  name: branchName
100
70
  });
101
- const branchArr: Branch[] = response.Branch || [];
71
+ const branchArr: BranchResponse[] = response.Branch || [];
102
72
  if (!branchArr.length) {
103
73
  throw new BranchNotFoundError(branchName);
104
74
  }
105
75
  return branchArr[0];
106
76
  }
107
77
 
108
- async create(input: BranchCreateInput): Promise<Branch> {
78
+ async create(input: BranchCreateInput): Promise<BranchResponse> {
109
79
  const inputData = {
110
80
  background_execution: input.wait_until_completion,
111
81
  data: {
@@ -126,18 +96,17 @@ export class InfrahubBranchManager {
126
96
  query: mutationQuery,
127
97
  name: 'CreateBranch'
128
98
  });
129
- // You may want to remove this log in production
99
+
130
100
  console.log(mutation.render());
131
- const response = await this.client.executeGraphQL(mutation.render(), {
132
- input: inputData
133
- });
101
+
102
+ const response = await this.client.executeGraphQL(mutation.render());
134
103
  // Adjust the response path as needed based on your API
135
104
  const branchData =
136
105
  response.BranchCreate?.object || response.BranchCreate?.task || null;
137
106
  if (!branchData) {
138
107
  throw new Error('Branch creation failed');
139
108
  }
140
- return branchData as Branch;
109
+ return branchData as BranchResponse;
141
110
  }
142
111
 
143
112
  async delete(branchName: string): Promise<boolean> {
@@ -0,0 +1,61 @@
1
+ import { gql } from 'graphql-request';
2
+ import { parse } from 'graphql';
3
+ import { TypedDocumentNode } from '@graphql-typed-document-node/core';
4
+
5
+ const BranchAllQueryDocument = parse(gql`
6
+ query BranchAllQuery {
7
+ Branch {
8
+ id
9
+ name
10
+ description
11
+ origin_branch
12
+ branched_from
13
+ is_default
14
+ sync_with_git
15
+ has_schema_changes
16
+ }
17
+ }
18
+ `);
19
+
20
+ const BranchGetQueryDocument = parse(gql`
21
+ query BranchGetQuery($name: String!) {
22
+ Branch(name: $name) {
23
+ id
24
+ name
25
+ description
26
+ origin_branch
27
+ branched_from
28
+ is_default
29
+ sync_with_git
30
+ has_schema_changes
31
+ }
32
+ }
33
+ `);
34
+
35
+ export interface BranchCreateInput {
36
+ name: string;
37
+ description?: string;
38
+ sync_with_git?: boolean;
39
+ wait_until_completion?: boolean;
40
+ }
41
+
42
+ export interface BranchResponse {
43
+ id: string;
44
+ name: string;
45
+ description: string;
46
+ origin_branch: string;
47
+ branched_from: string;
48
+ is_default: boolean;
49
+ sync_with_git: boolean;
50
+ has_schema_changes: boolean;
51
+ }
52
+
53
+ export const BranchAllQuery: TypedDocumentNode<
54
+ { Branch: BranchResponse[] },
55
+ Record<any, never>
56
+ > = BranchAllQueryDocument;
57
+
58
+ export const BranchGetQuery: TypedDocumentNode<
59
+ { Branch: BranchResponse[] },
60
+ Record<any, any>
61
+ > = BranchGetQueryDocument;