Skip to content

Commit

Permalink
Merge pull request #986 from microsoft/feature/better-enums-epression
Browse files Browse the repository at this point in the history
Express enums better in Typescript
  • Loading branch information
baywet authored Dec 14, 2023
2 parents abb5ce3 + 868286c commit 5ecae0c
Show file tree
Hide file tree
Showing 37 changed files with 185 additions and 172 deletions.
2 changes: 1 addition & 1 deletion packages/abstractions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-abstractions",
"version": "1.0.0-preview.35",
"version": "1.0.0-preview.36",
"description": "Core abstractions for kiota generated libraries in TypeScript and JavaScript",
"main": "dist/cjs/src/index.js",
"module": "dist/es/src/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/authentication/azure/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-authentication-azure",
"version": "1.0.0-preview.30",
"version": "1.0.0-preview.31",
"description": "Authentication provider for Kiota using Azure Identity",
"main": "dist/cjs/src/index.js",
"module": "dist/es/src/index.js",
Expand Down Expand Up @@ -30,7 +30,7 @@
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@azure/core-auth": "^1.3.2",
"@microsoft/kiota-abstractions": "^1.0.0-preview.35",
"@microsoft/kiota-abstractions": "^1.0.0-preview.36",
"@opentelemetry/api": "^1.2.0",
"tslib": "^2.3.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/authentication/spfx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-authentication-spfx",
"version": "1.0.0-preview.25",
"version": "1.0.0-preview.26",
"description": "Authentication provider for using Kiota in SPFx solutions",
"main": "dist/cjs/src/index.js",
"module": "dist/es/src/index.js",
Expand Down Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.35",
"@microsoft/kiota-abstractions": "^1.0.0-preview.36",
"@microsoft/sp-http": "^1.15.2",
"@opentelemetry/api": "^1.2.0",
"tslib": "^2.3.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/http/fetch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-http-fetchlibrary",
"version": "1.0.0-preview.34",
"version": "1.0.0-preview.35",
"description": "Kiota request adapter implementation with fetch",
"keywords": [
"Kiota",
Expand Down Expand Up @@ -38,7 +38,7 @@
"test:cjs": "mocha 'dist/cjs/test/common/**/*.js' && mocha 'dist/cjs/test/node/**/*.js'"
},
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.35",
"@microsoft/kiota-abstractions": "^1.0.0-preview.36",
"@opentelemetry/api": "^1.2.0",
"guid-typescript": "^1.0.9",
"node-fetch": "^2.6.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/serialization/form/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-serialization-form",
"version": "1.0.0-preview.24",
"version": "1.0.0-preview.25",
"description": "Implementation of Kiota Serialization interfaces for URI from encoded",
"main": "dist/cjs/src/index.js",
"browser": {
Expand Down Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.35",
"@microsoft/kiota-abstractions": "^1.0.0-preview.36",
"guid-typescript": "^1.0.9",
"tslib": "^2.3.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/serialization/json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-serialization-json",
"version": "1.0.0-preview.35",
"version": "1.0.0-preview.36",
"description": "Implementation of Kiota Serialization interfaces for JSON",
"main": "dist/cjs/src/index.js",
"browser": {
Expand Down Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.35",
"@microsoft/kiota-abstractions": "^1.0.0-preview.36",
"guid-typescript": "^1.0.9",
"tslib": "^2.3.1"
},
Expand Down
23 changes: 15 additions & 8 deletions packages/serialization/json/test/common/JsonParseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,34 @@ describe("JsonParseNode", () => {
});

it("Test enum values", async () => {
enum TestEnum {
A = "a",
B = "b",
C = "c"
}
const TestEnumObject = {
A: "a",
B: "b",
C: "c"
} as const;

type TestEnum = (typeof TestEnumObject)[keyof typeof TestEnumObject];

const result = new JsonParseNode([
"a",
"b",
"c"
]).getCollectionOfEnumValues(TestEnum) as TestEnum[];
]).getCollectionOfEnumValues(TestEnumObject) as TestEnum[];
assert.equal(result.length, 3);
assert.equal(result.shift(), "a");

const enumValuesResult = new JsonParseNode([
"d",
"b",
"c"
]).getCollectionOfEnumValues(TestEnum) as TestEnum[];
]).getCollectionOfEnumValues(TestEnumObject) as TestEnum[];
assert.equal(enumValuesResult.length, 2);
assert.equal(enumValuesResult.shift(), "b");
assert.equal(enumValuesResult.shift(), "b")

const enumValueResult = new JsonParseNode(
"a"
).getEnumValue(TestEnumObject) as TestEnum;
assert.equal(enumValueResult, TestEnumObject.A);
});

it("Test a null collection of object values", async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/serialization/multipart/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-serialization-multipart",
"version": "1.0.0-preview.14",
"version": "1.0.0-preview.15",
"description": "Implementation of Kiota Serialization interfaces for multipart form data",
"main": "dist/cjs/src/index.js",
"module": "dist/es/src/index.js",
Expand Down Expand Up @@ -35,12 +35,12 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.35",
"@microsoft/kiota-abstractions": "^1.0.0-preview.36",
"guid-typescript": "^1.0.9",
"tslib": "^2.3.1"
},
"devDependencies": {
"@microsoft/kiota-serialization-json": "^1.0.0-preview.35"
"@microsoft/kiota-serialization-json": "^1.0.0-preview.36"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 2 additions & 2 deletions packages/serialization/text/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-serialization-text",
"version": "1.0.0-preview.32",
"version": "1.0.0-preview.33",
"description": "Implementation of Kiota Serialization interfaces for text",
"main": "dist/cjs/src/index.js",
"browser": {
Expand Down Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "^1.0.0-preview.35",
"@microsoft/kiota-abstractions": "^1.0.0-preview.36",
"guid-typescript": "^1.0.9",
"tslib": "^2.3.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/test/generatedCode/kiota-lock.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"descriptionHash": "71BBEBED4B32121863A126C7FE6DFB7AD99DD20A39CF9834F1754D8253E36933317E3C9379E055E128B44A505492DA555F535EF5ED47A2E5BBB8209ED1BC4B27",
"descriptionHash": "293EC13E4EA89E6695FB19ADCE23A67734E8CC8018E1BA88D27E27EC369C01F1C6EA5C84B7F25E60128993F20D175018DDDB4BA206CB4A7336F09CB8352B74C3",
"descriptionLocation": "https://raw.githubusercontent.com/microsoftgraph/msgraph-sdk-powershell/dev/openApiDocs/v1.0/Mail.yml",
"lockFileVersion": "1.0.0",
"kiotaVersion": "1.9.0",
"kiotaVersion": "1.10.0",
"clientClassName": "ApiClient",
"clientNamespaceName": "ApiSdk",
"language": "TypeScript",
Expand Down
98 changes: 52 additions & 46 deletions packages/test/generatedCode/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export interface AttachmentCollectionResponse extends AdditionalDataHolder, Pars
*/
value?: Attachment[];
}
export enum BodyType {
Text = "text",
Html = "html",
}
export type BodyType = (typeof BodyTypeObject)[keyof typeof BodyTypeObject];
export function createAttachmentCollectionResponseFromDiscriminatorValue(parseNode: ParseNode | undefined) {
return deserializeIntoAttachmentCollectionResponse;
}
Expand Down Expand Up @@ -183,7 +180,7 @@ export function deserializeIntoFollowupFlag(followupFlag: FollowupFlag | undefin
return {
"completedDateTime": n => { followupFlag.completedDateTime = n.getObjectValue<DateTimeTimeZone>(createDateTimeTimeZoneFromDiscriminatorValue); },
"dueDateTime": n => { followupFlag.dueDateTime = n.getObjectValue<DateTimeTimeZone>(createDateTimeTimeZoneFromDiscriminatorValue); },
"flagStatus": n => { followupFlag.flagStatus = n.getEnumValue<FollowupFlagStatus>(FollowupFlagStatus); },
"flagStatus": n => { followupFlag.flagStatus = n.getEnumValue<FollowupFlagStatus>(FollowupFlagStatusObject); },
"startDateTime": n => { followupFlag.startDateTime = n.getObjectValue<DateTimeTimeZone>(createDateTimeTimeZoneFromDiscriminatorValue); },
}
}
Expand All @@ -196,7 +193,7 @@ export function deserializeIntoInferenceClassification(inferenceClassification:
export function deserializeIntoInferenceClassificationOverride(inferenceClassificationOverride: InferenceClassificationOverride | undefined = {} as InferenceClassificationOverride) : Record<string, (node: ParseNode) => void> {
return {
...deserializeIntoEntity(inferenceClassificationOverride),
"classifyAs": n => { inferenceClassificationOverride.classifyAs = n.getEnumValue<InferenceClassificationType>(InferenceClassificationType); },
"classifyAs": n => { inferenceClassificationOverride.classifyAs = n.getEnumValue<InferenceClassificationType>(InferenceClassificationTypeObject); },
"senderEmailAddress": n => { inferenceClassificationOverride.senderEmailAddress = n.getObjectValue<EmailAddress>(createEmailAddressFromDiscriminatorValue); },
}
}
Expand All @@ -215,7 +212,7 @@ export function deserializeIntoInternetMessageHeader(internetMessageHeader: Inte
export function deserializeIntoItemBody(itemBody: ItemBody | undefined = {} as ItemBody) : Record<string, (node: ParseNode) => void> {
return {
"content": n => { itemBody.content = n.getStringValue(); },
"contentType": n => { itemBody.contentType = n.getEnumValue<BodyType>(BodyType); },
"contentType": n => { itemBody.contentType = n.getEnumValue<BodyType>(BodyTypeObject); },
}
}
export function deserializeIntoMailFolder(mailFolder: MailFolder | undefined = {} as MailFolder) : Record<string, (node: ParseNode) => void> {
Expand Down Expand Up @@ -254,8 +251,8 @@ export function deserializeIntoMessage(message: Message | undefined = {} as Mess
"flag": n => { message.flag = n.getObjectValue<FollowupFlag>(createFollowupFlagFromDiscriminatorValue); },
"from": n => { message.from = n.getObjectValue<Recipient>(createRecipientFromDiscriminatorValue); },
"hasAttachments": n => { message.hasAttachments = n.getBooleanValue(); },
"importance": n => { message.importance = n.getEnumValue<Importance>(Importance); },
"inferenceClassification": n => { message.inferenceClassification = n.getEnumValue<InferenceClassificationType>(InferenceClassificationType); },
"importance": n => { message.importance = n.getEnumValue<Importance>(ImportanceObject); },
"inferenceClassification": n => { message.inferenceClassification = n.getEnumValue<InferenceClassificationType>(InferenceClassificationTypeObject); },
"internetMessageHeaders": n => { message.internetMessageHeaders = n.getCollectionOfObjectValues<InternetMessageHeader>(createInternetMessageHeaderFromDiscriminatorValue); },
"internetMessageId": n => { message.internetMessageId = n.getStringValue(); },
"isDeliveryReceiptRequested": n => { message.isDeliveryReceiptRequested = n.getBooleanValue(); },
Expand Down Expand Up @@ -302,7 +299,7 @@ export function deserializeIntoMessageRuleActions(messageRuleActions: MessageRul
"forwardAsAttachmentTo": n => { messageRuleActions.forwardAsAttachmentTo = n.getCollectionOfObjectValues<Recipient>(createRecipientFromDiscriminatorValue); },
"forwardTo": n => { messageRuleActions.forwardTo = n.getCollectionOfObjectValues<Recipient>(createRecipientFromDiscriminatorValue); },
"markAsRead": n => { messageRuleActions.markAsRead = n.getBooleanValue(); },
"markImportance": n => { messageRuleActions.markImportance = n.getEnumValue<Importance>(Importance); },
"markImportance": n => { messageRuleActions.markImportance = n.getEnumValue<Importance>(ImportanceObject); },
"moveToFolder": n => { messageRuleActions.moveToFolder = n.getStringValue(); },
"permanentDelete": n => { messageRuleActions.permanentDelete = n.getBooleanValue(); },
"redirectTo": n => { messageRuleActions.redirectTo = n.getCollectionOfObjectValues<Recipient>(createRecipientFromDiscriminatorValue); },
Expand All @@ -323,7 +320,7 @@ export function deserializeIntoMessageRulePredicates(messageRulePredicates: Mess
"fromAddresses": n => { messageRulePredicates.fromAddresses = n.getCollectionOfObjectValues<Recipient>(createRecipientFromDiscriminatorValue); },
"hasAttachments": n => { messageRulePredicates.hasAttachments = n.getBooleanValue(); },
"headerContains": n => { messageRulePredicates.headerContains = n.getCollectionOfPrimitiveValues<string>(); },
"importance": n => { messageRulePredicates.importance = n.getEnumValue<Importance>(Importance); },
"importance": n => { messageRulePredicates.importance = n.getEnumValue<Importance>(ImportanceObject); },
"isApprovalRequest": n => { messageRulePredicates.isApprovalRequest = n.getBooleanValue(); },
"isAutomaticForward": n => { messageRulePredicates.isAutomaticForward = n.getBooleanValue(); },
"isAutomaticReply": n => { messageRulePredicates.isAutomaticReply = n.getBooleanValue(); },
Expand All @@ -335,11 +332,11 @@ export function deserializeIntoMessageRulePredicates(messageRulePredicates: Mess
"isReadReceipt": n => { messageRulePredicates.isReadReceipt = n.getBooleanValue(); },
"isSigned": n => { messageRulePredicates.isSigned = n.getBooleanValue(); },
"isVoicemail": n => { messageRulePredicates.isVoicemail = n.getBooleanValue(); },
"messageActionFlag": n => { messageRulePredicates.messageActionFlag = n.getEnumValue<MessageActionFlag>(MessageActionFlag); },
"messageActionFlag": n => { messageRulePredicates.messageActionFlag = n.getEnumValue<MessageActionFlag>(MessageActionFlagObject); },
"notSentToMe": n => { messageRulePredicates.notSentToMe = n.getBooleanValue(); },
"recipientContains": n => { messageRulePredicates.recipientContains = n.getCollectionOfPrimitiveValues<string>(); },
"senderContains": n => { messageRulePredicates.senderContains = n.getCollectionOfPrimitiveValues<string>(); },
"sensitivity": n => { messageRulePredicates.sensitivity = n.getEnumValue<Sensitivity>(Sensitivity); },
"sensitivity": n => { messageRulePredicates.sensitivity = n.getEnumValue<Sensitivity>(SensitivityObject); },
"sentCcMe": n => { messageRulePredicates.sentCcMe = n.getBooleanValue(); },
"sentOnlyToMe": n => { messageRulePredicates.sentOnlyToMe = n.getBooleanValue(); },
"sentToAddresses": n => { messageRulePredicates.sentToAddresses = n.getCollectionOfObjectValues<Recipient>(createRecipientFromDiscriminatorValue); },
Expand Down Expand Up @@ -443,16 +440,8 @@ export interface FollowupFlag extends AdditionalDataHolder, Parsable {
*/
startDateTime?: DateTimeTimeZone;
}
export enum FollowupFlagStatus {
NotFlagged = "notFlagged",
Complete = "complete",
Flagged = "flagged",
}
export enum Importance {
Low = "low",
Normal = "normal",
High = "high",
}
export type FollowupFlagStatus = (typeof FollowupFlagStatusObject)[keyof typeof FollowupFlagStatusObject];
export type Importance = (typeof ImportanceObject)[keyof typeof ImportanceObject];
export interface InferenceClassification extends Entity, Parsable {
/**
* A set of overrides for a user to always classify messages from specific senders in certain ways: focused, or other. Read-only. Nullable.
Expand Down Expand Up @@ -483,10 +472,7 @@ export interface InferenceClassificationOverrideCollectionResponse extends Addit
*/
value?: InferenceClassificationOverride[];
}
export enum InferenceClassificationType {
Focused = "focused",
Other = "other",
}
export type InferenceClassificationType = (typeof InferenceClassificationTypeObject)[keyof typeof InferenceClassificationTypeObject];
export interface InternetMessageHeader extends AdditionalDataHolder, Parsable {
/**
* Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.
Expand Down Expand Up @@ -697,19 +683,7 @@ export interface Message extends OutlookItem, Parsable {
*/
webLink?: string;
}
export enum MessageActionFlag {
Any = "any",
Call = "call",
DoNotForward = "doNotForward",
FollowUp = "followUp",
Fyi = "fyi",
Forward = "forward",
NoResponseNecessary = "noResponseNecessary",
Read = "read",
Reply = "reply",
ReplyToAll = "replyToAll",
Review = "review",
}
export type MessageActionFlag = (typeof MessageActionFlagObject)[keyof typeof MessageActionFlagObject];
export interface MessageCollectionResponse extends AdditionalDataHolder, Parsable {
/**
* Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.
Expand Down Expand Up @@ -982,12 +956,7 @@ export interface Recipient extends AdditionalDataHolder, Parsable {
*/
emailAddress?: EmailAddress;
}
export enum Sensitivity {
Normal = "normal",
Personal = "personal",
Private = "private",
Confidential = "confidential",
}
export type Sensitivity = (typeof SensitivityObject)[keyof typeof SensitivityObject];
export function serializeAttachment(writer: SerializationWriter, attachment: Attachment | undefined = {} as Attachment) : void {
serializeEntity(writer, attachment)
writer.writeStringValue("contentType", attachment.contentType);
Expand Down Expand Up @@ -1218,5 +1187,42 @@ export interface SizeRange extends AdditionalDataHolder, Parsable {
*/
minimumSize?: number;
}
export const BodyTypeObject = {
Text: "text",
Html: "html",
} as const;
export const FollowupFlagStatusObject = {
NotFlagged: "notFlagged",
Complete: "complete",
Flagged: "flagged",
} as const;
export const ImportanceObject = {
Low: "low",
Normal: "normal",
High: "high",
} as const;
export const InferenceClassificationTypeObject = {
Focused: "focused",
Other: "other",
} as const;
export const MessageActionFlagObject = {
Any: "any",
Call: "call",
DoNotForward: "doNotForward",
FollowUp: "followUp",
Fyi: "fyi",
Forward: "forward",
NoResponseNecessary: "noResponseNecessary",
Read: "read",
Reply: "reply",
ReplyToAll: "replyToAll",
Review: "review",
} as const;
export const SensitivityObject = {
Normal: "normal",
Personal: "personal",
Private: "private",
Confidential: "confidential",
} as const;
// tslint:enable
// eslint-enable
2 changes: 1 addition & 1 deletion packages/test/generatedCode/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { BaseRequestBuilder, getPathParameters, type RequestAdapter } from '@mic
export class UsersRequestBuilder extends BaseRequestBuilder<UsersRequestBuilder> {
/**
* Gets an item from the ApiSdk.users.item collection
* @param userId Unique identifier of the item
* @param userId The unique identifier of user
* @returns a UserItemRequestBuilder
*/
public byUserId(userId: string) : UserItemRequestBuilder {
Expand Down
Loading

0 comments on commit 5ecae0c

Please sign in to comment.