Skip to content

Commit 8329c4b

Browse files
js2medkamyshov
andauthored
Release 7.0.1 (#215)
* fix: use `secure` option from global config (#214) Fixes #212. * refactor: logger * docs: update CHANGELOG * feat: global `secure` option for `axios` http client * bump: up version to 7.0.1 Co-authored-by: Danil Kamyshov <[email protected]>
1 parent 66e02ee commit 8329c4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+440
-99
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# next release
22

3+
# 7.0.1
4+
5+
Fixes:
6+
- "securityWorker" is only used if "secure" option is specified on each request (issue #212, thanks @dkamyshov)
7+
NOTE: added global `secure` option for `axios` http client
8+
- `index.d.ts` file (add `rawModelTypes`)
9+
310
# 7.0.0
411

512
BREAKING_CHANGES:

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export interface GenerateApiConfiguration {
298298
description: string;
299299
content: string;
300300
}[];
301-
modelTypes: SchemaComponent[];
301+
rawModelTypes: SchemaComponent[];
302302
hasFormDataRoutes: boolean;
303303
hasSecurityRoutes: boolean;
304304
hasQueryRoutes: boolean;

package-lock.json

Lines changed: 17 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-typescript-api",
3-
"version": "7.0.0",
3+
"version": "7.0.1",
44
"description": "Generate typescript/javascript api from swagger schema",
55
"scripts": {
66
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
@@ -57,7 +57,8 @@
5757
"js-yaml": "^4.0.0",
5858
"lodash": "^4.17.21",
5959
"make-dir": "^3.1.0",
60-
"nanoid": "^3.1.20",
60+
"nanoid": "^3.1.22",
61+
"node-emoji": "^1.10.0",
6162
"prettier": "^2.2.1",
6263
"swagger-schema-official": "2.0.0-bab6bed",
6364
"swagger2openapi": "^7.0.5",

src/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const { getTemplates, getTemplatePaths, renderTemplate, getTemplate } = require(
1919
const constants = require("./constants");
2020
const { generateOutputFiles } = require("./output");
2121
const formatFileContent = require("./formatFileContent");
22+
const { eventLog, successLog } = require("./logger");
2223

2324
module.exports = {
2425
constants: constants,
@@ -86,7 +87,7 @@ module.exports = {
8687

8788
const templatesToRender = getTemplates(config);
8889

89-
if (!config.silent) console.log("☄️ start generating your typescript api");
90+
eventLog("start generating your typescript api");
9091

9192
fixSwaggerScheme(usageSchema, originalSchema);
9293

@@ -173,17 +174,15 @@ module.exports = {
173174
content: file.declaration.content,
174175
withPrefix: true,
175176
});
176-
if (!config.silent)
177-
console.log(`✔️ your javascript api file created in "${output}"`);
177+
successLog(`javascript api file`, file.name, `created in ${output}`);
178178
} else {
179179
createFile({
180180
path: output,
181181
fileName: file.name,
182182
content: file.content,
183183
withPrefix: true,
184184
});
185-
if (!config.silent)
186-
console.log(`✔️ your typescript api file created in "${output}"`);
185+
successLog(`typescript api file`, file.name, `created in ${output}`);
187186
}
188187

189188
return file;

src/logger.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const _ = require("lodash");
2+
const { config } = require("./config");
3+
const { emojify } = require("node-emoji");
4+
5+
/**
6+
*
7+
* @param {"warn" | "log" | "error"} kind
8+
* @param {keyof emoji} emojiName
9+
* @param {...any} messages
10+
* @returns
11+
*/
12+
const createLogMessage = ({ kind, emojiName, messages }) => {
13+
if (config.silent) return;
14+
15+
const emoji = emojify(emojiName);
16+
17+
console[kind](
18+
emoji,
19+
" ",
20+
..._.map(messages, (message) =>
21+
_.startsWith(message, "\n") ? `\n${emoji} ${message.replace(/\n/, "")}` : message,
22+
),
23+
);
24+
};
25+
26+
const log = (...messages) =>
27+
createLogMessage({
28+
kind: "log",
29+
emojiName: ":sparkles:",
30+
messages,
31+
});
32+
const eventLog = (...messages) =>
33+
createLogMessage({
34+
kind: "log",
35+
emojiName: ":comet: ",
36+
messages,
37+
});
38+
const successLog = (...messages) =>
39+
createLogMessage({
40+
kind: "log",
41+
emojiName: ":white_check_mark:",
42+
messages,
43+
});
44+
const warnLog = (...messages) =>
45+
createLogMessage({
46+
kind: "warn",
47+
emojiName: ":warning: ",
48+
messages,
49+
});
50+
const errorLog = (...messages) =>
51+
createLogMessage({
52+
kind: "error",
53+
emojiName: ":exclamation:",
54+
messages,
55+
});
56+
57+
module.exports = {
58+
log,
59+
eventLog,
60+
successLog,
61+
warnLog,
62+
errorLog,
63+
};

src/modelNames.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require("lodash");
22
const { config } = require("./config");
3+
const { warnLog } = require("./logger");
34

45
const isValidName = (name) => /^([A-Za-z$_]{1,})$/g.test(name);
56

@@ -28,8 +29,7 @@ const fixModelName = (name) => {
2829

2930
const formatModelName = (name) => {
3031
if (typeof name !== "string") {
31-
if (!config.silent) console.warn("🔨 wrong name of the model name", name);
32-
32+
warnLog("wrong name of the model name", name);
3333
return name;
3434
}
3535

src/routeNames.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { config } = require("./config");
2+
const { warnLog } = require("./logger");
23
const { renderTemplate } = require("./templates");
34

45
const getRouteName = (routeInfo) => {
@@ -22,13 +23,13 @@ const getRouteName = (routeInfo) => {
2223
duplicateIdentifier,
2324
routeNameDuplicatesMap.get(duplicateIdentifier) + 1,
2425
);
25-
if (!config.silent)
26-
console.warn(
27-
`🥵 Module "${moduleName}" already have method "${routeName}()"`,
28-
`\n🥵 This method has been renamed to "${
29-
routeName + routeNameDuplicatesMap.get(duplicateIdentifier)
30-
}()" to solve conflict names.`,
31-
);
26+
27+
warnLog(
28+
`Module "${moduleName}" already have method "${routeName}()"`,
29+
`\nThis method has been renamed to "${
30+
routeName + routeNameDuplicatesMap.get(duplicateIdentifier)
31+
}()" to solve conflict names.`,
32+
);
3233
} else {
3334
routeNameDuplicatesMap.set(duplicateIdentifier, 1);
3435
}

src/routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const { config } = require("./config");
1717
const { nanoid } = require("nanoid");
1818
const { getRouteName } = require("./routeNames");
1919
const { createComponent } = require("./components");
20+
const { warnLog } = require("./logger");
2021

2122
const formDataTypes = _.uniq([types.file, types.string.binary]);
2223

@@ -132,7 +133,7 @@ const parseRoute = (route) => {
132133
if (!paramName) return pathParams;
133134

134135
if (_.includes(paramName, "-")) {
135-
if (!config.silent) console.warn("🔨 wrong path param name", paramName);
136+
warnLog("wrong path param name", paramName);
136137
}
137138

138139
return [

src/swagger.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const converter = require("swagger2openapi");
55
const https = require("https");
66
const { addToConfig, config } = require("./config");
77
const { pathIsExist, getFileContent } = require("./files");
8+
const { log } = require("./logger");
89

910
const parseSwaggerFile = (file) => {
1011
if (typeof file !== "string") return file;
@@ -19,10 +20,10 @@ const parseSwaggerFile = (file) => {
1920
const getSwaggerFile = (pathToSwagger, urlToSwagger, disableStrictSSL) =>
2021
new Promise((resolve) => {
2122
if (pathIsExist(pathToSwagger)) {
22-
if (!config.silent) console.log(`try to get swagger by path "${pathToSwagger}"`);
23+
log(`try to get swagger by path "${pathToSwagger}"`);
2324
resolve(getFileContent(pathToSwagger));
2425
} else {
25-
if (!config.silent) console.log(`try to get swagger by url "${urlToSwagger}"`);
26+
log(`try to get swagger by url "${urlToSwagger}"`);
2627
let agent = undefined;
2728
if (disableStrictSSL) {
2829
agent = new https.Agent({

src/templates.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Eta = require("eta");
33
const { getFileContent, pathIsExist } = require("./files");
44
const { config } = require("./config");
55
const { resolve } = require("path");
6+
const { warnLog, log } = require("./logger");
67

78
/**
89
* name - project template name,
@@ -56,11 +57,10 @@ const getTemplate = ({ fileName, name, path }) => {
5657
if (pathIsExist(baseFullPath)) {
5758
fileContent = getFileContent(baseFullPath);
5859
} else {
59-
if (!config.silent)
60-
console.log(
61-
`❗❗❗ ${_.lowerCase(name)} template not found in ${customFullPath}\n` +
62-
`Code generator will use the default template`,
63-
);
60+
warnLog(
61+
`${_.lowerCase(name)} template not found in ${customFullPath}`,
62+
`\nCode generator will use the default template`,
63+
);
6464
}
6565

6666
if (pathIsExist(originalFullPath)) {
@@ -72,8 +72,7 @@ const getTemplate = ({ fileName, name, path }) => {
7272
};
7373

7474
const getTemplates = ({ templatePaths }) => {
75-
if (!config.silent)
76-
console.log(`✨ try to read templates from directory "${templatePaths.custom}"`);
75+
log(`try to read templates from directory "${templatePaths.custom}"`);
7776

7877
const templatesMap = _.reduce(
7978
TEMPLATE_INFOS,

templates/base/http-clients/axios-http-client.eta

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type RequestParams = Omit<FullRequestParams, "body" | "method" | "query"
2525

2626
export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequestConfig, "data" | "cancelToken"> {
2727
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
28+
secure?: boolean;
2829
}
2930

3031
export enum ContentType {
@@ -37,9 +38,11 @@ export class HttpClient<SecurityDataType = unknown> {
3738
private instance: AxiosInstance;
3839
private securityData: SecurityDataType | null = null;
3940
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
41+
private secure?: boolean;
4042

41-
constructor({ securityWorker, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
43+
constructor({ securityWorker, secure, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
4244
this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
45+
this.secure = secure;
4346
this.securityWorker = securityWorker;
4447
}
4548

@@ -69,7 +72,7 @@ export class HttpClient<SecurityDataType = unknown> {
6972
body,
7073
...params
7174
}: FullRequestParams): Promise<AxiosResponse<T>> => {
72-
const secureParams = (secure && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
75+
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
7376
const requestParams = this.mergeRequestParams(params, secureParams);
7477

7578
return this.instance.request({

templates/base/http-clients/fetch-http-client.eta

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export class HttpClient<SecurityDataType = unknown> {
153153
cancelToken,
154154
...params
155155
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
156-
const secureParams = (secure && this.securityWorker && await this.securityWorker(this.securityData)) || {};
156+
const secureParams = ((typeof secure === 'boolean' ? secure : this.baseApiParams.secure) && this.securityWorker && await this.securityWorker(this.securityData)) || {};
157157
const requestParams = this.mergeRequestParams(params, secureParams);
158158
const queryString = query && this.toQueryString(query);
159159
const payloadFormatter = this.contentFormatters[type || ContentType.Json];

tests/generated/v2.0/adafruit.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,11 @@ export class HttpClient<SecurityDataType = unknown> {
313313
cancelToken,
314314
...params
315315
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
316-
const secureParams = (secure && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
316+
const secureParams =
317+
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
318+
this.securityWorker &&
319+
(await this.securityWorker(this.securityData))) ||
320+
{};
317321
const requestParams = this.mergeRequestParams(params, secureParams);
318322
const queryString = query && this.toQueryString(query);
319323
const payloadFormatter = this.contentFormatters[type || ContentType.Json];

tests/generated/v2.0/another-example.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ export class HttpClient<SecurityDataType = unknown> {
289289
cancelToken,
290290
...params
291291
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
292-
const secureParams = (secure && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
292+
const secureParams =
293+
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
294+
this.securityWorker &&
295+
(await this.securityWorker(this.securityData))) ||
296+
{};
293297
const requestParams = this.mergeRequestParams(params, secureParams);
294298
const queryString = query && this.toQueryString(query);
295299
const payloadFormatter = this.contentFormatters[type || ContentType.Json];

tests/generated/v2.0/another-schema.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ export class HttpClient<SecurityDataType = unknown> {
181181
cancelToken,
182182
...params
183183
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
184-
const secureParams = (secure && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
184+
const secureParams =
185+
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
186+
this.securityWorker &&
187+
(await this.securityWorker(this.securityData))) ||
188+
{};
185189
const requestParams = this.mergeRequestParams(params, secureParams);
186190
const queryString = query && this.toQueryString(query);
187191
const payloadFormatter = this.contentFormatters[type || ContentType.Json];

tests/generated/v2.0/api-with-examples.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ export class HttpClient<SecurityDataType = unknown> {
158158
cancelToken,
159159
...params
160160
}: FullRequestParams): Promise<HttpResponse<T, E>> => {
161-
const secureParams = (secure && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
161+
const secureParams =
162+
((typeof secure === "boolean" ? secure : this.baseApiParams.secure) &&
163+
this.securityWorker &&
164+
(await this.securityWorker(this.securityData))) ||
165+
{};
162166
const requestParams = this.mergeRequestParams(params, secureParams);
163167
const queryString = query && this.toQueryString(query);
164168
const payloadFormatter = this.contentFormatters[type || ContentType.Json];

0 commit comments

Comments
 (0)