Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/adapter/BaseGraphRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class BaseGraphRequestAdapter extends DefaultRequestAdapter {
graphServiceTargetVersion: string,
graphServiceLibraryClientVersion: string,
authenticationProvider: AuthenticationProvider,
parseNodeFactory: ParseNodeFactory = ParseNodeFactoryRegistry.defaultInstance,
serializationWriterFactory: SerializationWriterFactory = SerializationWriterFactoryRegistry.defaultInstance,
parseNodeFactory: ParseNodeFactory = new ParseNodeFactoryRegistry(),
serializationWriterFactory: SerializationWriterFactory = new SerializationWriterFactoryRegistry(),
httpClient: HttpClient = createGraphClientFactory({
graphServiceTargetVersion,
graphServiceLibraryClientVersion,
Expand Down
11 changes: 8 additions & 3 deletions src/content/BatchResponseContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

import { BatchResponseBody, BatchResponse } from "./BatchRequestStep.js";
import { Parsable, deserializeFromJson, ParsableFactory } from "@microsoft/kiota-abstractions";
import { Parsable, ParsableFactory, ParseNodeFactoryRegistry } from "@microsoft/kiota-abstractions";

/**
* @class
Expand Down Expand Up @@ -61,13 +61,18 @@ export class BatchResponseContent {
* Retrieves a parsable response by request ID.
* @template T - The type of the parsable response.
* @param {string} requestId - The request ID value.
* @param parseNodeFactoryRegistry - The registry to create parse nodes.
* @param {ParsableFactory<T>} factory - The factory to create the parsable response.
* @returns {T | undefined} The parsable response object instance for the particular request, or undefined if not found.
*/
public getParsableResponseById<T extends Parsable>(requestId: string, factory: ParsableFactory<T>): T | undefined {
public getParsableResponseById<T extends Parsable>(
requestId: string,
parseNodeFactoryRegistry: ParseNodeFactoryRegistry,
factory: ParsableFactory<T>,
): T | undefined {
const res = this.responses.get(requestId);
if (res?.body) {
const result = deserializeFromJson(res.body, factory);
const result = parseNodeFactoryRegistry.deserializeFromJson(res.body, factory);
return result as T;
}
return undefined;
Expand Down
14 changes: 11 additions & 3 deletions src/content/BatchResponseContentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@

import { BatchResponseContent } from "./BatchResponseContent.js";
import { BatchResponse } from "./BatchRequestStep.js";
import { deserializeFromJson, Parsable, ParsableFactory } from "@microsoft/kiota-abstractions";
import { Parsable, ParsableFactory, ParseNodeFactoryRegistry } from "@microsoft/kiota-abstractions";

/**
* Represents a collection of BatchResponseContent.
*/
export class BatchResponseContentCollection {
/**
* An array of BatchResponseContent.
*/
public readonly batchResponseContent: BatchResponseContent[] = [];

/**
Expand Down Expand Up @@ -54,13 +57,18 @@ export class BatchResponseContentCollection {
/**
* Gets the parsable response by the specified request ID.
* @param requestId - The ID of the request.
* @param parseNodeFactoryRegistry - The registry to create parse nodes.
* @param factory - The factory to create the Parsable instance.
* @returns The parsable response, or undefined if not found.
*/
public getParsableResponseById<T extends Parsable>(requestId: string, factory: ParsableFactory<T>): T | undefined {
public getParsableResponseById<T extends Parsable>(
requestId: string,
parseNodeFactoryRegistry: ParseNodeFactoryRegistry,
factory: ParsableFactory<T>,
): T | undefined {
const res = this.getResponseById(requestId);
if (res?.body) {
const result = deserializeFromJson(res.body, factory);
const result = parseNodeFactoryRegistry.deserializeFromJson(res.body, factory);
return result as T;
}
return undefined;
Expand Down
16 changes: 12 additions & 4 deletions test/content/BatchRequestContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
ParseNode,
Parsable,
Headers,
registerDefaultDeserializer,
ParseNodeFactoryRegistry,
} from "@microsoft/kiota-abstractions";
import { JsonParseNodeFactory, JsonSerializationWriterFactory } from "@microsoft/kiota-serialization-json";
import { JsonParseNodeFactory } from "@microsoft/kiota-serialization-json";
// @ts-ignore
import { createGraphErrorFromDiscriminatorValue } from "../tasks/PageIterator";
import { createCipheriv } from "node:crypto";
Expand Down Expand Up @@ -154,14 +154,22 @@ describe("BatchRequestContent tests", () => {
assert.equal(response?.status, 200);
});
it("Can parse a response a response by Id", () => {
registerDefaultDeserializer(JsonParseNodeFactory);
if (!(adapter.getParseNodeFactory() instanceof ParseNodeFactoryRegistry)) {
throw new Error("Invalid parse node factory");
}
const parseNodeFactoryRegistry = adapter.getParseNodeFactory() as ParseNodeFactoryRegistry;
parseNodeFactoryRegistry.registerDefaultDeserializer(JsonParseNodeFactory, adapter.getBackingStoreFactory());

const sampleArrayBuffer = new TextEncoder().encode(JSON.stringify({ value: [1, 2, 3], id: "1", name: "test" }));
const requestContent = new BatchResponseContent({
responses: [{ id: "1", status: 200, headers: {}, body: sampleArrayBuffer }],
});

const response = requestContent.getParsableResponseById<SampleResponse>("1", createSampleFromDiscriminatorValue);
const response = requestContent.getParsableResponseById<SampleResponse>(
"1",
parseNodeFactoryRegistry as ParseNodeFactoryRegistry,
createSampleFromDiscriminatorValue,
);
assert.isNotNull(response);
assert.equal(response?.value.length, 3);
assert.equal(response?.id, "1");
Expand Down
16 changes: 15 additions & 1 deletion test/utils/DummyRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import {
type ErrorMappings,
Parsable,
type ParsableFactory,
ParseNodeFactory,
type PrimitiveTypesForDeserialization,
type PrimitiveTypesForDeserializationType,
RequestAdapter,
type RequestInformation,
SerializationWriterFactory,
SerializationWriterFactoryRegistry,
InMemoryBackingStoreFactory,
ParseNodeFactoryRegistry,
} from "@microsoft/kiota-abstractions";

import { JsonSerializationWriterFactory } from "@microsoft/kiota-serialization-json";
Expand All @@ -35,7 +38,10 @@ export class DummyRequestAdapter implements RequestAdapter {
requests: RequestInformation[] = [];
serializationWriterFactory = new SerializationWriterFactoryRegistry();

constructor() {
constructor(
private parseNodeFactory: ParseNodeFactory = new ParseNodeFactoryRegistry(),
private backingStoreFactory = new InMemoryBackingStoreFactory(),
) {
const serializer = new JsonSerializationWriterFactory();
this.serializationWriterFactory.contentTypeAssociatedFactories.set(serializer.getValidContentType(), serializer);
}
Expand Down Expand Up @@ -128,4 +134,12 @@ export class DummyRequestAdapter implements RequestAdapter {
this.requests.push(requestInfo);
return Promise.resolve(this.response.shift());
}

getBackingStoreFactory(): BackingStoreFactory {
return this.backingStoreFactory;
}

getParseNodeFactory(): ParseNodeFactory {
return this.parseNodeFactory;
}
}