@azure-tools/typespec-ts 0.17.1 → 0.18.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/CHANGELOG.md +13 -0
- package/dist/src/lib.d.ts +10 -1
- package/dist/src/lib.d.ts.map +1 -1
- package/dist/src/lib.js +6 -0
- package/dist/src/lib.js.map +1 -1
- package/dist/src/modular/buildCodeModel.d.ts.map +1 -1
- package/dist/src/modular/buildCodeModel.js +52 -39
- package/dist/src/modular/buildCodeModel.js.map +1 -1
- package/dist/src/modular/buildOperations.d.ts.map +1 -1
- package/dist/src/modular/buildOperations.js +1 -1
- package/dist/src/modular/buildOperations.js.map +1 -1
- package/dist/src/modular/buildProjectFiles.js +4 -4
- package/dist/src/modular/buildProjectFiles.js.map +1 -1
- package/dist/src/modular/emitModels.d.ts.map +1 -1
- package/dist/src/modular/emitModels.js +83 -70
- package/dist/src/modular/emitModels.js.map +1 -1
- package/dist/src/modular/helpers/clientHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/clientHelpers.js +6 -1
- package/dist/src/modular/helpers/clientHelpers.js.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.d.ts +2 -1
- package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/operationHelpers.js +81 -30
- package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.d.ts +9 -2
- package/dist/src/modular/helpers/typeHelpers.d.ts.map +1 -1
- package/dist/src/modular/helpers/typeHelpers.js +174 -102
- package/dist/src/modular/helpers/typeHelpers.js.map +1 -1
- package/dist/src/modular/modularCodeModel.d.ts +3 -1
- package/dist/src/modular/modularCodeModel.d.ts.map +1 -1
- package/dist/src/transform/transformParameters.d.ts.map +1 -1
- package/dist/src/transform/transformParameters.js +10 -95
- package/dist/src/transform/transformParameters.js.map +1 -1
- package/dist/src/transform/transformPaths.d.ts +0 -5
- package/dist/src/transform/transformPaths.d.ts.map +1 -1
- package/dist/src/transform/transformPaths.js +4 -42
- package/dist/src/transform/transformPaths.js.map +1 -1
- package/dist/src/transform/transformResponses.d.ts.map +1 -1
- package/dist/src/transform/transformResponses.js +2 -2
- package/dist/src/transform/transformResponses.js.map +1 -1
- package/dist/src/utils/clientUtils.d.ts.map +1 -1
- package/dist/src/utils/clientUtils.js +5 -3
- package/dist/src/utils/clientUtils.js.map +1 -1
- package/dist/src/utils/modelUtils.d.ts +14 -2
- package/dist/src/utils/modelUtils.d.ts.map +1 -1
- package/dist/src/utils/modelUtils.js +172 -41
- package/dist/src/utils/modelUtils.js.map +1 -1
- package/dist/src/utils/operationUtil.d.ts +13 -3
- package/dist/src/utils/operationUtil.d.ts.map +1 -1
- package/dist/src/utils/operationUtil.js +66 -10
- package/dist/src/utils/operationUtil.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -17
- package/src/lib.ts +7 -0
- package/src/modular/buildCodeModel.ts +63 -44
- package/src/modular/buildOperations.ts +1 -0
- package/src/modular/buildProjectFiles.ts +4 -4
- package/src/modular/emitModels.ts +110 -79
- package/src/modular/helpers/clientHelpers.ts +6 -1
- package/src/modular/helpers/operationHelpers.ts +101 -44
- package/src/modular/helpers/typeHelpers.ts +208 -119
- package/src/modular/modularCodeModel.ts +4 -1
- package/src/transform/transformParameters.ts +13 -137
- package/src/transform/transformPaths.ts +8 -61
- package/src/transform/transformResponses.ts +3 -2
- package/src/utils/clientUtils.ts +11 -3
- package/src/utils/modelUtils.ts +232 -46
- package/src/utils/operationUtil.ts +82 -10
package/src/utils/modelUtils.ts
CHANGED
|
@@ -47,6 +47,7 @@ import {
|
|
|
47
47
|
} from "@typespec/compiler";
|
|
48
48
|
import { reportDiagnostic } from "../lib.js";
|
|
49
49
|
import {
|
|
50
|
+
ArraySchema,
|
|
50
51
|
DictionarySchema,
|
|
51
52
|
NameType,
|
|
52
53
|
normalizeName,
|
|
@@ -82,7 +83,10 @@ export function isByteOrByteUnion(dpgContext: SdkContext, type: Type) {
|
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
function isByteType(schema: any) {
|
|
85
|
-
return
|
|
86
|
+
return (
|
|
87
|
+
schema.type === "string" &&
|
|
88
|
+
(schema.format === "byte" || schema.format === "binary")
|
|
89
|
+
);
|
|
86
90
|
}
|
|
87
91
|
|
|
88
92
|
function isByteUnion(schema: any) {
|
|
@@ -147,20 +151,36 @@ export function getSchemaForType(
|
|
|
147
151
|
}
|
|
148
152
|
if (type.kind === "Model") {
|
|
149
153
|
const schema = getSchemaForModel(dpgContext, type, usage, needRef) as any;
|
|
150
|
-
if (
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
+
if (isAnonymousObjectSchema(schema)) {
|
|
155
|
+
if (Object.keys(schema.properties ?? {}).length === 0) {
|
|
156
|
+
// Handle empty anonymous model as Record
|
|
157
|
+
schema.typeName =
|
|
158
|
+
schema.type === "object" ? "Record<string, unknown>" : "unknown";
|
|
159
|
+
if (usage && usage.includes(SchemaContext.Output)) {
|
|
154
160
|
schema.outputTypeName =
|
|
155
161
|
schema.type === "object" ? "Record<string, any>" : "any";
|
|
156
|
-
schema.typeName =
|
|
157
|
-
schema.type === "object" ? "Record<string, unknown>" : "unknown";
|
|
158
|
-
schema.type = "unknown";
|
|
159
|
-
} else {
|
|
160
|
-
schema.outputTypeName = `${schema.name}Output`;
|
|
161
|
-
schema.typeName = `${schema.name}`;
|
|
162
162
|
}
|
|
163
|
+
schema.type = "unknown";
|
|
164
|
+
} else {
|
|
165
|
+
// Handle non-empty anonymous model as inline model
|
|
166
|
+
if (usage && usage.includes(SchemaContext.Output)) {
|
|
167
|
+
schema.outputTypeName = getModelInlineSigniture(schema, {
|
|
168
|
+
usage: [SchemaContext.Output]
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
schema.typeName = getModelInlineSigniture(schema, {
|
|
172
|
+
usage: [SchemaContext.Input]
|
|
173
|
+
});
|
|
174
|
+
schema.type = "object";
|
|
175
|
+
}
|
|
176
|
+
} else if (
|
|
177
|
+
!isArrayModelType(program, type) &&
|
|
178
|
+
!isRecordModelType(program, type)
|
|
179
|
+
) {
|
|
180
|
+
if (usage && usage.includes(SchemaContext.Output)) {
|
|
181
|
+
schema.outputTypeName = `${schema.name}Output`;
|
|
163
182
|
}
|
|
183
|
+
schema.typeName = `${schema.name}`;
|
|
164
184
|
}
|
|
165
185
|
schema.usage = usage;
|
|
166
186
|
return schema;
|
|
@@ -222,20 +242,74 @@ export function includeDerivedModel(model: Model): boolean {
|
|
|
222
242
|
);
|
|
223
243
|
}
|
|
224
244
|
|
|
245
|
+
function applyEncoding(
|
|
246
|
+
dpgContext: SdkContext,
|
|
247
|
+
typespecType: Scalar | ModelProperty,
|
|
248
|
+
target: any = {}
|
|
249
|
+
) {
|
|
250
|
+
const encodeData = getEncode(dpgContext.program, typespecType);
|
|
251
|
+
if (encodeData) {
|
|
252
|
+
const newTarget = { ...target };
|
|
253
|
+
const newType = getSchemaForScalar(dpgContext, encodeData.type);
|
|
254
|
+
newTarget["type"] = newType["type"];
|
|
255
|
+
newTarget["typeName"] = newType["typeName"];
|
|
256
|
+
newTarget["outputTypeName"] = newType["outputTypeName"];
|
|
257
|
+
// If the target already has a format it takes priority. (e.g. int32)
|
|
258
|
+
newTarget["format"] = mergeFormatAndEncoding(
|
|
259
|
+
newTarget.format,
|
|
260
|
+
encodeData.encoding,
|
|
261
|
+
newType["format"]
|
|
262
|
+
);
|
|
263
|
+
return newTarget;
|
|
264
|
+
}
|
|
265
|
+
return target;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function mergeFormatAndEncoding(
|
|
269
|
+
format: string | undefined,
|
|
270
|
+
encoding: string,
|
|
271
|
+
encodeAsFormat: string | undefined
|
|
272
|
+
): string {
|
|
273
|
+
switch (format) {
|
|
274
|
+
case undefined:
|
|
275
|
+
return encodeAsFormat ?? encoding;
|
|
276
|
+
case "date-time":
|
|
277
|
+
return encoding;
|
|
278
|
+
case "duration":
|
|
279
|
+
default:
|
|
280
|
+
return encodeAsFormat ?? encoding;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
225
284
|
function getSchemaForScalar(
|
|
226
285
|
dpgContext: SdkContext,
|
|
227
286
|
scalar: Scalar,
|
|
228
287
|
relevantProperty?: ModelProperty
|
|
229
288
|
) {
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
289
|
+
let result = {};
|
|
290
|
+
const isStd = dpgContext.program.checker.isStdType(scalar);
|
|
291
|
+
if (isStd) {
|
|
292
|
+
result = getSchemaForStdScalar(
|
|
293
|
+
dpgContext.program,
|
|
294
|
+
scalar,
|
|
295
|
+
relevantProperty
|
|
296
|
+
);
|
|
297
|
+
} else if (scalar.baseScalar) {
|
|
236
298
|
result = getSchemaForScalar(dpgContext, scalar.baseScalar);
|
|
237
299
|
}
|
|
238
|
-
|
|
300
|
+
const withDecorators = applyEncoding(
|
|
301
|
+
dpgContext,
|
|
302
|
+
scalar,
|
|
303
|
+
result
|
|
304
|
+
? applyIntrinsicDecorators(dpgContext.program, scalar, result)
|
|
305
|
+
: undefined
|
|
306
|
+
);
|
|
307
|
+
if (withDecorators.type === "string" && withDecorators.format === "binary") {
|
|
308
|
+
withDecorators.typeName =
|
|
309
|
+
"string | Uint8Array | ReadableStream<Uint8Array> | NodeJS.ReadableStream";
|
|
310
|
+
withDecorators.outputTypeName = "Uint8Array";
|
|
311
|
+
}
|
|
312
|
+
return withDecorators;
|
|
239
313
|
}
|
|
240
314
|
|
|
241
315
|
function getSchemaForUnion(
|
|
@@ -523,7 +597,7 @@ function getSchemaForModel(
|
|
|
523
597
|
dpgContext,
|
|
524
598
|
prop.type,
|
|
525
599
|
usage,
|
|
526
|
-
|
|
600
|
+
!isAnonymousModelType(prop.type),
|
|
527
601
|
prop
|
|
528
602
|
);
|
|
529
603
|
childSchema.discriminatorValue = propSchema.type.replace(/"/g, "");
|
|
@@ -578,9 +652,10 @@ function getSchemaForModel(
|
|
|
578
652
|
dpgContext,
|
|
579
653
|
prop.type,
|
|
580
654
|
usage,
|
|
581
|
-
true,
|
|
655
|
+
isAnonymousModelType(prop.type) ? false : true,
|
|
582
656
|
prop
|
|
583
657
|
);
|
|
658
|
+
|
|
584
659
|
if (propSchema === undefined) {
|
|
585
660
|
continue;
|
|
586
661
|
}
|
|
@@ -605,12 +680,7 @@ function getSchemaForModel(
|
|
|
605
680
|
}
|
|
606
681
|
|
|
607
682
|
// Apply decorators on the property to the type's schema
|
|
608
|
-
const newPropSchema = applyIntrinsicDecorators(
|
|
609
|
-
program,
|
|
610
|
-
|
|
611
|
-
prop,
|
|
612
|
-
propSchema
|
|
613
|
-
);
|
|
683
|
+
const newPropSchema = applyIntrinsicDecorators(program, prop, propSchema);
|
|
614
684
|
if (newPropSchema === undefined) {
|
|
615
685
|
continue;
|
|
616
686
|
}
|
|
@@ -677,7 +747,7 @@ function applyIntrinsicDecorators(
|
|
|
677
747
|
const isString = isStringType(program, getPropertyType(type));
|
|
678
748
|
const isNumeric = isNumericType(program, getPropertyType(type));
|
|
679
749
|
|
|
680
|
-
if (isString && !target
|
|
750
|
+
if (isString && !target?.documentation && docStr) {
|
|
681
751
|
newTarget.description = docStr;
|
|
682
752
|
}
|
|
683
753
|
|
|
@@ -785,7 +855,12 @@ function getSchemaForArrayModel(
|
|
|
785
855
|
if (isArrayModelType(program, type)) {
|
|
786
856
|
schema = {
|
|
787
857
|
type: "array",
|
|
788
|
-
items: getSchemaForType(
|
|
858
|
+
items: getSchemaForType(
|
|
859
|
+
dpgContext,
|
|
860
|
+
indexer.value!,
|
|
861
|
+
usage,
|
|
862
|
+
!isAnonymousModelType(indexer.value!)
|
|
863
|
+
),
|
|
789
864
|
description: getDoc(program, type)
|
|
790
865
|
};
|
|
791
866
|
if (
|
|
@@ -805,6 +880,19 @@ function getSchemaForArrayModel(
|
|
|
805
880
|
schema.typeName = `${schema.items.typeName}[]`;
|
|
806
881
|
} else if (schema.items.type === "union") {
|
|
807
882
|
schema.typeName = `(${schema.items.typeName})[]`;
|
|
883
|
+
} else if (
|
|
884
|
+
schema.items.format === "binary" &&
|
|
885
|
+
schema.items.type === "string"
|
|
886
|
+
) {
|
|
887
|
+
schema.typeName = `Array<${schema.items.typeName}>`;
|
|
888
|
+
if (usage && usage.includes(SchemaContext.Output)) {
|
|
889
|
+
schema.outputTypeName = `Array<${schema.items.outputTypeName}>`;
|
|
890
|
+
}
|
|
891
|
+
} else if (isAnonymousObjectSchema(schema.items)) {
|
|
892
|
+
schema.typeName = `${schema.items.typeName}[]`;
|
|
893
|
+
if (usage && usage.includes(SchemaContext.Output)) {
|
|
894
|
+
schema.outputTypeName = `${schema.items.outputTypeName}[]`;
|
|
895
|
+
}
|
|
808
896
|
} else {
|
|
809
897
|
schema.typeName = schema.items.typeName
|
|
810
898
|
.split("|")
|
|
@@ -848,7 +936,12 @@ function getSchemaForRecordModel(
|
|
|
848
936
|
return schema;
|
|
849
937
|
}
|
|
850
938
|
if (isRecordModelType(program, type)) {
|
|
851
|
-
const valueType = getSchemaForType(
|
|
939
|
+
const valueType = getSchemaForType(
|
|
940
|
+
dpgContext,
|
|
941
|
+
indexer?.value,
|
|
942
|
+
usage,
|
|
943
|
+
!isAnonymousModelType(indexer.value)
|
|
944
|
+
);
|
|
852
945
|
schema = {
|
|
853
946
|
type: "dictionary",
|
|
854
947
|
additionalProperties: valueType,
|
|
@@ -983,6 +1076,16 @@ function getSchemaForStdScalar(
|
|
|
983
1076
|
format: "float"
|
|
984
1077
|
});
|
|
985
1078
|
case "string":
|
|
1079
|
+
if (format === "binary") {
|
|
1080
|
+
return {
|
|
1081
|
+
type: "string",
|
|
1082
|
+
format: "binary",
|
|
1083
|
+
description,
|
|
1084
|
+
typeName:
|
|
1085
|
+
"string | Uint8Array | ReadableStream<Uint8Array> | NodeJS.ReadableStream",
|
|
1086
|
+
outputTypeName: "Uint8Array"
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
986
1089
|
return applyIntrinsicDecorators(program, type, {
|
|
987
1090
|
type: "string"
|
|
988
1091
|
});
|
|
@@ -1022,6 +1125,8 @@ function getSchemaForStdScalar(
|
|
|
1022
1125
|
};
|
|
1023
1126
|
case "duration":
|
|
1024
1127
|
return { type: "string", format, description };
|
|
1128
|
+
case "url":
|
|
1129
|
+
return { type: "string", format: "uri" };
|
|
1025
1130
|
}
|
|
1026
1131
|
}
|
|
1027
1132
|
|
|
@@ -1052,24 +1157,60 @@ export function getTypeName(schema: Schema, usage?: SchemaContext[]): string {
|
|
|
1052
1157
|
return getPriorityName(schema, usage) ?? schema.type ?? "any";
|
|
1053
1158
|
}
|
|
1054
1159
|
|
|
1055
|
-
export function getImportedModelName(
|
|
1160
|
+
export function getImportedModelName(
|
|
1161
|
+
schema: Schema,
|
|
1162
|
+
usage?: SchemaContext[]
|
|
1163
|
+
): string[] {
|
|
1056
1164
|
switch (schema.type) {
|
|
1057
|
-
case "array":
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
.
|
|
1061
|
-
|
|
1062
|
-
|
|
1165
|
+
case "array": {
|
|
1166
|
+
const ret = new Set<string>();
|
|
1167
|
+
[(schema as ArraySchema).items]
|
|
1168
|
+
.filter((i?: Schema) => !!i && i.type === "object")
|
|
1169
|
+
.forEach((i?: Schema) =>
|
|
1170
|
+
getImportedModelName(i!, usage).forEach((it) => ret.add(it))
|
|
1171
|
+
);
|
|
1172
|
+
return [...ret];
|
|
1173
|
+
}
|
|
1174
|
+
case "object": {
|
|
1175
|
+
if (isAnonymousObjectSchema(schema)) {
|
|
1176
|
+
const ret = new Set<string>();
|
|
1177
|
+
const properties = (schema as ObjectSchema).properties ?? {};
|
|
1178
|
+
for (const name in properties) {
|
|
1179
|
+
if (!properties[name]) {
|
|
1180
|
+
continue;
|
|
1181
|
+
}
|
|
1182
|
+
getImportedModelName(properties[name]!, usage).forEach((it) =>
|
|
1183
|
+
ret.add(it)
|
|
1184
|
+
);
|
|
1185
|
+
}
|
|
1186
|
+
return [...ret];
|
|
1187
|
+
}
|
|
1188
|
+
return getPriorityName(schema, usage)
|
|
1189
|
+
? [getPriorityName(schema, usage)]
|
|
1190
|
+
: [];
|
|
1191
|
+
}
|
|
1063
1192
|
case "dictionary": {
|
|
1064
|
-
const
|
|
1065
|
-
|
|
1193
|
+
const ret = new Set<string>();
|
|
1194
|
+
[(schema as DictionarySchema).additionalProperties]
|
|
1195
|
+
.filter((i?: Schema) => !!i && i.type === "object")
|
|
1196
|
+
.forEach((i?: Schema) =>
|
|
1197
|
+
getImportedModelName(i!, usage).forEach((it) => ret.add(it))
|
|
1198
|
+
);
|
|
1199
|
+
|
|
1200
|
+
return [...ret];
|
|
1201
|
+
}
|
|
1202
|
+
case "union": {
|
|
1203
|
+
const ret = new Set<string>();
|
|
1204
|
+
((schema as Schema).enum ?? [])
|
|
1205
|
+
.filter((i?: Schema) => !!i && i.type === "object")
|
|
1206
|
+
.forEach((i?: Schema) =>
|
|
1207
|
+
getImportedModelName(i!, usage).forEach((it) => ret.add(it))
|
|
1208
|
+
);
|
|
1209
|
+
|
|
1210
|
+
return [...ret];
|
|
1066
1211
|
}
|
|
1067
|
-
case "union":
|
|
1068
|
-
return (schema as any).enum
|
|
1069
|
-
.filter((i: Schema) => i.type === "object")
|
|
1070
|
-
.map((i: Schema) => getPriorityName(i) ?? "");
|
|
1071
1212
|
default:
|
|
1072
|
-
return;
|
|
1213
|
+
return [];
|
|
1073
1214
|
}
|
|
1074
1215
|
}
|
|
1075
1216
|
|
|
@@ -1080,9 +1221,7 @@ function getPriorityName(schema: Schema, usage?: SchemaContext[]): string {
|
|
|
1080
1221
|
? schema.typeName ?? schema.name
|
|
1081
1222
|
: schema.outputTypeName ?? schema.typeName ?? schema.name;
|
|
1082
1223
|
}
|
|
1083
|
-
|
|
1084
|
-
return schema.outputValueTypeName ?? schema.valueTypeName ?? undefined;
|
|
1085
|
-
}
|
|
1224
|
+
|
|
1086
1225
|
function getEnumStringDescription(type: any) {
|
|
1087
1226
|
if (type.name === "string" && type.enum && type.enum.length > 0) {
|
|
1088
1227
|
return `Possible values: ${type.enum.join(", ")}`;
|
|
@@ -1271,3 +1410,50 @@ export function isAzureCoreErrorType(t?: Type): boolean {
|
|
|
1271
1410
|
}
|
|
1272
1411
|
return namespaces.length == 0;
|
|
1273
1412
|
}
|
|
1413
|
+
|
|
1414
|
+
// Check if the schema is an anonymous object
|
|
1415
|
+
export function isAnonymousObjectSchema(schema: Schema) {
|
|
1416
|
+
return schema.name === "" && schema.type === "object";
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
// Check if the type is an anonymous model
|
|
1420
|
+
export function isAnonymousModelType(type: Type) {
|
|
1421
|
+
if (type.kind === "Model") {
|
|
1422
|
+
return type.name === "";
|
|
1423
|
+
}
|
|
1424
|
+
return false;
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
/**
|
|
1428
|
+
* Get the inline signiture of the model
|
|
1429
|
+
* @param schema object schema detail
|
|
1430
|
+
* @param options other optional parameters
|
|
1431
|
+
* @returns
|
|
1432
|
+
*/
|
|
1433
|
+
export function getModelInlineSigniture(
|
|
1434
|
+
schema: ObjectSchema,
|
|
1435
|
+
options: { importedModels?: Set<string>; usage?: SchemaContext[] } = {}
|
|
1436
|
+
) {
|
|
1437
|
+
let schemaSigiture = `{`;
|
|
1438
|
+
for (const propName in schema.properties) {
|
|
1439
|
+
const propType = schema.properties[propName]!;
|
|
1440
|
+
const propTypeName = getTypeName(propType, options.usage);
|
|
1441
|
+
if (!propType || !propTypeName) {
|
|
1442
|
+
continue;
|
|
1443
|
+
}
|
|
1444
|
+
if (options.importedModels) {
|
|
1445
|
+
const importNames = getImportedModelName(propType);
|
|
1446
|
+
if (importNames) {
|
|
1447
|
+
importNames!.forEach(
|
|
1448
|
+
options.importedModels.add,
|
|
1449
|
+
options.importedModels
|
|
1450
|
+
);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
const isOptional = propType.required ? "" : "?";
|
|
1454
|
+
schemaSigiture += `${propName}${isOptional}: ${propTypeName};`;
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
schemaSigiture += `}`;
|
|
1458
|
+
return schemaSigiture;
|
|
1459
|
+
}
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
ResponseMetadata,
|
|
8
8
|
ResponseTypes,
|
|
9
9
|
getLroLogicalResponseName,
|
|
10
|
+
getResponseTypeName,
|
|
10
11
|
normalizeName
|
|
11
12
|
} from "@azure-tools/rlc-common";
|
|
12
13
|
import {
|
|
@@ -23,7 +24,7 @@ import {
|
|
|
23
24
|
HttpOperation,
|
|
24
25
|
HttpOperationParameter,
|
|
25
26
|
HttpOperationResponse,
|
|
26
|
-
|
|
27
|
+
HttpStatusCodesEntry,
|
|
27
28
|
getHttpOperation
|
|
28
29
|
} from "@typespec/http";
|
|
29
30
|
import {
|
|
@@ -44,14 +45,85 @@ import {
|
|
|
44
45
|
import { isByteOrByteUnion } from "./modelUtils.js";
|
|
45
46
|
import { SdkContext } from "./interfaces.js";
|
|
46
47
|
|
|
48
|
+
// Sorts the responses by status code
|
|
49
|
+
export function sortedOperationResponses(responses: HttpOperationResponse[]) {
|
|
50
|
+
return responses.sort((a, b) => {
|
|
51
|
+
if (a.statusCodes === "*") {
|
|
52
|
+
return 1;
|
|
53
|
+
}
|
|
54
|
+
if (b.statusCodes === "*") {
|
|
55
|
+
return -1;
|
|
56
|
+
}
|
|
57
|
+
const aStatus =
|
|
58
|
+
typeof a.statusCodes === "number" ? a.statusCodes : a.statusCodes.start;
|
|
59
|
+
const bStatus =
|
|
60
|
+
typeof b.statusCodes === "number" ? b.statusCodes : b.statusCodes.start;
|
|
61
|
+
return aStatus - bStatus;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* This function computes all the response types error and success
|
|
67
|
+
* an operation can end up returning.
|
|
68
|
+
*/
|
|
69
|
+
export function getOperationResponseTypes(
|
|
70
|
+
dpgContext: SdkContext,
|
|
71
|
+
operation: HttpOperation
|
|
72
|
+
): ResponseTypes {
|
|
73
|
+
const returnTypes: ResponseTypes = {
|
|
74
|
+
error: [],
|
|
75
|
+
success: []
|
|
76
|
+
};
|
|
77
|
+
function getResponseType(responses: HttpOperationResponse[]) {
|
|
78
|
+
return responses.map((r) => {
|
|
79
|
+
const statusCode = getOperationStatuscode(r);
|
|
80
|
+
const responseName = getResponseTypeName(
|
|
81
|
+
getOperationGroupName(dpgContext, operation),
|
|
82
|
+
getOperationName(dpgContext.program, operation.operation),
|
|
83
|
+
statusCode
|
|
84
|
+
);
|
|
85
|
+
return responseName;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (operation.responses && operation.responses.length) {
|
|
89
|
+
returnTypes.error = getResponseType(
|
|
90
|
+
operation.responses.filter((r) => isDefaultStatusCode(r.statusCodes))
|
|
91
|
+
);
|
|
92
|
+
returnTypes.success = getResponseType(
|
|
93
|
+
operation.responses.filter((r) => isDefinedStatusCode(r.statusCodes))
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return returnTypes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Extracts all success or defined status codes for a give operation
|
|
101
|
+
*/
|
|
102
|
+
export function getOperationSuccessStatus(operation: HttpOperation): string[] {
|
|
103
|
+
const responses = operation.responses ?? [];
|
|
104
|
+
const status: string[] = [];
|
|
105
|
+
|
|
106
|
+
for (const response of responses) {
|
|
107
|
+
if (isDefinedStatusCode(response.statusCodes)) {
|
|
108
|
+
status.push(getOperationStatuscode(response));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return status;
|
|
113
|
+
}
|
|
114
|
+
|
|
47
115
|
export function getOperationStatuscode(
|
|
48
116
|
response: HttpOperationResponse
|
|
49
117
|
): string {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
118
|
+
const statusCodes = response.statusCodes;
|
|
119
|
+
if (statusCodes === "*") {
|
|
120
|
+
return "default";
|
|
121
|
+
} else if (typeof statusCodes === "number") {
|
|
122
|
+
return String(statusCodes);
|
|
123
|
+
} else {
|
|
124
|
+
// FIXME - this is a hack to get the first status code
|
|
125
|
+
// https://github.com/Azure/autorest.typescript/issues/2063
|
|
126
|
+
return String(statusCodes.start);
|
|
55
127
|
}
|
|
56
128
|
}
|
|
57
129
|
|
|
@@ -105,12 +177,12 @@ export function getOperationName(program: Program, operation: Operation) {
|
|
|
105
177
|
);
|
|
106
178
|
}
|
|
107
179
|
|
|
108
|
-
export function isDefaultStatusCode(
|
|
109
|
-
return
|
|
180
|
+
export function isDefaultStatusCode(statusCodes: HttpStatusCodesEntry) {
|
|
181
|
+
return statusCodes === "*";
|
|
110
182
|
}
|
|
111
183
|
|
|
112
|
-
export function isDefinedStatusCode(
|
|
113
|
-
return
|
|
184
|
+
export function isDefinedStatusCode(statusCodes: HttpStatusCodesEntry) {
|
|
185
|
+
return statusCodes !== "*";
|
|
114
186
|
}
|
|
115
187
|
|
|
116
188
|
export function isBinaryPayload(
|