@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,99 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import accountCmd from './commands-v2/account'
4
+ import deployCmd from './commands-v2/deploy'
5
+ import listCmd from './commands-v2/list'
6
+ import getPublicAccount from './commands-v2/getPublicAccount'
7
+ import {SDKConfigurationV2, StepZenCredentialsV2} from './shared/types'
8
+
9
+ export const ensureValidCredentials = async (
10
+ auth:
11
+ | {account: string; adminKey: string; deploymentType: string}
12
+ | {publicAccountToken: string; deploymentType: string},
13
+ sdkConfig: SDKConfigurationV2,
14
+ ) => {
15
+ let credentials: StepZenCredentialsV2
16
+
17
+ if ('publicAccountToken' in auth) {
18
+ // create an anonymous account and use it to initialize an SDK client instance
19
+ const {data, error} = await getPublicAccount({
20
+ token: auth.publicAccountToken,
21
+ deploymentType: auth.deploymentType,
22
+ sdkConfig,
23
+ })
24
+
25
+ if (error) {
26
+ throw new Error(`An unexpected error occurred: ${error.message}`)
27
+ }
28
+
29
+ credentials = data
30
+ } else {
31
+ // use the provided account to initialize an SDK client instance
32
+ const {data, error} = await accountCmd({
33
+ account: auth.account,
34
+ adminKey: auth.adminKey,
35
+ deploymentType: auth.deploymentType,
36
+ sdkConfig,
37
+ })
38
+
39
+ if (error) {
40
+ throw new Error(error.message)
41
+ }
42
+
43
+ credentials = data[0]
44
+ }
45
+
46
+ return credentials
47
+ }
48
+
49
+ export const createSdkClient = async (
50
+ auth:
51
+ | {account: string; adminKey: string; deploymentType: string}
52
+ | {publicAccountToken: string; deploymentType: string},
53
+ sdkConfig: SDKConfigurationV2,
54
+ ) => {
55
+ const credentials = await ensureValidCredentials(auth, sdkConfig)
56
+
57
+ return {
58
+ get credentials() {
59
+ // always return a copy to avoid accidential modification by the caller
60
+ return {...credentials}
61
+ },
62
+ deploy: ({
63
+ endpointName,
64
+ sdl,
65
+ folderName,
66
+ public: _public,
67
+ endpointType,
68
+ config,
69
+ }: {
70
+ endpointName: string
71
+ sdl: string
72
+ folderName?: string
73
+ public?: boolean
74
+ endpointType?: string
75
+ config?: string
76
+ }) => {
77
+ return deployCmd({
78
+ folderName: folderName || 'default',
79
+ endpointName,
80
+ public: typeof _public === 'boolean' ? _public : false,
81
+ endpointType: endpointType || 'dev',
82
+ sdl,
83
+ config: config || '{}',
84
+ account: credentials.account,
85
+ adminKey: credentials.adminKey,
86
+ deploymentType: credentials.deploymentType,
87
+ sdkConfig,
88
+ })
89
+ },
90
+ list: () => {
91
+ return listCmd({
92
+ account: credentials.account,
93
+ adminKey: credentials.adminKey,
94
+ deploymentType: credentials.deploymentType,
95
+ sdkConfig,
96
+ })
97
+ },
98
+ }
99
+ }
@@ -0,0 +1,89 @@
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
+ serviceKey: string
28
+ }>
29
+ }>({
30
+ query: `query (
31
+ $account: String!
32
+ $adminKey: String!
33
+ $deploymentType: String!
34
+ ) {
35
+ accounts: account(
36
+ account: $account
37
+ adminkey: $adminKey
38
+ deploymentType: $deploymentType
39
+ ) {
40
+ deploymentType: account_deployment_type
41
+ account: account_name
42
+ ownerEmail: account_owner_email
43
+ adminKey: key_value_admin
44
+ serviceKey: key_value_service
45
+ }
46
+ }`,
47
+ variables: {
48
+ account,
49
+ adminKey,
50
+ deploymentType,
51
+ },
52
+ sdkConfig,
53
+ })
54
+
55
+ if (errors) {
56
+ // strip graphql-specific error details, leave only the message
57
+ const message = errors
58
+ .map(({message}) => ({
59
+ message: message.startsWith(
60
+ 'ERROR: invalid input value for enum zenctl.deployment_type',
61
+ )
62
+ ? `Invalid deployment type: ${deploymentType}`
63
+ : message,
64
+ }))
65
+ .join('\n')
66
+
67
+ return {
68
+ data: undefined,
69
+ error: {
70
+ message,
71
+ },
72
+ }
73
+ }
74
+
75
+ const accounts = data.accounts.filter(Boolean)
76
+ if (!accounts.length) {
77
+ return {
78
+ data: undefined,
79
+ error: {
80
+ message: 'Invalid credentials',
81
+ },
82
+ }
83
+ }
84
+
85
+ return {
86
+ data: accounts,
87
+ error: undefined,
88
+ }
89
+ }
@@ -0,0 +1,144 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import {
4
+ SDKConfigurationV2,
5
+ StepZenEndpointV2,
6
+ ZenCtlResponseV2,
7
+ } from '../shared/types'
8
+ import {fetchZenCtlGraphQLQuery} from '../shared/graphql-client'
9
+
10
+ export default async ({
11
+ account,
12
+ deploymentType,
13
+ folderName,
14
+ endpointName,
15
+ public: _public,
16
+ endpointType,
17
+ sdl,
18
+ config,
19
+ adminKey,
20
+ sdkConfig,
21
+ }: {
22
+ account: string
23
+ deploymentType: string
24
+ folderName: string
25
+ endpointName: string
26
+ public: boolean
27
+ endpointType: string
28
+ sdl: string
29
+ config: string
30
+ adminKey: string
31
+ sdkConfig: SDKConfigurationV2
32
+ }): Promise<ZenCtlResponseV2<StepZenEndpointV2>> => {
33
+ const {data, errors} = await fetchZenCtlGraphQLQuery<{
34
+ endpoints: Array<{
35
+ account: string
36
+ deploymentType: string
37
+ endpointName: string
38
+ public: boolean
39
+ endpointType: string
40
+ folderName: string
41
+ }>
42
+ }>({
43
+ query: `query (
44
+ $account: String!
45
+ $deploymentType: String!
46
+ $folderName: String!
47
+ $endpointName: String!
48
+ $public: Boolean!
49
+ $endpointType: String!
50
+ $sdl: JSON!
51
+ $config: JSON!
52
+ $adminKey: String!
53
+ ) {
54
+ endpoints: addEndpoint(
55
+ account: $account
56
+ deploymentType: $deploymentType
57
+ folderName: $folderName
58
+ endpointName: $endpointName
59
+ public: $public
60
+ endpointType: $endpointType
61
+ sdl: $sdl
62
+ config: $config
63
+ adminkey: $adminKey
64
+ ) {
65
+ account: account_name
66
+ deploymentType: deployment_type
67
+ endpointName: endpoint_name
68
+ public: endpoint_public
69
+ endpointType: endpoint_type
70
+ folderName: folder_name
71
+ }
72
+ }`,
73
+ variables: {
74
+ account,
75
+ deploymentType,
76
+ folderName,
77
+ endpointName,
78
+ public: _public,
79
+ endpointType,
80
+ sdl,
81
+ config,
82
+ adminKey,
83
+ },
84
+ sdkConfig,
85
+ })
86
+
87
+ if (errors) {
88
+ // strip graphql-specific error details, leave only the message
89
+ const message = errors
90
+ .map(({message}) => {
91
+ let newMessage = message
92
+ if (
93
+ message.startsWith(
94
+ 'ERROR: invalid input value for enum zenctl.deployment_type',
95
+ )
96
+ ) {
97
+ newMessage =
98
+ `Invalid deployment type: ${deploymentType}.` +
99
+ ` Please check the 'deploymentType' parameter.`
100
+ } else if (
101
+ message.startsWith(
102
+ 'ERROR: invalid input value for enum zenctl.endpoint_type',
103
+ )
104
+ ) {
105
+ newMessage =
106
+ `Invalid endpoint type: ${endpointType}.` +
107
+ ` Please check the 'endpointType' parameter.`
108
+ } else if (
109
+ message.startsWith('ERROR: invalid input syntax for type json')
110
+ ) {
111
+ newMessage =
112
+ `Could not parse a string as JSON.` +
113
+ ` Please check the 'sdl' or 'config' parameters.`
114
+ }
115
+
116
+ return {
117
+ message: newMessage,
118
+ }
119
+ })
120
+ .join('\n')
121
+
122
+ return {
123
+ data: undefined,
124
+ error: {
125
+ message,
126
+ },
127
+ }
128
+ }
129
+
130
+ const endpoints = data.endpoints.filter(Boolean)
131
+ if (!endpoints.length) {
132
+ return {
133
+ data: undefined,
134
+ error: {
135
+ message: 'Invalid credentials',
136
+ },
137
+ }
138
+ }
139
+
140
+ return {
141
+ data: endpoints[0],
142
+ error: undefined,
143
+ }
144
+ }
@@ -0,0 +1,59 @@
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
+ // Inlclude 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
+ serviceKey: 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
+ serviceKey: 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 {data: {...data.account, deploymentType}, error: undefined}
59
+ }
@@ -0,0 +1,98 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import {
4
+ SDKConfigurationV2,
5
+ StepZenEndpointV2,
6
+ ZenCtlResponseV2,
7
+ } from '../shared/types'
8
+ import {fetchZenCtlGraphQLQuery} from '../shared/graphql-client'
9
+
10
+ export default async ({
11
+ account,
12
+ deploymentType,
13
+ adminKey,
14
+ sdkConfig,
15
+ }: {
16
+ account: string
17
+ deploymentType: string
18
+ adminKey: string
19
+ sdkConfig: SDKConfigurationV2
20
+ }): Promise<ZenCtlResponseV2<StepZenEndpointV2[]>> => {
21
+ const {data, errors} = await fetchZenCtlGraphQLQuery<{
22
+ endpoints: Array<{
23
+ endpointName: string
24
+ folderName: string
25
+ }>
26
+ }>({
27
+ query: `query (
28
+ $account: String!
29
+ $deploymentType: String!
30
+ $adminKey: String!
31
+ ) {
32
+ endpoints: endpointsForAccount(
33
+ account: $account
34
+ deploymentType: $deploymentType
35
+ adminkey: $adminKey
36
+ ) {
37
+ endpointName: endpoint_name
38
+ folderName: folder_name
39
+ }
40
+ }`,
41
+ variables: {
42
+ account,
43
+ deploymentType,
44
+ adminKey,
45
+ },
46
+ sdkConfig,
47
+ })
48
+
49
+ if (errors) {
50
+ // strip graphql-specific error details, leave only the message
51
+ const message = errors
52
+ .map(({message}) => {
53
+ let newMessage = message
54
+ if (
55
+ message.startsWith(
56
+ 'ERROR: invalid input value for enum zenctl.deployment_type',
57
+ )
58
+ ) {
59
+ newMessage =
60
+ `Invalid deployment type: ${deploymentType}.` +
61
+ ` Please check the 'deploymentType' parameter.`
62
+ }
63
+
64
+ return {
65
+ message: newMessage,
66
+ }
67
+ })
68
+ .join('\n')
69
+
70
+ return {
71
+ data: undefined,
72
+ error: {
73
+ message,
74
+ },
75
+ }
76
+ }
77
+
78
+ const endpoints = data.endpoints.filter(Boolean)
79
+ if (!endpoints.length) {
80
+ return {
81
+ data: undefined,
82
+ error: {
83
+ message: 'Invalid credentials',
84
+ },
85
+ }
86
+ }
87
+
88
+ return {
89
+ data: endpoints.map(endpoint => ({
90
+ ...endpoint,
91
+ public: false, // TODO: implement
92
+ endpointType: 'dev', // TODO: implement
93
+ account,
94
+ deploymentType,
95
+ })),
96
+ error: undefined,
97
+ }
98
+ }
package/src/index.ts CHANGED
@@ -1,45 +1,10 @@
1
1
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
2
 
3
3
  import * as path from 'path'
4
- import authenticate from './commands/authenticate'
5
- import {createSdkClient} from './client'
6
- import {
7
- AnonymousClientOptions,
8
- SDKConfiguration,
9
- UserCredentialsClientOptions,
10
- } from './shared/types'
4
+ import {init, SDK} from './init'
11
5
 
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
- }
36
- }
37
-
38
- // a helper type to unwrap Promise<U> into U
39
- type PromisedType<T> = T extends Promise<infer U> ? U : T
40
-
41
- export type SDK = ReturnType<typeof init>
42
- export type SDKClient = PromisedType<ReturnType<SDK['client']>>
6
+ export * from './init'
7
+ export * from './init-v2'
43
8
 
44
9
  /**
45
10
  * The default SDK instance that does not know the name of the app using the SDK.
package/src/init-v2.ts ADDED
@@ -0,0 +1,70 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import accountCommand from './commands-v2/account'
4
+ import {createSdkClient} from './client-v2'
5
+ import {SDKConfigurationV2} from './shared/types'
6
+
7
+ export const initV2 = (config: {
8
+ appName: string
9
+ zenctlApiUrl?: string
10
+ publicAccountApiUrl?: string
11
+ }) => {
12
+ const defaults = {
13
+ zenctlApiUrl:
14
+ process.env.STEPZEN_ZENCTL_API_URL ||
15
+ 'https://braselton.stepzen.net/api/zenctl2/__graphql',
16
+ publicAccountApiUrl:
17
+ process.env.STEPZEN_PUBLIC_ACCOUNT_API_URL ||
18
+ 'https://stepzen.stepzen.net/api/publicaccount/__graphql',
19
+ deploymentType:
20
+ process.env.STEPZEN_DEPLOYMENT_TYPE ||
21
+ process.env.STEPZEN_DOMAIN?.replace('.io', '') ||
22
+ 'steprz',
23
+ }
24
+
25
+ const sdkConfig: SDKConfigurationV2 = {
26
+ apiVersion: 'v2',
27
+ zenctlApiUrl: defaults.zenctlApiUrl,
28
+ publicAccountApiUrl: defaults.publicAccountApiUrl,
29
+ ...config,
30
+ }
31
+
32
+ return {
33
+ verify: async (
34
+ account: string,
35
+ adminkey: string,
36
+ deploymentType: string = defaults.deploymentType,
37
+ ) => {
38
+ try {
39
+ const {data} = await accountCommand({
40
+ account,
41
+ adminKey: adminkey,
42
+ deploymentType,
43
+ sdkConfig,
44
+ })
45
+ return Boolean(data?.length)
46
+ } catch {
47
+ return false
48
+ }
49
+ },
50
+ client: async (
51
+ auth:
52
+ | {account: string; adminKey: string; deploymentType?: string}
53
+ | {publicAccountToken: string; deploymentType?: string},
54
+ ) => {
55
+ return createSdkClient(
56
+ {
57
+ deploymentType: defaults.deploymentType,
58
+ ...auth,
59
+ },
60
+ sdkConfig,
61
+ )
62
+ },
63
+ }
64
+ }
65
+
66
+ // a helper type to unwrap Promise<U> into U
67
+ type PromisedType<T> = T extends Promise<infer U> ? U : T
68
+
69
+ export type SDKV2 = ReturnType<typeof initV2>
70
+ export type SDKClientV2 = PromisedType<ReturnType<SDKV2['client']>>
package/src/init.ts ADDED
@@ -0,0 +1,41 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import authenticate from './commands/authenticate'
4
+ import {createSdkClient} from './client'
5
+ import {
6
+ AnonymousClientOptions,
7
+ SDKConfiguration,
8
+ UserCredentialsClientOptions,
9
+ } from './shared/types'
10
+
11
+ export const init = (sdkConfig: SDKConfiguration) => {
12
+ const defaults = {
13
+ domain: process.env.STEPZEN_DOMAIN || 'stepzen.io',
14
+ server: process.env.STEPZEN_SERVER_URL || 'https://{account}.stepzen.io',
15
+ }
16
+
17
+ return {
18
+ verify: (account: string, adminkey: string) => {
19
+ return authenticate(
20
+ {
21
+ account,
22
+ adminkey,
23
+ server: defaults.server.replace('{account}', account),
24
+ domain: defaults.domain,
25
+ },
26
+ sdkConfig,
27
+ )
28
+ },
29
+ client: async (
30
+ options: UserCredentialsClientOptions | AnonymousClientOptions,
31
+ ) => {
32
+ return createSdkClient({...defaults, ...options}, sdkConfig)
33
+ },
34
+ }
35
+ }
36
+
37
+ // a helper type to unwrap Promise<U> into U
38
+ type PromisedType<T> = T extends Promise<infer U> ? U : T
39
+
40
+ export type SDK = ReturnType<typeof init>
41
+ export type SDKClient = PromisedType<ReturnType<SDK['client']>>