@stepzen/sdk 0.11.1 → 1.0.0-alpha.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.
Files changed (60) hide show
  1. package/lib/client-v2.d.ts +33 -0
  2. package/lib/client-v2.js +71 -0
  3. package/lib/client-v2.js.map +1 -0
  4. package/lib/client.js +9 -9
  5. package/lib/client.js.map +1 -1
  6. package/lib/commands/account.js +2 -2
  7. package/lib/commands/account.js.map +1 -1
  8. package/lib/commands/authenticate.js +2 -2
  9. package/lib/commands/authenticate.js.map +1 -1
  10. package/lib/commands/deploy.js +2 -2
  11. package/lib/commands/deploy.js.map +1 -1
  12. package/lib/commands/getPublicAccount.js +2 -2
  13. package/lib/commands/getPublicAccount.js.map +1 -1
  14. package/lib/commands/list.js +2 -2
  15. package/lib/commands/list.js.map +1 -1
  16. package/lib/commands/upload.js +8 -8
  17. package/lib/commands/upload.js.map +1 -1
  18. package/lib/commands-v2/account.d.ts +8 -0
  19. package/lib/commands-v2/account.js +61 -0
  20. package/lib/commands-v2/account.js.map +1 -0
  21. package/lib/commands-v2/deploy.d.ts +14 -0
  22. package/lib/commands-v2/deploy.js +96 -0
  23. package/lib/commands-v2/deploy.js.map +1 -0
  24. package/lib/commands-v2/getPublicAccount.d.ts +7 -0
  25. package/lib/commands-v2/getPublicAccount.js +38 -0
  26. package/lib/commands-v2/getPublicAccount.js.map +1 -0
  27. package/lib/commands-v2/list.d.ts +8 -0
  28. package/lib/commands-v2/list.js +66 -0
  29. package/lib/commands-v2/list.js.map +1 -0
  30. package/lib/index.d.ts +3 -33
  31. package/lib/index.js +5 -24
  32. package/lib/index.js.map +1 -1
  33. package/lib/init-v2.d.ts +35 -0
  34. package/lib/init-v2.js +40 -0
  35. package/lib/init-v2.js.map +1 -0
  36. package/lib/init.d.ts +34 -0
  37. package/lib/init.js +27 -0
  38. package/lib/init.js.map +1 -0
  39. package/lib/shared/graphql-client.d.ts +34 -0
  40. package/lib/shared/graphql-client.js +60 -0
  41. package/lib/shared/graphql-client.js.map +1 -0
  42. package/lib/shared/request.d.ts +3 -1
  43. package/lib/shared/request.js +3 -3
  44. package/lib/shared/request.js.map +1 -1
  45. package/lib/shared/transpiling.js +2 -2
  46. package/lib/shared/transpiling.js.map +1 -1
  47. package/lib/shared/types.d.ts +64 -0
  48. package/package.json +4 -4
  49. package/src/client-v2.ts +99 -0
  50. package/src/commands-v2/account.ts +89 -0
  51. package/src/commands-v2/deploy.ts +144 -0
  52. package/src/commands-v2/getPublicAccount.ts +59 -0
  53. package/src/commands-v2/list.ts +98 -0
  54. package/src/index.ts +3 -38
  55. package/src/init-v2.ts +70 -0
  56. package/src/init.ts +41 -0
  57. package/src/shared/graphql-client.ts +108 -0
  58. package/src/shared/request.ts +2 -2
  59. package/src/shared/transpiling.ts +1 -2
  60. package/src/shared/types.ts +77 -0
@@ -0,0 +1,108 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from 'node-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
+ const json = await response.json()
56
+ debug('stepzen:sdk:response')(json)
57
+
58
+ const {data, errors} = json
59
+ if (errors) {
60
+ return {data, errors}
61
+ } else {
62
+ return {data, errors: undefined}
63
+ }
64
+ } catch (error) {
65
+ debug('stepzen:sdk:response')(`Failed to fetch from a GraphQL API`, error)
66
+ throw error
67
+ }
68
+ }
69
+
70
+ export const fetchZenCtlGraphQLQuery = async <T>({
71
+ query,
72
+ variables = {},
73
+ sdkConfig,
74
+ }: {
75
+ query: string
76
+ variables: Record<string, any>
77
+ sdkConfig: SDKConfigurationV2
78
+ }): Promise<GraphQLResponse<T>> => {
79
+ return fetchGraphQLQuery({
80
+ url: sdkConfig.zenctlApiUrl,
81
+ query,
82
+ variables,
83
+ headers: {
84
+ 'user-agent': getUserAgent(sdkConfig),
85
+ },
86
+ })
87
+ }
88
+
89
+ export const fetchPublicAccountGraphQLQuery = async <T>({
90
+ url,
91
+ query,
92
+ variables = {},
93
+ sdkConfig,
94
+ }: {
95
+ url: URL | string
96
+ query: string
97
+ variables: Record<string, any>
98
+ sdkConfig: SDKConfigurationV2
99
+ }): Promise<GraphQLResponse<T>> => {
100
+ return fetchGraphQLQuery({
101
+ url,
102
+ query,
103
+ variables,
104
+ headers: {
105
+ 'user-agent': getUserAgent(sdkConfig),
106
+ },
107
+ })
108
+ }
@@ -11,8 +11,8 @@ const {version} = require('../../package.json')
11
11
  const arch = os.arch() === 'ia32' ? 'x86' : (os.arch() as any)
12
12
  const platform = isWsl ? 'wsl' : (os.platform() as any)
13
13
 
14
- export const getUserAgent = (sdkConfig: SDKConfiguration): string => {
15
- return `${sdkConfig.appName} stepzen-sdk/${version} (${platform}; ${arch}; node-${process.version})`
14
+ export const getUserAgent = ({appName}: {appName: string}): string => {
15
+ return `${appName} stepzen-sdk/${version} (${platform}; ${arch}; node-${process.version})`
16
16
  }
17
17
 
18
18
  export const getRequestHeaders = (
@@ -4,8 +4,7 @@ import * as dotenv from 'dotenv'
4
4
  import * as fs from 'fs-extra'
5
5
  import * as os from 'os'
6
6
  import * as path from 'path'
7
-
8
- const {transpile} = require('@stepzen/transpiler')
7
+ import {transpile} from '@stepzen/transpiler'
9
8
 
10
9
  export const transpileConfigurationset = async (
11
10
  file: string | undefined,
@@ -90,4 +90,81 @@ export interface SDKConfiguration {
90
90
  * through the SDK and becomes availabe in the log analytics.
91
91
  */
92
92
  appName: string
93
+
94
+ /**
95
+ * Version of ZenCtl API to use:
96
+ * - `undefined`, `v1`: implies the v1 REST API at /ctl/admin/
97
+ * - `v2`: implies the v2 GraphQL API
98
+ */
99
+ apiVersion?: string
100
+ }
101
+
102
+ export interface SDKConfigurationV2 extends SDKConfiguration {
103
+ /**
104
+ * The name and version of that app that uses the SDK,
105
+ * e.g. `stepzen-cli/0.9.32`
106
+ *
107
+ * It is appended to the user-agent string in all requests made to StepZen
108
+ * through the SDK and becomes availabe in the log analytics.
109
+ */
110
+ appName: string
111
+
112
+ /**
113
+ * Version of ZenCtl API to use:
114
+ * - `undefined`, `v1`: implies the v1 REST API at /ctl/admin/
115
+ * - `v2`: implies the v2 GraphQL API
116
+ */
117
+ apiVersion: string
118
+
119
+ /**
120
+ * URL of the ZenCtl GraphQL API,
121
+ * e.g. https://braselton.stepzen.net/api/zenctl2/__graphql
122
+ */
123
+ zenctlApiUrl: string
124
+
125
+ /**
126
+ * URL of the StepZen getPublicAccount GraphQL API,
127
+ * e.g. https://stepzen.stepzen.net/api/publicaccount/__graphql
128
+ */
129
+ publicAccountApiUrl: string
130
+ }
131
+
132
+ export interface ZenCtlSuccessResponseV2<T> {
133
+ data: T
134
+ error: undefined
135
+ }
136
+
137
+ export interface ZenCtlErrorResponseV2 {
138
+ data: undefined
139
+ error: {
140
+ message: string
141
+ }
142
+ }
143
+
144
+ export type ZenCtlResponseV2<T> =
145
+ | ZenCtlSuccessResponseV2<T>
146
+ | ZenCtlErrorResponseV2
147
+
148
+ export interface StepZenCredentialsV2 {
149
+ account: string
150
+ adminKey: string
151
+ serviceKey: string
152
+ deploymentType: string
153
+ }
154
+
155
+ export interface StepZenAccountV2 {
156
+ account: string
157
+ ownerEmail: string
158
+ adminKey: string
159
+ serviceKey: string
160
+ deploymentType: string
161
+ }
162
+
163
+ export interface StepZenEndpointV2 {
164
+ account: string
165
+ deploymentType: string
166
+ folderName: string
167
+ endpointName: string
168
+ public: boolean
169
+ endpointType: string
93
170
  }