@microsoft/kiota-http-fetchlibrary
Advanced tools
Comparing version 1.0.0-preview.1 to 1.0.0-preview.2
@@ -21,8 +21,11 @@ import { AuthenticationProvider, BackingStoreFactory, Parsable, ParsableFactory, ParseNodeFactory, RequestAdapter, RequestInformation, ResponseHandler, SerializationWriterFactory } from "@microsoft/kiota-abstractions"; | ||
sendCollectionOfPrimitiveAsync: <ResponseType_1>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ResponseType_1[] | undefined>; | ||
sendCollectionAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType[]>; | ||
sendAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType>; | ||
sendPrimitiveAsync: <ResponseType_1>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date" | "ArrayBuffer", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ResponseType_1>; | ||
sendCollectionAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType[] | undefined>; | ||
sendAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType | undefined>; | ||
sendPrimitiveAsync: <ResponseType_1>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date" | "ArrayBuffer", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ResponseType_1 | undefined>; | ||
sendNoResponseContentAsync: (requestInfo: RequestInformation, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<void>; | ||
enableBackingStore: (backingStoreFactory?: BackingStoreFactory | undefined) => void; | ||
private getRootParseNode; | ||
private shouldReturnUndefined; | ||
/** purges the response body if it hasn't been read to release the connection to the server */ | ||
private purgeResponseBody; | ||
private throwFailedResponses; | ||
@@ -29,0 +32,0 @@ private getHttpResponseMessage; |
@@ -42,34 +42,41 @@ "use strict"; | ||
else { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
switch (responseType) { | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = yield this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
try { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
switch (responseType) { | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = yield this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
} | ||
} | ||
finally { | ||
yield this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -86,6 +93,13 @@ }); | ||
else { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
const rootNode = yield this.getRootParseNode(response); | ||
const result = rootNode.getCollectionOfObjectValues(type); | ||
return result; | ||
try { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
const rootNode = yield this.getRootParseNode(response); | ||
const result = rootNode.getCollectionOfObjectValues(type); | ||
return result; | ||
} | ||
finally { | ||
yield this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -102,6 +116,13 @@ }); | ||
else { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
const rootNode = yield this.getRootParseNode(response); | ||
const result = rootNode.getObjectValue(type); | ||
return result; | ||
try { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
const rootNode = yield this.getRootParseNode(response); | ||
const result = rootNode.getObjectValue(type); | ||
return result; | ||
} | ||
finally { | ||
yield this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -118,36 +139,43 @@ }); | ||
else { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
switch (responseType) { | ||
case "ArrayBuffer": | ||
return (yield response.arrayBuffer()); | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = yield this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getStringValue(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getNumberValue(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getBooleanValue(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getDateValue(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getDurationValue(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getDateOnlyValue(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getTimeOnlyValue(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
try { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
switch (responseType) { | ||
case "ArrayBuffer": | ||
return (yield response.arrayBuffer()); | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = yield this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getStringValue(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getNumberValue(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getBooleanValue(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getDateValue(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getDurationValue(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getDateOnlyValue(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getTimeOnlyValue(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
} | ||
} | ||
finally { | ||
yield this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -163,3 +191,8 @@ }); | ||
} | ||
yield this.throwFailedResponses(response, errorMappings); | ||
try { | ||
yield this.throwFailedResponses(response, errorMappings); | ||
} | ||
finally { | ||
yield this.purgeResponseBody(response); | ||
} | ||
}); | ||
@@ -182,2 +215,11 @@ this.enableBackingStore = (backingStoreFactory) => { | ||
}); | ||
this.shouldReturnUndefined = (response) => { | ||
return response.status === 204; | ||
}; | ||
/** purges the response body if it hasn't been read to release the connection to the server */ | ||
this.purgeResponseBody = (response) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (!response.bodyUsed && response.body) { | ||
yield response.arrayBuffer(); | ||
} | ||
}); | ||
this.throwFailedResponses = (response, errorMappings) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
@@ -184,0 +226,0 @@ var _a, _b; |
@@ -21,8 +21,11 @@ import { AuthenticationProvider, BackingStoreFactory, Parsable, ParsableFactory, ParseNodeFactory, RequestAdapter, RequestInformation, ResponseHandler, SerializationWriterFactory } from "@microsoft/kiota-abstractions"; | ||
sendCollectionOfPrimitiveAsync: <ResponseType_1>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ResponseType_1[] | undefined>; | ||
sendCollectionAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType[]>; | ||
sendAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType>; | ||
sendPrimitiveAsync: <ResponseType_1>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date" | "ArrayBuffer", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ResponseType_1>; | ||
sendCollectionAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType[] | undefined>; | ||
sendAsync: <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ModelType | undefined>; | ||
sendPrimitiveAsync: <ResponseType_1>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date" | "ArrayBuffer", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<ResponseType_1 | undefined>; | ||
sendNoResponseContentAsync: (requestInfo: RequestInformation, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined) => Promise<void>; | ||
enableBackingStore: (backingStoreFactory?: BackingStoreFactory | undefined) => void; | ||
private getRootParseNode; | ||
private shouldReturnUndefined; | ||
/** purges the response body if it hasn't been read to release the connection to the server */ | ||
private purgeResponseBody; | ||
private throwFailedResponses; | ||
@@ -29,0 +32,0 @@ private getHttpResponseMessage; |
@@ -38,34 +38,41 @@ import { ApiError, BackingStoreFactorySingleton, enableBackingStoreForParseNodeFactory, enableBackingStoreForSerializationWriterFactory, ParseNodeFactoryRegistry, SerializationWriterFactoryRegistry } from "@microsoft/kiota-abstractions"; | ||
else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
switch (responseType) { | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
switch (responseType) { | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
} | ||
} | ||
finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -82,6 +89,13 @@ }; | ||
else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getCollectionOfObjectValues(type); | ||
return result; | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getCollectionOfObjectValues(type); | ||
return result; | ||
} | ||
finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -98,6 +112,13 @@ }; | ||
else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getObjectValue(type); | ||
return result; | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getObjectValue(type); | ||
return result; | ||
} | ||
finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -114,36 +135,43 @@ }; | ||
else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
switch (responseType) { | ||
case "ArrayBuffer": | ||
return (await response.arrayBuffer()); | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getStringValue(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getNumberValue(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getBooleanValue(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getDateValue(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getDurationValue(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getDateOnlyValue(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getTimeOnlyValue(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) | ||
return undefined; | ||
switch (responseType) { | ||
case "ArrayBuffer": | ||
return (await response.arrayBuffer()); | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getStringValue(); | ||
} | ||
else if (responseType === "number") { | ||
return rootNode.getNumberValue(); | ||
} | ||
else if (responseType === "boolean") { | ||
return rootNode.getBooleanValue(); | ||
} | ||
else if (responseType === "Date") { | ||
return rootNode.getDateValue(); | ||
} | ||
else if (responseType === "Duration") { | ||
return rootNode.getDurationValue(); | ||
} | ||
else if (responseType === "DateOnly") { | ||
return rootNode.getDateOnlyValue(); | ||
} | ||
else if (responseType === "TimeOnly") { | ||
return rootNode.getTimeOnlyValue(); | ||
} | ||
else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
} | ||
} | ||
finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
} | ||
@@ -159,3 +187,8 @@ }; | ||
} | ||
await this.throwFailedResponses(response, errorMappings); | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
} | ||
finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
}; | ||
@@ -178,2 +211,11 @@ this.enableBackingStore = (backingStoreFactory) => { | ||
}; | ||
this.shouldReturnUndefined = (response) => { | ||
return response.status === 204; | ||
}; | ||
/** purges the response body if it hasn't been read to release the connection to the server */ | ||
this.purgeResponseBody = async (response) => { | ||
if (!response.bodyUsed && response.body) { | ||
await response.arrayBuffer(); | ||
} | ||
}; | ||
this.throwFailedResponses = async (response, errorMappings) => { | ||
@@ -180,0 +222,0 @@ var _a, _b; |
{ | ||
"name": "@microsoft/kiota-http-fetchlibrary", | ||
"version": "1.0.0-preview.1", | ||
"version": "1.0.0-preview.2", | ||
"description": "Kiota request adapter implementation with fetch", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@microsoft/kiota-abstractions": "^1.0.0-preview.1", | ||
"@microsoft/kiota-abstractions": "^1.0.0-preview.2", | ||
"node-fetch": "^2.6.5", | ||
@@ -50,0 +50,0 @@ "tslib": "^2.3.1" |
@@ -1,19 +0,11 @@ | ||
# To-do | ||
[![npm version badge](https://img.shields.io/npm/v/@microsoft/kiota-http-fetchlibrary?color=blue)](https://www.npmjs.com/package/@microsoft/kiota-http-fetchlibrary) | ||
![TypeScript](https://github.com/microsoft/kiota/actions/workflows/http-typescript-fetch.yml/badge.svg) | ||
The Kiota Http Fetch Library is an implementation using the Fetch API to make requests. | ||
- [ ] browserlist configuration for compat | ||
- [ ] eslint configuration for linting | ||
- [ ] unit tests (chai + mocha + chai as promised + some coverage reporter) | ||
- [ ] doc comments | ||
A [Kiota](https://github.com/microsoft/kiota) generated project will need a reference to an HTTP implementation to make calls to an API endpoint. | ||
## Using the Http fetch implementations | ||
Read more about Kiota [here](https://github.com/microsoft/kiota/blob/main/README.md). | ||
1. Add a `.npmrc` file with the following content | ||
## Using the Kiota Fetch library implementations | ||
```Config | ||
@microsoft:registry=https://npm.pkg.github.com/ | ||
``` | ||
1. `npm login --scope=@microsoft --registry=https://npm.pkg.github.com` (use a token with package:read, repo and SSO enabled for the Microsoft organization as the password) | ||
1. `npm i @microsoft/kiota-http-fetchlibrary -S`. | ||
1. `npm i @microsoft/kiota-http-fetchlibrary`. |
@@ -47,30 +47,35 @@ import { ApiError, AuthenticationProvider, BackingStoreFactory, BackingStoreFactorySingleton, DateOnly, Duration, enableBackingStoreForParseNodeFactory, enableBackingStoreForSerializationWriterFactory, Parsable, ParsableFactory, ParseNode,ParseNodeFactory, ParseNodeFactoryRegistry, RequestAdapter, RequestInformation, ResponseHandler, SerializationWriterFactory, SerializationWriterFactoryRegistry, TimeOnly } from "@microsoft/kiota-abstractions"; | ||
} else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
switch (responseType) { | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getCollectionOfPrimitiveValues<string>() as unknown as ResponseType[]; | ||
} else if (responseType === "number") { | ||
return rootNode.getCollectionOfPrimitiveValues<number>() as unknown as ResponseType[]; | ||
} else if (responseType === "boolean") { | ||
return rootNode.getCollectionOfPrimitiveValues<boolean>() as unknown as ResponseType[]; | ||
} else if (responseType === "Date") { | ||
return rootNode.getCollectionOfPrimitiveValues<Date>() as unknown as ResponseType[]; | ||
} else if (responseType === "Duration") { | ||
return rootNode.getCollectionOfPrimitiveValues<Duration>() as unknown as ResponseType[]; | ||
} else if (responseType === "DateOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues<DateOnly>() as unknown as ResponseType[]; | ||
} else if (responseType === "TimeOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues<TimeOnly>() as unknown as ResponseType[]; | ||
} else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) return undefined; | ||
switch (responseType) { | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getCollectionOfPrimitiveValues<string>() as unknown as ResponseType[]; | ||
} else if (responseType === "number") { | ||
return rootNode.getCollectionOfPrimitiveValues<number>() as unknown as ResponseType[]; | ||
} else if (responseType === "boolean") { | ||
return rootNode.getCollectionOfPrimitiveValues<boolean>() as unknown as ResponseType[]; | ||
} else if (responseType === "Date") { | ||
return rootNode.getCollectionOfPrimitiveValues<Date>() as unknown as ResponseType[]; | ||
} else if (responseType === "Duration") { | ||
return rootNode.getCollectionOfPrimitiveValues<Duration>() as unknown as ResponseType[]; | ||
} else if (responseType === "DateOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues<DateOnly>() as unknown as ResponseType[]; | ||
} else if (responseType === "TimeOnly") { | ||
return rootNode.getCollectionOfPrimitiveValues<TimeOnly>() as unknown as ResponseType[]; | ||
} else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
} | ||
} finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
} | ||
}; | ||
public sendCollectionAsync = async <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined): Promise<ModelType[]> => { | ||
public sendCollectionAsync = async <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined): Promise<ModelType[] | undefined> => { | ||
if (!requestInfo) { | ||
@@ -83,9 +88,14 @@ throw new Error("requestInfo cannot be null"); | ||
} else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getCollectionOfObjectValues(type); | ||
return result as unknown as ModelType[]; | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) return undefined; | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getCollectionOfObjectValues(type); | ||
return result as unknown as ModelType[]; | ||
} finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
} | ||
}; | ||
public sendAsync = async <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined): Promise<ModelType> => { | ||
public sendAsync = async <ModelType extends Parsable>(requestInfo: RequestInformation, type: ParsableFactory<ModelType>, responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined): Promise<ModelType | undefined> => { | ||
if (!requestInfo) { | ||
@@ -98,9 +108,14 @@ throw new Error("requestInfo cannot be null"); | ||
} else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getObjectValue(type); | ||
return result as unknown as ModelType; | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) return undefined; | ||
const rootNode = await this.getRootParseNode(response); | ||
const result = rootNode.getObjectValue(type); | ||
return result as unknown as ModelType; | ||
} finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
} | ||
}; | ||
public sendPrimitiveAsync = async <ResponseType>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date" | "ArrayBuffer", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined): Promise<ResponseType> => { | ||
public sendPrimitiveAsync = async <ResponseType>(requestInfo: RequestInformation, responseType: "string" | "number" | "boolean" | "Date" | "ArrayBuffer", responseHandler: ResponseHandler | undefined, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined): Promise<ResponseType | undefined> => { | ||
if (!requestInfo) { | ||
@@ -113,28 +128,33 @@ throw new Error("requestInfo cannot be null"); | ||
} else { | ||
await this.throwFailedResponses(response, errorMappings); | ||
switch (responseType) { | ||
case "ArrayBuffer": | ||
return (await response.arrayBuffer()) as unknown as ResponseType; | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getStringValue() as unknown as ResponseType; | ||
} else if (responseType === "number") { | ||
return rootNode.getNumberValue() as unknown as ResponseType; | ||
} else if (responseType === "boolean") { | ||
return rootNode.getBooleanValue() as unknown as ResponseType; | ||
} else if (responseType === "Date") { | ||
return rootNode.getDateValue() as unknown as ResponseType; | ||
} else if (responseType === "Duration") { | ||
return rootNode.getDurationValue() as unknown as ResponseType; | ||
} else if (responseType === "DateOnly") { | ||
return rootNode.getDateOnlyValue() as unknown as ResponseType; | ||
} else if (responseType === "TimeOnly") { | ||
return rootNode.getTimeOnlyValue() as unknown as ResponseType; | ||
} else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
if (this.shouldReturnUndefined(response)) return undefined; | ||
switch (responseType) { | ||
case "ArrayBuffer": | ||
return (await response.arrayBuffer()) as unknown as ResponseType; | ||
case "string": | ||
case "number": | ||
case "boolean": | ||
case "Date": | ||
const rootNode = await this.getRootParseNode(response); | ||
if (responseType === "string") { | ||
return rootNode.getStringValue() as unknown as ResponseType; | ||
} else if (responseType === "number") { | ||
return rootNode.getNumberValue() as unknown as ResponseType; | ||
} else if (responseType === "boolean") { | ||
return rootNode.getBooleanValue() as unknown as ResponseType; | ||
} else if (responseType === "Date") { | ||
return rootNode.getDateValue() as unknown as ResponseType; | ||
} else if (responseType === "Duration") { | ||
return rootNode.getDurationValue() as unknown as ResponseType; | ||
} else if (responseType === "DateOnly") { | ||
return rootNode.getDateOnlyValue() as unknown as ResponseType; | ||
} else if (responseType === "TimeOnly") { | ||
return rootNode.getTimeOnlyValue() as unknown as ResponseType; | ||
} else { | ||
throw new Error("unexpected type to deserialize"); | ||
} | ||
} | ||
} finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
@@ -151,3 +171,7 @@ } | ||
} | ||
await this.throwFailedResponses(response, errorMappings); | ||
try { | ||
await this.throwFailedResponses(response, errorMappings); | ||
} finally { | ||
await this.purgeResponseBody(response); | ||
} | ||
}; | ||
@@ -169,2 +193,11 @@ public enableBackingStore = (backingStoreFactory?: BackingStoreFactory | undefined): void => { | ||
}; | ||
private shouldReturnUndefined = (response: Response): boolean => { | ||
return response.status === 204; | ||
}; | ||
/** purges the response body if it hasn't been read to release the connection to the server */ | ||
private purgeResponseBody = async (response: Response): Promise<void> => { | ||
if (!response.bodyUsed && response.body) { | ||
await response.arrayBuffer(); | ||
} | ||
}; | ||
private throwFailedResponses = async (response: Response, errorMappings: Record<string, ParsableFactory<Parsable>> | undefined): Promise<void> => { | ||
@@ -171,0 +204,0 @@ if (response.ok) return; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
264529
4790
12