stepzen 0.11.0-beta.0 → 0.11.0-beta.3
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.
- package/README.md +22 -9
- package/lib/commands/import.js +44 -12
- package/lib/generate/curl2sdl.d.ts +13 -5
- package/lib/generate/curl2sdl.js +70 -5
- package/lib/shared/constants.d.ts +1 -0
- package/lib/shared/constants.js +2 -1
- package/lib/shared/curl-parser.d.ts +2 -0
- package/lib/shared/curl-parser.js +88 -23
- package/lib/shared/errors.js +4 -3
- package/lib/shared/path-params-parser.d.ts +7 -0
- package/lib/shared/path-params-parser.js +73 -0
- package/oclif.manifest.json +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@ $ npm install -g stepzen
|
|
|
29
29
|
$ stepzen COMMAND
|
|
30
30
|
running command...
|
|
31
31
|
$ stepzen (-v|--version|version)
|
|
32
|
-
stepzen/0.11.0-beta.
|
|
32
|
+
stepzen/0.11.0-beta.3 darwin-x64 node-v14.19.1
|
|
33
33
|
$ stepzen --help [COMMAND]
|
|
34
34
|
USAGE
|
|
35
35
|
$ stepzen COMMAND
|
|
@@ -91,18 +91,31 @@ USAGE
|
|
|
91
91
|
$ stepzen import SCHEMAS
|
|
92
92
|
|
|
93
93
|
OPTIONS
|
|
94
|
-
-h, --help
|
|
95
|
-
|
|
94
|
+
-h, --help
|
|
95
|
+
show CLI help
|
|
96
96
|
|
|
97
|
-
--
|
|
98
|
-
|
|
97
|
+
--dir=dir
|
|
98
|
+
working directory
|
|
99
99
|
|
|
100
|
-
--
|
|
100
|
+
--name=name
|
|
101
|
+
subfolder inside the workspace folder to save the imported schema files, defaults to the imported schema name
|
|
101
102
|
|
|
102
|
-
--
|
|
103
|
+
--path-params=path-params
|
|
104
|
+
[curl] specifies path parameters in the URL path. Can be formed by taking the original path and replacing the
|
|
105
|
+
variable segments with $paramName placeholders.
|
|
103
106
|
|
|
104
|
-
|
|
105
|
-
|
|
107
|
+
Example:
|
|
108
|
+
stepzen import curl https://example.com/users/jane/posts/12 --path-params '/users/$userId/posts/$postId'
|
|
109
|
+
|
|
110
|
+
--prefix=prefix
|
|
111
|
+
[curl] prefix to add every type in the generated schema.
|
|
112
|
+
|
|
113
|
+
--query-name=query-name
|
|
114
|
+
[curl] property name to add to the Query type as a way to access the imported cURL endpoint.
|
|
115
|
+
|
|
116
|
+
--query-type=query-type
|
|
117
|
+
[curl] name for the type returned by the cURL endpoint in the generated schema. The name specified by --query-type
|
|
118
|
+
is not prefixed by --prefix if both flags are present.
|
|
106
119
|
```
|
|
107
120
|
|
|
108
121
|
## `stepzen list TYPE`
|
package/lib/commands/import.js
CHANGED
|
@@ -12,9 +12,11 @@ const generate_1 = require("../generate");
|
|
|
12
12
|
const utils_1 = require("../shared/utils");
|
|
13
13
|
const helpers_1 = require("../generate/helpers");
|
|
14
14
|
const curl2sdl_1 = require("../generate/curl2sdl");
|
|
15
|
+
const curl_parser_1 = require("../shared/curl-parser");
|
|
15
16
|
const workspace_1 = require("../shared/workspace");
|
|
16
17
|
const init_1 = require("./init");
|
|
17
18
|
const constants_1 = require("../shared/constants");
|
|
19
|
+
const path_params_parser_1 = require("../shared/path-params-parser");
|
|
18
20
|
class Import extends command_1.Command {
|
|
19
21
|
async run() {
|
|
20
22
|
const { args, argv, flags } = this.parse(Import);
|
|
@@ -40,25 +42,45 @@ class Import extends command_1.Command {
|
|
|
40
42
|
if (schemas.length > 1) {
|
|
41
43
|
throw new errors_1.CLIError('Please run cURL import separately from importing other schemas');
|
|
42
44
|
}
|
|
43
|
-
if (argv.length < 2) {
|
|
44
|
-
throw new errors_1.CLIError('Please append the full cURL command, e.g. ' +
|
|
45
|
-
chalk.bold(`${this.config.name} import curl https://test.stepzen.net/version`));
|
|
46
|
-
}
|
|
47
45
|
this.log(chalk.yellow(`NOTE: ${chalk.bold('stepzen import curl')} is a ${chalk.bold('new')} feature.`));
|
|
48
46
|
this.log(chalk.yellow('If you have any issues, please check if they have been addressed ' +
|
|
49
47
|
'in the latest version, or reach out to StepZen on Discord: ' +
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
cli_ux_1.cli.action.start('Starting');
|
|
54
|
-
const resultOrError = await curl2sdl_1.curl2sdl({
|
|
55
|
-
argv,
|
|
48
|
+
constants_1.STEPZEN_DISCORD_URL));
|
|
49
|
+
let curl2sdlOptions;
|
|
50
|
+
const fixedOptions = {
|
|
56
51
|
name: flags.name,
|
|
57
52
|
source: workspace.schema,
|
|
53
|
+
};
|
|
54
|
+
const editableOptions = {
|
|
58
55
|
queryName: flags['query-name'],
|
|
59
56
|
rootType: flags['query-type'],
|
|
60
57
|
typePrefix: flags.prefix,
|
|
61
|
-
|
|
58
|
+
pathParams: flags['path-params'],
|
|
59
|
+
};
|
|
60
|
+
if (argv.length === 1) {
|
|
61
|
+
// no parameters given: start an interactive prompt
|
|
62
|
+
console.log();
|
|
63
|
+
console.log('This command introspects the response of a REST endpoint and generates' +
|
|
64
|
+
' a GraphQL schema allowing you to access this endpoint through your ' +
|
|
65
|
+
'StepZen API. curl syntax is supported so that you copy and paste a ' +
|
|
66
|
+
'curl command instead of the URL.');
|
|
67
|
+
console.log();
|
|
68
|
+
curl2sdlOptions = Object.assign(Object.assign({}, fixedOptions), (await curl2sdl_1.askCurlQuestions(editableOptions)));
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// run non-interative
|
|
72
|
+
const argsOrError = curl_parser_1.parseCurlArgv(argv);
|
|
73
|
+
if ('error' in argsOrError) {
|
|
74
|
+
throw new errors_1.CLIError(argsOrError.error);
|
|
75
|
+
}
|
|
76
|
+
const parsedPathParamsOrError = path_params_parser_1.parsePathParamsPattern(argsOrError.url, flags['path-params']);
|
|
77
|
+
if ('error' in parsedPathParamsOrError) {
|
|
78
|
+
throw new errors_1.CLIError(parsedPathParamsOrError.error);
|
|
79
|
+
}
|
|
80
|
+
curl2sdlOptions = Object.assign(Object.assign(Object.assign({}, fixedOptions), editableOptions), { pathParams: parsedPathParamsOrError, curlArgs: argsOrError });
|
|
81
|
+
}
|
|
82
|
+
cli_ux_1.cli.action.start('Starting');
|
|
83
|
+
const resultOrError = await curl2sdl_1.curl2sdl(curl2sdlOptions);
|
|
62
84
|
cli_ux_1.cli.action.stop();
|
|
63
85
|
if ('error' in resultOrError) {
|
|
64
86
|
this.log('A problem occured while processing your import. ' +
|
|
@@ -70,7 +92,7 @@ class Import extends command_1.Command {
|
|
|
70
92
|
}
|
|
71
93
|
else {
|
|
72
94
|
;
|
|
73
|
-
['prefix', 'query-type', 'query-name'].forEach(flag => {
|
|
95
|
+
['prefix', 'query-type', 'query-name', 'path-params'].forEach(flag => {
|
|
74
96
|
if (flag in flags) {
|
|
75
97
|
this.log(chalk.gray(`The ${chalk.bold(`--${flag}`)} flag only applies when importing ${chalk.bold('curl')}. It will be ignored now.`));
|
|
76
98
|
}
|
|
@@ -118,6 +140,16 @@ Import.flags = {
|
|
|
118
140
|
description: '[curl] name for the type returned by the cURL endpoint in the ' +
|
|
119
141
|
`generated schema. The name specified by ${chalk.bold('--query-type')} is not prefixed by ${chalk.bold('--prefix')} if both flags are present.`,
|
|
120
142
|
}),
|
|
143
|
+
'path-params': command_1.flags.string({
|
|
144
|
+
description: `[curl] specifies path parameters in the URL path.` +
|
|
145
|
+
` Can be formed by taking the original path and replacing the` +
|
|
146
|
+
` variable segments with ${chalk.bold('$paramName')} placeholders.` +
|
|
147
|
+
`\n` +
|
|
148
|
+
`\nExample:` +
|
|
149
|
+
`\nstepzen import curl https://example.com/users/jane/posts/12` +
|
|
150
|
+
` --path-params` +
|
|
151
|
+
` '/users/${chalk.bold('$userId')}/posts/${chalk.bold('$postId')}'`,
|
|
152
|
+
}),
|
|
121
153
|
};
|
|
122
154
|
Import.args = [
|
|
123
155
|
{
|
|
@@ -1,16 +1,24 @@
|
|
|
1
|
+
import { CurlArguments } from '../shared/curl-parser';
|
|
2
|
+
import { PathParam } from '../shared/path-params-parser';
|
|
1
3
|
export declare type Curl2SdlOptions = {
|
|
2
|
-
|
|
4
|
+
curlArgs: CurlArguments;
|
|
3
5
|
name: string;
|
|
4
6
|
source: string;
|
|
5
7
|
queryName?: string;
|
|
6
8
|
rootType?: string;
|
|
7
9
|
typePrefix?: string;
|
|
10
|
+
pathParams: readonly PathParam[];
|
|
8
11
|
};
|
|
9
|
-
export declare type
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
export declare type EditableCurl2SdlOptions = Pick<Curl2SdlOptions, 'curlArgs' | 'queryName' | 'rootType' | 'typePrefix' | 'pathParams'>;
|
|
13
|
+
export declare type CurlAnswers = {
|
|
14
|
+
url: string;
|
|
15
|
+
queryName: string;
|
|
16
|
+
rootType: string;
|
|
17
|
+
typePrefix: string;
|
|
18
|
+
pathParams: string;
|
|
12
19
|
};
|
|
13
|
-
export declare const
|
|
20
|
+
export declare const askCurlQuestions: (defaultAnswers?: Partial<CurlAnswers>) => Promise<EditableCurl2SdlOptions>;
|
|
21
|
+
export declare const curl2sdl: ({ curlArgs, name, source, queryName, rootType, typePrefix, pathParams, }: Curl2SdlOptions) => Promise<{
|
|
14
22
|
error: string;
|
|
15
23
|
} | {
|
|
16
24
|
outPath: string;
|
package/lib/generate/curl2sdl.js
CHANGED
|
@@ -1,24 +1,80 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.curl2sdl = void 0;
|
|
4
|
+
exports.curl2sdl = exports.askCurlQuestions = void 0;
|
|
5
5
|
const errors_1 = require("@oclif/errors");
|
|
6
|
+
const chalk = require("chalk");
|
|
6
7
|
const fs = require("fs-extra");
|
|
8
|
+
const inquirer = require("inquirer");
|
|
9
|
+
const lodash_1 = require("lodash");
|
|
7
10
|
const os = require("os");
|
|
8
11
|
const path = require("path");
|
|
12
|
+
const string_argv_1 = require("string-argv");
|
|
9
13
|
const node_fetch_1 = require("node-fetch");
|
|
10
14
|
const debug = require("debug");
|
|
11
15
|
const prettier = require("prettier");
|
|
12
16
|
const transpiler_1 = require("@stepzen/transpiler");
|
|
13
17
|
const curl_parser_1 = require("../shared/curl-parser");
|
|
18
|
+
const path_params_parser_1 = require("../shared/path-params-parser");
|
|
14
19
|
const constants_1 = require("../shared/constants");
|
|
15
20
|
const errors_2 = require("../shared/errors");
|
|
16
|
-
exports.
|
|
17
|
-
let
|
|
18
|
-
|
|
21
|
+
exports.askCurlQuestions = async (defaultAnswers = {}) => {
|
|
22
|
+
let questions = [
|
|
23
|
+
{
|
|
24
|
+
name: 'url',
|
|
25
|
+
message: 'Endpoint URL or a full curl command',
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'pathParams',
|
|
29
|
+
message: 'Parameters for endpoint URL path',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: 'queryName',
|
|
33
|
+
message: 'Query name to use when generating schema (leave blank to use defaults)',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: 'rootType',
|
|
37
|
+
message: 'Query type name to use when generating schema (leave blank to use defaults)',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'typePrefix',
|
|
41
|
+
message: 'Prefix to add to all generated type name (leave blank to use defaults)',
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
questions = questions.map(question => (Object.assign(Object.assign({}, question), { default: lodash_1.get(defaultAnswers, question.name) })));
|
|
45
|
+
const answers = await inquirer.prompt(questions);
|
|
46
|
+
const curlArgs = curl_parser_1.parseCurlArgv(string_argv_1.default(answers.url));
|
|
19
47
|
if ('error' in curlArgs) {
|
|
20
|
-
|
|
48
|
+
console.log();
|
|
49
|
+
console.log(chalk.red('A problem occurred while parsing the curl command:\n', curlArgs.error), '\nPlease enter a URL or a supported curl command.');
|
|
50
|
+
console.log();
|
|
51
|
+
return exports.askCurlQuestions(answers);
|
|
52
|
+
}
|
|
53
|
+
const parsedPathParamsOrError = path_params_parser_1.parsePathParamsPattern(curlArgs.url, answers.pathParams);
|
|
54
|
+
if ('error' in parsedPathParamsOrError) {
|
|
55
|
+
console.log();
|
|
56
|
+
console.log(chalk.red('A problem occurred while parsing path parameters pattern:\n', parsedPathParamsOrError.error));
|
|
57
|
+
console.log();
|
|
58
|
+
console.log(`Please enter a path parameter specification that` +
|
|
59
|
+
` can be formed by taking the original path and replacing the variable` +
|
|
60
|
+
` segments with ${chalk.bold('$paramName')} placeholders.`);
|
|
61
|
+
console.log();
|
|
62
|
+
console.log('Example:\n' +
|
|
63
|
+
`\tendpoint URL: https://example.com/users/jane/posts/12\n ` +
|
|
64
|
+
`\tpath parameters: /users/${chalk.bold('$userId')}/posts/${chalk.bold('$postId')}`);
|
|
65
|
+
console.log();
|
|
66
|
+
return exports.askCurlQuestions(answers);
|
|
21
67
|
}
|
|
68
|
+
return {
|
|
69
|
+
curlArgs,
|
|
70
|
+
queryName: answers.queryName,
|
|
71
|
+
rootType: answers.rootType,
|
|
72
|
+
typePrefix: answers.typePrefix,
|
|
73
|
+
pathParams: parsedPathParamsOrError,
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
exports.curl2sdl = async ({ curlArgs, name, source, queryName, rootType, typePrefix, pathParams, }) => {
|
|
77
|
+
let json;
|
|
22
78
|
try {
|
|
23
79
|
const url = `${constants_1.STEPZEN_JSON2SDL_SERVER_URL}/api/graphql`;
|
|
24
80
|
const query = `query (
|
|
@@ -27,6 +83,9 @@ exports.curl2sdl = async ({ argv, name, source, queryName, rootType, typePrefix,
|
|
|
27
83
|
$rootType: String
|
|
28
84
|
$typePrefix: String
|
|
29
85
|
$headers: [HeaderInput!]
|
|
86
|
+
$data: String
|
|
87
|
+
$method: HTTPMethod
|
|
88
|
+
$pathParams: [PathParamInput!]
|
|
30
89
|
) {
|
|
31
90
|
getSDLFromCurl(
|
|
32
91
|
command: $command
|
|
@@ -34,6 +93,9 @@ exports.curl2sdl = async ({ argv, name, source, queryName, rootType, typePrefix,
|
|
|
34
93
|
rootType: $rootType
|
|
35
94
|
typePrefix: $typePrefix
|
|
36
95
|
headers: $headers
|
|
96
|
+
data: $data
|
|
97
|
+
method: $method
|
|
98
|
+
pathParams: $pathParams
|
|
37
99
|
) {
|
|
38
100
|
sdl
|
|
39
101
|
config
|
|
@@ -45,6 +107,9 @@ exports.curl2sdl = async ({ argv, name, source, queryName, rootType, typePrefix,
|
|
|
45
107
|
rootType: rootType || null,
|
|
46
108
|
typePrefix: typePrefix || null,
|
|
47
109
|
headers: curlArgs.headers.length > 0 ? curlArgs.headers : null,
|
|
110
|
+
data: curlArgs.data || null,
|
|
111
|
+
method: curlArgs.method,
|
|
112
|
+
pathParams: pathParams.length > 0 ? pathParams : null,
|
|
48
113
|
};
|
|
49
114
|
debug('stepzen:curl2sdl')(url);
|
|
50
115
|
debug('stepzen:curl2sdl')(query);
|
|
@@ -11,3 +11,4 @@ export declare const ADMIN_LIST_URL = "/cli/admin/list";
|
|
|
11
11
|
export declare const ADMIN_UPLOAD_URL = "/cli/admin/upload";
|
|
12
12
|
export declare const ADMIN_VERIFY_URL = "/cli/admin/verify";
|
|
13
13
|
export declare const STEPZEN_JSON2SDL_SERVER_URL: string;
|
|
14
|
+
export declare const STEPZEN_DISCORD_URL = "https://discord.gg/9k2VdPn2FR";
|
package/lib/shared/constants.js
CHANGED
|
@@ -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.STEPZEN_JSON2SDL_SERVER_URL = exports.ADMIN_VERIFY_URL = exports.ADMIN_UPLOAD_URL = exports.ADMIN_LIST_URL = exports.ADMIN_DEPLOY_URL = exports.STEPZEN_API_TEMPLATES_REPOSITORY = exports.STEPZEN_GENERATOR_ENGINES_ENDPOINT = exports.STEPZEN_GENERATOR_ENGINES_SCHEMA = exports.STEPZEN_SERVER_URL = exports.STEPZEN_DOMAIN = exports.STEPZEN_CONFIG_FILE = exports.STEPZEN_LAST_UPDATE_CHECK_TIMESTAMP = exports.STEPZEN_CONFIG_DIRECTORY = void 0;
|
|
4
|
+
exports.STEPZEN_DISCORD_URL = exports.STEPZEN_JSON2SDL_SERVER_URL = exports.ADMIN_VERIFY_URL = exports.ADMIN_UPLOAD_URL = exports.ADMIN_LIST_URL = exports.ADMIN_DEPLOY_URL = exports.STEPZEN_API_TEMPLATES_REPOSITORY = exports.STEPZEN_GENERATOR_ENGINES_ENDPOINT = exports.STEPZEN_GENERATOR_ENGINES_SCHEMA = exports.STEPZEN_SERVER_URL = exports.STEPZEN_DOMAIN = exports.STEPZEN_CONFIG_FILE = exports.STEPZEN_LAST_UPDATE_CHECK_TIMESTAMP = exports.STEPZEN_CONFIG_DIRECTORY = void 0;
|
|
5
5
|
// This file contains constants and all magic strings
|
|
6
6
|
const dotenv = require("dotenv");
|
|
7
7
|
const os = require("os");
|
|
@@ -32,3 +32,4 @@ exports.ADMIN_UPLOAD_URL = '/cli/admin/upload';
|
|
|
32
32
|
exports.ADMIN_VERIFY_URL = '/cli/admin/verify';
|
|
33
33
|
exports.STEPZEN_JSON2SDL_SERVER_URL = ENV_VAR_STEPZEN_JSON2SDL_SERVER_URL ||
|
|
34
34
|
'https://jsonintrospection-ng-prod-xynkgeaaaa-uc.a.run.app';
|
|
35
|
+
exports.STEPZEN_DISCORD_URL = 'https://discord.gg/9k2VdPn2FR';
|
|
@@ -15,10 +15,12 @@ export declare const parseCurlHeaderString: (header: string) => {
|
|
|
15
15
|
};
|
|
16
16
|
export interface CurlArguments {
|
|
17
17
|
url: string;
|
|
18
|
+
method: 'Get' | 'Post';
|
|
18
19
|
headers: Array<{
|
|
19
20
|
name: string;
|
|
20
21
|
value: string;
|
|
21
22
|
}>;
|
|
23
|
+
data?: string;
|
|
22
24
|
}
|
|
23
25
|
export interface ParseError {
|
|
24
26
|
error: string;
|
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.parseCurlArgv = exports.parseCurlHeaderString = void 0;
|
|
5
|
+
const constants_1 = require("./constants");
|
|
5
6
|
// Based on the existing curl flags at https://curl.se/docs/manpage.html
|
|
6
7
|
const curlFlagRegex = /^(-[a-zA-Z0-9.:#])|(--[a-zA-Z0-9.-]+)$/;
|
|
7
8
|
// Based on the definition of a `token` at
|
|
8
9
|
// https://datatracker.ietf.org/doc/html/rfc2616#page-17
|
|
9
10
|
const httpHeaderRegex = /^(?<name>[a-zA-Z0-9!#$%&'*+,-.^_`|~]+)\s*:(?<value>.*)$/;
|
|
11
|
+
const httpURLRegex = /^https?:\/\//i;
|
|
10
12
|
/**
|
|
11
13
|
* Parse a header string according to https://curl.se/docs/manpage.html#-H
|
|
12
14
|
* and https://datatracker.ietf.org/doc/html/rfc2616#section-4.2
|
|
@@ -43,7 +45,58 @@ exports.parseCurlHeaderString = (header) => {
|
|
|
43
45
|
};
|
|
44
46
|
};
|
|
45
47
|
const isAFlagArg = (arg) => curlFlagRegex.test(arg);
|
|
46
|
-
const
|
|
48
|
+
const isAURL = (arg) => httpURLRegex.test(arg);
|
|
49
|
+
const parseHeaderFlag = (argv, i, partialCurlArgs) => {
|
|
50
|
+
if (i + 1 >= argv.length) {
|
|
51
|
+
return {
|
|
52
|
+
error: `The '${argv[i]}' curl flag requires a value`,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const headerOrError = exports.parseCurlHeaderString(argv[i + 1]);
|
|
56
|
+
if (headerOrError && 'error' in headerOrError) {
|
|
57
|
+
return headerOrError; // error
|
|
58
|
+
}
|
|
59
|
+
const headers = partialCurlArgs.headers || [];
|
|
60
|
+
// A `null` from parseCurlHeaderString() means a header should NOT be
|
|
61
|
+
// sent. This is not supported by zenserv / the introspection service
|
|
62
|
+
// so the CLI simply omits such headers
|
|
63
|
+
if (headerOrError) {
|
|
64
|
+
headers.push(headerOrError); // header
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
result: Object.assign(Object.assign({}, partialCurlArgs), { headers }),
|
|
68
|
+
skip: 1,
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const parseDataFlag = (argv, i, partialCurlArgs) => {
|
|
72
|
+
if (i + 1 >= argv.length) {
|
|
73
|
+
return {
|
|
74
|
+
error: `The '${argv[i]}' curl flag requires a value`,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (argv[i] !== '--data-raw' && argv[i + 1].charAt(0) === '@') {
|
|
78
|
+
return {
|
|
79
|
+
error: `Reading request data from local files in not currently supported ` +
|
|
80
|
+
`by StepZen CLI (${argv[i]} ${argv[i + 1]}). If this is a blocker ` +
|
|
81
|
+
`for you, please let us know on Discord (${constants_1.STEPZEN_DISCORD_URL})`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const data = (partialCurlArgs.data ? partialCurlArgs.data + '&' : '') + argv[i + 1];
|
|
85
|
+
return {
|
|
86
|
+
result: Object.assign(Object.assign({}, partialCurlArgs), { data }),
|
|
87
|
+
skip: 1,
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
const flags = [
|
|
91
|
+
{
|
|
92
|
+
matches: ['-H', '--header'],
|
|
93
|
+
parse: parseHeaderFlag,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
matches: ['-d', '--data', '--data-ascii', '--data-raw', '--data-binary'],
|
|
97
|
+
parse: parseDataFlag,
|
|
98
|
+
},
|
|
99
|
+
];
|
|
47
100
|
/**
|
|
48
101
|
* Parse a curl command line arguments array to a JSON structure consumable by
|
|
49
102
|
* the StepZen introspection service backend.
|
|
@@ -60,39 +113,33 @@ const isAHeaderFlag = (flag) => ['-H', '--header'].includes(flag);
|
|
|
60
113
|
* @returns {*} a structured object with the curl arguments
|
|
61
114
|
*/
|
|
62
115
|
exports.parseCurlArgv = (argv) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
116
|
+
var _a;
|
|
117
|
+
if (((_a = argv[0]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'curl') {
|
|
118
|
+
argv = argv.slice(1);
|
|
119
|
+
}
|
|
120
|
+
let result = {};
|
|
67
121
|
for (let i = 0; i < argv.length; i++) {
|
|
68
122
|
if (isAFlagArg(argv[i])) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
123
|
+
const knownFlag = flags.find(flag => flag.matches.includes(argv[i]));
|
|
124
|
+
if (knownFlag) {
|
|
125
|
+
const resultOrError = knownFlag.parse(argv, i, result);
|
|
126
|
+
if ('error' in resultOrError) {
|
|
127
|
+
return resultOrError; // error
|
|
74
128
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return headerOrError; // error
|
|
78
|
-
}
|
|
79
|
-
// A `null` from parseCurlHeaderString() means a header should NOT be
|
|
80
|
-
// sent. This is not supported by zenserv / the introspection service
|
|
81
|
-
// so the CLI simply omits such headers
|
|
82
|
-
if (headerOrError) {
|
|
83
|
-
result.headers.push(headerOrError); // header
|
|
84
|
-
}
|
|
85
|
-
i += 1; // skip over the flag value
|
|
129
|
+
result = resultOrError.result; // result
|
|
130
|
+
i += resultOrError.skip || 0;
|
|
86
131
|
}
|
|
87
132
|
else {
|
|
88
133
|
return {
|
|
89
|
-
error: `The '${argv[i]}' curl flag is not currently supported by StepZen CLI
|
|
134
|
+
error: `The '${argv[i]}' curl flag is not currently supported by StepZen CLI.` +
|
|
135
|
+
` If this is a blocker for you, please let us know on Discord (${constants_1.STEPZEN_DISCORD_URL})`,
|
|
90
136
|
};
|
|
91
137
|
}
|
|
92
138
|
}
|
|
93
139
|
else if (result.url) {
|
|
94
140
|
return {
|
|
95
|
-
error: `Multiple URLs are not currently supported by StepZen CLI (${argv[i]})
|
|
141
|
+
error: `Multiple URLs are not currently supported by StepZen CLI (${argv[i]}).` +
|
|
142
|
+
` If this is a blocker for you, please let us know on Discord (${constants_1.STEPZEN_DISCORD_URL})`,
|
|
96
143
|
};
|
|
97
144
|
}
|
|
98
145
|
else {
|
|
@@ -104,5 +151,23 @@ exports.parseCurlArgv = (argv) => {
|
|
|
104
151
|
error: 'curl: a URL is required',
|
|
105
152
|
};
|
|
106
153
|
}
|
|
154
|
+
if (!isAURL(result.url)) {
|
|
155
|
+
return {
|
|
156
|
+
error: `Unsupported URL schema in '${result.url}'.` +
|
|
157
|
+
` StepZen CLI currently supports 'https' and 'http'.` +
|
|
158
|
+
` If this is a blocker for you, please let us know on Discord (${constants_1.STEPZEN_DISCORD_URL})`,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
result.headers = result.headers || [];
|
|
162
|
+
result.method = result.method || result.data ? 'Post' : 'Get';
|
|
163
|
+
// Add the default content-type header if the request has any data
|
|
164
|
+
// in it, and no content-type header is explicitly provided.
|
|
165
|
+
if (result.headers.findIndex(header => header.name.toLowerCase() === 'content-type') === -1 &&
|
|
166
|
+
result.data) {
|
|
167
|
+
result.headers.push({
|
|
168
|
+
name: 'content-type',
|
|
169
|
+
value: 'application/x-www-form-urlencoded',
|
|
170
|
+
});
|
|
171
|
+
}
|
|
107
172
|
return result;
|
|
108
173
|
};
|
package/lib/shared/errors.js
CHANGED
|
@@ -2,15 +2,16 @@
|
|
|
2
2
|
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.UPGRADE_CHECK_ERROR = exports.PERMANENT_STEPZEN_ERROR = exports.formatTemporaryErrorMessage = void 0;
|
|
5
|
+
const constants_1 = require("./constants");
|
|
5
6
|
exports.formatTemporaryErrorMessage = (errors) => `A problem occurred while processing your import${errors.length > 0 && errors[0].message ? `: "${errors[0].message}"` : ''}. Please try again.` +
|
|
6
7
|
` If the issue persists, make sure you are on the latest version of StepZen ` +
|
|
7
8
|
`CLI. And if the issue still persists contact support via the StepZen discord ` +
|
|
8
|
-
`channel (
|
|
9
|
+
`channel (${constants_1.STEPZEN_DISCORD_URL}).`;
|
|
9
10
|
exports.PERMANENT_STEPZEN_ERROR = 'An unexpected problem occurred while processing your import.' +
|
|
10
11
|
` If the issue persists, make sure you are on the latest version of StepZen ` +
|
|
11
12
|
`CLI. And if the issue still persists contact support via the StepZen discord ` +
|
|
12
|
-
`channel (
|
|
13
|
+
`channel (${constants_1.STEPZEN_DISCORD_URL}).`;
|
|
13
14
|
exports.UPGRADE_CHECK_ERROR = 'An unexpected problem occurred while checking for new CLI versions.' +
|
|
14
15
|
` If the issue persists, make sure you are on the latest version of StepZen ` +
|
|
15
16
|
`CLI. And if the issue still persists contact support via the StepZen discord ` +
|
|
16
|
-
`channel (
|
|
17
|
+
`channel (${constants_1.STEPZEN_DISCORD_URL}).`;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ParseError } from './curl-parser';
|
|
2
|
+
export declare type PathParam = {
|
|
3
|
+
index: number;
|
|
4
|
+
name: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const formatPatternDiffWithPath: (patternSegments: readonly string[], pathSegments: readonly string[]) => string | null;
|
|
7
|
+
export declare const parsePathParamsPattern: (url: string, pathParamsPattern?: string | undefined) => PathParam[] | ParseError;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) 2020,2021,2022, StepZen, Inc.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.parsePathParamsPattern = exports.formatPatternDiffWithPath = void 0;
|
|
5
|
+
const chalk = require("chalk");
|
|
6
|
+
const pathParamRegex = /^\$[_A-Za-z]\w*$/;
|
|
7
|
+
exports.formatPatternDiffWithPath = (patternSegments, pathSegments) => {
|
|
8
|
+
let hasErrors = false;
|
|
9
|
+
const diff = [];
|
|
10
|
+
for (let i = 0; i < patternSegments.length; i++) {
|
|
11
|
+
const patternSegment = patternSegments[i];
|
|
12
|
+
if (i >= pathSegments.length) {
|
|
13
|
+
diff.push(chalk.red(`< ${patternSegment} >`));
|
|
14
|
+
hasErrors = true;
|
|
15
|
+
}
|
|
16
|
+
else if (patternSegment.charAt(0) === '$') {
|
|
17
|
+
diff.push(patternSegment);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
const pathSegment = pathSegments[i];
|
|
21
|
+
if (pathSegment === patternSegment) {
|
|
22
|
+
diff.push(pathSegment);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
diff.push(chalk.red(`< ${patternSegment} >`));
|
|
26
|
+
hasErrors = true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
for (let i = patternSegments.length; i < pathSegments.length; i++) {
|
|
31
|
+
diff.push(chalk.magenta(`< ${pathSegments[i]} >`));
|
|
32
|
+
hasErrors = true;
|
|
33
|
+
}
|
|
34
|
+
return hasErrors ? diff.join('/') : null;
|
|
35
|
+
};
|
|
36
|
+
exports.parsePathParamsPattern = (url, pathParamsPattern) => {
|
|
37
|
+
if (!pathParamsPattern) {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
if (pathParamsPattern.charAt(0) === '/') {
|
|
41
|
+
pathParamsPattern = pathParamsPattern.slice(1);
|
|
42
|
+
}
|
|
43
|
+
let pathname = new URL(url).pathname;
|
|
44
|
+
if (pathname.charAt(0) === '/') {
|
|
45
|
+
pathname = pathname.slice(1);
|
|
46
|
+
}
|
|
47
|
+
const pathSegments = pathname.split('/');
|
|
48
|
+
const patternSegments = pathParamsPattern.split('/');
|
|
49
|
+
const maybeDiff = exports.formatPatternDiffWithPath(patternSegments, pathSegments);
|
|
50
|
+
if (maybeDiff) {
|
|
51
|
+
return {
|
|
52
|
+
error: `Path parameters do not align with the URL path: /${maybeDiff}`,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const invalidParameterSegments = patternSegments.filter(segment => segment.charAt(0) === '$' && !pathParamRegex.test(segment));
|
|
56
|
+
if (invalidParameterSegments.length > 0) {
|
|
57
|
+
return {
|
|
58
|
+
error: `The path parameter name '${invalidParameterSegments[0]}' is not` +
|
|
59
|
+
` allowed. Path parameter names should be valid GraphQL identifiers` +
|
|
60
|
+
` prefixed with $ (dollar sign).`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return patternSegments
|
|
64
|
+
.map((segment, index) => ({
|
|
65
|
+
index,
|
|
66
|
+
name: segment,
|
|
67
|
+
}))
|
|
68
|
+
.filter(({ name }) => name.charAt(0) === '$')
|
|
69
|
+
.map(({ index, name }) => ({
|
|
70
|
+
index,
|
|
71
|
+
name: name.slice(1),
|
|
72
|
+
}));
|
|
73
|
+
};
|
package/oclif.manifest.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.11.0-beta.
|
|
1
|
+
{"version":"0.11.0-beta.3","commands":{"deploy":{"id":"deploy","description":"deploy to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"configurationsets":{"name":"configurationsets","type":"option","description":"Configurationsets to use","default":""},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"schema":{"name":"schema","type":"option","description":"Schema to use","required":true},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"destination","description":"destination","required":true}]},"import":{"id":"import","description":"import schemas from stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"working directory"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"silent":{"name":"silent","type":"boolean","hidden":true,"allowNo":false},"name":{"name":"name","type":"option","description":"subfolder inside the workspace folder to save the imported schema files, defaults to the imported schema name"},"prefix":{"name":"prefix","type":"option","description":"[curl] prefix to add every type in the generated schema."},"query-name":{"name":"query-name","type":"option","description":"[curl] property name to add to the Query type as a way to access the imported cURL endpoint."},"query-type":{"name":"query-type","type":"option","description":"[curl] name for the type returned by the cURL endpoint in the generated schema. The name specified by --query-type is not prefixed by --prefix if both flags are present."},"path-params":{"name":"path-params","type":"option","description":"[curl] specifies path parameters in the URL path. Can be formed by taking the original path and replacing the variable segments with $paramName placeholders.\n\nExample:\nstepzen import curl https://example.com/users/jane/posts/12 --path-params '/users/$userId/posts/$postId'"}},"args":[{"name":"schemas","required":true}]},"init":{"id":"init","description":"stepzen init","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"endpoint":{"name":"endpoint","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"yes":{"name":"yes","type":"boolean","hidden":true,"allowNo":false}},"args":[{"name":"directory","hidden":true}]},"lint":{"id":"lint","description":"stepzen lint","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"dir":{"name":"dir","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"list":{"id":"list","description":"list your items","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"type","description":"type","required":true,"options":["configurationsets","schemas"]}]},"login":{"id":"login","description":"log in to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"account":{"name":"account","type":"option","char":"a","hidden":true},"adminkey":{"name":"adminkey","type":"option","char":"k","hidden":true},"config":{"name":"config","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"logout":{"id":"logout","description":"log out of stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"start":{"id":"start","description":"upload and deploy your schema","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"working directory"},"endpoint":{"name":"endpoint","type":"option","description":"Override workspace endpoint"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"no-console":{"name":"no-console","type":"boolean","hidden":true,"allowNo":false},"no-dashboard":{"name":"no-dashboard","type":"boolean","hidden":true,"allowNo":false},"no-init":{"name":"no-init","type":"boolean","hidden":true,"allowNo":false},"no-server":{"name":"no-server","type":"boolean","hidden":true,"allowNo":false},"no-validate":{"name":"no-validate","type":"boolean","hidden":true,"allowNo":false},"no-watcher":{"name":"no-watcher","type":"boolean","hidden":true,"allowNo":false},"port":{"name":"port","type":"option","default":5001}},"args":[]},"transpile":{"id":"transpile","description":"transpile a graphql schema","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"config":{"name":"config","type":"option","hidden":true},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"hide-output":{"name":"hide-output","type":"boolean","hidden":true,"allowNo":false},"inspect":{"name":"inspect","type":"boolean","char":"i","hidden":true,"allowNo":false},"inspect-after":{"name":"inspect-after","type":"boolean","hidden":true,"allowNo":false},"output-configuration":{"name":"output-configuration","type":"boolean","allowNo":false},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"folder","required":true}]},"upload":{"id":"upload","description":"upload to stepzen","pluginName":"stepzen","pluginType":"core","aliases":[],"flags":{"dir":{"name":"dir","type":"option","description":"A directory to upload"},"file":{"name":"file","type":"option","description":"A file to upload"},"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"silent":{"name":"silent","type":"boolean","allowNo":false}},"args":[{"name":"type","description":"type","required":true,"options":["configurationset","schema"]},{"name":"destination","description":"destination","required":true}]},"validate":{"id":"validate","description":"validate a graphql schema","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[{"name":"folder","required":true}]},"whoami":{"id":"whoami","description":"stepzen whoami","pluginName":"stepzen","pluginType":"core","hidden":true,"aliases":[],"flags":{"help":{"name":"help","type":"boolean","char":"h","description":"show CLI help","allowNo":false},"showkeys":{"name":"showkeys","type":"boolean","allowNo":false},"apikey":{"name":"apikey","type":"boolean","allowNo":false},"adminkey":{"name":"adminkey","type":"boolean","allowNo":false}},"args":[]}}}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stepzen",
|
|
3
3
|
"description": "The StepZen CLI",
|
|
4
|
-
"version": "0.11.0-beta.
|
|
4
|
+
"version": "0.11.0-beta.3",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Darren Waddell <darren@stepzen.com>",
|
|
7
7
|
"contributors": [
|
|
@@ -119,6 +119,8 @@
|
|
|
119
119
|
"posttest": "eslint . --ignore-path .prettierignore",
|
|
120
120
|
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
|
|
121
121
|
"test": "tsc -b && STEPZEN_DOMAIN=steprz.io STEPZEN_SERVER_URL=http://localhost nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
|
|
122
|
+
"test:single:example": "npm run test:single -- test/src/shared/curl-parser.test.ts",
|
|
123
|
+
"test:single": "tsc -b && STEPZEN_DOMAIN=steprz.io STEPZEN_SERVER_URL=http://localhost mocha --forbid-only test/_global.test.ts",
|
|
122
124
|
"version": "oclif-dev readme && git add README.md",
|
|
123
125
|
"prepare": "husky install"
|
|
124
126
|
},
|