Skip to content

Commit 5ce4f29

Browse files
authored
Merge pull request #1084 from microsoft/andrueastman/untypedNodes
[TypeScript] Untyped Nodes
2 parents cd45905 + 47be78f commit 5ce4f29

21 files changed

+583
-20
lines changed

packages/abstractions/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/kiota-abstractions",
3-
"version": "1.0.0-preview.47",
3+
"version": "1.0.0-preview.48",
44
"description": "Core abstractions for kiota generated libraries in TypeScript and JavaScript",
55
"main": "dist/cjs/src/index.js",
66
"module": "dist/es/src/index.js",

packages/abstractions/src/serialization/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ export * from "./serializationWriterFactory";
1212
export * from "./serializationWriterFactoryRegistry";
1313
export * from "./serializationWriterProxyFactory";
1414
export * from "./serializationFunctionTypes";
15+
export * from "./untypedNode";
16+
export * from "./untypedNumber";
17+
export * from "./untypedArray";
18+
export * from "./untypedNull";
19+
export * from "./untypedObject";
20+
export * from "./untypedString";
21+
export * from "./untypedBoolean";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { isUntypedNode, UntypedNode } from "./untypedNode";
2+
3+
/** Defines an interface for defining an untyped array. */
4+
export interface UntypedArray extends UntypedNode {
5+
/**
6+
* Gets the value of the UntypedNode as an array of UntypedNodes.
7+
*/
8+
getValue(): UntypedNode[];
9+
}
10+
11+
/**
12+
* Type guard to assert that an UntypedNode instance is an UntypedArray.
13+
* @param node The UntypedNode to check.
14+
* @return boolean indicating if the node is an UntypedArray.
15+
*/
16+
export function isUntypedArray(node: UntypedNode): node is UntypedArray {
17+
const proposedNode = node as UntypedArray;
18+
return (
19+
proposedNode &&
20+
proposedNode.value instanceof Array &&
21+
proposedNode.value.every((item) => isUntypedNode(item))
22+
);
23+
}
24+
25+
/**
26+
* Factory to create an UntypedArray from an array of UntypedNodes.
27+
* @param value The value to create from.
28+
* @return The created UntypedArray.
29+
*/
30+
export function createUntypedArray(value: UntypedNode[]): UntypedArray {
31+
return {
32+
value: value,
33+
getValue: () => value as UntypedNode[],
34+
};
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { UntypedNode } from "./untypedNode";
2+
3+
/** Defines an interface for defining an untyped boolean. */
4+
export interface UntypedBoolean extends UntypedNode {
5+
/**
6+
* Gets the value of the UntypedNode as a boolean value.
7+
*/
8+
getValue(): boolean;
9+
}
10+
11+
/**
12+
* Type guard to assert that an UntypedNode instance is an UntypedBoolean.
13+
* @param node The UntypedNode to check.
14+
* @return boolean indicating if the node is an UntypedBoolean.
15+
*/
16+
export function isUntypedBoolean(node: UntypedNode): node is UntypedBoolean {
17+
const proposedNode = node as UntypedBoolean;
18+
return proposedNode && typeof proposedNode.value === "boolean";
19+
}
20+
21+
/**
22+
* Factory to create an UntypedBoolean from a boolean.
23+
* @param value The boolean value to create from.
24+
* @return The created UntypedBoolean.
25+
*/
26+
export function createUntypedBoolean(value: boolean): UntypedBoolean {
27+
return {
28+
value: value,
29+
getValue: () => value as boolean,
30+
};
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
import type { Parsable } from "./parsable";
3+
import type { ParseNode } from "./parseNode";
4+
import type { SerializationWriter } from "./serializationWriter";
5+
6+
/** Defines the base interface for defining an untyped node. */
7+
export interface UntypedNode extends Parsable {
8+
/**
9+
* Gets the value of the UntypedNode.
10+
*/
11+
getValue(): any;
12+
/**
13+
* The value represented by the UntypedNode.
14+
*/
15+
value?: any;
16+
}
17+
18+
/**
19+
* Factory to create an UntypedNode from a string during deserialization.
20+
*/
21+
export function createUntypedNodeFromDiscriminatorValue(
22+
_parseNode: ParseNode | undefined,
23+
): (instance?: Parsable) => Record<string, (node: ParseNode) => void> {
24+
return deserializeIntoUntypedNode;
25+
}
26+
27+
/**
28+
* Type guard to assert that an object instance is an UntypedNode.
29+
* @param node The object to check.
30+
* @return boolean indicating if the node is an UntypedNode.
31+
*/
32+
export function isUntypedNode(node: any): node is UntypedNode {
33+
const potentialNode = node as UntypedNode;
34+
return potentialNode && potentialNode.getValue !== undefined;
35+
}
36+
37+
/**
38+
* The deserialization implementation for UntypedNode.
39+
*/
40+
export function deserializeIntoUntypedNode(
41+
untypedNode: Partial<UntypedNode> | undefined = {},
42+
): Record<string, (node: ParseNode) => void> {
43+
return {
44+
value: (n) => {
45+
untypedNode.value = null;
46+
},
47+
getValue: (n) => {
48+
untypedNode.getValue = () => untypedNode.value;
49+
},
50+
};
51+
}
52+
53+
/**
54+
* The serialization implementation for UntypedNode.
55+
*/
56+
export function serializeUntypedNode(
57+
_writer: SerializationWriter,
58+
_errorDetails: Partial<UntypedNode> | undefined = {},
59+
): void {
60+
return;
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { UntypedNode } from "./untypedNode";
2+
3+
/** Defines the interface for defining an untyped null value. */
4+
export interface UntypedNull extends UntypedNode {
5+
/**
6+
* Gets the value of the UntypedNode as null.
7+
*/
8+
getValue(): null;
9+
}
10+
11+
/**
12+
* Type guard to assert that an object instance is an UntypedNull.
13+
* @param node The object to check.
14+
* @return boolean indicating if the node is an UntypedNull.
15+
*/
16+
export function isUntypedNull(node: UntypedNode): node is UntypedNull {
17+
return node.value === null;
18+
}
19+
20+
/**
21+
* Factory to create an UntypedNull from a boolean.
22+
* @return The created UntypedNull.
23+
*/
24+
export function createUntypedNull(): UntypedNull {
25+
return {
26+
value: null,
27+
getValue: () => null,
28+
};
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { UntypedNode } from "./untypedNode";
2+
3+
/** Defines the interface for defining an untyped number value. */
4+
export interface UntypedNumber extends UntypedNode {
5+
/**
6+
* Gets the value of the UntypedNode as a number.
7+
*/
8+
getValue(): number;
9+
}
10+
11+
/**
12+
* Type guard to assert that an object instance is an UntypedNumber.
13+
* @param node The object to check.
14+
* @return boolean indicating if the node is an UntypedNumber.
15+
*/
16+
export function isUntypedNumber(node: UntypedNode): node is UntypedNumber {
17+
const proposedNode = node as UntypedNumber;
18+
return proposedNode && typeof proposedNode.value === "number";
19+
}
20+
21+
/**
22+
* Factory to create an UntypedNumber from a number.
23+
* @param value The number value to create from.
24+
* @return The created UntypedNumber.
25+
*/
26+
export function createUntypedNumber(value: number): UntypedNumber {
27+
return {
28+
value: value,
29+
getValue: () => value as number,
30+
};
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { isUntypedNode, UntypedNode } from "./untypedNode";
2+
3+
/** Defines the interface for defining an untyped object value. */
4+
export interface UntypedObject extends UntypedNode {
5+
/**
6+
* Gets the value of the UntypedNode as a Record<string, UntypedNode>.
7+
*/
8+
getValue(): Record<string, UntypedNode>;
9+
}
10+
11+
/**
12+
* Type guard to assert that an object instance is an UntypedObject.
13+
* @param node The object to check.
14+
* @return boolean indicating if the node is an UntypedObject.
15+
*/
16+
export function isUntypedObject(node: UntypedNode): node is UntypedObject {
17+
const proposedNode = node as UntypedObject;
18+
return (
19+
proposedNode &&
20+
proposedNode.value instanceof Object &&
21+
proposedNode.value instanceof Array === false &&
22+
Object.values(proposedNode.value).every((item) => isUntypedNode(item))
23+
);
24+
}
25+
26+
/**
27+
* Factory to create an UntypedObject from a Record<string, UntypedNode>.
28+
* @param value The Record<string, UntypedNode> value to create from.
29+
* @return The created UntypedObject.
30+
*/
31+
export function createUntypedObject(
32+
value: Record<string, UntypedNode>,
33+
): UntypedObject {
34+
return {
35+
value: value,
36+
getValue: () => value as Record<string, UntypedNode>,
37+
};
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { UntypedNode } from "./untypedNode";
2+
3+
/** Defines the interface for defining an untyped string value. */
4+
export interface UntypedString extends UntypedNode {
5+
/**
6+
* Gets the value of the UntypedNode as a Record<string, UntypedNode>.
7+
*/
8+
getValue(): string;
9+
}
10+
11+
/**
12+
* Type guard to assert that an object instance is an UntypedString.
13+
* @param node The object to check.
14+
* @return boolean indicating if the node is an UntypedString.
15+
*/
16+
export function isUntypedString(node: UntypedNode): node is UntypedString {
17+
const proposedNode = node as UntypedString;
18+
return proposedNode && typeof proposedNode.value === "string";
19+
}
20+
21+
/**
22+
* Factory to create an UntypedString from a string.
23+
* @param value The string value to create from.
24+
* @return The created UntypedString.
25+
*/
26+
export function createUntypedString(value: string): UntypedString {
27+
return {
28+
value: value,
29+
getValue: () => value as string,
30+
};
31+
}

packages/authentication/azure/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/kiota-authentication-azure",
3-
"version": "1.0.0-preview.42",
3+
"version": "1.0.0-preview.43",
44
"description": "Authentication provider for Kiota using Azure Identity",
55
"main": "dist/cjs/src/index.js",
66
"module": "dist/es/src/index.js",
@@ -30,7 +30,7 @@
3030
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
3131
"dependencies": {
3232
"@azure/core-auth": "^1.5.0",
33-
"@microsoft/kiota-abstractions": "^1.0.0-preview.47",
33+
"@microsoft/kiota-abstractions": "^1.0.0-preview.48",
3434
"@opentelemetry/api": "^1.7.0",
3535
"tslib": "^2.6.2"
3636
},

packages/authentication/spfx/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/kiota-authentication-spfx",
3-
"version": "1.0.0-preview.37",
3+
"version": "1.0.0-preview.38",
44
"description": "Authentication provider for using Kiota in SPFx solutions",
55
"main": "dist/cjs/src/index.js",
66
"module": "dist/es/src/index.js",
@@ -39,7 +39,7 @@
3939
},
4040
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
4141
"dependencies": {
42-
"@microsoft/kiota-abstractions": "^1.0.0-preview.47",
42+
"@microsoft/kiota-abstractions": "^1.0.0-preview.48",
4343
"@microsoft/sp-http": "^1.15.2",
4444
"@opentelemetry/api": "^1.7.0",
4545
"tslib": "^2.6.2"

packages/http/fetch/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/kiota-http-fetchlibrary",
3-
"version": "1.0.0-preview.46",
3+
"version": "1.0.0-preview.47",
44
"description": "Kiota request adapter implementation with fetch",
55
"keywords": [
66
"Kiota",
@@ -38,7 +38,7 @@
3838
"test:cjs": "mocha 'dist/cjs/test/common/**/*.js' && mocha 'dist/cjs/test/node/**/*.js'"
3939
},
4040
"dependencies": {
41-
"@microsoft/kiota-abstractions": "^1.0.0-preview.47",
41+
"@microsoft/kiota-abstractions": "^1.0.0-preview.48",
4242
"@opentelemetry/api": "^1.7.0",
4343
"guid-typescript": "^1.0.9",
4444
"tslib": "^2.6.2"

packages/serialization/form/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/kiota-serialization-form",
3-
"version": "1.0.0-preview.36",
3+
"version": "1.0.0-preview.37",
44
"description": "Implementation of Kiota Serialization interfaces for URI from encoded",
55
"main": "dist/cjs/src/index.js",
66
"browser": {
@@ -39,7 +39,7 @@
3939
},
4040
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
4141
"dependencies": {
42-
"@microsoft/kiota-abstractions": "^1.0.0-preview.47",
42+
"@microsoft/kiota-abstractions": "^1.0.0-preview.48",
4343
"guid-typescript": "^1.0.9",
4444
"tslib": "^2.6.2"
4545
},

packages/serialization/json/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/kiota-serialization-json",
3-
"version": "1.0.0-preview.47",
3+
"version": "1.0.0-preview.48",
44
"description": "Implementation of Kiota Serialization interfaces for JSON",
55
"main": "dist/cjs/src/index.js",
66
"browser": {
@@ -39,7 +39,7 @@
3939
},
4040
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
4141
"dependencies": {
42-
"@microsoft/kiota-abstractions": "^1.0.0-preview.47",
42+
"@microsoft/kiota-abstractions": "^1.0.0-preview.48",
4343
"guid-typescript": "^1.0.9",
4444
"tslib": "^2.6.2"
4545
},

0 commit comments

Comments
 (0)