@stepzen/sdk 0.9.41 → 0.9.44

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 (48) hide show
  1. package/README.md +14 -14
  2. package/lib/commands/account.d.ts +9 -0
  3. package/lib/commands/account.js +39 -0
  4. package/lib/commands/account.js.map +1 -0
  5. package/lib/commands/authenticate.d.ts +2 -2
  6. package/lib/commands/authenticate.js +6 -5
  7. package/lib/commands/authenticate.js.map +1 -0
  8. package/lib/commands/deploy.d.ts +2 -2
  9. package/lib/commands/deploy.js +9 -8
  10. package/lib/commands/deploy.js.map +1 -0
  11. package/lib/commands/list.d.ts +2 -2
  12. package/lib/commands/list.js +7 -6
  13. package/lib/commands/list.js.map +1 -0
  14. package/lib/commands/upload.d.ts +2 -2
  15. package/lib/commands/upload.js +13 -12
  16. package/lib/commands/upload.js.map +1 -0
  17. package/lib/index.d.ts +22 -5
  18. package/lib/index.js +72 -35
  19. package/lib/index.js.map +1 -0
  20. package/lib/shared/constants.d.ts +1 -0
  21. package/lib/shared/constants.js +7 -5
  22. package/lib/shared/constants.js.map +1 -0
  23. package/lib/shared/payloads.d.ts +1 -1
  24. package/lib/shared/payloads.js +12 -11
  25. package/lib/shared/payloads.js.map +1 -0
  26. package/lib/shared/request.d.ts +2 -2
  27. package/lib/shared/request.js +13 -7
  28. package/lib/shared/request.js.map +1 -0
  29. package/lib/shared/transpiling.js +4 -3
  30. package/lib/shared/transpiling.js.map +1 -0
  31. package/lib/shared/types.d.ts +36 -3
  32. package/lib/shared/types.js +1 -0
  33. package/lib/shared/types.js.map +1 -0
  34. package/lib/shared/validation.js +9 -8
  35. package/lib/shared/validation.js.map +1 -0
  36. package/package.json +18 -9
  37. package/src/commands/account.ts +54 -0
  38. package/src/commands/authenticate.ts +30 -0
  39. package/src/commands/deploy.ts +72 -0
  40. package/src/commands/list.ts +50 -0
  41. package/src/commands/upload.ts +95 -0
  42. package/src/index.ts +121 -0
  43. package/src/shared/constants.ts +7 -0
  44. package/src/shared/payloads.ts +105 -0
  45. package/src/shared/request.ts +26 -0
  46. package/src/shared/transpiling.ts +36 -0
  47. package/src/shared/types.ts +75 -0
  48. package/src/shared/validation.ts +42 -0
@@ -6,13 +6,13 @@ const dotenv = require("dotenv");
6
6
  const fs = require("fs-extra");
7
7
  const os = require("os");
8
8
  const path = require("path");
9
- const { transpile } = require("@stepzen/transpiler");
9
+ const { transpile } = require('@stepzen/transpiler');
10
10
  const transpileConfigurationset = async (file) => {
11
11
  if (!file) {
12
12
  return;
13
13
  }
14
- const source = file.substring(0, file.lastIndexOf("/"));
15
- dotenv.config({ path: path.resolve(source, ".env") });
14
+ const source = file.substring(0, file.lastIndexOf('/'));
15
+ dotenv.config({ path: path.resolve(source, '.env') });
16
16
  const tmp = path.join(os.tmpdir(), `stepzen-transpiler-${Date.now()}`);
17
17
  const configPath = path.join(tmp, 'config.yaml');
18
18
  fs.ensureDirSync(tmp);
@@ -27,3 +27,4 @@ const transpileConfigurationset = async (file) => {
27
27
  return;
28
28
  };
29
29
  exports.transpileConfigurationset = transpileConfigurationset;
30
+ //# sourceMappingURL=transpiling.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transpiling.js","sourceRoot":"","sources":["../../src/shared/transpiling.ts"],"names":[],"mappings":";AAAA,8CAA8C;;;AAE9C,iCAAgC;AAChC,+BAA8B;AAC9B,yBAAwB;AACxB,6BAA4B;AAE5B,MAAM,EAAC,SAAS,EAAC,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAA;AAE3C,MAAM,yBAAyB,GAAG,KAAK,EAC5C,IAAwB,EACK,EAAE;IAC/B,IAAI,CAAC,IAAI,EAAE;QACT,OAAM;KACP;IAED,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAA;IACvD,MAAM,CAAC,MAAM,CAAC,EAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,EAAC,CAAC,CAAA;IAEnD,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,EAAE,sBAAsB,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAA;IACtE,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,aAAa,CAAC,CAAA;IAEhD,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,CAAA;IACrB,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;IAEjC,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,GAAG,CAAC,CAAA;IAEnC,IAAI,MAAM,CAAC,UAAU,EAAE;QACrB,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;QACpB,EAAE,CAAC,aAAa,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;QAC3C,OAAO,UAAU,CAAA;KAClB;IAED,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;IAClB,OAAM;AACR,CAAC,CAAA;AA1BY,QAAA,yBAAyB,6BA0BrC"}
@@ -1,8 +1,31 @@
1
1
  export interface StepZenAccount {
2
2
  account: string;
3
3
  adminkey: string;
4
- domain: string;
4
+ /**
5
+ * ZenCtl base URL, e.g. `https://fakefish.stepzen.io` for production,
6
+ * or `http://localhost` for local tests.
7
+ *
8
+ * Determined by taking the first option that is defined:
9
+ * - the `server` config property explicily passed into the `init()` function
10
+ * - the `STEPZEN_SERVER_URL` environment variable
11
+ * - the `https://{account}.stepzen.io` default value
12
+ *
13
+ * The `{account}` placeholder (if present) is replaced by the provided account
14
+ * name
15
+ */
5
16
  server: string;
17
+ /**
18
+ * ZenCtl domain, e.g. `stepzen.io`. This is only relevant for local testing
19
+ * when ZenCtl is running on localhost. In that case an additional `Host` HTTP
20
+ * header is added to requests, using a non-localhost host name constructed
21
+ * from this property.
22
+ *
23
+ * Determined by taking the first option that is defined:
24
+ * - the `domain` config property explicily passed into the `init()` function
25
+ * - the `STEPZEN_DOMAIN` environment variable
26
+ * - the `stepzen.io` default value
27
+ */
28
+ domain: string;
6
29
  }
7
30
  export interface StepZenDeploy {
8
31
  configurationsets?: string[];
@@ -21,8 +44,8 @@ export interface StepZenUpload {
21
44
  export interface ZenCtlRequestHeaders {
22
45
  authorization: string;
23
46
  host: string;
24
- "stepzen-cli-version": string;
25
- "user-agent": string;
47
+ 'stepzen-cli-version': string;
48
+ 'user-agent': string;
26
49
  }
27
50
  export interface ZenCtlResponse {
28
51
  errors?: Array<string>;
@@ -30,3 +53,13 @@ export interface ZenCtlResponse {
30
53
  results?: Array<string>;
31
54
  success: boolean;
32
55
  }
56
+ export interface SDKConfiguration {
57
+ /**
58
+ * The name and version of that app that uses the SDK,
59
+ * e.g. `stepzen-cli/0.9.32`
60
+ *
61
+ * It is appended to the user-agent string in all requests made to StepZen
62
+ * through the SDK and becomes availabe in the log analytics.
63
+ */
64
+ appName: string;
65
+ }
@@ -1,3 +1,4 @@
1
1
  "use strict";
2
2
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/shared/types.ts"],"names":[],"mappings":";AAAA,8CAA8C"}
@@ -8,33 +8,34 @@ const yaml = require("yaml");
8
8
  // Validate the Configurationset file
9
9
  const validateConfigurationset = async (file) => {
10
10
  if (!file) {
11
- throw new Error("You must provide a file path");
11
+ throw new Error('You must provide a file path');
12
12
  }
13
13
  if (!fs.existsSync(file)) {
14
- throw new Error("The file does not exist");
14
+ throw new Error('The file does not exist');
15
15
  }
16
- const content = fs.readFileSync(file, "utf8");
16
+ const content = fs.readFileSync(file, 'utf8');
17
17
  // Ensure the file is valid YAML
18
18
  try {
19
19
  yaml.parse(content);
20
20
  }
21
21
  catch (_a) {
22
- throw new Error("The file is not valid YAML");
22
+ throw new Error('The file is not valid YAML');
23
23
  }
24
24
  };
25
25
  exports.validateConfigurationset = validateConfigurationset;
26
26
  // Validate the Schema directory
27
27
  const validateSchema = async (directory) => {
28
28
  if (!directory) {
29
- throw new Error("You must provide a directory path");
29
+ throw new Error('You must provide a directory path');
30
30
  }
31
31
  if (!fs.existsSync(directory)) {
32
- throw new Error("The directory does not exist");
32
+ throw new Error('The directory does not exist');
33
33
  }
34
34
  // Ensure there's a root `index.graphql` file
35
- const allSchemaFiles = glob.sync("index.graphql", { cwd: directory });
35
+ const allSchemaFiles = glob.sync('index.graphql', { cwd: directory });
36
36
  if (allSchemaFiles.length === 0) {
37
- throw new Error("Schemas must include an `index.graphql` file");
37
+ throw new Error('Schemas must include an `index.graphql` file');
38
38
  }
39
39
  };
40
40
  exports.validateSchema = validateSchema;
41
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/shared/validation.ts"],"names":[],"mappings":";AAAA,8CAA8C;;;AAE9C,yBAAwB;AACxB,6BAA4B;AAC5B,6BAA4B;AAE5B,qCAAqC;AAC9B,MAAM,wBAAwB,GAAG,KAAK,EAAE,IAAwB,EAAE,EAAE;IACzE,IAAI,CAAC,IAAI,EAAE;QACT,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;KAChD;IAED,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACxB,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;KAC3C;IAED,MAAM,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IAE7C,gCAAgC;IAChC,IAAI;QACF,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;KACpB;IAAC,WAAM;QACN,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;KAC9C;AACH,CAAC,CAAA;AAjBY,QAAA,wBAAwB,4BAiBpC;AAED,gCAAgC;AACzB,MAAM,cAAc,GAAG,KAAK,EAAE,SAA6B,EAAE,EAAE;IACpE,IAAI,CAAC,SAAS,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAA;KACrD;IAED,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE;QAC7B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;KAChD;IAED,6CAA6C;IAC7C,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,EAAC,GAAG,EAAE,SAAS,EAAC,CAAC,CAAA;IACnE,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE;QAC/B,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAA;KAChE;AACH,CAAC,CAAA;AAdY,QAAA,cAAc,kBAc1B"}
package/package.json CHANGED
@@ -1,31 +1,35 @@
1
1
  {
2
2
  "name": "@stepzen/sdk",
3
- "version": "0.9.41",
3
+ "version": "0.9.44",
4
4
  "license": "MIT",
5
5
  "author": "Darren Waddell <darren@stepzen.com>",
6
6
  "homepage": "https://stepzen.com",
7
7
  "files": [
8
- "lib/**/*"
8
+ "lib/**/*",
9
+ "src/**/*"
9
10
  ],
10
11
  "engines": {
11
12
  "node": ">=14.0.1",
12
13
  "npm": ">=6.14"
13
14
  },
14
15
  "main": "lib/index.js",
16
+ "types": "lib/index.d.ts",
15
17
  "scripts": {
18
+ "prepare": "husky install",
16
19
  "build": "tsc -b",
17
20
  "prepack": "rm -rf lib && tsc -b ",
18
- "test": "tsc -b && nyc --extension .ts mocha --config test/mocha.opts.json"
21
+ "test": "tsc -b && nyc --extension .ts mocha --config test/mocha.opts.json",
22
+ "posttest": "prettier . --check"
19
23
  },
20
24
  "dependencies": {
21
- "@stepzen/transpiler": "0.0.24",
25
+ "@stepzen/transpiler": "0.0.36",
22
26
  "archiver": "^5.3.0",
23
- "debug": "^4.3.2",
27
+ "debug": "^4.3.3",
24
28
  "form-data": "^4.0.0",
25
29
  "fs-extra": "^10.0.0",
26
- "glob": "^7.1.6",
27
- "node-fetch": "^2.6.6",
28
- "tslib": "^2.2.0",
30
+ "glob": "^7.2.0",
31
+ "is-wsl": "^2.2.0",
32
+ "node-fetch": "^2.6.7",
29
33
  "yaml": "^1.10.2"
30
34
  },
31
35
  "devDependencies": {
@@ -38,11 +42,16 @@
38
42
  "@types/node-fetch": "^2.5.10",
39
43
  "chai": "^4.3.4",
40
44
  "fancy-test": "^1.4.10",
45
+ "husky": "^7.0.4",
41
46
  "license-checker": "^25.0.1",
47
+ "lint-staged": "^12.3.4",
42
48
  "mocha": "^8.3.2",
43
- "node-ts": "^5.1.1",
44
49
  "nyc": "^15.1.0",
50
+ "prettier": "^2.5.1",
45
51
  "ts-node": "^9.1.1",
46
52
  "typescript": "^4.2.4"
53
+ },
54
+ "lint-staged": {
55
+ "*.{ts,js,css,md,yaml,json}": "prettier --write"
47
56
  }
48
57
  }
@@ -0,0 +1,54 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from 'node-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 >= 400 && response.status < 500) {
29
+ throw new Error(
30
+ 'Could not complete the request. Please check your authentication details are correct.',
31
+ )
32
+ }
33
+
34
+ if (response.status >= 500) {
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 'node-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 'node-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,50 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from 'node-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,95 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from 'node-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:transpiler')(transpiled)
31
+ if (transpiled) {
32
+ payload = await generateYamlPayload(transpiled)
33
+ } else {
34
+ payload = await generateYamlPayload(details.file)
35
+ }
36
+ break
37
+
38
+ case 'schema':
39
+ await validateSchema(details.directory)
40
+ payload = await generateZipPayload(
41
+ details.directory,
42
+ {
43
+ destination: details.destination,
44
+ stitcherName: 'index.graphql',
45
+ },
46
+ [/.*\.graphql$/i],
47
+ )
48
+ break
49
+ }
50
+
51
+ debug('stepzen:headers')(headers)
52
+ debug('stepzen:payload')(payload)
53
+
54
+ const response = await fetch(
55
+ `${account.server}${ADMIN_UPLOAD_URL}/${details.type}/${details.destination}`,
56
+ {
57
+ body: payload,
58
+ headers: headers as any,
59
+ method: 'POST',
60
+ },
61
+ )
62
+
63
+ debug('stepzen:response')(response)
64
+
65
+ if (response.status >= 400 && response.status < 500) {
66
+ return {
67
+ message:
68
+ 'Could not complete the request. Please check your authentication details are correct.',
69
+ success: false,
70
+ }
71
+ }
72
+
73
+ if (response.status >= 500) {
74
+ return {
75
+ errors: ['Internal Server Error'],
76
+ message: response.statusText,
77
+ success: false,
78
+ }
79
+ }
80
+
81
+ const text = await response.text()
82
+ let json
83
+
84
+ try {
85
+ json = JSON.parse(text)
86
+ } catch {
87
+ return {
88
+ errors: [`An unexpected error occurred.\n\n${text}`],
89
+ message: `An unexpected error occurred.\n\n${text}`,
90
+ success: false,
91
+ }
92
+ }
93
+
94
+ return json
95
+ }
package/src/index.ts ADDED
@@ -0,0 +1,121 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as path from 'path'
4
+ import accountCommand from './commands/account'
5
+ import authenticate from './commands/authenticate'
6
+ import deploy from './commands/deploy'
7
+ import {SDKConfiguration, StepZenAccount} from './shared/types'
8
+ import list from './commands/list'
9
+ import upload from './commands/upload'
10
+
11
+ export * from './shared/types'
12
+
13
+ export const init = (sdkConfig: SDKConfiguration) => {
14
+ const defaults = {
15
+ domain: process.env.STEPZEN_DOMAIN || 'stepzen.io',
16
+ server: process.env.STEPZEN_SERVER_URL || 'https://{account}.stepzen.io',
17
+ }
18
+
19
+ return {
20
+ verify: (account: string, adminkey: string) => {
21
+ return authenticate(
22
+ {
23
+ account,
24
+ adminkey,
25
+ server: defaults.server.replace('{account}', account),
26
+ domain: defaults.domain,
27
+ },
28
+ sdkConfig,
29
+ )
30
+ },
31
+ client: async (
32
+ options: Pick<StepZenAccount, 'account' | 'adminkey'> &
33
+ Partial<StepZenAccount>,
34
+ ) => {
35
+ if (!options.account) {
36
+ throw new Error('You must provide an account.')
37
+ }
38
+
39
+ if (!options.adminkey) {
40
+ throw new Error('You must provide an admin key.')
41
+ }
42
+
43
+ const account: StepZenAccount = {
44
+ ...defaults,
45
+ ...options,
46
+ }
47
+
48
+ account.server = account.server.replace('{account}', options.account)
49
+ const authorised = await authenticate(account, sdkConfig)
50
+
51
+ if (!authorised) {
52
+ throw new Error('Your credentials are invalid.')
53
+ }
54
+
55
+ return {
56
+ account: () => {
57
+ return accountCommand(account, sdkConfig)
58
+ },
59
+ deploy: (
60
+ destination: string,
61
+ properties: {
62
+ configurationsets?: string[]
63
+ schema: string
64
+ },
65
+ ) => {
66
+ return deploy({destination, ...properties}, account, sdkConfig)
67
+ },
68
+ list: {
69
+ configurationsets: async () => {
70
+ return list({type: 'configurationsets'}, account, sdkConfig)
71
+ },
72
+ schemas: async () => {
73
+ return list({type: 'schemas'}, account, sdkConfig)
74
+ },
75
+ },
76
+ upload: {
77
+ configurationset: async (destination: string, file: string) => {
78
+ return upload(
79
+ {type: 'configurationset', destination, file},
80
+ account,
81
+ sdkConfig,
82
+ )
83
+ },
84
+ schema: async (destination: string, directory: string) => {
85
+ return upload(
86
+ {type: 'schema', destination, directory},
87
+ account,
88
+ sdkConfig,
89
+ )
90
+ },
91
+ },
92
+ }
93
+ },
94
+ }
95
+ }
96
+
97
+ // a helper type to unwrap Promise<U> into U
98
+ type PromisedType<T> = T extends Promise<infer U> ? U : T
99
+
100
+ export type SDK = ReturnType<typeof init>
101
+ export type SDKClient = PromisedType<ReturnType<SDK['client']>>
102
+
103
+ /**
104
+ * The default SDK instance that does not know the name of the app using the SDK.
105
+ * It tries to guess the app name from `process.argv`
106
+ *
107
+ * @deprecated use the init({appName: 'my-app/1.2.3'}) method to initialis an SDK instance
108
+ */
109
+ const stepzen: SDK = init({
110
+ // For legacy apps use the script name (e.g. `index.js` as the app name)
111
+ // Fallback to `node` if this file is imported into an interactive node shell
112
+ appName: path.basename(process.argv[1] || process.argv[0]),
113
+ })
114
+
115
+ export default stepzen
116
+
117
+ // Hack: support both ESM-style imports: `import stepzen from '@stepzen/sdk'`
118
+ // and keep the backwards-compat with CJS-style imports: `const stepzen = require('@stepzen/sdk')`
119
+ // NOTE: if at any point in the future there is a name collision between `stepzen` and ES exports
120
+ // this hack will break
121
+ module.exports = {...module.exports, ...stepzen}
@@ -0,0 +1,7 @@
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'