swaggie 0.7.2 → 0.7.4

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/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- var _commander = require('commander');
4
3
  var _nanocolors = require('nanocolors');
4
+ var _commander = require('commander');
5
5
 
6
6
  var _index = require('./index');
7
7
 
@@ -23,7 +23,7 @@ program
23
23
  )
24
24
  .option(
25
25
  '-o, --out <filePath>',
26
- 'The path to the file where the API would be generated',
26
+ 'The path to the file where the API would be generated. Use stdout if left empty',
27
27
  process.env.OPEN_API_OUT
28
28
  )
29
29
  .option(
@@ -47,7 +47,14 @@ const options = program.opts() ;
47
47
 
48
48
  _index.runCodeGenerator.call(void 0, options).then(complete, error);
49
49
 
50
- function complete(spec) {
50
+ function complete([code, opts]) {
51
+ if (opts.out) {
52
+ const from = typeof opts.src === 'string' ? `from ${_nanocolors.bold.call(void 0, opts.src)} ` : '';
53
+ console.info(_nanocolors.cyan.call(void 0, `Api ${from}code generated into ${_nanocolors.bold.call(void 0, opts.out)}`));
54
+ } else {
55
+ console.log(code);
56
+ }
57
+
51
58
  process.exit(0);
52
59
  }
53
60
 
@@ -11,8 +11,10 @@ var _util = require('../util');
11
11
  let [fileContents, queryDefinitions] = await _genOperations2.default.call(void 0, spec, operations, options);
12
12
  fileContents += _genTypes2.default.call(void 0, spec, queryDefinitions, options);
13
13
 
14
- const destFile = _util.prepareOutputFilename.call(void 0, options.out);
15
- await _util.saveFile.call(void 0, destFile, fileContents);
14
+ if (options.out) {
15
+ const destFile = _util.prepareOutputFilename.call(void 0, options.out);
16
+ await _util.saveFile.call(void 0, destFile, fileContents);
17
+ }
16
18
 
17
- return spec;
19
+ return fileContents;
18
20
  } exports.default = genCode;
@@ -0,0 +1,6 @@
1
+ import { ApiSpec, ClientOptions, FullAppOptions } from './types';
2
+ /** Runs whole code generation process. @returns generated code */
3
+ export declare function runCodeGenerator(options: FullAppOptions): Promise<string>;
4
+ /** Validates if the spec is correct and if is supported */
5
+ export declare function verifySpec(spec: ApiSpec): Promise<ApiSpec>;
6
+ export declare function applyConfigFile(options: FullAppOptions): Promise<ClientOptions>;
package/dist/index.js CHANGED
@@ -1,39 +1,33 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
2
- var _nanocolors = require('nanocolors');
3
2
 
4
3
  var _js = require('./gen/js'); var _js2 = _interopRequireDefault(_js);
5
-
6
4
  var _templateManager = require('./gen/templateManager');
7
5
  var _swagger = require('./swagger');
8
6
 
9
7
 
8
+ /** Runs whole code generation process. @returns generated code */
10
9
  function runCodeGenerator(options) {
11
10
  return verifyOptions(options)
12
11
  .then(applyConfigFile)
13
- .then((options) =>
14
- _swagger.resolveSpec.call(void 0, options.src, { ignoreRefType: '#/definitions/' })
12
+ .then((opts) =>
13
+ _swagger.resolveSpec.call(void 0, opts.src, { ignoreRefType: '#/definitions/' })
15
14
  .then((spec) => verifySpec(spec))
16
- .then((spec) => gen(spec, options))
17
- .then(() => {
18
- console.info(
19
- _nanocolors.cyan.call(void 0, `Api from ${_nanocolors.bold.call(void 0, options.src)} code generated into ${_nanocolors.bold.call(void 0, options.out)}`)
20
- );
21
- return true;
22
- })
15
+ .then((spec) => gen(spec, opts))
16
+ .then((code) => [code, opts] )
23
17
  );
24
18
  } exports.runCodeGenerator = runCodeGenerator;
25
19
 
26
20
  function verifyOptions(options) {
27
- try {
28
- if (!options.config && (!options.src || !options.out)) {
29
- return Promise.reject('You need to provide --config or --src and --out parameters');
30
- }
31
- return Promise.resolve(options);
32
- } catch (e) {
33
- return Promise.reject(e);
21
+ if (!options) {
22
+ return Promise.reject('Options were not provided');
23
+ }
24
+ if (!!options.config === !!options.src) {
25
+ return Promise.reject('You need to provide either --config or --src parameters');
34
26
  }
27
+ return Promise.resolve(options);
35
28
  }
36
29
 
30
+ /** Validates if the spec is correct and if is supported */
37
31
  function verifySpec(spec) {
38
32
  if (!spec || !spec.swagger)
39
33
  return Promise.reject('Spec does not look like Swagger / OpenAPI 2! Open API 3 support is WIP');
@@ -73,3 +67,5 @@ function readFile(filePath) {
73
67
  return _fs2.default.readFile(filePath, 'utf8', (err, contents) => (err ? rej(err) : res(contents)));
74
68
  });
75
69
  }
70
+
71
+
@@ -0,0 +1,117 @@
1
+ export interface ClientOptions {
2
+ /**
3
+ * Path or URL to the Swagger specification file (JSON or YAML).
4
+ * Alternatively you can provide parsed object here instead
5
+ **/
6
+ src: string | object;
7
+ /** Path to the file which will contain generated TypeScript code */
8
+ out?: string;
9
+ /** Template to be used for generation */
10
+ template: Template;
11
+ baseUrl?: string;
12
+ preferAny?: boolean;
13
+ servicePrefix?: string;
14
+ /** Generate models for query string instead list of parameters */
15
+ queryModels?: boolean;
16
+ /** How date should be handled. It does not do any special serialization */
17
+ dateFormat?: DateSupport;
18
+ }
19
+ export interface FullAppOptions extends ClientOptions {
20
+ /** Path to the configuration file that contains actual config tp be used */
21
+ config?: string;
22
+ }
23
+ export interface ApiRequestData {
24
+ method: HttpMethod;
25
+ url: string;
26
+ headers: {
27
+ [index: string]: string;
28
+ };
29
+ body: any;
30
+ }
31
+ export interface ApiInfo {
32
+ version: string;
33
+ title: string;
34
+ }
35
+ export interface ApiSpec {
36
+ swagger: string;
37
+ info: ApiInfo;
38
+ host?: string;
39
+ basePath?: string;
40
+ schemes?: string[];
41
+ securityDefinitions?: any;
42
+ paths: any;
43
+ definitions: any;
44
+ accepts: string[];
45
+ contentTypes: string[];
46
+ }
47
+ export declare type Template = 'axios' | 'fetch' | 'ng1' | 'ng2' | 'swr-axios';
48
+ export declare type HttpMethod = 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch';
49
+ export declare type DateSupport = 'string' | 'Date';
50
+ export interface ApiOperation {
51
+ id: string;
52
+ summary: string;
53
+ description: string;
54
+ method: HttpMethod;
55
+ group: string;
56
+ path: string;
57
+ parameters: ApiOperationParam[];
58
+ responses: ApiOperationResponse[];
59
+ security?: ApiOperationSecurity[];
60
+ accepts: string[];
61
+ contentTypes: string[];
62
+ tags?: string[];
63
+ }
64
+ export interface ApiOperationParam extends ApiOperationParamBase {
65
+ name: string;
66
+ in: 'header' | 'path' | 'query' | 'body' | 'formData';
67
+ description: string;
68
+ required: boolean;
69
+ readonly?: boolean;
70
+ allowEmptyValue: boolean;
71
+ schema: object;
72
+ 'x-nullable'?: boolean;
73
+ 'x-schema'?: object;
74
+ }
75
+ declare type CollectionFormat = 'csv' | 'ssv' | 'tsv' | 'pipes' | 'multi';
76
+ export interface ApiOperationParamBase {
77
+ type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'file';
78
+ format: 'int32' | 'int64' | 'float' | 'double' | 'byte' | 'binary' | 'date' | 'date-time' | 'password';
79
+ items: ApiOperationParamBase;
80
+ collectionFormat: CollectionFormat;
81
+ default: any;
82
+ maximum: number;
83
+ exclusiveMaximum: boolean;
84
+ minimum: number;
85
+ exclusiveMinimum: boolean;
86
+ maxLength: number;
87
+ minLength: number;
88
+ pattern: string;
89
+ maxItems: number;
90
+ minItems: number;
91
+ uniqueItems: boolean;
92
+ enum: any[];
93
+ multipleOf: number;
94
+ }
95
+ export interface ApiOperationParamGroups {
96
+ header?: any;
97
+ path?: any;
98
+ query?: any;
99
+ formData?: any;
100
+ body?: any;
101
+ }
102
+ export interface ApiOperationResponse {
103
+ code: string;
104
+ description: string;
105
+ schema: object;
106
+ headers: object;
107
+ examples: object;
108
+ }
109
+ export interface ApiOperationSecurity {
110
+ id: string;
111
+ scopes?: string[];
112
+ }
113
+ export interface ApiRights {
114
+ query?: any;
115
+ headers?: any;
116
+ }
117
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swaggie",
3
- "version": "0.7.2",
3
+ "version": "0.7.4",
4
4
  "description": "Generate ES6 or TypeScript service integration code from an OpenAPI spec",
5
5
  "author": {
6
6
  "name": "Piotr Dabrowski",
@@ -19,12 +19,14 @@
19
19
  "node": ">=12.0.0"
20
20
  },
21
21
  "main": "dist/index.js",
22
+ "types": "dist/index.d.ts",
22
23
  "bin": {
23
24
  "swaggie": "dist/cli.js"
24
25
  },
25
26
  "scripts": {
26
- "build": "sucrase ./src -d ./dist --transforms typescript,imports && npm run rm-tests",
27
+ "build": "sucrase ./src -d ./dist --transforms typescript,imports && npm run rm-tests && npm run types",
27
28
  "rm-tests": "find dist/ -name '*.spec.js' -type f -delete",
29
+ "types": "tsc src/types.ts --outDir dist/ --declaration --emitDeclarationOnly && cp test/index.d.ts ./dist/",
28
30
  "test": "mocha 'src/**/*.spec.ts'"
29
31
  },
30
32
  "files": [
@@ -47,16 +49,16 @@
47
49
  "eta": "^1.12.3",
48
50
  "js-yaml": "^4.1.0",
49
51
  "nanocolors": "^0.2.0",
50
- "node-fetch": "^2.6.3"
52
+ "node-fetch": "^2.6.7"
51
53
  },
52
54
  "devDependencies": {
53
55
  "@types/chai": "4.3.0",
54
56
  "@types/js-yaml": "4.0.5",
55
- "@types/mocha": "9.0.0",
56
- "@types/node-fetch": "2.5.3",
57
- "chai": "4.3.4",
58
- "mocha": "9.1.3",
57
+ "@types/mocha": "9.1.0",
58
+ "@types/node-fetch": "2.5.12",
59
+ "chai": "4.3.6",
60
+ "mocha": "9.2.0",
59
61
  "sucrase": "3.20.3",
60
- "typescript": "4.5.4"
62
+ "typescript": "4.5.5"
61
63
  }
62
64
  }
@@ -7,6 +7,7 @@
7
7
  // </auto-generated>
8
8
  //----------------------
9
9
  // ReSharper disable InconsistentNaming
10
+ // deno-lint-ignore-file
10
11
 
11
12
  import Axios, { AxiosPromise, AxiosRequestConfig } from "axios";
12
13
 
@@ -7,6 +7,7 @@
7
7
  // </auto-generated>
8
8
  //----------------------
9
9
  // ReSharper disable InconsistentNaming
10
+ // deno-lint-ignore-file
10
11
 
11
12
  export const defaults = {
12
13
  baseUrl: '<%= it.baseUrl || '' %>',
@@ -7,6 +7,7 @@
7
7
  // </auto-generated>
8
8
  //----------------------
9
9
  // ReSharper disable InconsistentNaming
10
+ // deno-lint-ignore-file
10
11
 
11
12
  import { IHttpService, IRequestShortcutConfig, IPromise } from 'angular';
12
13
 
@@ -7,6 +7,7 @@
7
7
  // </auto-generated>
8
8
  //----------------------
9
9
  // ReSharper disable InconsistentNaming
10
+ // deno-lint-ignore-file
10
11
 
11
12
  import { Observable, throwError as _observableThrow, of as _observableOf } from "rxjs";
12
13
  import { Injectable, Inject, Optional, InjectionToken } from "@angular/core";
@@ -7,6 +7,7 @@
7
7
  // </auto-generated>
8
8
  //----------------------
9
9
  // ReSharper disable InconsistentNaming
10
+ // deno-lint-ignore-file
10
11
 
11
12
  import Axios, { AxiosPromise, AxiosRequestConfig } from "axios";
12
13
  import useSWR, { SWRConfiguration, Key } from 'swr';