|
| 1 | +# End-to-end usage of kiota-generated SDKs in Javascript |
| 2 | + |
| 3 | +While working with the Kiota + Javascript teams, it became clear that we needed more context and provide a better vision on how we should be using a kiota-generated SDK. This design document aims at providing clarity and a better understanding on how developers will be using our SDKs and the underlying kiota packages. |
| 4 | + |
| 5 | +## Constraints |
| 6 | + |
| 7 | +Before we jump into the end-to-end walk-through, it's important to set some constraints. |
| 8 | + |
| 9 | +| Type | Description | |
| 10 | +| --------- | --------------------------------------------------------------------------------------------------------------------------------------------- | |
| 11 | +| Platforms | Node : Current and Previous LTS (14 / 16)<br /> Web : Edge, Chrome, Firefox and Safari (Latest released version + immediate previous version) | |
| 12 | +| Modules | Node : CommonJS<br /> Web : ES Module (ES6) | |
| 13 | +| Types | Typings should be available for both the core and the service libraries for Graph models | |
| 14 | + |
| 15 | +## NodeJS e2e using the Service library |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install @microsoft/msgraph-sdk-typescript --save |
| 19 | +npm install @microsoft/kiota-authentication-azure --save |
| 20 | +``` |
| 21 | + |
| 22 | +```typescript |
| 23 | +// App.ts |
| 24 | + |
| 25 | +import { Client, User, Message, BodyType } from "@microsoft/msgraph-sdk-javascript"; |
| 26 | +import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure"; |
| 27 | +import { DeviceCodeCredential } from "@azure/identity"; |
| 28 | + |
| 29 | +const deviceCodeCredentials = new DeviceCodeCredential({ |
| 30 | + tenantId: "b61f9af1-d6cf-4cc0-a6f6-befb38bc00ed", |
| 31 | + clientId: "bde251a6-0ef9-42a8-a40b-9ad9bb594b2c", |
| 32 | +}); |
| 33 | + |
| 34 | +const scopes = ["User.Read", "Mail.Send"]; |
| 35 | + |
| 36 | +const graphClient = Client.init({ |
| 37 | + accessTokenProvider: new AzureIdentityAccessTokenProvider(deviceCodeCredentials, scopes), |
| 38 | +}); |
| 39 | + |
| 40 | +const me = await getMe(); |
| 41 | +const meRaw = await getMeRaw(); |
| 42 | +await sendMail(); |
| 43 | +await sendMailRaw(); |
| 44 | + |
| 45 | +async function getMe(): Promise<User | undefined> { |
| 46 | + return await graphClient.me.get(); |
| 47 | +} |
| 48 | + |
| 49 | +async function getMe(): Promise<User | undefined> { |
| 50 | + return await graphClient.api("/me").get(); |
| 51 | +} |
| 52 | + |
| 53 | +async function sendMail(): Promise<void> { |
| 54 | + const message: Message = { |
| 55 | + subject: "Hello Graph TypeScript SDK!", |
| 56 | + body: { |
| 57 | + contentType: BodyType.Html, |
| 58 | + content: "<bold>Hello Graph TypeScript SDK!</bold>", |
| 59 | + }, |
| 60 | + toRecipients: [ |
| 61 | + { |
| 62 | + emailAddress: { |
| 63 | + |
| 64 | + }, |
| 65 | + }, |
| 66 | + ], |
| 67 | + }; |
| 68 | + |
| 69 | + return await client.me.sendMail.post(message); |
| 70 | +} |
| 71 | + |
| 72 | +async function sendMailRaw(): Promise<void> { |
| 73 | + const message: Message = { |
| 74 | + subject: "Hello Graph TypeScript SDK!", |
| 75 | + body: { |
| 76 | + contentType: BodyType.Html, |
| 77 | + content: "<bold>Hello Graph TypeScript SDK!</bold>", |
| 78 | + }, |
| 79 | + toRecipients: [ |
| 80 | + { |
| 81 | + emailAddress: { |
| 82 | + |
| 83 | + }, |
| 84 | + }, |
| 85 | + ], |
| 86 | + }; |
| 87 | + |
| 88 | + return await client.api("/me/sendMail").post({ |
| 89 | + message: message, |
| 90 | + }); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## NodeJS e2e using the Core library |
| 95 | + |
| 96 | +```bash |
| 97 | +npm install @microsoft/msgraph-sdk-javascript-core --save |
| 98 | +npm install @microsoft/msgraph-sdk-javascript-types --save |
| 99 | +npm install @microsoft/kiota-authentication-azure --save |
| 100 | +``` |
| 101 | + |
| 102 | +```typescript |
| 103 | +// App.ts |
| 104 | + |
| 105 | +import { Client } from "@microsoft/msgraph-sdk-javascript-core"; |
| 106 | +import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure"; |
| 107 | +import { DeviceCodeCredential } from "@azure/identity"; |
| 108 | + |
| 109 | +const deviceCodeCredentials = new DeviceCodeCredential({ |
| 110 | + tenantId: "b61f9af1-d6cf-4cc0-a6f6-befb38bc00ed", |
| 111 | + clientId: "bde251a6-0ef9-42a8-a40b-9ad9bb594b2c", |
| 112 | +}); |
| 113 | + |
| 114 | +const scopes = ["User.Read", "Mail.Send"]; |
| 115 | + |
| 116 | +const graphClient = Client.init({ |
| 117 | + accessTokenProvider: new AzureIdentityAccessTokenProvider(deviceCodeCredentials, scopes), |
| 118 | +}); |
| 119 | + |
| 120 | +const me = await getMe(); |
| 121 | +await sendMail(); |
| 122 | + |
| 123 | +async function getMe(): Promise<User | undefined> { |
| 124 | + return await graphClient.api("/me").get(); |
| 125 | +} |
| 126 | + |
| 127 | +async function sendMail(): Promise<void> { |
| 128 | + const message: Message = { |
| 129 | + subject: "Hello Graph TypeScript SDK!", |
| 130 | + body: { |
| 131 | + contentType: BodyType.Html, |
| 132 | + content: "<bold>Hello Graph TypeScript SDK!</bold>", |
| 133 | + }, |
| 134 | + toRecipients: [ |
| 135 | + { |
| 136 | + emailAddress: { |
| 137 | + |
| 138 | + }, |
| 139 | + }, |
| 140 | + ], |
| 141 | + }; |
| 142 | + |
| 143 | + return await client.api("/me/sendMail").post({ |
| 144 | + message: message, |
| 145 | + }); |
| 146 | +} |
| 147 | +``` |
0 commit comments