@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
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@stepzen/sdk",
3
+ "version": "0.0.0-experimental-20221114-610843f6",
4
+ "license": "MIT",
5
+ "author": "Darren Waddell <darren@stepzen.com>",
6
+ "homepage": "https://stepzen.com",
7
+ "files": [
8
+ "lib/**/*",
9
+ "src/**/*"
10
+ ],
11
+ "engines": {
12
+ "node": ">=14.0.1",
13
+ "npm": ">=6.14"
14
+ },
15
+ "main": "lib/index.js",
16
+ "types": "lib/index.d.ts",
17
+ "scripts": {
18
+ "clean": "rm -rf lib tsconfig.tsbuildinfo",
19
+ "build": "../../node_modules/.bin/tsc -b",
20
+ "test": "npm run build && nyc --extension .ts mocha --config test/mocha.opts.json",
21
+ "test:single": "npm run build && mocha --config test/mocha.opts.json",
22
+ "posttest": "prettier . --check"
23
+ },
24
+ "dependencies": {
25
+ "@stepzen/fetch": "^0.0.0-experimental-20221114-610843f6",
26
+ "@stepzen/transpiler": "^0.0.0-experimental-20221114-610843f6",
27
+ "archiver": "^5.3.0",
28
+ "debug": "^4.3.4",
29
+ "dotenv": "^16.0.1",
30
+ "form-data": "^4.0.0",
31
+ "fs-extra": "^10.0.1",
32
+ "glob": "^7.2.0",
33
+ "is-wsl": "^2.2.0",
34
+ "yaml": "^1.10.2"
35
+ },
36
+ "devDependencies": {
37
+ "@types/archiver": "^5.1.0",
38
+ "@types/debug": "^4.1.5",
39
+ "@types/fs-extra": "^9.0.12",
40
+ "@types/glob": "^7.1.3",
41
+ "@types/license-checker": "^25.0.1",
42
+ "@types/mocha": "^8.2.2",
43
+ "chai": "^4.3.4",
44
+ "fancy-test": "^1.4.10",
45
+ "license-checker": "^25.0.1",
46
+ "mocha": "^8.3.2",
47
+ "nyc": "^15.1.0",
48
+ "prettier": "^2.5.1",
49
+ "ts-node": "^10.8.2"
50
+ },
51
+ "gitHead": "956064c78cdc0cdcfa085a34ab3d9267bd08e649"
52
+ }
@@ -0,0 +1,120 @@
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 listEndpointsCmd from './commands-v2/list'
6
+ import getPublicAccount from './commands-v2/getPublicAccount'
7
+ import {
8
+ generateConfigurationPayload,
9
+ generateSchemaFilesPayload,
10
+ } from './shared/payloads'
11
+ import {SDKConfigurationV2} from './shared/types'
12
+
13
+ export const ensureValidCredentials = async (
14
+ auth:
15
+ | {account: string; adminKey: string; deploymentType: string}
16
+ | {publicAccountToken: string; deploymentType: string},
17
+ sdkConfig: SDKConfigurationV2,
18
+ ) => {
19
+ let credentials
20
+
21
+ if ('publicAccountToken' in auth) {
22
+ // create an anonymous account and use it to initialize an SDK client instance
23
+ const {data, error} = await getPublicAccount({
24
+ token: auth.publicAccountToken,
25
+ deploymentType: auth.deploymentType,
26
+ sdkConfig,
27
+ })
28
+
29
+ if (error) {
30
+ throw new Error(`An unexpected error occurred: ${error.message}`)
31
+ }
32
+
33
+ credentials = data
34
+ } else {
35
+ // use the provided account to initialize an SDK client instance
36
+ const {data, error} = await accountCmd({
37
+ account: auth.account,
38
+ adminKey: auth.adminKey,
39
+ deploymentType: auth.deploymentType,
40
+ sdkConfig,
41
+ })
42
+
43
+ if (error) {
44
+ throw new Error(error.message)
45
+ }
46
+
47
+ credentials = data
48
+ }
49
+
50
+ return credentials
51
+ }
52
+
53
+ export const createSdkClient = async (
54
+ auth:
55
+ | {account: string; adminKey: string; deploymentType: string}
56
+ | {publicAccountToken: string; deploymentType: string},
57
+ sdkConfig: SDKConfigurationV2,
58
+ ) => {
59
+ const credentials = await ensureValidCredentials(auth, sdkConfig)
60
+
61
+ return {
62
+ get credentials(): typeof credentials {
63
+ // always return a copy to avoid accidential modification by the caller
64
+ return {...credentials}
65
+ },
66
+ /**
67
+ * Create or update a GraphQL endpoint at StepZen.
68
+ * The URL of the endpoint is formed as {StepZen instance URL}/{folder}/{name}/__graphql
69
+ * e.g. https://account.stepzen.net/api/users/__graphql
70
+ *
71
+ * @param folder - folder name (e.g. `api`); will be a part of the endpoint URL
72
+ * @param name - endpoint name (e.g. `users`); will be a part of the endpoint URL
73
+ * @param directory - full path to a GraphQL schema directory (should have `index.graphql` in it)
74
+ * @param config - full path to a config `.yml` file
75
+ * @param public - whether or not to allow public access to the endpoint (`false` by default)
76
+ * @returns details of the created / updated endpoint
77
+ */
78
+ deploy: async ({
79
+ folder,
80
+ name,
81
+ directory,
82
+ config,
83
+ public: _public = false,
84
+ }: {
85
+ folder: string
86
+ name: string
87
+ directory: string
88
+ config?: string
89
+ public?: boolean
90
+ }) => {
91
+ return deployCmd({
92
+ account: credentials.account,
93
+ adminKey: credentials.adminKey,
94
+ deploymentType: credentials.deploymentType,
95
+ endpointName: name,
96
+ endpointType: 'dev',
97
+ folderName: folder,
98
+ public: _public,
99
+ schemaFiles: generateSchemaFilesPayload(directory),
100
+ configuration: await generateConfigurationPayload(config),
101
+ sdkConfig,
102
+ })
103
+ },
104
+ list: {
105
+ /**
106
+ * List deployed endpoints.
107
+ *
108
+ * @returns list of deployed endpoints
109
+ */
110
+ endpoints: () => {
111
+ return listEndpointsCmd({
112
+ account: credentials.account,
113
+ adminKey: credentials.adminKey,
114
+ deploymentType: credentials.deploymentType,
115
+ sdkConfig,
116
+ })
117
+ },
118
+ },
119
+ }
120
+ }
package/src/client.ts ADDED
@@ -0,0 +1,133 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import accountCommand from './commands/account'
4
+ import deploy from './commands/deploy'
5
+ import {
6
+ AnonymousClientOptions,
7
+ SDKConfiguration,
8
+ StepZenAccount,
9
+ } from './shared/types'
10
+ import list from './commands/list'
11
+ import upload from './commands/upload'
12
+ import getPublicAccount from './commands/getPublicAccount'
13
+
14
+ export * from './shared/types'
15
+
16
+ export const validateCreateClientOptions = async (
17
+ options: StepZenAccount | Required<AnonymousClientOptions>,
18
+ sdkConfig: SDKConfiguration,
19
+ ) => {
20
+ let credentials
21
+ let account
22
+
23
+ if ('publicAccountToken' in options) {
24
+ // create an anonymous account and use it to initialize an SDK client instance
25
+ const credentialsOrError = await getPublicAccount(options, sdkConfig)
26
+ if ('errors' in credentialsOrError) {
27
+ throw new Error(
28
+ `An unexpected error occurred. ${JSON.stringify(
29
+ credentialsOrError.errors,
30
+ )}`,
31
+ )
32
+ }
33
+
34
+ credentials = credentialsOrError
35
+ account = {
36
+ ...credentialsOrError,
37
+ server: options.server,
38
+ domain: options.domain,
39
+ }
40
+ } else {
41
+ // use the provided account to initialize an SDK client instance
42
+ options.server = options.server.replace('{account}', options.account)
43
+ let accountOrError
44
+ try {
45
+ accountOrError = await accountCommand(options, sdkConfig)
46
+ } catch (error) {
47
+ if (
48
+ error instanceof Error &&
49
+ error.message.includes('Please check your authentication details')
50
+ ) {
51
+ throw new Error('Your credentials are invalid.')
52
+ } else {
53
+ throw new Error(`An unexpected error occurred. ${error}`)
54
+ }
55
+ }
56
+
57
+ if ('errors' in accountOrError) {
58
+ throw new Error(
59
+ `An unexpected error occurred. ${JSON.stringify(
60
+ accountOrError.errors,
61
+ )}`,
62
+ )
63
+ }
64
+
65
+ account = options
66
+
67
+ credentials = {
68
+ account: options.account,
69
+ adminkey: options.adminkey,
70
+ apikey: accountOrError.apikey,
71
+ }
72
+ }
73
+ return {account, credentials}
74
+ }
75
+
76
+ export const createSdkClient = async (
77
+ options: Required<StepZenAccount | AnonymousClientOptions>,
78
+ sdkConfig: SDKConfiguration,
79
+ ) => {
80
+ const {account, credentials} = await validateCreateClientOptions(
81
+ options,
82
+ sdkConfig,
83
+ )
84
+
85
+ return {
86
+ get credentials(): typeof credentials {
87
+ // always return a copy to avoid accidential modification by the caller
88
+ return {...credentials}
89
+ },
90
+ /**
91
+ * @deprecated use `.credentials` or create a new SDK client instead
92
+ **/
93
+ account: () => {
94
+ return accountCommand(account, sdkConfig)
95
+ },
96
+ deploy: (
97
+ destination: string,
98
+ properties: {
99
+ configurationsets?: string[]
100
+ schema: string
101
+ },
102
+ ) => {
103
+ return deploy({destination, ...properties}, account, sdkConfig)
104
+ },
105
+ list: {
106
+ deployments: async () => {
107
+ return list({type: 'deployments'}, account, sdkConfig)
108
+ },
109
+ configurationsets: async () => {
110
+ return list({type: 'configurationsets'}, account, sdkConfig)
111
+ },
112
+ schemas: async () => {
113
+ return list({type: 'schemas'}, account, sdkConfig)
114
+ },
115
+ },
116
+ upload: {
117
+ configurationset: async (destination: string, file: string) => {
118
+ return upload(
119
+ {type: 'configurationset', destination, file},
120
+ account,
121
+ sdkConfig,
122
+ )
123
+ },
124
+ schema: async (destination: string, directory: string) => {
125
+ return upload(
126
+ {type: 'schema', destination, directory},
127
+ account,
128
+ sdkConfig,
129
+ )
130
+ },
131
+ },
132
+ }
133
+ }
@@ -0,0 +1,54 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from '@stepzen/fetch'
5
+
6
+ import {ADMIN_ACCOUNT_URL} from '../shared/constants'
7
+ import {getRequestHeaders} from '../shared/request'
8
+ import {SDKConfiguration, StepZenAccount} from '../shared/types'
9
+
10
+ export default async (
11
+ account: StepZenAccount,
12
+ sdkConfig: SDKConfiguration,
13
+ ): Promise<
14
+ {account: string; apikey: string} | {success: false; errors: string[]}
15
+ > => {
16
+ const headers = getRequestHeaders(account, sdkConfig)
17
+ const url = `${account.server}${ADMIN_ACCOUNT_URL}`
18
+
19
+ debug('stepzen:headers')(headers)
20
+
21
+ const response = await fetch(url, {
22
+ headers: headers as any,
23
+ method: 'POST',
24
+ })
25
+
26
+ debug('stepzen:response')(response)
27
+
28
+ if (response.status === 401 || response.status === 403) {
29
+ throw new Error(
30
+ 'Could not complete the request. Please check your authentication details are correct.',
31
+ )
32
+ }
33
+
34
+ if (response.status >= 400) {
35
+ return {
36
+ errors: [response.statusText],
37
+ success: false,
38
+ }
39
+ }
40
+
41
+ const text = await response.text()
42
+ let json
43
+
44
+ try {
45
+ json = JSON.parse(text)
46
+ } catch {
47
+ return {
48
+ errors: [`An unexpected error occurred.\n\n${text}`],
49
+ success: false,
50
+ }
51
+ }
52
+
53
+ return json
54
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from '@stepzen/fetch'
5
+
6
+ import {ADMIN_VERIFY_URL} from '../shared/constants'
7
+ import {getRequestHeaders} from '../shared/request'
8
+ import {SDKConfiguration, StepZenAccount} from '../shared/types'
9
+
10
+ export default async (
11
+ settings: StepZenAccount,
12
+ sdkConfig: SDKConfiguration,
13
+ ): Promise<boolean> => {
14
+ const headers = getRequestHeaders(settings, sdkConfig)
15
+ const url = `${settings.server}${ADMIN_VERIFY_URL}`
16
+
17
+ debug('stepzen:headers')(headers)
18
+ debug('stepzen:url')(url)
19
+
20
+ try {
21
+ const response = await fetch(url, {
22
+ headers: headers as any,
23
+ method: 'POST',
24
+ })
25
+ await response.json()
26
+ return true
27
+ } catch {
28
+ return false
29
+ }
30
+ }
@@ -0,0 +1,72 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from '@stepzen/fetch'
5
+
6
+ import {ADMIN_DEPLOY_URL} from '../shared/constants'
7
+ import {getRequestHeaders} from '../shared/request'
8
+ import {
9
+ SDKConfiguration,
10
+ StepZenAccount,
11
+ StepZenDeploy,
12
+ ZenCtlResponse,
13
+ } from '../shared/types'
14
+
15
+ export default async (
16
+ details: StepZenDeploy,
17
+ account: StepZenAccount,
18
+ sdkConfig: SDKConfiguration,
19
+ ): Promise<ZenCtlResponse> => {
20
+ const headers = getRequestHeaders(account, sdkConfig)
21
+
22
+ const payload: any = {
23
+ schema: `/workspaces/schemas/${details.schema}`,
24
+ }
25
+
26
+ if (details.configurationsets) {
27
+ payload.configurationsets = details.configurationsets.map(
28
+ config => `/workspaces/configurationsets/${config}`,
29
+ )
30
+ }
31
+
32
+ debug('stepzen:headers')(headers)
33
+ debug('stepzen:payload')(payload)
34
+
35
+ const response = await fetch(
36
+ `${account.server}${ADMIN_DEPLOY_URL}/${details.destination}`,
37
+ {
38
+ body: JSON.stringify(payload),
39
+ headers: headers as any,
40
+ method: 'POST',
41
+ },
42
+ )
43
+
44
+ debug('stepzen:response')(response)
45
+
46
+ if (response.status >= 400 && response.status < 500) {
47
+ throw new Error(
48
+ 'Could not complete the request. Please check your authentication details are correct.',
49
+ )
50
+ }
51
+
52
+ if (response.status >= 500) {
53
+ return {
54
+ errors: [response.statusText],
55
+ success: false,
56
+ }
57
+ }
58
+
59
+ const text = await response.text()
60
+ let json
61
+
62
+ try {
63
+ json = JSON.parse(text)
64
+ } catch {
65
+ return {
66
+ errors: [`An unexpected error occurred.\n\n${text}`],
67
+ success: false,
68
+ }
69
+ }
70
+
71
+ return json
72
+ }
@@ -0,0 +1,77 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from '@stepzen/fetch'
5
+
6
+ import {getUserAgent} from '../shared/request'
7
+ import {
8
+ AnonymousClientOptions,
9
+ SDKConfiguration,
10
+ StepZenCredentials,
11
+ } from '../shared/types'
12
+
13
+ export default async (
14
+ options: Required<AnonymousClientOptions>,
15
+ sdkConfig: SDKConfiguration,
16
+ ): Promise<
17
+ | StepZenCredentials
18
+ | {
19
+ success: false
20
+ errors: string[]
21
+ }
22
+ > => {
23
+ const account = process.env.STEPZEN_PUBLIC_ACCOUNT_API_ACCOUNT || 'stepzen'
24
+ const endpoint =
25
+ process.env.STEPZEN_PUBLIC_ACCOUNT_API_ENDPOINT || 'api/publicaccount'
26
+ const server = options.server
27
+ .replace('{account}', account)
28
+ .replace('.io', '.net')
29
+
30
+ const url = new URL(`${server}/${endpoint}/__graphql`)
31
+ // Inlclude the token into the URL so that it is visible in the logs
32
+ // (allows StepZen to do analytics based on the GCP logs).
33
+ url.searchParams.set('token', options.publicAccountToken)
34
+
35
+ try {
36
+ debug('stepzen:createAnonymousAccount')(url)
37
+ const response = await fetch(url, {
38
+ method: 'POST',
39
+ headers: {
40
+ 'Content-Type': 'application/json',
41
+ 'User-Agent': getUserAgent(sdkConfig),
42
+ },
43
+ body: JSON.stringify({
44
+ query: `query (
45
+ $token: String!
46
+ ) {
47
+ getAccountDetails(
48
+ token: $token
49
+ ) {
50
+ account: accountName
51
+ adminkey: adminKey
52
+ apikey: apiKey
53
+ }
54
+ }`,
55
+ variables: {
56
+ token: options.publicAccountToken,
57
+ },
58
+ }),
59
+ })
60
+ const json = await response.json()
61
+ debug('stepzen:createAnonymousAccount')(json)
62
+ if (json.errors) {
63
+ return {
64
+ success: false,
65
+ errors: json.errors,
66
+ }
67
+ }
68
+
69
+ if (!json.data?.getAccountDetails) {
70
+ throw new Error('No data returned from the API endpoint.')
71
+ }
72
+
73
+ return json.data?.getAccountDetails
74
+ } catch (error) {
75
+ throw new Error(`Could not create a public account (${error})`)
76
+ }
77
+ }
@@ -0,0 +1,50 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from '@stepzen/fetch'
5
+
6
+ import {ADMIN_LIST_URL} from '../shared/constants'
7
+ import {getRequestHeaders} from '../shared/request'
8
+ import {
9
+ SDKConfiguration,
10
+ StepZenAccount,
11
+ StepZenList,
12
+ ZenCtlResponse,
13
+ } from '../shared/types'
14
+
15
+ export default async (
16
+ details: StepZenList,
17
+ account: StepZenAccount,
18
+ sdkConfig: SDKConfiguration,
19
+ ): Promise<ZenCtlResponse> => {
20
+ const headers = getRequestHeaders(account, sdkConfig)
21
+
22
+ debug('stepzen:headers')(headers)
23
+
24
+ const response = await fetch(
25
+ `${account.server}${ADMIN_LIST_URL}/${details.type}`,
26
+ {
27
+ headers: headers as any,
28
+ method: 'GET',
29
+ },
30
+ )
31
+
32
+ debug('stepzen:response')(response)
33
+
34
+ if (response.status >= 400 && response.status < 500) {
35
+ throw new Error(
36
+ 'Could not complete the request. Please check your authentication details are correct.',
37
+ )
38
+ }
39
+
40
+ const text = await response.text()
41
+ let json
42
+
43
+ try {
44
+ json = JSON.parse(text)
45
+ } catch {
46
+ throw new Error(`An unexpected error occurred.\n\n${text}`)
47
+ }
48
+
49
+ return json
50
+ }
@@ -0,0 +1,91 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from '@stepzen/fetch'
5
+
6
+ import {ADMIN_UPLOAD_URL} from '../shared/constants'
7
+ import {generateYamlPayload, generateZipPayload} from '../shared/payloads'
8
+ import {getRequestHeaders} from '../shared/request'
9
+ import {
10
+ SDKConfiguration,
11
+ StepZenAccount,
12
+ StepZenUpload,
13
+ ZenCtlResponse,
14
+ } from '../shared/types'
15
+ import {transpileConfigurationset} from '../shared/transpiling'
16
+ import {validateConfigurationset, validateSchema} from '../shared/validation'
17
+
18
+ export default async (
19
+ details: StepZenUpload,
20
+ account: StepZenAccount,
21
+ sdkConfig: SDKConfiguration,
22
+ ): Promise<ZenCtlResponse> => {
23
+ const headers = getRequestHeaders(account, sdkConfig)
24
+ let payload
25
+
26
+ switch (details.type) {
27
+ case 'configurationset':
28
+ await validateConfigurationset(details.file)
29
+ const transpiled = await transpileConfigurationset(details.file!)
30
+ debug('stepzen:sdk')(`effective config written to ${transpiled}`)
31
+ payload = await generateYamlPayload(transpiled)
32
+ break
33
+
34
+ case 'schema':
35
+ await validateSchema(details.directory)
36
+ payload = await generateZipPayload(
37
+ details.directory,
38
+ {
39
+ destination: details.destination,
40
+ stitcherName: 'index.graphql',
41
+ },
42
+ [/.*\.graphql$/i],
43
+ )
44
+ break
45
+ }
46
+
47
+ debug('stepzen:headers')(headers)
48
+ debug('stepzen:payload')(payload)
49
+
50
+ const response = await fetch(
51
+ `${account.server}${ADMIN_UPLOAD_URL}/${details.type}/${details.destination}`,
52
+ {
53
+ body: payload,
54
+ headers: headers as any,
55
+ method: 'POST',
56
+ },
57
+ )
58
+
59
+ debug('stepzen:response')(response)
60
+
61
+ if (response.status >= 400 && response.status < 500) {
62
+ return {
63
+ message:
64
+ 'Could not complete the request. Please check your authentication details are correct.',
65
+ success: false,
66
+ }
67
+ }
68
+
69
+ if (response.status >= 500) {
70
+ return {
71
+ errors: ['Internal Server Error'],
72
+ message: response.statusText,
73
+ success: false,
74
+ }
75
+ }
76
+
77
+ const text = await response.text()
78
+ let json
79
+
80
+ try {
81
+ json = JSON.parse(text)
82
+ } catch {
83
+ return {
84
+ errors: [`An unexpected error occurred.\n\n${text}`],
85
+ message: `An unexpected error occurred.\n\n${text}`,
86
+ success: false,
87
+ }
88
+ }
89
+
90
+ return json
91
+ }