Skip to content

Commit bc5a246

Browse files
committed
move to registry
1 parent a1c4d05 commit bc5a246

11 files changed

+48
-47
lines changed

packages/abstractions/src/apiClientBuilder.ts

+1-22
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,8 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77
import { type ParseNodeFactory, ParseNodeFactoryRegistry, type SerializationWriterFactory, SerializationWriterFactoryRegistry } from "./serialization";
8-
import { BackingStoreFactory, BackingStoreParseNodeFactory, BackingStoreSerializationWriterProxyFactory } from "./store";
8+
import { BackingStoreParseNodeFactory, BackingStoreSerializationWriterProxyFactory } from "./store";
99

10-
/**
11-
* Registers the default serializer to the registry.
12-
* @param serializationWriterFactoryRegistry The serialization writer factory registry to register the default serializer to.
13-
* @param type the class of the factory to be registered.
14-
*/
15-
export function registerDefaultSerializer(serializationWriterFactoryRegistry: SerializationWriterFactoryRegistry, type: new () => SerializationWriterFactory): void {
16-
if (!type) throw new Error("Type is required");
17-
const serializer = new type();
18-
serializationWriterFactoryRegistry.contentTypeAssociatedFactories.set(serializer.getValidContentType(), serializer);
19-
}
20-
/**
21-
* Registers the default deserializer to the registry.
22-
* @param parseNodeFactoryRegistry The parse node factory registry to register the default deserializer to.
23-
* @param type the class of the factory to be registered.
24-
* @param backingStoreFactory The backing store factory to use.
25-
*/
26-
export function registerDefaultDeserializer(parseNodeFactoryRegistry: ParseNodeFactoryRegistry, type: new (backingStoreFactory: BackingStoreFactory) => ParseNodeFactory, backingStoreFactory: BackingStoreFactory): void {
27-
if (!type) throw new Error("Type is required");
28-
const deserializer = new type(backingStoreFactory);
29-
parseNodeFactoryRegistry.contentTypeAssociatedFactories.set(deserializer.getValidContentType(), deserializer);
30-
}
3110
/**
3211
* Enables the backing store on default serialization writers and the given serialization writer.
3312
* @param serializationWriterFactoryRegistry The serialization writer factory registry to enable the backing store on.

packages/abstractions/src/serialization/parseNodeFactoryRegistry.ts

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { ParseNode } from "./parseNode";
88
import type { ParseNodeFactory } from "./parseNodeFactory";
99
import { Parsable } from "./parsable";
1010
import type { ParsableFactory } from "./parsableFactory";
11+
import { BackingStoreFactory } from "../store";
1112

1213
/**
1314
* This factory holds a list of all the registered factories for the various types of nodes.
@@ -50,6 +51,17 @@ export class ParseNodeFactoryRegistry implements ParseNodeFactory {
5051
throw new Error(`Content type ${cleanedContentType} does not have a factory registered to be parsed`);
5152
}
5253

54+
/**
55+
* Registers the default deserializer to the registry.
56+
* @param type the class of the factory to be registered.
57+
* @param backingStoreFactory The backing store factory to use.
58+
*/
59+
public registerDefaultDeserializer(type: new (backingStoreFactory: BackingStoreFactory) => ParseNodeFactory, backingStoreFactory: BackingStoreFactory): void {
60+
if (!type) throw new Error("Type is required");
61+
const deserializer = new type(backingStoreFactory);
62+
this.contentTypeAssociatedFactories.set(deserializer.getValidContentType(), deserializer);
63+
}
64+
5365
/**
5466
* Deserializes a buffer into a parsable object
5567
* @param bufferOrString the value to serialize

packages/abstractions/src/serialization/serializationWriterFactoryRegistry.ts

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export class SerializationWriterFactoryRegistry implements SerializationWriterFa
3838
throw new Error(`Content type ${cleanedContentType} does not have a factory registered to be serialized`);
3939
}
4040

41+
/**
42+
* Registers the default serializer to the registry.
43+
* @param type the class of the factory to be registered.
44+
*/
45+
public registerDefaultSerializer(type: new () => SerializationWriterFactory): void {
46+
if (!type) throw new Error("Type is required");
47+
const serializer = new type();
48+
this.contentTypeAssociatedFactories.set(serializer.getValidContentType(), serializer);
49+
}
50+
4151
/**
4252
* Serializes a parsable object into a buffer
4353
* @param value the value to serialize

packages/bundle/src/defaultRequestAdapter.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { AuthenticationProvider, ParseNodeFactory, ParseNodeFactoryRegistry, registerDefaultDeserializer, registerDefaultSerializer, SerializationWriterFactory, SerializationWriterFactoryRegistry } from "@microsoft/kiota-abstractions";
8+
import { AuthenticationProvider, ParseNodeFactory, ParseNodeFactoryRegistry, SerializationWriterFactory, SerializationWriterFactoryRegistry } from "@microsoft/kiota-abstractions";
99
import { FormParseNodeFactory, FormSerializationWriterFactory } from "@microsoft/kiota-serialization-form";
1010
import { JsonParseNodeFactory, JsonSerializationWriterFactory } from "@microsoft/kiota-serialization-json";
1111
import { MultipartSerializationWriterFactory } from "@microsoft/kiota-serialization-multipart";
@@ -45,12 +45,12 @@ export class DefaultRequestAdapter extends FetchRequestAdapter {
4545
}
4646

4747
const backingStoreFactory = super.getBackingStoreFactory();
48-
registerDefaultSerializer(serializationWriterFactoryRegistry, JsonSerializationWriterFactory);
49-
registerDefaultSerializer(serializationWriterFactoryRegistry, TextSerializationWriterFactory);
50-
registerDefaultSerializer(serializationWriterFactoryRegistry, FormSerializationWriterFactory);
51-
registerDefaultSerializer(serializationWriterFactoryRegistry, MultipartSerializationWriterFactory);
52-
registerDefaultDeserializer(parseNodeFactoryRegistry, JsonParseNodeFactory, backingStoreFactory);
53-
registerDefaultDeserializer(parseNodeFactoryRegistry, TextParseNodeFactory, backingStoreFactory);
54-
registerDefaultDeserializer(parseNodeFactoryRegistry, FormParseNodeFactory, backingStoreFactory);
48+
serializationWriterFactoryRegistry.registerDefaultSerializer(JsonSerializationWriterFactory);
49+
serializationWriterFactoryRegistry.registerDefaultSerializer(TextSerializationWriterFactory);
50+
serializationWriterFactoryRegistry.registerDefaultSerializer(FormSerializationWriterFactory);
51+
serializationWriterFactoryRegistry.registerDefaultSerializer(MultipartSerializationWriterFactory);
52+
parseNodeFactoryRegistry.registerDefaultDeserializer(JsonParseNodeFactory, backingStoreFactory);
53+
parseNodeFactoryRegistry.registerDefaultDeserializer(TextParseNodeFactory, backingStoreFactory);
54+
parseNodeFactoryRegistry.registerDefaultDeserializer(FormParseNodeFactory, backingStoreFactory);
5555
}
5656
}

packages/serialization/form/src/formParseNode.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ export class FormParseNode implements ParseNode {
1212
/**
1313
* Creates a new instance of FormParseNode
1414
* @param _rawString the raw string to parse
15-
* @param backingStoreFactory the backing store factory to use
15+
* @param backingStoreFactory the factory to create backing stores
1616
*/
1717
constructor(
1818
private readonly _rawString: string,
19-
private readonly backingStoreFactory: BackingStoreFactory,
19+
private readonly backingStoreFactory?: BackingStoreFactory,
2020
) {
2121
if (!_rawString) {
2222
throw new Error("rawString cannot be undefined");
@@ -94,7 +94,7 @@ export class FormParseNode implements ParseNode {
9494
public getObjectValue = <T extends Parsable>(parsableFactory: ParsableFactory<T>): T => {
9595
const temp: T = {} as T;
9696
const enableBackingStore = isBackingStoreEnabled(parsableFactory(this)(temp));
97-
const value: T = enableBackingStore ? new Proxy(temp, createBackedModelProxyHandler<T>(this.backingStoreFactory)) : temp;
97+
const value: T = enableBackingStore && this.backingStoreFactory ? new Proxy(temp, createBackedModelProxyHandler<T>(this.backingStoreFactory)) : temp;
9898
if (this.onBeforeAssignFieldValues) {
9999
this.onBeforeAssignFieldValues(value);
100100
}

packages/serialization/form/src/formParseNodeFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class FormParseNodeFactory implements ParseNodeFactory {
1414
* Creates an instance of JsonParseNode.
1515
* @param backingStoreFactory - The factory to create backing stores.
1616
*/
17-
constructor(private readonly backingStoreFactory: BackingStoreFactory) {}
17+
constructor(private readonly backingStoreFactory?: BackingStoreFactory) {}
1818

1919
public getValidContentType(): string {
2020
return "application/x-www-form-urlencoded";

packages/serialization/json/src/jsonParseNode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class JsonParseNode implements ParseNode {
1515
*/
1616
constructor(
1717
private readonly _jsonNode: unknown,
18-
private readonly backingStoreFactory: BackingStoreFactory,
18+
private readonly backingStoreFactory?: BackingStoreFactory,
1919
) {}
2020
public onBeforeAssignFieldValues: ((value: Parsable) => void) | undefined;
2121
public onAfterAssignFieldValues: ((value: Parsable) => void) | undefined;
@@ -98,7 +98,7 @@ export class JsonParseNode implements ParseNode {
9898
return value;
9999
}
100100
const enableBackingStore = isBackingStoreEnabled(parsableFactory(this)(temp));
101-
const objectValue: T = enableBackingStore ? new Proxy(temp, createBackedModelProxyHandler<T>(this.backingStoreFactory)) : temp;
101+
const objectValue: T = enableBackingStore && this.backingStoreFactory ? new Proxy(temp, createBackedModelProxyHandler<T>(this.backingStoreFactory)) : temp;
102102
if (this.onBeforeAssignFieldValues) {
103103
this.onBeforeAssignFieldValues(objectValue);
104104
}

packages/serialization/json/src/jsonParseNodeFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class JsonParseNodeFactory implements ParseNodeFactory {
1414
* Creates an instance of JsonParseNode.
1515
* @param backingStoreFactory - The factory to create backing stores.
1616
*/
17-
constructor(private readonly backingStoreFactory: BackingStoreFactory) {}
17+
constructor(private readonly backingStoreFactory?: BackingStoreFactory) {}
1818

1919
public getValidContentType(): string {
2020
return "application/json";

packages/serialization/text/src/browser/textParseNodeFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class TextParseNodeFactory implements ParseNodeFactory {
1414
* Creates an instance of TextParseNode.
1515
* @param backingStoreFactory - The factory to create backing stores.
1616
*/
17-
constructor(private readonly backingStoreFactory: BackingStoreFactory) {}
17+
constructor(private readonly backingStoreFactory?: BackingStoreFactory) {}
1818
public getValidContentType(): string {
1919
return "text/plain";
2020
}

packages/serialization/text/src/textParseNodeFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class TextParseNodeFactory implements ParseNodeFactory {
1414
* Creates an instance of TextParseNode.
1515
* @param backingStoreFactory - The factory to create backing stores.
1616
*/
17-
constructor(private readonly backingStoreFactory: BackingStoreFactory) {}
17+
constructor(private readonly backingStoreFactory?: BackingStoreFactory) {}
1818
public getValidContentType(): string {
1919
return "text/plain";
2020
}

packages/test/generatedCode/apiClient.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @ts-ignore
55
import { type UsersRequestBuilder, UsersRequestBuilderNavigationMetadata } from './users/index.js';
66
// @ts-ignore
7-
import { apiClientProxifier,registerDefaultDeserializer, registerDefaultSerializer, type BaseRequestBuilder, type KeysToExcludeForNavigationMetadata, type NavigationMetadata, type RequestAdapter, ParseNodeFactoryRegistry, SerializationWriterFactoryRegistry } from '@microsoft/kiota-abstractions';
7+
import { apiClientProxifier,type BaseRequestBuilder, type KeysToExcludeForNavigationMetadata, type NavigationMetadata, type RequestAdapter, ParseNodeFactoryRegistry, SerializationWriterFactoryRegistry } from '@microsoft/kiota-abstractions';
88
// @ts-ignore
99
import { FormParseNodeFactory, FormSerializationWriterFactory } from '@microsoft/kiota-serialization-form';
1010
// @ts-ignore
@@ -47,13 +47,13 @@ export function createApiClient(requestAdapter: RequestAdapter) {
4747
}
4848

4949
const backingStoreFactory = requestAdapter.getBackingStoreFactory();
50-
registerDefaultSerializer(serializationWriterFactory, JsonSerializationWriterFactory);
51-
registerDefaultSerializer(serializationWriterFactory, TextSerializationWriterFactory);
52-
registerDefaultSerializer(serializationWriterFactory, FormSerializationWriterFactory);
53-
registerDefaultSerializer(serializationWriterFactory, MultipartSerializationWriterFactory);
54-
registerDefaultDeserializer(parseNodeFactoryRegistry, JsonParseNodeFactory, backingStoreFactory);
55-
registerDefaultDeserializer(parseNodeFactoryRegistry, TextParseNodeFactory, backingStoreFactory);
56-
registerDefaultDeserializer(parseNodeFactoryRegistry, FormParseNodeFactory, backingStoreFactory);
50+
serializationWriterFactory.registerDefaultSerializer( JsonSerializationWriterFactory);
51+
serializationWriterFactory.registerDefaultSerializer( TextSerializationWriterFactory);
52+
serializationWriterFactory.registerDefaultSerializer( FormSerializationWriterFactory);
53+
serializationWriterFactory.registerDefaultSerializer( MultipartSerializationWriterFactory);
54+
parseNodeFactoryRegistry.registerDefaultDeserializer( JsonParseNodeFactory, backingStoreFactory);
55+
parseNodeFactoryRegistry.registerDefaultDeserializer( TextParseNodeFactory, backingStoreFactory);
56+
parseNodeFactoryRegistry.registerDefaultDeserializer( FormParseNodeFactory, backingStoreFactory);
5757
if (requestAdapter.baseUrl === undefined || requestAdapter.baseUrl === "") {
5858
requestAdapter.baseUrl = "https://graph.microsoft.com/v1.0";
5959
}

0 commit comments

Comments
 (0)