@stepzen/sdk 0.10.0 → 0.11.2

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.
@@ -0,0 +1,35 @@
1
+ import { AnonymousClientOptions, SDKConfiguration, StepZenAccount, StepZenCredentials } from './shared/types';
2
+ export * from './shared/types';
3
+ export declare const validateCreateClientOptions: (options: StepZenAccount | Required<AnonymousClientOptions>, sdkConfig: SDKConfiguration) => Promise<{
4
+ account: StepZenAccount;
5
+ credentials: StepZenCredentials;
6
+ }>;
7
+ export declare const createSdkClient: (options: Required<StepZenAccount | AnonymousClientOptions>, sdkConfig: SDKConfiguration) => Promise<{
8
+ readonly credentials: {
9
+ account: string;
10
+ adminkey: string;
11
+ apikey: string;
12
+ };
13
+ /**
14
+ * @deprecated use `.credentials` or create a new SDK client instead
15
+ **/
16
+ account: () => Promise<{
17
+ account: string;
18
+ apikey: string;
19
+ } | {
20
+ success: false;
21
+ errors: string[];
22
+ }>;
23
+ deploy: (destination: string, properties: {
24
+ configurationsets?: string[];
25
+ schema: string;
26
+ }) => Promise<import("./shared/types").ZenCtlResponse>;
27
+ list: {
28
+ configurationsets: () => Promise<import("./shared/types").ZenCtlResponse>;
29
+ schemas: () => Promise<import("./shared/types").ZenCtlResponse>;
30
+ };
31
+ upload: {
32
+ configurationset: (destination: string, file: string) => Promise<import("./shared/types").ZenCtlResponse>;
33
+ schema: (destination: string, directory: string) => Promise<import("./shared/types").ZenCtlResponse>;
34
+ };
35
+ }>;
package/lib/client.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.createSdkClient = exports.validateCreateClientOptions = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const account_1 = require("./commands/account");
7
+ const deploy_1 = require("./commands/deploy");
8
+ const list_1 = require("./commands/list");
9
+ const upload_1 = require("./commands/upload");
10
+ const getPublicAccount_1 = require("./commands/getPublicAccount");
11
+ tslib_1.__exportStar(require("./shared/types"), exports);
12
+ const validateCreateClientOptions = async (options, sdkConfig) => {
13
+ let credentials;
14
+ let account;
15
+ if ('publicAccountToken' in options) {
16
+ // create an anonymous account and use it to initialize an SDK client instance
17
+ const credentialsOrError = await getPublicAccount_1.default(options, sdkConfig);
18
+ if ('errors' in credentialsOrError) {
19
+ throw new Error(`An unexpected error occurred. ${JSON.stringify(credentialsOrError.errors)}`);
20
+ }
21
+ credentials = credentialsOrError;
22
+ account = Object.assign(Object.assign({}, credentialsOrError), { server: options.server, domain: options.domain });
23
+ }
24
+ else {
25
+ // use the provided account to initialize an SDK client instance
26
+ options.server = options.server.replace('{account}', options.account);
27
+ let accountOrError;
28
+ try {
29
+ accountOrError = await account_1.default(options, sdkConfig);
30
+ }
31
+ catch (error) {
32
+ if (error instanceof Error &&
33
+ error.message.includes('Please check your authentication details')) {
34
+ throw new Error('Your credentials are invalid.');
35
+ }
36
+ else {
37
+ throw new Error(`An unexpected error occurred. ${error}`);
38
+ }
39
+ }
40
+ if ('errors' in accountOrError) {
41
+ throw new Error(`An unexpected error occurred. ${JSON.stringify(accountOrError.errors)}`);
42
+ }
43
+ account = options;
44
+ credentials = {
45
+ account: options.account,
46
+ adminkey: options.adminkey,
47
+ apikey: accountOrError.apikey,
48
+ };
49
+ }
50
+ return { account, credentials };
51
+ };
52
+ exports.validateCreateClientOptions = validateCreateClientOptions;
53
+ const createSdkClient = async (options, sdkConfig) => {
54
+ const { account, credentials } = await exports.validateCreateClientOptions(options, sdkConfig);
55
+ return {
56
+ get credentials() {
57
+ // always return a copy to avoid accidential modification by the caller
58
+ return Object.assign({}, credentials);
59
+ },
60
+ /**
61
+ * @deprecated use `.credentials` or create a new SDK client instead
62
+ **/
63
+ account: () => {
64
+ return account_1.default(account, sdkConfig);
65
+ },
66
+ deploy: (destination, properties) => {
67
+ return deploy_1.default(Object.assign({ destination }, properties), account, sdkConfig);
68
+ },
69
+ list: {
70
+ configurationsets: async () => {
71
+ return list_1.default({ type: 'configurationsets' }, account, sdkConfig);
72
+ },
73
+ schemas: async () => {
74
+ return list_1.default({ type: 'schemas' }, account, sdkConfig);
75
+ },
76
+ },
77
+ upload: {
78
+ configurationset: async (destination, file) => {
79
+ return upload_1.default({ type: 'configurationset', destination, file }, account, sdkConfig);
80
+ },
81
+ schema: async (destination, directory) => {
82
+ return upload_1.default({ type: 'schema', destination, directory }, account, sdkConfig);
83
+ },
84
+ },
85
+ };
86
+ };
87
+ exports.createSdkClient = createSdkClient;
88
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":";AAAA,8CAA8C;;;;AAE9C,gDAA+C;AAC/C,8CAAsC;AAOtC,0CAAkC;AAClC,8CAAsC;AACtC,kEAA0D;AAE1D,yDAA8B;AAEvB,MAAM,2BAA2B,GAAG,KAAK,EAC9C,OAA0D,EAC1D,SAA2B,EAC3B,EAAE;IACF,IAAI,WAA+B,CAAA;IACnC,IAAI,OAAuB,CAAA;IAE3B,IAAI,oBAAoB,IAAI,OAAO,EAAE;QACnC,8EAA8E;QAC9E,MAAM,kBAAkB,GAAG,MAAM,0BAAgB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;QACrE,IAAI,QAAQ,IAAI,kBAAkB,EAAE;YAClC,MAAM,IAAI,KAAK,CACb,iCAAiC,IAAI,CAAC,SAAS,CAC7C,kBAAkB,CAAC,MAAM,CAC1B,EAAE,CACJ,CAAA;SACF;QAED,WAAW,GAAG,kBAAkB,CAAA;QAChC,OAAO,mCACF,kBAAkB,KACrB,MAAM,EAAE,OAAO,CAAC,MAAM,EACtB,MAAM,EAAE,OAAO,CAAC,MAAM,GACvB,CAAA;KACF;SAAM;QACL,gEAAgE;QAChE,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,OAAO,CAAC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,IAAK,UAAU,GAAG,OAAO,EAAE,SAAS,CAAC,CAAA;QACjE,CAAC;QACD,IAAI,EAAE;YACJ,iBAAiB,EAAE,KAAK,IAAI,EAAE;gBAC5B,OAAO,cAAI,CAAC,EAAC,IAAI,EAAE,mBAAmB,EAAC,EAAE,OAAO,EAAE,SAAS,CAAC,CAAA;YAC9D,CAAC;YACD,OAAO,EAAE,KAAK,IAAI,EAAE;gBAClB,OAAO,cAAI,CAAC,EAAC,IAAI,EAAE,SAAS,EAAC,EAAE,OAAO,EAAE,SAAS,CAAC,CAAA;YACpD,CAAC;SACF;QACD,MAAM,EAAE;YACN,gBAAgB,EAAE,KAAK,EAAE,WAAmB,EAAE,IAAY,EAAE,EAAE;gBAC5D,OAAO,gBAAM,CACX,EAAC,IAAI,EAAE,kBAAkB,EAAE,WAAW,EAAE,IAAI,EAAC,EAC7C,OAAO,EACP,SAAS,CACV,CAAA;YACH,CAAC;YACD,MAAM,EAAE,KAAK,EAAE,WAAmB,EAAE,SAAiB,EAAE,EAAE;gBACvD,OAAO,gBAAM,CACX,EAAC,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAC,EACxC,OAAO,EACP,SAAS,CACV,CAAA;YACH,CAAC;SACF;KACF,CAAA;AACH,CAAC,CAAA;AAtDY,QAAA,eAAe,mBAsD3B"}
@@ -14,10 +14,10 @@ exports.default = async (account, sdkConfig) => {
14
14
  method: 'POST',
15
15
  });
16
16
  debug('stepzen:response')(response);
17
- if (response.status >= 400 && response.status < 500) {
17
+ if (response.status === 401 || response.status === 403) {
18
18
  throw new Error('Could not complete the request. Please check your authentication details are correct.');
19
19
  }
20
- if (response.status >= 500) {
20
+ if (response.status >= 400) {
21
21
  return {
22
22
  errors: [response.statusText],
23
23
  success: false,
@@ -1 +1 @@
1
- {"version":3,"file":"account.js","sourceRoot":"","sources":["../../src/commands/account.ts"],"names":[],"mappings":";AAAA,8CAA8C;;AAE9C,+BAA8B;AAC9B,2CAA8B;AAE9B,m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EAClB,OAAuB,EACvB,SAA2B,EAG3B,EAAE;IACF,MAAM,OAAO,GAAG,2BAAiB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;IACrD,MAAM,GAAG,GAAG,GAAG,OAAO,CAAC,MAAM,GAAG,6BAAiB,EAAE,CAAA;IAEnD,KAAK,CAAC,iBAAiB,CAAC,CAAC,OAAO,CAAC,CAAA;IAEjC,MAAM,QAAQ,GAAG,MAAM,oBAAK,CAAC,GAAG,EAAE;QAChC,OAAO,EAAE,OAAc;QACvB,MAAM,EAAE,MAAM;KACf,CAAC,CAAA;IAEF,KAAK,CAAC,kBAAkB,CAAC,CAAC,QAAQ,CAAC,CAAA;IAEnC,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE;QACtD,MAAM,IAAI,KAAK,CACb,uFAAuF,CACxF,CAAA;KACF;IAED,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG,EAAE;QAC1B,OAAO;YACL,MAAM,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC;YAC7B,OAAO,EAAE,KAAK;SACf,CAAA;KACF;IAED,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAA;IAClC,IAAI,IAAI,CAAA;IAER,IAAI;QACF,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;KACxB;IAAC,WAAM;QACN,OAAO;YACL,MAAM,EAAE,CAAC,oCAAoC,IAAI,EAAE,CAAC;YACpD,OAAO,EAAE,KAAK;SACf,CAAA;KACF;IAED,OAAO,IAAI,CAAA;AACb,CAAC,CAAA"}
@@ -0,0 +1,6 @@
1
+ import { AnonymousClientOptions, SDKConfiguration, StepZenCredentials } from '../shared/types';
2
+ declare const _default: (options: Required<AnonymousClientOptions>, sdkConfig: SDKConfiguration) => Promise<StepZenCredentials | {
3
+ success: false;
4
+ errors: string[];
5
+ }>;
6
+ export default _default;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const debug = require("debug");
5
+ const node_fetch_1 = require("node-fetch");
6
+ const request_1 = require("../shared/request");
7
+ exports.default = async (options, sdkConfig) => {
8
+ var _a, _b;
9
+ const account = process.env.STEPZEN_PUBLIC_ACCOUNT_API_ACCOUNT || 'stepzen';
10
+ const endpoint = process.env.STEPZEN_PUBLIC_ACCOUNT_API_ENDPOINT || 'api/publicaccount';
11
+ const server = options.server
12
+ .replace('{account}', account)
13
+ .replace('.io', '.net');
14
+ const url = new URL(`${server}/${endpoint}/__graphql`);
15
+ // Inlclude the token into the URL so that it is visible in the logs
16
+ // (allows StepZen to do analytics based on the GCP logs).
17
+ url.searchParams.set('token', options.publicAccountToken);
18
+ try {
19
+ debug('stepzen:createAnonymousAccount')(url);
20
+ const response = await node_fetch_1.default(url, {
21
+ method: 'POST',
22
+ headers: {
23
+ 'Content-Type': 'application/json',
24
+ 'User-Agent': request_1.getUserAgent(sdkConfig),
25
+ },
26
+ body: JSON.stringify({
27
+ query: `query (
28
+ $token: String!
29
+ ) {
30
+ getAccountDetails(
31
+ token: $token
32
+ ) {
33
+ account: accountName
34
+ adminkey: adminKey
35
+ apikey: apiKey
36
+ }
37
+ }`,
38
+ variables: {
39
+ token: options.publicAccountToken,
40
+ },
41
+ }),
42
+ });
43
+ const json = await response.json();
44
+ debug('stepzen:createAnonymousAccount')(json);
45
+ if (json.errors) {
46
+ return {
47
+ success: false,
48
+ errors: json.errors,
49
+ };
50
+ }
51
+ if (!((_a = json.data) === null || _a === void 0 ? void 0 : _a.getAccountDetails)) {
52
+ throw new Error('No data returned from the API endpoint.');
53
+ }
54
+ return (_b = json.data) === null || _b === void 0 ? void 0 : _b.getAccountDetails;
55
+ }
56
+ catch (error) {
57
+ throw new Error(`Could not create a public account (${error})`);
58
+ }
59
+ };
60
+ //# sourceMappingURL=getPublicAccount.js.map
@@ -0,0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;QAC7C,IAAI,IAAI,CAAC,MAAM,EAAE;YACf,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,IAAI,CAAC,MAAM;aACpB,CAAA;SACF;QAED,IAAI,CAAC,CAAA,MAAA,IAAI,CAAC,IAAI,0CAAE,iBAAiB,CAAA,EAAE;YACjC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAA;SAC3D;QAED,OAAO,MAAA,IAAI,CAAC,IAAI,0CAAE,iBAAiB,CAAA;KACpC;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,sCAAsC,KAAK,GAAG,CAAC,CAAA;KAChE;AACH,CAAC,CAAA"}
package/lib/index.d.ts CHANGED
@@ -1,16 +1,12 @@
1
- import { SDKConfiguration, StepZenAccount } from './shared/types';
2
- export * from './shared/types';
1
+ import { AnonymousClientOptions, SDKConfiguration, UserCredentialsClientOptions } from './shared/types';
3
2
  export declare const init: (sdkConfig: SDKConfiguration) => {
4
3
  verify: (account: string, adminkey: string) => Promise<boolean>;
5
- client: (options: Pick<StepZenAccount, 'account' | 'adminkey'> & Partial<StepZenAccount>) => Promise<{
4
+ client: (options: UserCredentialsClientOptions | AnonymousClientOptions) => Promise<{
6
5
  readonly credentials: {
7
6
  account: string;
8
7
  adminkey: string;
9
8
  apikey: string;
10
9
  };
11
- /**
12
- * @deprecated use `.credentials` or create a new SDK client instead
13
- **/
14
10
  account: () => Promise<{
15
11
  account: string;
16
12
  apikey: string;
@@ -19,16 +15,16 @@ export declare const init: (sdkConfig: SDKConfiguration) => {
19
15
  errors: string[];
20
16
  }>;
21
17
  deploy: (destination: string, properties: {
22
- configurationsets?: string[];
18
+ configurationsets?: string[] | undefined;
23
19
  schema: string;
24
- }) => Promise<import("./shared/types").ZenCtlResponse>;
20
+ }) => Promise<import("./client").ZenCtlResponse>;
25
21
  list: {
26
- configurationsets: () => Promise<import("./shared/types").ZenCtlResponse>;
27
- schemas: () => Promise<import("./shared/types").ZenCtlResponse>;
22
+ configurationsets: () => Promise<import("./client").ZenCtlResponse>;
23
+ schemas: () => Promise<import("./client").ZenCtlResponse>;
28
24
  };
29
25
  upload: {
30
- configurationset: (destination: string, file: string) => Promise<import("./shared/types").ZenCtlResponse>;
31
- schema: (destination: string, directory: string) => Promise<import("./shared/types").ZenCtlResponse>;
26
+ configurationset: (destination: string, file: string) => Promise<import("./client").ZenCtlResponse>;
27
+ schema: (destination: string, directory: string) => Promise<import("./client").ZenCtlResponse>;
32
28
  };
33
29
  }>;
34
30
  };
package/lib/index.js CHANGED
@@ -2,14 +2,9 @@
2
2
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.init = void 0;
5
- const tslib_1 = require("tslib");
6
5
  const path = require("path");
7
- const account_1 = require("./commands/account");
8
6
  const authenticate_1 = require("./commands/authenticate");
9
- const deploy_1 = require("./commands/deploy");
10
- const list_1 = require("./commands/list");
11
- const upload_1 = require("./commands/upload");
12
- tslib_1.__exportStar(require("./shared/types"), exports);
7
+ const client_1 = require("./client");
13
8
  const init = (sdkConfig) => {
14
9
  const defaults = {
15
10
  domain: process.env.STEPZEN_DOMAIN || 'stepzen.io',
@@ -25,66 +20,7 @@ const init = (sdkConfig) => {
25
20
  }, sdkConfig);
26
21
  },
27
22
  client: async (options) => {
28
- if (!options.account) {
29
- throw new Error('You must provide an account.');
30
- }
31
- if (!options.adminkey) {
32
- throw new Error('You must provide an admin key.');
33
- }
34
- const account = Object.assign(Object.assign({}, defaults), options);
35
- account.server = account.server.replace('{account}', options.account);
36
- let accountOrError;
37
- try {
38
- accountOrError = await account_1.default(account, sdkConfig);
39
- }
40
- catch (error) {
41
- if (error instanceof Error &&
42
- error.message.includes('Please check your authentication details')) {
43
- throw new Error('Your credentials are invalid.');
44
- }
45
- else {
46
- throw new Error(`An unexpected error occurred. ${error}`);
47
- }
48
- }
49
- if ('errors' in accountOrError) {
50
- throw new Error(`An unexpected error occurred. ${JSON.stringify(accountOrError.errors)}`);
51
- }
52
- const credentials = {
53
- account: options.account,
54
- adminkey: options.adminkey,
55
- apikey: accountOrError.apikey,
56
- };
57
- return {
58
- get credentials() {
59
- // always return a copy to avoid accidential modification by the caller
60
- return Object.assign({}, credentials);
61
- },
62
- /**
63
- * @deprecated use `.credentials` or create a new SDK client instead
64
- **/
65
- account: () => {
66
- return account_1.default(account, sdkConfig);
67
- },
68
- deploy: (destination, properties) => {
69
- return deploy_1.default(Object.assign({ destination }, properties), account, sdkConfig);
70
- },
71
- list: {
72
- configurationsets: async () => {
73
- return list_1.default({ type: 'configurationsets' }, account, sdkConfig);
74
- },
75
- schemas: async () => {
76
- return list_1.default({ type: 'schemas' }, account, sdkConfig);
77
- },
78
- },
79
- upload: {
80
- configurationset: async (destination, file) => {
81
- return upload_1.default({ type: 'configurationset', destination, file }, account, sdkConfig);
82
- },
83
- schema: async (destination, directory) => {
84
- return upload_1.default({ type: 'schema', destination, directory }, account, sdkConfig);
85
- },
86
- },
87
- };
23
+ return client_1.createSdkClient(Object.assign(Object.assign({}, defaults), options), sdkConfig);
88
24
  },
89
25
  };
90
26
  };
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,8CAA8C;;;;AAE9C,6BAA4B;AAC5B,gDAA+C;AAC/C,0DAAkD;AAClD,8CAAsC;AAEtC,0CAAkC;AAClC,8CAAsC;AAEtC,yDAA8B;AAEvB,MAAM,IAAI,GAAG,CAAC,SAA2B,EAAE,EAAE;IAClD,MAAM,QAAQ,GAAG;QACf,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,YAAY;QAClD,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,8BAA8B;KACzE,CAAA;IAED,OAAO;QACL,MAAM,EAAE,CAAC,OAAe,EAAE,QAAgB,EAAE,EAAE;YAC5C,OAAO,sBAAY,CAC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AC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CAC3D,CAAC,CAAA;AAEF,kBAAe,OAAO,CAAA;AAEtB,6EAA6E;AAC7E,kGAAkG;AAClG,iGAAiG;AACjG,uBAAuB;AACvB,MAAM,CAAC,OAAO,mCAAO,MAAM,CAAC,OAAO,GAAK,OAAO,CAAC,CAAA"}
@@ -1,2 +1,3 @@
1
1
  import { SDKConfiguration, StepZenAccount, ZenCtlRequestHeaders } from './types';
2
+ export declare const getUserAgent: (sdkConfig: SDKConfiguration) => string;
2
3
  export declare const getRequestHeaders: (account: StepZenAccount, sdkConfig: SDKConfiguration) => ZenCtlRequestHeaders;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.getRequestHeaders = void 0;
4
+ exports.getRequestHeaders = exports.getUserAgent = void 0;
5
5
  const os = require("os");
6
6
  const isWsl = require("is-wsl");
7
7
  const { version } = require('../../package.json');
@@ -9,13 +9,16 @@ const { version } = require('../../package.json');
9
9
  // https://github.com/oclif/core/blob/d7067d13c7d80c9e0064455c27ac1ebb6ee53fd2/src/config/config.ts#L128
10
10
  const arch = os.arch() === 'ia32' ? 'x86' : os.arch();
11
11
  const platform = isWsl ? 'wsl' : os.platform();
12
+ const getUserAgent = (sdkConfig) => {
13
+ return `${sdkConfig.appName} stepzen-sdk/${version} (${platform}; ${arch}; node-${process.version})`;
14
+ };
15
+ exports.getUserAgent = getUserAgent;
12
16
  const getRequestHeaders = (account, sdkConfig) => {
13
- const userAgent = `${sdkConfig.appName} stepzen-sdk/${version} (${platform}; ${arch}; node-${process.version})`;
14
17
  return {
15
18
  authorization: `Apikey ${account.adminkey}`,
16
19
  host: `${account.account}.${account.domain}`,
17
20
  'stepzen-cli-version': version,
18
- 'user-agent': userAgent,
21
+ 'user-agent': exports.getUserAgent(sdkConfig),
19
22
  };
20
23
  };
21
24
  exports.getRequestHeaders = getRequestHeaders;
@@ -1 +1 @@
1
- {"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/shared/request.ts"],"names":[],"mappings":";AAAA,8CAA8C;;;AAE9C,yBAAwB;AACxB,gCAA+B;AAG/B,MAAM,EAAC,OAAO,EAAC,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAA;AAE/C,sCAAsC;AACtC,wGAAwG;AACxG,MAAM,IAAI,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,EAAE,CAAC,IAAI,EAAU,CAAA;AAC9D,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,EAAE,CAAC,QAAQ,EAAU,CAAA;AAEhD,MAAM,iBAAiB,GAAG,CAC/B,OAAuB,EACvB,SAA2B,EACL,EAAE;IACxB,MAAM,SAAS,GAAG,GAAG,SAAS,CAAC,OAAO,gBAAgB,OAAO,KAAK,QAAQ,KAAK,IAAI,UAAU,OAAO,CAAC,OAAO,GAAG,CAAA;IAE/G,OAAO;QACL,aAAa,EAAE,UAAU,OAAO,CAAC,QAAQ,EAAE;QAC3C,IAAI,EAAE,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE;QAC5C,qBAAqB,EAAE,OAAO;QAC9B,YAAY,EAAE,SAAS;KACxB,CAAA;AACH,CAAC,CAAA;AAZY,QAAA,iBAAiB,qBAY7B"}
1
+ {"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/shared/request.ts"],"names":[],"mappings":";AAAA,8CAA8C;;;AAE9C,yBAAwB;AACxB,gCAA+B;AAG/B,MAAM,EAAC,OAAO,EAAC,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAA;AAE/C,sCAAsC;AACtC,wGAAwG;AACxG,MAAM,IAAI,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,EAAE,CAAC,IAAI,EAAU,CAAA;AAC9D,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAE,EAAE,CAAC,QAAQ,EAAU,CAAA;AAEhD,MAAM,YAAY,GAAG,CAAC,SAA2B,EAAU,EAAE;IAClE,OAAO,GAAG,SAAS,CAAC,OAAO,gBAAgB,OAAO,KAAK,QAAQ,KAAK,IAAI,UAAU,OAAO,CAAC,OAAO,GAAG,CAAA;AACtG,CAAC,CAAA;AAFY,QAAA,YAAY,gBAExB;AAEM,MAAM,iBAAiB,GAAG,CAC/B,OAAuB,EACvB,SAA2B,EACL,EAAE;IACxB,OAAO;QACL,aAAa,EAAE,UAAU,OAAO,CAAC,QAAQ,EAAE;QAC3C,IAAI,EAAE,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,EAAE;QAC5C,qBAAqB,EAAE,OAAO;QAC9B,YAAY,EAAE,oBAAY,CAAC,SAAS,CAAC;KACtC,CAAA;AACH,CAAC,CAAA;AAVY,QAAA,iBAAiB,qBAU7B"}
@@ -27,6 +27,17 @@ export interface StepZenAccount {
27
27
  */
28
28
  domain: string;
29
29
  }
30
+ export declare type StepZenCredentials = {
31
+ account: string;
32
+ adminkey: string;
33
+ apikey: string;
34
+ };
35
+ export declare type UserCredentialsClientOptions = Pick<StepZenAccount, 'account' | 'adminkey'> & Partial<StepZenAccount>;
36
+ export declare type AnonymousClientOptions = {
37
+ publicAccountToken: string;
38
+ server?: StepZenAccount['server'];
39
+ domain?: StepZenAccount['domain'];
40
+ };
30
41
  export interface StepZenDeploy {
31
42
  configurationsets?: string[];
32
43
  destination: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stepzen/sdk",
3
- "version": "0.10.0",
3
+ "version": "0.11.2",
4
4
  "license": "MIT",
5
5
  "author": "Darren Waddell <darren@stepzen.com>",
6
6
  "homepage": "https://stepzen.com",
@@ -22,9 +22,9 @@
22
22
  "posttest": "prettier . --check"
23
23
  },
24
24
  "dependencies": {
25
- "@stepzen/transpiler": "0.0.38",
25
+ "@stepzen/transpiler": "0.0.39",
26
26
  "archiver": "^5.3.0",
27
- "debug": "^4.3.3",
27
+ "debug": "^4.3.4",
28
28
  "form-data": "^4.0.0",
29
29
  "fs-extra": "^10.0.1",
30
30
  "glob": "^7.2.0",
package/src/client.ts ADDED
@@ -0,0 +1,131 @@
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
+ StepZenCredentials,
10
+ } from './shared/types'
11
+ import list from './commands/list'
12
+ import upload from './commands/upload'
13
+ import getPublicAccount from './commands/getPublicAccount'
14
+
15
+ export * from './shared/types'
16
+
17
+ export const validateCreateClientOptions = async (
18
+ options: StepZenAccount | Required<AnonymousClientOptions>,
19
+ sdkConfig: SDKConfiguration,
20
+ ) => {
21
+ let credentials: StepZenCredentials
22
+ let account: StepZenAccount
23
+
24
+ if ('publicAccountToken' in options) {
25
+ // create an anonymous account and use it to initialize an SDK client instance
26
+ const credentialsOrError = await getPublicAccount(options, sdkConfig)
27
+ if ('errors' in credentialsOrError) {
28
+ throw new Error(
29
+ `An unexpected error occurred. ${JSON.stringify(
30
+ credentialsOrError.errors,
31
+ )}`,
32
+ )
33
+ }
34
+
35
+ credentials = credentialsOrError
36
+ account = {
37
+ ...credentialsOrError,
38
+ server: options.server,
39
+ domain: options.domain,
40
+ }
41
+ } else {
42
+ // use the provided account to initialize an SDK client instance
43
+ options.server = options.server.replace('{account}', options.account)
44
+ let accountOrError
45
+ try {
46
+ accountOrError = await accountCommand(options, sdkConfig)
47
+ } catch (error) {
48
+ if (
49
+ error instanceof Error &&
50
+ error.message.includes('Please check your authentication details')
51
+ ) {
52
+ throw new Error('Your credentials are invalid.')
53
+ } else {
54
+ throw new Error(`An unexpected error occurred. ${error}`)
55
+ }
56
+ }
57
+
58
+ if ('errors' in accountOrError) {
59
+ throw new Error(
60
+ `An unexpected error occurred. ${JSON.stringify(
61
+ accountOrError.errors,
62
+ )}`,
63
+ )
64
+ }
65
+
66
+ account = options
67
+
68
+ credentials = {
69
+ account: options.account,
70
+ adminkey: options.adminkey,
71
+ apikey: accountOrError.apikey,
72
+ }
73
+ }
74
+ return {account, credentials}
75
+ }
76
+
77
+ export const createSdkClient = async (
78
+ options: Required<StepZenAccount | AnonymousClientOptions>,
79
+ sdkConfig: SDKConfiguration,
80
+ ) => {
81
+ const {account, credentials} = await validateCreateClientOptions(
82
+ options,
83
+ sdkConfig,
84
+ )
85
+
86
+ return {
87
+ get credentials() {
88
+ // always return a copy to avoid accidential modification by the caller
89
+ return {...credentials}
90
+ },
91
+ /**
92
+ * @deprecated use `.credentials` or create a new SDK client instead
93
+ **/
94
+ account: () => {
95
+ return accountCommand(account, sdkConfig)
96
+ },
97
+ deploy: (
98
+ destination: string,
99
+ properties: {
100
+ configurationsets?: string[]
101
+ schema: string
102
+ },
103
+ ) => {
104
+ return deploy({destination, ...properties}, account, sdkConfig)
105
+ },
106
+ list: {
107
+ configurationsets: async () => {
108
+ return list({type: 'configurationsets'}, account, sdkConfig)
109
+ },
110
+ schemas: async () => {
111
+ return list({type: 'schemas'}, account, sdkConfig)
112
+ },
113
+ },
114
+ upload: {
115
+ configurationset: async (destination: string, file: string) => {
116
+ return upload(
117
+ {type: 'configurationset', destination, file},
118
+ account,
119
+ sdkConfig,
120
+ )
121
+ },
122
+ schema: async (destination: string, directory: string) => {
123
+ return upload(
124
+ {type: 'schema', destination, directory},
125
+ account,
126
+ sdkConfig,
127
+ )
128
+ },
129
+ },
130
+ }
131
+ }
@@ -25,13 +25,13 @@ export default async (
25
25
 
26
26
  debug('stepzen:response')(response)
27
27
 
28
- if (response.status >= 400 && response.status < 500) {
28
+ if (response.status === 401 || response.status === 403) {
29
29
  throw new Error(
30
30
  'Could not complete the request. Please check your authentication details are correct.',
31
31
  )
32
32
  }
33
33
 
34
- if (response.status >= 500) {
34
+ if (response.status >= 400) {
35
35
  return {
36
36
  errors: [response.statusText],
37
37
  success: false,
@@ -0,0 +1,77 @@
1
+ // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
+
3
+ import * as debug from 'debug'
4
+ import fetch from 'node-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
+ }
package/src/index.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  // Copyright (c) 2020,2021,2022, StepZen, Inc.
2
2
 
3
3
  import * as path from 'path'
4
- import accountCommand from './commands/account'
5
4
  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'
5
+ import {createSdkClient} from './client'
6
+ import {
7
+ AnonymousClientOptions,
8
+ SDKConfiguration,
9
+ UserCredentialsClientOptions,
10
+ } from './shared/types'
12
11
 
13
12
  export const init = (sdkConfig: SDKConfiguration) => {
14
13
  const defaults = {
@@ -29,96 +28,9 @@ export const init = (sdkConfig: SDKConfiguration) => {
29
28
  )
30
29
  },
31
30
  client: async (
32
- options: Pick<StepZenAccount, 'account' | 'adminkey'> &
33
- Partial<StepZenAccount>,
31
+ options: UserCredentialsClientOptions | AnonymousClientOptions,
34
32
  ) => {
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
- let accountOrError
50
- try {
51
- accountOrError = await accountCommand(account, sdkConfig)
52
- } catch (error) {
53
- if (
54
- error instanceof Error &&
55
- error.message.includes('Please check your authentication details')
56
- ) {
57
- throw new Error('Your credentials are invalid.')
58
- } else {
59
- throw new Error(`An unexpected error occurred. ${error}`)
60
- }
61
- }
62
-
63
- if ('errors' in accountOrError) {
64
- throw new Error(
65
- `An unexpected error occurred. ${JSON.stringify(
66
- accountOrError.errors,
67
- )}`,
68
- )
69
- }
70
-
71
- const credentials = {
72
- account: options.account,
73
- adminkey: options.adminkey,
74
- apikey: accountOrError.apikey,
75
- }
76
-
77
- return {
78
- get credentials() {
79
- // always return a copy to avoid accidential modification by the caller
80
- return {...credentials}
81
- },
82
- /**
83
- * @deprecated use `.credentials` or create a new SDK client instead
84
- **/
85
- account: () => {
86
- return accountCommand(account, sdkConfig)
87
- },
88
- deploy: (
89
- destination: string,
90
- properties: {
91
- configurationsets?: string[]
92
- schema: string
93
- },
94
- ) => {
95
- return deploy({destination, ...properties}, account, sdkConfig)
96
- },
97
- list: {
98
- configurationsets: async () => {
99
- return list({type: 'configurationsets'}, account, sdkConfig)
100
- },
101
- schemas: async () => {
102
- return list({type: 'schemas'}, account, sdkConfig)
103
- },
104
- },
105
- upload: {
106
- configurationset: async (destination: string, file: string) => {
107
- return upload(
108
- {type: 'configurationset', destination, file},
109
- account,
110
- sdkConfig,
111
- )
112
- },
113
- schema: async (destination: string, directory: string) => {
114
- return upload(
115
- {type: 'schema', destination, directory},
116
- account,
117
- sdkConfig,
118
- )
119
- },
120
- },
121
- }
33
+ return createSdkClient({...defaults, ...options}, sdkConfig)
122
34
  },
123
35
  }
124
36
  }
@@ -11,16 +11,18 @@ 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})`
16
+ }
17
+
14
18
  export const getRequestHeaders = (
15
19
  account: StepZenAccount,
16
20
  sdkConfig: SDKConfiguration,
17
21
  ): ZenCtlRequestHeaders => {
18
- const userAgent = `${sdkConfig.appName} stepzen-sdk/${version} (${platform}; ${arch}; node-${process.version})`
19
-
20
22
  return {
21
23
  authorization: `Apikey ${account.adminkey}`,
22
24
  host: `${account.account}.${account.domain}`,
23
25
  'stepzen-cli-version': version,
24
- 'user-agent': userAgent,
26
+ 'user-agent': getUserAgent(sdkConfig),
25
27
  }
26
28
  }
@@ -32,6 +32,24 @@ export interface StepZenAccount {
32
32
  domain: string
33
33
  }
34
34
 
35
+ export type StepZenCredentials = {
36
+ account: string
37
+ adminkey: string
38
+ apikey: string
39
+ }
40
+
41
+ export type UserCredentialsClientOptions = Pick<
42
+ StepZenAccount,
43
+ 'account' | 'adminkey'
44
+ > &
45
+ Partial<StepZenAccount>
46
+
47
+ export type AnonymousClientOptions = {
48
+ publicAccountToken: string
49
+ server?: StepZenAccount['server']
50
+ domain?: StepZenAccount['domain']
51
+ }
52
+
35
53
  export interface StepZenDeploy {
36
54
  configurationsets?: string[]
37
55
  destination: string