dtsgenerator 3.18.0 → 3.19.0
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/core/dtsGenerator.js +4 -5
- package/dist/core/openApiV3.d.ts +2 -2
- package/dist/core/schemaId.js +1 -1
- package/dist/core/type.js +1 -2
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +15 -20
- package/package.json +19 -18
- package/src/cli.ts +3 -3
- package/src/commandOptions.ts +4 -4
- package/src/core/astBuilder.ts +28 -28
- package/src/core/config.ts +1 -1
- package/src/core/dtsGenerator.ts +63 -63
- package/src/core/jsonSchema.ts +37 -37
- package/src/core/openApiV2.ts +2 -2
- package/src/core/openApiV3.ts +1 -8
- package/src/core/referenceResolver.ts +9 -9
- package/src/core/schemaId.ts +4 -1
- package/src/core/type.ts +9 -9
- package/src/core/utils.ts +3 -3
- package/src/core/validateIdentifier.ts +2 -1
- package/src/jsonPointer.ts +1 -1
- package/src/utils.ts +23 -25
package/src/core/jsonSchema.ts
CHANGED
|
@@ -32,11 +32,11 @@ export interface NormalizedSchema extends Schema {
|
|
|
32
32
|
export function getSubSchema(
|
|
33
33
|
rootSchema: Schema,
|
|
34
34
|
pointer: string,
|
|
35
|
-
id?: SchemaId
|
|
35
|
+
id?: SchemaId,
|
|
36
36
|
): Schema {
|
|
37
37
|
const content = JsonPointer.get(
|
|
38
38
|
rootSchema.content,
|
|
39
|
-
JsonPointer.parse(pointer)
|
|
39
|
+
JsonPointer.parse(pointer),
|
|
40
40
|
) as JsonSchema;
|
|
41
41
|
if (id == null) {
|
|
42
42
|
const subId =
|
|
@@ -76,7 +76,7 @@ export function getId(type: SchemaType, content: JsonSchemaObject): string {
|
|
|
76
76
|
export function setId(
|
|
77
77
|
type: SchemaType,
|
|
78
78
|
content: JsonSchemaObject,
|
|
79
|
-
id: string
|
|
79
|
+
id: string,
|
|
80
80
|
): void {
|
|
81
81
|
if (isJsonSchemaDraft04(content, type)) {
|
|
82
82
|
content[Draft04Id] ??= id;
|
|
@@ -88,12 +88,12 @@ export function setId(
|
|
|
88
88
|
export function searchAllSubSchema(
|
|
89
89
|
schema: Schema,
|
|
90
90
|
onFoundSchema: (subSchema: Schema) => void,
|
|
91
|
-
onFoundReference: (refId: SchemaId) => void
|
|
91
|
+
onFoundReference: (refId: SchemaId) => void,
|
|
92
92
|
): void {
|
|
93
93
|
const walkArray = (
|
|
94
94
|
array: JsonSchema[] | undefined,
|
|
95
95
|
paths: string[],
|
|
96
|
-
parentIds: string[]
|
|
96
|
+
parentIds: string[],
|
|
97
97
|
): void => {
|
|
98
98
|
if (array == null) {
|
|
99
99
|
return;
|
|
@@ -105,7 +105,7 @@ export function searchAllSubSchema(
|
|
|
105
105
|
const walkObject = (
|
|
106
106
|
obj: { [name: string]: JsonSchema } | undefined,
|
|
107
107
|
paths: string[],
|
|
108
|
-
parentIds: string[]
|
|
108
|
+
parentIds: string[],
|
|
109
109
|
): void => {
|
|
110
110
|
if (obj == null) {
|
|
111
111
|
return;
|
|
@@ -120,7 +120,7 @@ export function searchAllSubSchema(
|
|
|
120
120
|
const walkMaybeArray = (
|
|
121
121
|
item: JsonSchema | JsonSchema[] | undefined,
|
|
122
122
|
paths: string[],
|
|
123
|
-
parentIds: string[]
|
|
123
|
+
parentIds: string[],
|
|
124
124
|
): void => {
|
|
125
125
|
if (Array.isArray(item)) {
|
|
126
126
|
walkArray(item, paths, parentIds);
|
|
@@ -131,7 +131,7 @@ export function searchAllSubSchema(
|
|
|
131
131
|
const walk = (
|
|
132
132
|
s: JsonSchema | undefined,
|
|
133
133
|
paths: string[],
|
|
134
|
-
parentIds: string[]
|
|
134
|
+
parentIds: string[],
|
|
135
135
|
) => {
|
|
136
136
|
if (s == null || typeof s !== 'object') {
|
|
137
137
|
return;
|
|
@@ -165,14 +165,14 @@ export function searchAllSubSchema(
|
|
|
165
165
|
walk(
|
|
166
166
|
s.additionalProperties,
|
|
167
167
|
paths.concat('additionalProperties'),
|
|
168
|
-
parentIds
|
|
168
|
+
parentIds,
|
|
169
169
|
);
|
|
170
170
|
walkObject(s.definitions, paths.concat('definitions'), parentIds);
|
|
171
171
|
walkObject(s.properties, paths.concat('properties'), parentIds);
|
|
172
172
|
walkObject(
|
|
173
173
|
s.patternProperties,
|
|
174
174
|
paths.concat('patternProperties'),
|
|
175
|
-
parentIds
|
|
175
|
+
parentIds,
|
|
176
176
|
);
|
|
177
177
|
walkMaybeArray(s.dependencies, paths.concat('dependencies'), parentIds);
|
|
178
178
|
if (schema.type === 'Draft07') {
|
|
@@ -203,7 +203,7 @@ export function searchAllSubSchema(
|
|
|
203
203
|
function setSubIdToAnyObject<T>(
|
|
204
204
|
f: (t: T, keys: string[]) => void,
|
|
205
205
|
obj: { [key: string]: T } | undefined,
|
|
206
|
-
keys: string[]
|
|
206
|
+
keys: string[],
|
|
207
207
|
): void {
|
|
208
208
|
if (obj == null) {
|
|
209
209
|
return;
|
|
@@ -222,7 +222,7 @@ export function searchAllSubSchema(
|
|
|
222
222
|
| OpenApisV2.SchemaJson.Definitions.ParameterDefinitions
|
|
223
223
|
| OpenApisV3.SchemaJson.Definitions.ParametersOrReferences
|
|
224
224
|
| undefined,
|
|
225
|
-
keys: string[]
|
|
225
|
+
keys: string[],
|
|
226
226
|
): void {
|
|
227
227
|
if (obj == null) {
|
|
228
228
|
return;
|
|
@@ -231,7 +231,7 @@ export function searchAllSubSchema(
|
|
|
231
231
|
}
|
|
232
232
|
function setSubIdToParameters(
|
|
233
233
|
array: ParametersList | undefined,
|
|
234
|
-
keys: string[]
|
|
234
|
+
keys: string[],
|
|
235
235
|
): void {
|
|
236
236
|
if (array == null) {
|
|
237
237
|
return;
|
|
@@ -240,18 +240,18 @@ export function searchAllSubSchema(
|
|
|
240
240
|
array.map(
|
|
241
241
|
(
|
|
242
242
|
item: ParameterOrReference,
|
|
243
|
-
index: number
|
|
243
|
+
index: number,
|
|
244
244
|
): [string, ParameterOrReference] => {
|
|
245
245
|
const key = 'name' in item ? item.name : `${index}`;
|
|
246
246
|
return [key, item];
|
|
247
|
-
}
|
|
247
|
+
},
|
|
248
248
|
),
|
|
249
|
-
keys
|
|
249
|
+
keys,
|
|
250
250
|
);
|
|
251
251
|
}
|
|
252
252
|
function addParameterSchema(
|
|
253
253
|
input: readonly [string, ParameterOrReference][],
|
|
254
|
-
keys: string[]
|
|
254
|
+
keys: string[],
|
|
255
255
|
): void {
|
|
256
256
|
const map = new Map<string, [string, Parameter][]>();
|
|
257
257
|
const pushItem = (key: string, po: [string, Parameter]) => {
|
|
@@ -279,7 +279,7 @@ export function searchAllSubSchema(
|
|
|
279
279
|
if ('type' in item && item.in !== undefined) {
|
|
280
280
|
setSubId(
|
|
281
281
|
item as JsonSchemaDraft04.Schema,
|
|
282
|
-
keys.concat(key)
|
|
282
|
+
keys.concat(key),
|
|
283
283
|
);
|
|
284
284
|
pushItem(item.in, [key, item]);
|
|
285
285
|
}
|
|
@@ -292,7 +292,7 @@ export function searchAllSubSchema(
|
|
|
292
292
|
function buildParameterSchema(
|
|
293
293
|
inType: string,
|
|
294
294
|
params: [string, Parameter][],
|
|
295
|
-
keys: string[]
|
|
295
|
+
keys: string[],
|
|
296
296
|
): [string[], JsonSchemaObject] {
|
|
297
297
|
const paths = keys
|
|
298
298
|
.slice(0, keys.length - 1)
|
|
@@ -319,13 +319,13 @@ export function searchAllSubSchema(
|
|
|
319
319
|
/// for OpenAPI V2 only
|
|
320
320
|
const setSubIdToResponsesV2 = (
|
|
321
321
|
responses: OpenApisV2.SchemaJson.Definitions.Responses | undefined,
|
|
322
|
-
keys: string[]
|
|
322
|
+
keys: string[],
|
|
323
323
|
) => setSubIdToAnyObject(setSubIdToResponseV2, responses, keys);
|
|
324
324
|
function setSubIdToResponseV2(
|
|
325
325
|
response:
|
|
326
326
|
| OpenApisV2.SchemaJson.Definitions.ResponseValue
|
|
327
327
|
| undefined,
|
|
328
|
-
keys: string[]
|
|
328
|
+
keys: string[],
|
|
329
329
|
): void {
|
|
330
330
|
if (response == null) {
|
|
331
331
|
return;
|
|
@@ -340,7 +340,7 @@ export function searchAllSubSchema(
|
|
|
340
340
|
}
|
|
341
341
|
function setSubIdToOperationV2(
|
|
342
342
|
ops: OpenApisV2.SchemaJson.Definitions.Operation | undefined,
|
|
343
|
-
keys: string[]
|
|
343
|
+
keys: string[],
|
|
344
344
|
): void {
|
|
345
345
|
if (ops == null) {
|
|
346
346
|
return;
|
|
@@ -355,15 +355,15 @@ export function searchAllSubSchema(
|
|
|
355
355
|
}
|
|
356
356
|
const setSubIdToPathsV2 = (
|
|
357
357
|
paths: OpenApisV2.SchemaJson.Definitions.Paths,
|
|
358
|
-
keys: string[]
|
|
358
|
+
keys: string[],
|
|
359
359
|
) => setSubIdToAnyObject(setSubIdToPathItemV2, paths, keys);
|
|
360
360
|
function setSubIdToPathItemV2(
|
|
361
361
|
pathItem: OpenApisV2.SchemaJson.Definitions.PathItem,
|
|
362
|
-
keys: string[]
|
|
362
|
+
keys: string[],
|
|
363
363
|
): void {
|
|
364
364
|
setSubIdToParameters(
|
|
365
365
|
pathItem.parameters,
|
|
366
|
-
keys.concat('parameters')
|
|
366
|
+
keys.concat('parameters'),
|
|
367
367
|
);
|
|
368
368
|
setSubIdToOperationV2(pathItem.get, keys.concat('get'));
|
|
369
369
|
setSubIdToOperationV2(pathItem.put, keys.concat('put'));
|
|
@@ -377,7 +377,7 @@ export function searchAllSubSchema(
|
|
|
377
377
|
/// for OpenAPI V3 only
|
|
378
378
|
function setSubIdToMediaTypes(
|
|
379
379
|
types: OpenApisV3.SchemaJson.Definitions.MediaTypes | undefined,
|
|
380
|
-
keys: string[]
|
|
380
|
+
keys: string[],
|
|
381
381
|
): void {
|
|
382
382
|
if (types == null) {
|
|
383
383
|
return;
|
|
@@ -395,13 +395,13 @@ export function searchAllSubSchema(
|
|
|
395
395
|
bodies:
|
|
396
396
|
| OpenApisV3.SchemaJson.Definitions.RequestBodiesOrReferences
|
|
397
397
|
| undefined,
|
|
398
|
-
keys: string[]
|
|
398
|
+
keys: string[],
|
|
399
399
|
) => setSubIdToAnyObject(setSubIdToRequestBody, bodies, keys);
|
|
400
400
|
function setSubIdToRequestBody(
|
|
401
401
|
body:
|
|
402
402
|
| OpenApisV3.SchemaJson.Definitions.RequestBodyOrReference
|
|
403
403
|
| undefined,
|
|
404
|
-
keys: string[]
|
|
404
|
+
keys: string[],
|
|
405
405
|
): void {
|
|
406
406
|
if (body == null) {
|
|
407
407
|
return;
|
|
@@ -418,13 +418,13 @@ export function searchAllSubSchema(
|
|
|
418
418
|
responses:
|
|
419
419
|
| OpenApisV3.SchemaJson.Definitions.ResponsesOrReferences
|
|
420
420
|
| undefined,
|
|
421
|
-
keys: string[]
|
|
421
|
+
keys: string[],
|
|
422
422
|
) => setSubIdToAnyObject(setSubIdToResponseV3, responses, keys);
|
|
423
423
|
function setSubIdToResponseV3(
|
|
424
424
|
response:
|
|
425
425
|
| OpenApisV3.SchemaJson.Definitions.ResponseOrReference
|
|
426
426
|
| undefined,
|
|
427
|
-
keys: string[]
|
|
427
|
+
keys: string[],
|
|
428
428
|
): void {
|
|
429
429
|
if (response == null) {
|
|
430
430
|
return;
|
|
@@ -439,7 +439,7 @@ export function searchAllSubSchema(
|
|
|
439
439
|
}
|
|
440
440
|
function setSubIdToOperationV3(
|
|
441
441
|
ops: OpenApisV3.SchemaJson.Definitions.Operation | undefined,
|
|
442
|
-
keys: string[]
|
|
442
|
+
keys: string[],
|
|
443
443
|
): void {
|
|
444
444
|
if (ops == null) {
|
|
445
445
|
return;
|
|
@@ -455,15 +455,15 @@ export function searchAllSubSchema(
|
|
|
455
455
|
}
|
|
456
456
|
const setSubIdToPathsV3 = (
|
|
457
457
|
paths: OpenApisV3.SchemaJson.Definitions.Paths,
|
|
458
|
-
keys: string[]
|
|
458
|
+
keys: string[],
|
|
459
459
|
) => setSubIdToAnyObject(setSubIdToPathItemV3, paths, keys);
|
|
460
460
|
function setSubIdToPathItemV3(
|
|
461
461
|
pathItem: OpenApisV3.SchemaJson.Definitions.PathItem,
|
|
462
|
-
keys: string[]
|
|
462
|
+
keys: string[],
|
|
463
463
|
): void {
|
|
464
464
|
setSubIdToParameters(
|
|
465
465
|
pathItem.parameters,
|
|
466
|
-
keys.concat('parameters')
|
|
466
|
+
keys.concat('parameters'),
|
|
467
467
|
);
|
|
468
468
|
setSubIdToOperationV3(pathItem.get, keys.concat('get'));
|
|
469
469
|
setSubIdToOperationV3(pathItem.put, keys.concat('put'));
|
|
@@ -477,7 +477,7 @@ export function searchAllSubSchema(
|
|
|
477
477
|
|
|
478
478
|
function setSubIdToObject(
|
|
479
479
|
obj: { [name: string]: JsonSchema } | undefined,
|
|
480
|
-
paths: string[]
|
|
480
|
+
paths: string[],
|
|
481
481
|
): void {
|
|
482
482
|
if (obj == null) {
|
|
483
483
|
return;
|
|
@@ -554,7 +554,7 @@ export function selectSchemaType(content: JsonSchema | OpenApiSchema): {
|
|
|
554
554
|
}
|
|
555
555
|
if (typeof content !== 'object') {
|
|
556
556
|
throw new Error(
|
|
557
|
-
`expect parameter of type object, received ${typeof content}
|
|
557
|
+
`expect parameter of type object, received ${typeof content}`,
|
|
558
558
|
);
|
|
559
559
|
}
|
|
560
560
|
if ('$schema' in content) {
|
|
@@ -564,7 +564,7 @@ export function selectSchemaType(content: JsonSchema | OpenApiSchema): {
|
|
|
564
564
|
}
|
|
565
565
|
const match =
|
|
566
566
|
/^https?:\/\/json-schema\.org\/(?:draft\/(\d{4}-\d{2})|draft-(\d+))\/schema#?$/.exec(
|
|
567
|
-
schema
|
|
567
|
+
schema,
|
|
568
568
|
);
|
|
569
569
|
if (match) {
|
|
570
570
|
if (match[1] != null) {
|
package/src/core/openApiV2.ts
CHANGED
|
@@ -532,11 +532,11 @@ export namespace OpenApisV2 {
|
|
|
532
532
|
| Schema
|
|
533
533
|
| [
|
|
534
534
|
/* A deterministic version of a JSON Schema object. */ Schema,
|
|
535
|
-
.../* A deterministic version of a JSON Schema object. */ Schema[]
|
|
535
|
+
.../* A deterministic version of a JSON Schema object. */ Schema[],
|
|
536
536
|
];
|
|
537
537
|
allOf?: [
|
|
538
538
|
/* A deterministic version of a JSON Schema object. */ Schema,
|
|
539
|
-
.../* A deterministic version of a JSON Schema object. */ Schema[]
|
|
539
|
+
.../* A deterministic version of a JSON Schema object. */ Schema[],
|
|
540
540
|
];
|
|
541
541
|
properties?: {
|
|
542
542
|
[
|
package/src/core/openApiV3.ts
CHANGED
|
@@ -57,13 +57,7 @@ export namespace OpenApisV3 {
|
|
|
57
57
|
url?: string; // uri
|
|
58
58
|
email?: string; // email
|
|
59
59
|
}
|
|
60
|
-
export type DefaultType =
|
|
61
|
-
| null
|
|
62
|
-
| any[]
|
|
63
|
-
| unknown
|
|
64
|
-
| number
|
|
65
|
-
| boolean
|
|
66
|
-
| string;
|
|
60
|
+
export type DefaultType = null | any[] | number | boolean | string;
|
|
67
61
|
/**
|
|
68
62
|
* When request bodies or response payloads may be one of a number of different schemas, a `discriminator` object can be used to aid in serialization, deserialization, and validation. The discriminator is a specific object in a schema which is used to inform the consumer of the specification of an alternative schema based on the value associated with it. When using the discriminator, _inline_ schemas will not be considered.
|
|
69
63
|
*/
|
|
@@ -342,7 +336,6 @@ export namespace OpenApisV3 {
|
|
|
342
336
|
| number
|
|
343
337
|
| boolean
|
|
344
338
|
| string
|
|
345
|
-
| unknown
|
|
346
339
|
| any[];
|
|
347
340
|
export interface Strings {
|
|
348
341
|
[name: string]: string;
|
|
@@ -27,7 +27,7 @@ export default class ReferenceResolver {
|
|
|
27
27
|
|
|
28
28
|
public async resolve(): Promise<void> {
|
|
29
29
|
debug(
|
|
30
|
-
`resolve reference: reference schema count=${this.referenceCache.size}
|
|
30
|
+
`resolve reference: reference schema count=${this.referenceCache.size}.`,
|
|
31
31
|
);
|
|
32
32
|
// debug(' schemaCache:');
|
|
33
33
|
// debug(Array.from(this.schemaCache.keys()).join('\n'));
|
|
@@ -45,8 +45,8 @@ export default class ReferenceResolver {
|
|
|
45
45
|
const refSchema = this.schemaCache.get(fileId);
|
|
46
46
|
debug(
|
|
47
47
|
`get from schema cache, fileId=${fileId}, exists=${String(
|
|
48
|
-
!!refSchema
|
|
49
|
-
)}, ${id.getAbsoluteId()}
|
|
48
|
+
!!refSchema,
|
|
49
|
+
)}, ${id.getAbsoluteId()}`,
|
|
50
50
|
);
|
|
51
51
|
if (refSchema == null && id.isFetchable()) {
|
|
52
52
|
try {
|
|
@@ -56,8 +56,8 @@ export default class ReferenceResolver {
|
|
|
56
56
|
} catch (e) {
|
|
57
57
|
error.push(
|
|
58
58
|
`Fail to fetch the $ref target: ${id.getAbsoluteId()}, ${String(
|
|
59
|
-
e
|
|
60
|
-
)}
|
|
59
|
+
e,
|
|
60
|
+
)}`,
|
|
61
61
|
);
|
|
62
62
|
continue;
|
|
63
63
|
}
|
|
@@ -72,21 +72,21 @@ export default class ReferenceResolver {
|
|
|
72
72
|
const rootSchema = this.searchParentSchema(id);
|
|
73
73
|
if (rootSchema == null) {
|
|
74
74
|
error.push(
|
|
75
|
-
`The $ref targets root is not found: ${id.getAbsoluteId()}
|
|
75
|
+
`The $ref targets root is not found: ${id.getAbsoluteId()}`,
|
|
76
76
|
);
|
|
77
77
|
continue;
|
|
78
78
|
}
|
|
79
79
|
const targetSchema = getSubSchema(
|
|
80
80
|
rootSchema,
|
|
81
81
|
id.getJsonPointerHash(),
|
|
82
|
-
id
|
|
82
|
+
id,
|
|
83
83
|
);
|
|
84
84
|
this.addSchema(targetSchema);
|
|
85
85
|
this.registerSchema(targetSchema);
|
|
86
86
|
this.referenceCache.set(id.getAbsoluteId(), targetSchema);
|
|
87
87
|
} else {
|
|
88
88
|
error.push(
|
|
89
|
-
`The $ref target is not found: ${id.getAbsoluteId()}
|
|
89
|
+
`The $ref target is not found: ${id.getAbsoluteId()}`,
|
|
90
90
|
);
|
|
91
91
|
continue;
|
|
92
92
|
}
|
|
@@ -125,7 +125,7 @@ export default class ReferenceResolver {
|
|
|
125
125
|
},
|
|
126
126
|
(refId) => {
|
|
127
127
|
this.addReference(refId);
|
|
128
|
-
}
|
|
128
|
+
},
|
|
129
129
|
);
|
|
130
130
|
}
|
|
131
131
|
|
package/src/core/schemaId.ts
CHANGED
|
@@ -6,7 +6,10 @@ export default class SchemaId {
|
|
|
6
6
|
public readonly id: url.Url;
|
|
7
7
|
public static readonly empty = new SchemaId('');
|
|
8
8
|
|
|
9
|
-
constructor(
|
|
9
|
+
constructor(
|
|
10
|
+
public readonly inputId: string,
|
|
11
|
+
parentIds?: string[],
|
|
12
|
+
) {
|
|
10
13
|
let absoluteId = url.resolve('', inputId);
|
|
11
14
|
if (parentIds) {
|
|
12
15
|
parentIds.forEach((parent: string) => {
|
package/src/core/type.ts
CHANGED
|
@@ -29,7 +29,7 @@ export type SchemaType =
|
|
|
29
29
|
| 'Latest';
|
|
30
30
|
export function isJsonSchemaDraft04(
|
|
31
31
|
_content: JsonSchemaObject,
|
|
32
|
-
type: SchemaType
|
|
32
|
+
type: SchemaType,
|
|
33
33
|
): _content is JsonSchemaDraft04.Schema {
|
|
34
34
|
return type === 'Draft04';
|
|
35
35
|
}
|
|
@@ -77,7 +77,7 @@ export async function readSchemasFromFile(pattern: string): Promise<Schema[]> {
|
|
|
77
77
|
});
|
|
78
78
|
const content = parseFileContent(data);
|
|
79
79
|
return parseSchema(content);
|
|
80
|
-
})
|
|
80
|
+
}),
|
|
81
81
|
);
|
|
82
82
|
}
|
|
83
83
|
export async function readSchemaFromUrl(url: string): Promise<Schema> {
|
|
@@ -88,7 +88,7 @@ export async function readSchemaFromUrl(url: string): Promise<Schema> {
|
|
|
88
88
|
|
|
89
89
|
export function parseFileContent(
|
|
90
90
|
content: string,
|
|
91
|
-
filename?: string
|
|
91
|
+
filename?: string,
|
|
92
92
|
): JsonSchema {
|
|
93
93
|
const ext = filename ? extname(filename).toLowerCase() : '';
|
|
94
94
|
const maybeYaml = ext === '.yaml' || ext === '.yml';
|
|
@@ -126,16 +126,16 @@ export type Plugin = {
|
|
|
126
126
|
description?: string;
|
|
127
127
|
};
|
|
128
128
|
preProcess?: (
|
|
129
|
-
context: PluginContext
|
|
129
|
+
context: PluginContext,
|
|
130
130
|
) => Promise<PreProcessHandler | undefined>;
|
|
131
131
|
postProcess?: (
|
|
132
|
-
context: PluginContext
|
|
132
|
+
context: PluginContext,
|
|
133
133
|
) => Promise<TransformerFactory<SourceFile> | undefined>;
|
|
134
134
|
};
|
|
135
135
|
|
|
136
136
|
export async function loadPlugin(
|
|
137
137
|
name: string,
|
|
138
|
-
option: boolean | Record<string, unknown
|
|
138
|
+
option: boolean | Record<string, unknown>,
|
|
139
139
|
): Promise<Plugin | undefined> {
|
|
140
140
|
if (!option) {
|
|
141
141
|
return undefined;
|
|
@@ -144,14 +144,14 @@ export async function loadPlugin(
|
|
|
144
144
|
const mod = (await import(name)) as { default?: Plugin };
|
|
145
145
|
if (!mod.default) {
|
|
146
146
|
console.warn(
|
|
147
|
-
`The plugin (${name}) is invalid module. That is not default export format
|
|
147
|
+
`The plugin (${name}) is invalid module. That is not default export format.`,
|
|
148
148
|
);
|
|
149
149
|
return undefined;
|
|
150
150
|
}
|
|
151
151
|
const plugin = mod.default;
|
|
152
152
|
if (plugin.preProcess != null && typeof plugin.preProcess !== 'function') {
|
|
153
153
|
console.warn(
|
|
154
|
-
`The plugin (${name}) is invalid module. The 'preProcess' is not a function
|
|
154
|
+
`The plugin (${name}) is invalid module. The 'preProcess' is not a function.`,
|
|
155
155
|
);
|
|
156
156
|
return undefined;
|
|
157
157
|
}
|
|
@@ -160,7 +160,7 @@ export async function loadPlugin(
|
|
|
160
160
|
typeof plugin.postProcess !== 'function'
|
|
161
161
|
) {
|
|
162
162
|
console.warn(
|
|
163
|
-
`The plugin (${name}) is invalid module. The 'postProcess' is not a function
|
|
163
|
+
`The plugin (${name}) is invalid module. The 'postProcess' is not a function.`,
|
|
164
164
|
);
|
|
165
165
|
return undefined;
|
|
166
166
|
}
|
package/src/core/utils.ts
CHANGED
|
@@ -11,7 +11,7 @@ const debug = Debug('dtsgen');
|
|
|
11
11
|
|
|
12
12
|
export function toTSType(
|
|
13
13
|
type: string,
|
|
14
|
-
debugSource?: JsonSchemaObject
|
|
14
|
+
debugSource?: JsonSchemaObject,
|
|
15
15
|
): ts.KeywordTypeSyntaxKind | ts.SyntaxKind.NullKeyword | undefined {
|
|
16
16
|
switch (type) {
|
|
17
17
|
case 'any':
|
|
@@ -39,8 +39,8 @@ export function toTSType(
|
|
|
39
39
|
`toTSType: unknown type: ${JSON.stringify(
|
|
40
40
|
debugSource,
|
|
41
41
|
null,
|
|
42
|
-
2
|
|
43
|
-
)}
|
|
42
|
+
2,
|
|
43
|
+
)}`,
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
46
|
throw new Error('unknown type: ' + type);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
|
2
3
|
import { ScriptTarget } from 'typescript';
|
|
3
4
|
|
|
4
5
|
// via. https://github.com/microsoft/TypeScript/blob/v3.7.5/src/services/codefixes/importFixes.ts#L689
|
|
5
6
|
export function checkInvalidCharacter(
|
|
6
7
|
text: string,
|
|
7
|
-
target: ScriptTarget
|
|
8
|
+
target: ScriptTarget,
|
|
8
9
|
): boolean {
|
|
9
10
|
const firstCharCode = text.charCodeAt(0);
|
|
10
11
|
if (!isIdentifierStart(firstCharCode, target)) {
|
package/src/jsonPointer.ts
CHANGED
package/src/utils.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
2
|
import { URL } from 'url';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { GlobOptions, globIterate } from 'glob';
|
|
4
|
+
import { HttpProxyAgent } from 'http-proxy-agent';
|
|
5
|
+
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
5
6
|
import { ScriptTarget } from 'typescript';
|
|
6
7
|
import { CommandOptions, defaultConfigFile } from './commandOptions';
|
|
7
8
|
import { PartialConfig } from './core/config';
|
|
8
9
|
|
|
9
10
|
export function readStream(
|
|
10
11
|
stream: NodeJS.ReadableStream,
|
|
11
|
-
encoding: BufferEncoding = 'utf8'
|
|
12
|
+
encoding: BufferEncoding = 'utf8',
|
|
12
13
|
): Promise<string> {
|
|
13
14
|
stream.setEncoding(encoding);
|
|
14
15
|
return new Promise((resolve, reject) => {
|
|
@@ -25,7 +26,7 @@ export async function readUrl(url: string): Promise<string> {
|
|
|
25
26
|
const data = await res.text();
|
|
26
27
|
if (!res.ok) {
|
|
27
28
|
throw new Error(
|
|
28
|
-
`Error on fetch from url(${url}): ${res.status}, ${data}
|
|
29
|
+
`Error on fetch from url(${url}): ${res.status}, ${data}`,
|
|
29
30
|
);
|
|
30
31
|
}
|
|
31
32
|
return data;
|
|
@@ -40,41 +41,37 @@ function noProxy(url: URL): boolean {
|
|
|
40
41
|
}
|
|
41
42
|
return false;
|
|
42
43
|
}
|
|
43
|
-
export function buildProxyOptions
|
|
44
|
+
export function buildProxyOptions<Uri extends string>(
|
|
45
|
+
url: Uri,
|
|
46
|
+
): RequestInit | undefined {
|
|
44
47
|
const parsedUrl = new URL(url);
|
|
45
|
-
let proxyUrl;
|
|
46
48
|
if (!noProxy(parsedUrl)) {
|
|
47
49
|
if (parsedUrl.protocol === 'http:' && process.env.HTTP_PROXY) {
|
|
48
|
-
|
|
50
|
+
return {
|
|
51
|
+
agent: new HttpProxyAgent(process.env.HTTP_PROXY),
|
|
52
|
+
} as RequestInit;
|
|
49
53
|
} else if (parsedUrl.protocol === 'https:' && process.env.HTTPS_PROXY) {
|
|
50
|
-
|
|
54
|
+
return {
|
|
55
|
+
agent: new HttpsProxyAgent(process.env.HTTPS_PROXY),
|
|
56
|
+
} as RequestInit;
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
|
-
if (proxyUrl) {
|
|
54
|
-
const agentOptions: proxy.HttpsProxyAgentOptions = {};
|
|
55
|
-
agentOptions.protocol = proxyUrl.protocol;
|
|
56
|
-
agentOptions.host = proxyUrl.hostname;
|
|
57
|
-
agentOptions.port = proxyUrl.port;
|
|
58
|
-
if (proxyUrl.username) {
|
|
59
|
-
agentOptions.auth = proxyUrl.username + ':' + proxyUrl.password;
|
|
60
|
-
}
|
|
61
|
-
return { agent: proxy(agentOptions) } as RequestInit;
|
|
62
|
-
}
|
|
63
59
|
return undefined;
|
|
64
60
|
}
|
|
65
61
|
|
|
66
62
|
export async function globFiles(
|
|
67
63
|
pattern: string,
|
|
68
|
-
options?: GlobOptions
|
|
64
|
+
options?: GlobOptions,
|
|
69
65
|
): Promise<string[]> {
|
|
70
|
-
const
|
|
71
|
-
|
|
66
|
+
const result: string[] = [];
|
|
67
|
+
for await (const r of globIterate(pattern, options ?? {})) {
|
|
72
68
|
if (typeof r === 'string') {
|
|
73
|
-
|
|
69
|
+
result.push(r);
|
|
74
70
|
} else {
|
|
75
|
-
|
|
71
|
+
result.push(r.fullpath());
|
|
76
72
|
}
|
|
77
|
-
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
78
75
|
}
|
|
79
76
|
|
|
80
77
|
export function readConfig(options: CommandOptions): PartialConfig {
|
|
@@ -86,7 +83,7 @@ export function readConfig(options: CommandOptions): PartialConfig {
|
|
|
86
83
|
} catch (err) {
|
|
87
84
|
if (options.configFile != null) {
|
|
88
85
|
console.error(
|
|
89
|
-
'Error to load config file from ' + options.configFile
|
|
86
|
+
'Error to load config file from ' + options.configFile,
|
|
90
87
|
);
|
|
91
88
|
}
|
|
92
89
|
}
|
|
@@ -112,6 +109,7 @@ export function readConfig(options: CommandOptions): PartialConfig {
|
|
|
112
109
|
pc.input.stdin = options.stdin;
|
|
113
110
|
} else {
|
|
114
111
|
pc.input.stdin =
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
115
113
|
pc.input.stdin ||
|
|
116
114
|
(pc.input.files.length === 0 && pc.input.urls.length === 0);
|
|
117
115
|
}
|