beer-swagger 1.0.4 → 1.1.1
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/generate-apis.js +28 -30
- package/package.json +1 -1
package/generate-apis.js
CHANGED
|
@@ -5,7 +5,6 @@ import path from 'path';
|
|
|
5
5
|
import { createInterface } from 'readline';
|
|
6
6
|
|
|
7
7
|
const ENV_PATH = path.join(process.cwd(), '.env');
|
|
8
|
-
const CLIENT_TYPES = new Set(['WEB', 'WeChatMiniProgram']);
|
|
9
8
|
const SKIPPED_SERVICES = new Set([
|
|
10
9
|
'billbear-common-web-gateway',
|
|
11
10
|
'billbear-common-data-panel'
|
|
@@ -403,7 +402,7 @@ function toPascal(value) {
|
|
|
403
402
|
}
|
|
404
403
|
|
|
405
404
|
// Generate API file content for a service.
|
|
406
|
-
function generateForSpec(spec, serviceName, fileBase,
|
|
405
|
+
function generateForSpec(spec, serviceName, fileBase, isDefaultFetch) {
|
|
407
406
|
const {
|
|
408
407
|
components = {},
|
|
409
408
|
tags: specTags = [],
|
|
@@ -702,7 +701,7 @@ function generateForSpec(spec, serviceName, fileBase, clientType) {
|
|
|
702
701
|
lines.push(' * This file is auto-generated. Do not edit manually.');
|
|
703
702
|
lines.push(' * To update, run the generate-apis.js script.');
|
|
704
703
|
lines.push(' */');
|
|
705
|
-
const fetchImport =
|
|
704
|
+
const fetchImport = isDefaultFetch ? 'beer-network/api' : './fetch';
|
|
706
705
|
lines.push(`import type { JsonResponse } from '${fetchImport}';`);
|
|
707
706
|
lines.push(`import Fetch from '${fetchImport}';`);
|
|
708
707
|
if (fileBase) {
|
|
@@ -918,22 +917,23 @@ function writeEnvFile(nextEnv) {
|
|
|
918
917
|
fs.writeFileSync(ENV_PATH, `${lines.join('\n')}\n`, 'utf8');
|
|
919
918
|
}
|
|
920
919
|
|
|
921
|
-
function
|
|
922
|
-
if (
|
|
923
|
-
return
|
|
920
|
+
function normalizeBoolean(value) {
|
|
921
|
+
if (value === undefined || value === null || value === '') {
|
|
922
|
+
return undefined;
|
|
924
923
|
}
|
|
925
|
-
if (
|
|
924
|
+
if (typeof value === 'boolean') {
|
|
926
925
|
return value;
|
|
927
926
|
}
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
}
|
|
931
|
-
const normalized = value.replace(/[-_\s]/g, '')
|
|
927
|
+
const normalized = String(value)
|
|
928
|
+
.trim()
|
|
932
929
|
.toLowerCase();
|
|
933
|
-
if (
|
|
934
|
-
return
|
|
930
|
+
if (['true', '1', 'yes', 'y'].includes(normalized)) {
|
|
931
|
+
return true;
|
|
935
932
|
}
|
|
936
|
-
|
|
933
|
+
if (['false', '0', 'no', 'n'].includes(normalized)) {
|
|
934
|
+
return false;
|
|
935
|
+
}
|
|
936
|
+
return undefined;
|
|
937
937
|
}
|
|
938
938
|
|
|
939
939
|
// Ask user for missing values via CLI.
|
|
@@ -953,13 +953,12 @@ async function promptForMissing(env) {
|
|
|
953
953
|
if (!result.SWAGGER_OUT_DIRECTORY) {
|
|
954
954
|
result.SWAGGER_OUT_DIRECTORY = (await ask('DIRECTORY: ')).trim();
|
|
955
955
|
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
}
|
|
961
|
-
result.CLIENT_TYPE = selected;
|
|
956
|
+
let isDefaultFetch = normalizeBoolean(result.IS_DEFAULT_FETCH);
|
|
957
|
+
while (isDefaultFetch === undefined) {
|
|
958
|
+
const selected = (await ask('IS_DEFAULT_FETCH (true/false): ')).trim();
|
|
959
|
+
isDefaultFetch = normalizeBoolean(selected);
|
|
962
960
|
}
|
|
961
|
+
result.IS_DEFAULT_FETCH = String(isDefaultFetch);
|
|
963
962
|
rl.close();
|
|
964
963
|
return result;
|
|
965
964
|
}
|
|
@@ -970,23 +969,23 @@ async function main() {
|
|
|
970
969
|
let {
|
|
971
970
|
SWAGGER_BASE_URL: baseUrl,
|
|
972
971
|
SWAGGER_OUT_DIRECTORY: directory,
|
|
973
|
-
|
|
972
|
+
IS_DEFAULT_FETCH: isDefaultFetch
|
|
974
973
|
} = { ...process.env, ...envFile };
|
|
975
|
-
|
|
974
|
+
isDefaultFetch = normalizeBoolean(isDefaultFetch);
|
|
976
975
|
|
|
977
|
-
if (!baseUrl || !directory ||
|
|
976
|
+
if (!baseUrl || !directory || isDefaultFetch === undefined) {
|
|
978
977
|
const filled = await promptForMissing({
|
|
979
978
|
SWAGGER_BASE_URL: baseUrl || '',
|
|
980
979
|
SWAGGER_OUT_DIRECTORY: directory || '',
|
|
981
|
-
|
|
980
|
+
IS_DEFAULT_FETCH: isDefaultFetch === undefined ? '' : String(isDefaultFetch)
|
|
982
981
|
});
|
|
983
982
|
baseUrl = filled.SWAGGER_BASE_URL;
|
|
984
983
|
directory = filled.SWAGGER_OUT_DIRECTORY;
|
|
985
|
-
|
|
984
|
+
isDefaultFetch = normalizeBoolean(filled.IS_DEFAULT_FETCH);
|
|
986
985
|
writeEnvFile({
|
|
987
986
|
SWAGGER_BASE_URL: baseUrl,
|
|
988
987
|
SWAGGER_OUT_DIRECTORY: directory,
|
|
989
|
-
|
|
988
|
+
IS_DEFAULT_FETCH: String(isDefaultFetch)
|
|
990
989
|
});
|
|
991
990
|
}
|
|
992
991
|
|
|
@@ -996,10 +995,9 @@ async function main() {
|
|
|
996
995
|
if (!directory) {
|
|
997
996
|
throw new Error('SWAGGER_OUT_DIRECTORY is required.');
|
|
998
997
|
}
|
|
999
|
-
if (
|
|
1000
|
-
throw new Error('
|
|
998
|
+
if (isDefaultFetch === undefined) {
|
|
999
|
+
throw new Error('IS_DEFAULT_FETCH is required.');
|
|
1001
1000
|
}
|
|
1002
|
-
|
|
1003
1001
|
const configUrl = `${baseUrl}/v3/api-docs/swagger-config`;
|
|
1004
1002
|
const outputDirectory = path.isAbsolute(directory) ? directory : path.join(process.cwd(), directory);
|
|
1005
1003
|
const typesDirectory = path.join(outputDirectory, 'types');
|
|
@@ -1037,7 +1035,7 @@ async function main() {
|
|
|
1037
1035
|
const specUrl = `${baseUrl}${url}`;
|
|
1038
1036
|
const spec = await fetchJson(specUrl);
|
|
1039
1037
|
const fileBase = toFileBase(serviceName);
|
|
1040
|
-
const content = generateForSpec(spec, serviceName, fileBase,
|
|
1038
|
+
const content = generateForSpec(spec, serviceName, fileBase, isDefaultFetch);
|
|
1041
1039
|
const typesContent = generateTypes(spec);
|
|
1042
1040
|
|
|
1043
1041
|
const outPath = path.join(outputDirectory, `${fileBase}.ts`);
|