@stepzen/sdk 0.0.0-experimental-20221114-610843f6

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.
Files changed (113) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -0
  3. package/lib/client-v2.d.ts +50 -0
  4. package/lib/client-v2.d.ts.map +1 -0
  5. package/lib/client-v2.js +91 -0
  6. package/lib/client-v2.js.map +1 -0
  7. package/lib/client.d.ts +33 -0
  8. package/lib/client.d.ts.map +1 -0
  9. package/lib/client.js +95 -0
  10. package/lib/client.js.map +1 -0
  11. package/lib/commands/account.d.ts +10 -0
  12. package/lib/commands/account.d.ts.map +1 -0
  13. package/lib/commands/account.js +39 -0
  14. package/lib/commands/account.js.map +1 -0
  15. package/lib/commands/authenticate.d.ts +4 -0
  16. package/lib/commands/authenticate.d.ts.map +1 -0
  17. package/lib/commands/authenticate.js +25 -0
  18. package/lib/commands/authenticate.js.map +1 -0
  19. package/lib/commands/deploy.d.ts +4 -0
  20. package/lib/commands/deploy.d.ts.map +1 -0
  21. package/lib/commands/deploy.js +46 -0
  22. package/lib/commands/deploy.js.map +1 -0
  23. package/lib/commands/getPublicAccount.d.ts +7 -0
  24. package/lib/commands/getPublicAccount.d.ts.map +1 -0
  25. package/lib/commands/getPublicAccount.js +59 -0
  26. package/lib/commands/getPublicAccount.js.map +1 -0
  27. package/lib/commands/list.d.ts +4 -0
  28. package/lib/commands/list.d.ts.map +1 -0
  29. package/lib/commands/list.js +29 -0
  30. package/lib/commands/list.js.map +1 -0
  31. package/lib/commands/upload.d.ts +4 -0
  32. package/lib/commands/upload.d.ts.map +1 -0
  33. package/lib/commands/upload.js +64 -0
  34. package/lib/commands/upload.js.map +1 -0
  35. package/lib/commands-v2/account.d.ts +9 -0
  36. package/lib/commands-v2/account.d.ts.map +1 -0
  37. package/lib/commands-v2/account.js +68 -0
  38. package/lib/commands-v2/account.js.map +1 -0
  39. package/lib/commands-v2/deploy.d.ts +15 -0
  40. package/lib/commands-v2/deploy.d.ts.map +1 -0
  41. package/lib/commands-v2/deploy.js +90 -0
  42. package/lib/commands-v2/deploy.js.map +1 -0
  43. package/lib/commands-v2/getPublicAccount.d.ts +8 -0
  44. package/lib/commands-v2/getPublicAccount.d.ts.map +1 -0
  45. package/lib/commands-v2/getPublicAccount.js +46 -0
  46. package/lib/commands-v2/getPublicAccount.js.map +1 -0
  47. package/lib/commands-v2/list.d.ts +12 -0
  48. package/lib/commands-v2/list.d.ts.map +1 -0
  49. package/lib/commands-v2/list.js +58 -0
  50. package/lib/commands-v2/list.js.map +1 -0
  51. package/lib/index.d.ts +12 -0
  52. package/lib/index.d.ts.map +1 -0
  53. package/lib/index.js +26 -0
  54. package/lib/index.js.map +1 -0
  55. package/lib/init-v2.d.ts +33 -0
  56. package/lib/init-v2.d.ts.map +1 -0
  57. package/lib/init-v2.js +33 -0
  58. package/lib/init-v2.js.map +1 -0
  59. package/lib/init.d.ts +33 -0
  60. package/lib/init.d.ts.map +1 -0
  61. package/lib/init.js +31 -0
  62. package/lib/init.js.map +1 -0
  63. package/lib/shared/constants.d.ts +7 -0
  64. package/lib/shared/constants.d.ts.map +1 -0
  65. package/lib/shared/constants.js +11 -0
  66. package/lib/shared/constants.js.map +1 -0
  67. package/lib/shared/graphql-client.d.ts +38 -0
  68. package/lib/shared/graphql-client.d.ts.map +1 -0
  69. package/lib/shared/graphql-client.js +80 -0
  70. package/lib/shared/graphql-client.js.map +1 -0
  71. package/lib/shared/payloads.d.ts +10 -0
  72. package/lib/shared/payloads.d.ts.map +1 -0
  73. package/lib/shared/payloads.js +123 -0
  74. package/lib/shared/payloads.js.map +1 -0
  75. package/lib/shared/request.d.ts +4 -0
  76. package/lib/shared/request.d.ts.map +1 -0
  77. package/lib/shared/request.js +25 -0
  78. package/lib/shared/request.js.map +1 -0
  79. package/lib/shared/transpiling.d.ts +15 -0
  80. package/lib/shared/transpiling.d.ts.map +1 -0
  81. package/lib/shared/transpiling.js +29 -0
  82. package/lib/shared/transpiling.js.map +1 -0
  83. package/lib/shared/types.d.ts +194 -0
  84. package/lib/shared/types.d.ts.map +1 -0
  85. package/lib/shared/types.js +4 -0
  86. package/lib/shared/types.js.map +1 -0
  87. package/lib/shared/validation.d.ts +3 -0
  88. package/lib/shared/validation.d.ts.map +1 -0
  89. package/lib/shared/validation.js +47 -0
  90. package/lib/shared/validation.js.map +1 -0
  91. package/package.json +52 -0
  92. package/src/client-v2.ts +120 -0
  93. package/src/client.ts +133 -0
  94. package/src/commands/account.ts +54 -0
  95. package/src/commands/authenticate.ts +30 -0
  96. package/src/commands/deploy.ts +72 -0
  97. package/src/commands/getPublicAccount.ts +77 -0
  98. package/src/commands/list.ts +50 -0
  99. package/src/commands/upload.ts +91 -0
  100. package/src/commands-v2/account.ts +98 -0
  101. package/src/commands-v2/deploy.ts +145 -0
  102. package/src/commands-v2/getPublicAccount.ts +67 -0
  103. package/src/commands-v2/list.ts +91 -0
  104. package/src/index.ts +27 -0
  105. package/src/init-v2.ts +44 -0
  106. package/src/init.ts +45 -0
  107. package/src/shared/constants.ts +9 -0
  108. package/src/shared/graphql-client.ts +136 -0
  109. package/src/shared/payloads.ts +153 -0
  110. package/src/shared/request.ts +28 -0
  111. package/src/shared/transpiling.ts +49 -0
  112. package/src/shared/types.ts +235 -0
  113. package/src/shared/validation.ts +52 -0
@@ -0,0 +1,98 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import {
4
+ SDKConfigurationV2,
5
+ StepZenAccountV2,
6
+ ZenCtlResponseV2,
7
+ } from '../shared/types'
8
+ import {fetchZenCtlGraphQLQuery} from '../shared/graphql-client'
9
+
10
+ export default async ({
11
+ account,
12
+ adminKey,
13
+ deploymentType,
14
+ sdkConfig,
15
+ }: {
16
+ account: string
17
+ adminKey: string
18
+ deploymentType: string
19
+ sdkConfig: SDKConfigurationV2
20
+ }): Promise<ZenCtlResponseV2<StepZenAccountV2>> => {
21
+ const {data, errors} = await fetchZenCtlGraphQLQuery<{
22
+ accounts: Array<{
23
+ deploymentType: string
24
+ account: string
25
+ ownerEmail: string
26
+ adminKey: string
27
+ apiKeysAsJsonArray: string
28
+ }>
29
+ }>({
30
+ account,
31
+ adminKey,
32
+ deploymentType,
33
+ query: `query (
34
+ $account: String!
35
+ $adminKey: String!
36
+ $deploymentType: String!
37
+ ) {
38
+ accounts: account(
39
+ account: $account
40
+ adminkey: $adminKey
41
+ deploymentType: $deploymentType
42
+ ) {
43
+ deploymentType: account_deployment_type
44
+ account: account_name
45
+ ownerEmail: account_owner_email
46
+ adminKey: key_value_admin
47
+ apiKeysAsJsonArray: key_value_api
48
+ }
49
+ }`,
50
+ variables: {
51
+ account,
52
+ adminKey,
53
+ deploymentType,
54
+ },
55
+ sdkConfig,
56
+ })
57
+
58
+ if (errors) {
59
+ // strip graphql-specific error details, leave only the message
60
+ const message = errors
61
+ .map(({message}) =>
62
+ message.startsWith(
63
+ 'ERROR: invalid input value for enum zenctl.deployment_type',
64
+ )
65
+ ? `Invalid deployment type: ${deploymentType}`
66
+ : message,
67
+ )
68
+ .join('\n')
69
+
70
+ return {
71
+ data: undefined,
72
+ error: {
73
+ message,
74
+ },
75
+ }
76
+ }
77
+
78
+ const accounts = data.accounts.filter(Boolean)
79
+ if (!accounts.length) {
80
+ return {
81
+ data: undefined,
82
+ error: {
83
+ message: 'Your credentials are invalid',
84
+ },
85
+ }
86
+ }
87
+
88
+ return {
89
+ data: {
90
+ account: accounts[0].account,
91
+ deploymentType: accounts[0].deploymentType,
92
+ ownerEmail: accounts[0].ownerEmail,
93
+ adminKey: accounts[0].adminKey,
94
+ apiKeys: JSON.parse(accounts[0].apiKeysAsJsonArray),
95
+ },
96
+ error: undefined,
97
+ }
98
+ }
@@ -0,0 +1,145 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import {
4
+ Configuration,
5
+ SchemaFiles,
6
+ SDKConfigurationV2,
7
+ StepZenEndpointV2,
8
+ ZenCtlResponseV2,
9
+ } from '../shared/types'
10
+ import {fetchZenCtlGraphQLQuery} from '../shared/graphql-client'
11
+
12
+ export default async ({
13
+ account,
14
+ deploymentType,
15
+ folderName,
16
+ endpointName,
17
+ public: _public,
18
+ endpointType,
19
+ schemaFiles,
20
+ configuration,
21
+ adminKey,
22
+ sdkConfig,
23
+ }: {
24
+ account: string
25
+ deploymentType: string
26
+ folderName: string
27
+ endpointName: string
28
+ public: boolean
29
+ endpointType: string
30
+ schemaFiles: Readonly<SchemaFiles>
31
+ configuration: Readonly<Configuration> | null
32
+ adminKey: string
33
+ sdkConfig: SDKConfigurationV2
34
+ }): Promise<ZenCtlResponseV2<StepZenEndpointV2>> => {
35
+ const {data, errors} = await fetchZenCtlGraphQLQuery<{
36
+ endpoints: Array<{
37
+ account: string
38
+ deploymentType: string
39
+ folderName: string
40
+ endpointName: string
41
+ public: boolean
42
+ endpointType: string
43
+ }>
44
+ }>({
45
+ account,
46
+ adminKey,
47
+ deploymentType,
48
+ query: /* GraphQL */ `
49
+ mutation (
50
+ $account: String!
51
+ $deploymentType: String!
52
+ $folderName: String!
53
+ $endpointName: String!
54
+ $public: Boolean!
55
+ $endpointType: String!
56
+ $schemaFiles: SchemaFilesInput!
57
+ $configuration: Configuration
58
+ $adminKey: String!
59
+ ) {
60
+ endpoints: addEndpoint(
61
+ account: $account
62
+ deploymentType: $deploymentType
63
+ folderName: $folderName
64
+ endpointName: $endpointName
65
+ public: $public
66
+ endpointType: $endpointType
67
+ schemaFiles: $schemaFiles
68
+ configuration: $configuration
69
+ adminkey: $adminKey
70
+ ) {
71
+ account: account_name
72
+ deploymentType: deployment_type
73
+ folderName: folder_name
74
+ endpointName: endpoint_name
75
+ public: endpoint_public
76
+ endpointType: endpoint_type
77
+ }
78
+ }
79
+ `,
80
+ variables: {
81
+ account,
82
+ deploymentType,
83
+ folderName,
84
+ endpointName,
85
+ public: _public,
86
+ endpointType,
87
+ schemaFiles,
88
+ configuration,
89
+ adminKey,
90
+ },
91
+ sdkConfig,
92
+ })
93
+
94
+ if (errors) {
95
+ // strip graphql-specific error details, leave only the message
96
+ const message = errors
97
+ .map(({message}) => {
98
+ if (
99
+ message.startsWith(
100
+ 'ERROR: invalid input value for enum zenctl.deployment_type',
101
+ )
102
+ ) {
103
+ return (
104
+ `Invalid deployment type: ${deploymentType}.` +
105
+ ` Please check the 'deploymentType' parameter.`
106
+ )
107
+ }
108
+
109
+ if (
110
+ message.startsWith(
111
+ 'ERROR: invalid input value for enum zenctl.endpoint_type',
112
+ )
113
+ ) {
114
+ return (
115
+ `Invalid endpoint type: ${endpointType}.` +
116
+ ` Please check the 'endpointType' parameter.`
117
+ )
118
+ }
119
+
120
+ return message
121
+ })
122
+ .join('\n')
123
+
124
+ return {
125
+ data: undefined,
126
+ error: {
127
+ message,
128
+ },
129
+ }
130
+ }
131
+
132
+ if (!data.endpoints.length) {
133
+ return {
134
+ data: undefined,
135
+ error: {
136
+ message: 'Invalid credentials',
137
+ },
138
+ }
139
+ }
140
+
141
+ return {
142
+ data: data.endpoints[0],
143
+ error: undefined,
144
+ }
145
+ }
@@ -0,0 +1,67 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import {
4
+ SDKConfigurationV2,
5
+ StepZenCredentialsV2,
6
+ ZenCtlResponseV2,
7
+ } from '../shared/types'
8
+ import {fetchPublicAccountGraphQLQuery} from '../shared/graphql-client'
9
+
10
+ export default async ({
11
+ token,
12
+ deploymentType,
13
+ sdkConfig,
14
+ }: {
15
+ token: string
16
+ deploymentType: string
17
+ sdkConfig: SDKConfigurationV2
18
+ }): Promise<ZenCtlResponseV2<StepZenCredentialsV2>> => {
19
+ const url = new URL(sdkConfig.publicAccountApiUrl)
20
+ // Include the token into the URL so that it is visible in the logs
21
+ // (allows StepZen to do analytics based on the GCP logs).
22
+ url.searchParams.set('token', token)
23
+
24
+ const {data, errors} = await fetchPublicAccountGraphQLQuery<{
25
+ account: {
26
+ account: string
27
+ adminKey: string
28
+ apiKey: string
29
+ }
30
+ }>({
31
+ url,
32
+ query: `query (
33
+ $token: String!
34
+ ) {
35
+ account: getAccountDetails(
36
+ token: $token
37
+ ) {
38
+ account: accountName
39
+ adminKey
40
+ apiKey
41
+ }
42
+ }`,
43
+ variables: {
44
+ token,
45
+ },
46
+ sdkConfig,
47
+ })
48
+
49
+ if (errors) {
50
+ // strip graphql-specific error details, leave only the message
51
+ const message = errors.map(error => error.message).join('\n')
52
+ return {
53
+ data: undefined,
54
+ error: {message},
55
+ }
56
+ }
57
+
58
+ return {
59
+ data: {
60
+ account: data.account.account,
61
+ adminKey: data.account.adminKey,
62
+ apiKeys: [data.account.apiKey],
63
+ deploymentType,
64
+ },
65
+ error: undefined,
66
+ }
67
+ }
@@ -0,0 +1,91 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import {SDKConfigurationV2, ZenCtlResponseV2} from '../shared/types'
4
+ import {fetchZenCtlGraphQLQuery} from '../shared/graphql-client'
5
+
6
+ export default async ({
7
+ account,
8
+ deploymentType,
9
+ adminKey,
10
+ sdkConfig,
11
+ }: {
12
+ account: string
13
+ deploymentType: string
14
+ adminKey: string
15
+ sdkConfig: SDKConfigurationV2
16
+ }): Promise<
17
+ ZenCtlResponseV2<
18
+ Array<{
19
+ folderName: string
20
+ endpointName: string
21
+ }>
22
+ >
23
+ > => {
24
+ const {data, errors} = await fetchZenCtlGraphQLQuery<{
25
+ endpoints: Array<{
26
+ folderName: string
27
+ endpointName: string
28
+ }>
29
+ }>({
30
+ account,
31
+ adminKey,
32
+ deploymentType,
33
+ query: /* GraphQL */ `
34
+ query ($account: String!, $deploymentType: String!, $adminKey: String!) {
35
+ endpoints: endpointsForAccount(
36
+ account: $account
37
+ deploymentType: $deploymentType
38
+ adminkey: $adminKey
39
+ ) {
40
+ endpointName: endpoint_name
41
+ folderName: folder_name
42
+ }
43
+ }
44
+ `,
45
+ variables: {
46
+ account,
47
+ deploymentType,
48
+ adminKey,
49
+ },
50
+ sdkConfig,
51
+ })
52
+
53
+ if (errors) {
54
+ // strip graphql-specific error details, leave only the message
55
+ const message = errors
56
+ .map(({message}) => {
57
+ if (
58
+ message.startsWith(
59
+ 'ERROR: invalid input value for enum zenctl.deployment_type',
60
+ )
61
+ ) {
62
+ return (
63
+ `Invalid deployment type: ${deploymentType}.` +
64
+ ` Please check the 'deploymentType' parameter.`
65
+ )
66
+ }
67
+
68
+ return message
69
+ })
70
+ .join('\n')
71
+
72
+ return {
73
+ data: undefined,
74
+ error: {
75
+ message,
76
+ },
77
+ }
78
+ }
79
+
80
+ // The case with data.endpoints === [] is ambiguous.
81
+ // It may mean "invalid credentials" and it may mean "no endpoints".
82
+ // At this point it makes more sense to assume "no endpoints" since
83
+ // the "invalid credentials" error would be detected earlier, when
84
+ // creating an SDK client instance.
85
+ // See https://github.com/steprz/zen/issues/6361
86
+
87
+ return {
88
+ data: data.endpoints,
89
+ error: undefined,
90
+ }
91
+ }
package/src/index.ts ADDED
@@ -0,0 +1,27 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as path from 'path'
4
+ import {init, SDK} from './init'
5
+
6
+ export * from './init'
7
+ export * from './init-v2'
8
+
9
+ /**
10
+ * The default SDK instance that does not know the name of the app using the SDK.
11
+ * It tries to guess the app name from `process.argv`
12
+ *
13
+ * @deprecated use the init({appName: 'my-app/1.2.3'}) method to initialis an SDK instance
14
+ */
15
+ const stepzen: SDK = init({
16
+ // For legacy apps use the script name (e.g. `index.js` as the app name)
17
+ // Fallback to `node` if this file is imported into an interactive node shell
18
+ appName: path.basename(process.argv[1] || process.argv[0]),
19
+ })
20
+
21
+ export default stepzen
22
+
23
+ // Hack: support both ESM-style imports: `import stepzen from '@stepzen/sdk'`
24
+ // and keep the backwards-compat with CJS-style imports: `const stepzen = require('@stepzen/sdk')`
25
+ // NOTE: if at any point in the future there is a name collision between `stepzen` and ES exports
26
+ // this hack will break
27
+ module.exports = {...module.exports, ...stepzen}
package/src/init-v2.ts ADDED
@@ -0,0 +1,44 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import accountCommand from './commands-v2/account'
4
+ import {createSdkClient} from './client-v2'
5
+ import {getUserAgent} from './shared/request'
6
+ import {SDKConfigurationV2} from './shared/types'
7
+
8
+ export const initV2 = (sdkConfig: SDKConfigurationV2) => {
9
+ return {
10
+ verify: async (
11
+ account: string,
12
+ adminkey: string,
13
+ deploymentType: string,
14
+ ) => {
15
+ try {
16
+ const {data} = await accountCommand({
17
+ account,
18
+ adminKey: adminkey,
19
+ deploymentType,
20
+ sdkConfig,
21
+ })
22
+ return Boolean(data)
23
+ } catch {
24
+ return false
25
+ }
26
+ },
27
+ client: async (
28
+ auth:
29
+ | {account: string; adminKey: string; deploymentType: string}
30
+ | {publicAccountToken: string; deploymentType: string},
31
+ ) => {
32
+ return createSdkClient(auth, sdkConfig)
33
+ },
34
+ get userAgent() {
35
+ return getUserAgent(sdkConfig)
36
+ },
37
+ }
38
+ }
39
+
40
+ // a helper type to unwrap Promise<U> into U
41
+ type PromisedType<T> = T extends Promise<infer U> ? U : T
42
+
43
+ export type SDKV2 = ReturnType<typeof initV2>
44
+ export type SDKClientV2 = PromisedType<ReturnType<SDKV2['client']>>
package/src/init.ts ADDED
@@ -0,0 +1,45 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import authenticate from './commands/authenticate'
4
+ import {createSdkClient} from './client'
5
+ import {getUserAgent} from './shared/request'
6
+ import {
7
+ AnonymousClientOptions,
8
+ SDKConfiguration,
9
+ UserCredentialsClientOptions,
10
+ } from './shared/types'
11
+
12
+ export const init = (sdkConfig: SDKConfiguration) => {
13
+ const defaults = {
14
+ domain: process.env.STEPZEN_DOMAIN || 'stepzen.io',
15
+ server: process.env.STEPZEN_SERVER_URL || 'https://{account}.stepzen.io',
16
+ }
17
+
18
+ return {
19
+ verify: (account: string, adminkey: string) => {
20
+ return authenticate(
21
+ {
22
+ account,
23
+ adminkey,
24
+ server: defaults.server.replace('{account}', account),
25
+ domain: defaults.domain,
26
+ },
27
+ sdkConfig,
28
+ )
29
+ },
30
+ client: async (
31
+ options: UserCredentialsClientOptions | AnonymousClientOptions,
32
+ ) => {
33
+ return createSdkClient({...defaults, ...options}, sdkConfig)
34
+ },
35
+ get userAgent() {
36
+ return getUserAgent(sdkConfig)
37
+ },
38
+ }
39
+ }
40
+
41
+ // a helper type to unwrap Promise<U> into U
42
+ type PromisedType<T> = T extends Promise<infer U> ? U : T
43
+
44
+ export type SDK = ReturnType<typeof init>
45
+ export type SDKClient = PromisedType<ReturnType<SDK['client']>>
@@ -0,0 +1,9 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ export const ADMIN_ACCOUNT_URL = '/cli/admin/account'
4
+ export const ADMIN_DEPLOY_URL = '/cli/admin/deploy'
5
+ export const ADMIN_LIST_URL = '/cli/admin/list'
6
+ export const ADMIN_UPLOAD_URL = '/cli/admin/upload'
7
+ export const ADMIN_VERIFY_URL = '/cli/admin/verify'
8
+
9
+ export const ZENCTL2_URL = '/api/zenctl/__graphql'
@@ -0,0 +1,136 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from '@stepzen/fetch'
5
+
6
+ import {SDKConfigurationV2} from './types'
7
+ import {getUserAgent} from './request'
8
+
9
+ export type GraphQLSuccessResponse<T> = {
10
+ data: T
11
+ errors: undefined
12
+ }
13
+
14
+ export type GraphQLErrorResponse<T> = {
15
+ data: T | null
16
+ errors: Array<{
17
+ message: string
18
+ locations?: Array<{line: number; column: number}>
19
+ path?: Array<string | number>
20
+ }>
21
+ }
22
+
23
+ export type GraphQLResponse<T> =
24
+ | GraphQLSuccessResponse<T>
25
+ | GraphQLErrorResponse<T>
26
+
27
+ export const fetchGraphQLQuery = async <T>({
28
+ url,
29
+ query,
30
+ variables = {},
31
+ headers = {},
32
+ }: {
33
+ url: URL | string
34
+ query: string
35
+ variables: Record<string, any>
36
+ headers: Record<string, any>
37
+ }): Promise<GraphQLResponse<T>> => {
38
+ debug('stepzen:sdk:url')(url)
39
+ debug('stepzen:sdk:headers')(headers)
40
+ debug('stepzen:sdk:query')(query)
41
+ debug('stepzen:sdk:variables')(variables)
42
+
43
+ try {
44
+ const response = await fetch(url, {
45
+ method: 'POST',
46
+ headers: {
47
+ 'content-type': 'application/json',
48
+ ...headers,
49
+ },
50
+ body: JSON.stringify({
51
+ query,
52
+ variables,
53
+ }),
54
+ })
55
+
56
+ if (response.status !== 200) {
57
+ throw new Error(
58
+ `Unexpected HTTP status ${response.status}` +
59
+ ` from the GraphQL endpoint '${url}' (expected 200)`,
60
+ )
61
+ }
62
+
63
+ const json = await response.json()
64
+ debug('stepzen:sdk:response')(json)
65
+
66
+ const {data, errors} = json
67
+ if (errors) {
68
+ return {data, errors}
69
+ } else if (data) {
70
+ return {data, errors: undefined}
71
+ }
72
+
73
+ throw new Error(
74
+ `Unexpected response structure from the GraphQL endpoint '${url}'` +
75
+ ` (expected {data, errors}, recieved ${JSON.stringify(json)})`,
76
+ )
77
+ } catch (error) {
78
+ debug('stepzen:sdk:response')(`Failed to fetch from a GraphQL API`, error)
79
+ throw error
80
+ }
81
+ }
82
+
83
+ export const fetchZenCtlGraphQLQuery = async <T>({
84
+ account,
85
+ adminKey,
86
+ deploymentType,
87
+ query,
88
+ variables = {},
89
+ sdkConfig,
90
+ }: {
91
+ account: string
92
+ adminKey: string
93
+ deploymentType: string
94
+ query: string
95
+ variables: Record<string, any>
96
+ sdkConfig: SDKConfigurationV2
97
+ }): Promise<GraphQLResponse<T>> => {
98
+ try {
99
+ return await fetchGraphQLQuery({
100
+ url: sdkConfig.zenctlApiUrl,
101
+ query,
102
+ variables,
103
+ headers: {
104
+ 'user-agent': getUserAgent(sdkConfig),
105
+ host: `stepzen.${deploymentType}.net`,
106
+ },
107
+ })
108
+ } catch (error) {
109
+ throw new Error(`Failed to connect to ZenCtl API. ${error}`)
110
+ }
111
+ }
112
+
113
+ export const fetchPublicAccountGraphQLQuery = async <T>({
114
+ url,
115
+ query,
116
+ variables = {},
117
+ sdkConfig,
118
+ }: {
119
+ url: URL | string
120
+ query: string
121
+ variables: Record<string, any>
122
+ sdkConfig: SDKConfigurationV2
123
+ }): Promise<GraphQLResponse<T>> => {
124
+ try {
125
+ return await fetchGraphQLQuery({
126
+ url,
127
+ query,
128
+ variables,
129
+ headers: {
130
+ 'user-agent': getUserAgent(sdkConfig),
131
+ },
132
+ })
133
+ } catch (error) {
134
+ throw new Error(`Failed to connect to Public Account API. ${error}`)
135
+ }
136
+ }