Skip to content

Commit c54405f

Browse files
committed
support for Enums or collection of enum values as root of the payload
1 parent cd45905 commit c54405f

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

packages/abstractions/src/apiClientProxifier.ts

+21
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ function send(
150150
metadata.responseBodyFactory as ParsableFactory<Parsable>,
151151
metadata.errorMappings,
152152
);
153+
case "sendEnum":
154+
if (!metadata.enumObject) {
155+
throw new Error("couldn't find response body factory");
156+
}
157+
return requestAdapter.sendEnum(
158+
requestInfo,
159+
metadata.enumObject,
160+
metadata.errorMappings,
161+
);
162+
case "sendCollectionOfEnum":
163+
if (!metadata.enumObject) {
164+
throw new Error("couldn't find response body factory");
165+
}
166+
return requestAdapter.sendCollectionOfEnum(
167+
requestInfo,
168+
metadata.enumObject,
169+
metadata.errorMappings,
170+
);
153171
case "sendCollectionOfPrimitive":
154172
if (!metadata.responseBodyFactory) {
155173
throw new Error("couldn't find response body factory");
@@ -416,6 +434,7 @@ export interface RequestMetadata {
416434
requestInformationContentSetMethod?: keyof RequestInformationSetContent;
417435
queryParametersMapper?: Record<string, string>;
418436
uriTemplate: string;
437+
enumObject?: EnumObject;
419438
}
420439
export interface RequestsMetadata {
421440
delete?: RequestMetadata;
@@ -435,6 +454,8 @@ export interface NavigationMetadata {
435454
pathParametersMappings?: string[];
436455
}
437456

457+
type EnumObject<T extends Record<string, unknown> = Record<string, unknown>> = T;
458+
438459
export type KeysToExcludeForNavigationMetadata =
439460
| KeysOfRequestsMetadata
440461
| "toDeleteRequestInformation"

packages/abstractions/src/requestAdapter.ts

+28
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ export interface RequestAdapter {
8484
requestInfo: RequestInformation,
8585
errorMappings: ErrorMappings | undefined,
8686
): Promise<void>;
87+
/**
88+
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized enum response model.
89+
*
90+
* @template EnumObject - The type of the enum object. Must extend Record<string, unknown>.
91+
* @param {RequestInformation} requestInfo - The request info to execute.
92+
* @param {EnumObject} enumObject - The Enum object expected in the response.
93+
* @param {ErrorMappings | undefined} errorMappings - the error factories mapping to use in case of a failed request.
94+
* @returns {Promise<EnumObject[keyof EnumObject] | undefined>} - A promise that resolves to the response of the request, or undefined if an error occurred.
95+
*/
96+
sendEnum<EnumObject extends Record<string, unknown>>(
97+
requestInfo: RequestInformation,
98+
enumObject: EnumObject,
99+
errorMappings: ErrorMappings | undefined,
100+
): Promise<EnumObject[keyof EnumObject] | undefined>;
101+
/**
102+
* Executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection.
103+
*
104+
* @template EnumObject - The type of the enum objects. Must extend Record<string, unknown>.
105+
* @param {RequestInformation} requestInfo - The request info to execute.
106+
* @param {EnumObject} enumObject - The Enum object expected in the response.
107+
* @param {ErrorMappings | undefined} errorMappings - the error factories mapping to use in case of a failed request.
108+
* @returns {Promise<EnumObject[keyof EnumObject][] | undefined>} - with the deserialized response model collection.
109+
*/
110+
sendCollectionOfEnum<EnumObject extends Record<string, unknown>>(
111+
requestInfo: RequestInformation,
112+
enumObject: EnumObject,
113+
errorMappings: ErrorMappings | undefined,
114+
): Promise<EnumObject[keyof EnumObject][] | undefined>;
87115
/**
88116
* Enables the backing store proxies for the SerializationWriters and ParseNodes in use.
89117
* @param backingStoreFactory the backing store factory to use.

packages/http/fetch/src/fetchRequestAdapter.ts

+68
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,74 @@ export class FetchRequestAdapter implements RequestAdapter {
281281
}
282282
});
283283
};
284+
public sendEnum = <EnumObject extends Record<string, unknown>>(requestInfo: RequestInformation, enumObject: EnumObject, errorMappings: ErrorMappings | undefined): Promise<EnumObject[keyof EnumObject] | undefined> => {
285+
if (!requestInfo) {
286+
throw new Error("requestInfo cannot be null");
287+
}
288+
return this.startTracingSpan(requestInfo, "sendEnum", async (span) => {
289+
try {
290+
const response = await this.getHttpResponseMessage(requestInfo, span);
291+
const responseHandler = this.getResponseHandler(requestInfo);
292+
if (responseHandler) {
293+
span.addEvent(FetchRequestAdapter.eventResponseHandlerInvokedKey);
294+
return await responseHandler.handleResponse(response, errorMappings);
295+
} else {
296+
try {
297+
await this.throwIfFailedResponse(response, errorMappings, span);
298+
if (this.shouldReturnUndefined(response)) return undefined;
299+
const rootNode = await this.getRootParseNode(response);
300+
return trace.getTracer(this.observabilityOptions.getTracerInstrumentationName()).startActiveSpan("getEnumValue", (deserializeSpan) => {
301+
try {
302+
span.setAttribute(FetchRequestAdapter.responseTypeAttributeKey, "enum");
303+
const result = rootNode.getEnumValue(enumObject);
304+
return result as unknown as EnumObject[keyof EnumObject];
305+
} finally {
306+
deserializeSpan.end();
307+
}
308+
});
309+
} finally {
310+
await this.purgeResponseBody(response);
311+
}
312+
}
313+
} finally {
314+
span.end();
315+
}
316+
}) as Promise<EnumObject[keyof EnumObject]>;
317+
}
318+
public sendCollectionOfEnum = <EnumObject extends Record<string, unknown>>(requestInfo: RequestInformation, enumObject: EnumObject, errorMappings: ErrorMappings | undefined): Promise<EnumObject[keyof EnumObject][] | undefined> => {
319+
if (!requestInfo) {
320+
throw new Error("requestInfo cannot be null");
321+
}
322+
return this.startTracingSpan(requestInfo, "sendCollectionOfEnum", async (span) => {
323+
try {
324+
const response = await this.getHttpResponseMessage(requestInfo, span);
325+
const responseHandler = this.getResponseHandler(requestInfo);
326+
if (responseHandler) {
327+
span.addEvent(FetchRequestAdapter.eventResponseHandlerInvokedKey);
328+
return await responseHandler.handleResponse(response, errorMappings);
329+
} else {
330+
try {
331+
await this.throwIfFailedResponse(response, errorMappings, span);
332+
if (this.shouldReturnUndefined(response)) return undefined;
333+
const rootNode = await this.getRootParseNode(response);
334+
return trace.getTracer(this.observabilityOptions.getTracerInstrumentationName()).startActiveSpan("getCollectionOfEnumValues", (deserializeSpan) => {
335+
try {
336+
const result = rootNode.getCollectionOfEnumValues(enumObject);
337+
span.setAttribute(FetchRequestAdapter.responseTypeAttributeKey, "enum[]");
338+
return result as unknown as EnumObject[keyof EnumObject][];
339+
} finally {
340+
deserializeSpan.end();
341+
}
342+
});
343+
} finally {
344+
await this.purgeResponseBody(response);
345+
}
346+
}
347+
} finally {
348+
span.end();
349+
}
350+
});
351+
}
284352
public enableBackingStore = (backingStoreFactory?: BackingStoreFactory | undefined): void => {
285353
this.parseNodeFactory = enableBackingStoreForParseNodeFactory(this.parseNodeFactory);
286354
this.serializationWriterFactory = enableBackingStoreForSerializationWriterFactory(this.serializationWriterFactory);

0 commit comments

Comments
 (0)