Skip to content

Commit d0b58dc

Browse files
Added complete docs CustomMiddleware, CustomAuthProvider, GettingRawResponse. Added Graph request and error handlers
1 parent b3c525b commit d0b58dc

File tree

82 files changed

+2172
-4110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2172
-4110
lines changed

.vscode/launch.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
"cwd": "${workspaceRoot}",
2525
"outFiles": [],
2626
"internalConsoleOptions": "openOnSessionStart"
27+
},
28+
{
29+
"type": "node",
30+
"request": "launch",
31+
"name": "Run workload tests",
32+
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
33+
"args": [
34+
"${workspaceRoot}/spec/development/workload/*.js"
35+
],
36+
"cwd": "${workspaceRoot}",
37+
"outFiles": [],
38+
"internalConsoleOptions": "openOnSessionStart"
2739
}
2840
]
29-
}
41+
}

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const options = {
6666
const client = MicrosoftGraph.Client.init(options);
6767
```
6868

69-
For more information, refer: [default options](docs/DefaultOptions.md), [custom middleware chain](docs/CustomMiddlewareChain.md)
69+
For more information on initializing client, refer [this](./docs/CreatingClientInstance.md).
7070

7171
### 4. Make requests to the graph
7272

@@ -109,6 +109,16 @@ try {
109109

110110
For more information, refer: [Calling Pattern](docs/CallingPattern.md), [Actions](docs/Actions.md), [Query Params](docs/QueryParameters.md), [API Methods](docs/APIMethods.md) and [more](docs/).
111111

112+
## Documentation
113+
114+
* [Batching](docs/content/Batching.md)
115+
* [Large File Upload Task](docs/tasks/LargeFileUploadTask.md)
116+
* [Page Iterator](docs/tasks/PageIterator.md)
117+
* [Actions](docs/Actions.md)
118+
* [Query Parameters](docs/QueryParameters.md)
119+
* [Other APIs](docs/OtherAPIs.md)
120+
* [Getting Raw Response](docs/GettingRawResponse.md)
121+
112122
## Questions and comments
113123

114124
We'd love to get your feedback about the Microsoft Graph JavaScript client library. You can send your questions and suggestions to us in the [Issues](https://github.com/microsoftgraph/msgraph-sdk-javascript/issues) section of this repository.

docs/CreatingClientInstance.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Library is shipped with one such authentication provider named [MSALAuthenticati
1515
```typescript
1616
// Instantiating Client with MSALAuthenticationProvider
1717
let clientOptions: ClientOptions = {
18-
authProvider: new MSALAuthenticationProvider(<CLIENT_ID>, <SCOPES>)
18+
authProvider: new MSALAuthenticationProvider(<CLIENT_ID>, <SCOPES>, <OPTIONS>)
1919
};
2020
const client = new Client(clientOptions);
2121
```
2222

23-
Want to use own preferred authentication library, for which one has to implement [AuthenticationProvider](../src/IAuthenticationProvider.ts) interface and pass in the instance of it as `authProvider` in [ClientOptions](../src/IClientOptions.ts). Refer [implementing custom authentication provider](./CustomAuthenticationProvider.md) for more detailed information.
23+
Want to use own preferred authentication library, for which one has to implement [AuthenticationProvider](../src/IAuthenticationProvider.ts) interface and pass in the instance of it as `authProvider` in [ClientOptions](../src/IClientOptions.ts).
2424

2525
```typescript
2626
let clientOptions: ClientOptions = {
@@ -30,10 +30,12 @@ let clientOptions: ClientOptions = {
3030
const client = new Client(clientOptions);
3131
```
3232

33+
Refer, [custom authentication provider](./CustomAuthenticationProvider.md) for more detailed information.
34+
3335
### 2. Custom Middleware chain
3436

3537
Want to have complete control over the request and the response objects, one can provide his own chain of middleware.
36-
Have to pass first middleware in the chain as `middleware` in [ClientOptions](../src/IClientOptions.ts). Refer [implementing custom middleware chain](./CustomMiddlewareChain.md) for more detailed information.
38+
Have to pass first middleware in the chain as `middleware` in [ClientOptions](../src/IClientOptions.ts).
3739

3840
```typescript
3941
let clientOptions: ClientOptions = {
@@ -43,6 +45,8 @@ let clientOptions: ClientOptions = {
4345
const client = new Client(clientOptions);
4446
```
4547

48+
Refer, [custom middleware chain](./CustomMiddlewareChain.md) for more detailed information.
49+
4650
## 2. Init With Options
4751

4852
Pass an [authProvider function](../src/IAuthProvider.ts) in [Options](../src/IOptions.ts) while initializing the Client. In this case, user have to provide his own implementation for getting and refreshing accessToken. A callback will be passed into this authProvider function, accessToken or error needs to be passed in to that callback.
@@ -61,4 +65,4 @@ let options: Options = {
6165
authProvider
6266
};
6367
const client = Client.init(options);
64-
```
68+
```

docs/CustomAuthenticationProvider.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
# Implementing Custom Authentication Provider
1+
# Using Custom Authentication Provider
2+
3+
Using preferred choice of Authentication library for authenticating with Microsoft is possible.
4+
5+
## Step by step procedure
6+
7+
### Implement AuthenticationProvider
8+
9+
Create own implementation of Authentication provider which implements [AuthenticationProvider](../src/iAuthenticationProvider.ts) interface.
10+
11+
```typescript
12+
// MyAuthenticationProvider.ts
13+
import { AuthenticationProvider } from "@microsoft/microsoft-graph-client";
14+
15+
class MyAuthenticationProvider implements AuthenticationProvider {
16+
17+
/**
18+
* This method will get called before every request to the msgraph server
19+
* This should return a Promise that resolves to an accessToken (in case of success) or rejects with error (in case of failure)
20+
* Basically this method will contain the implementation for getting and refreshing accessTokens
21+
*/
22+
public async getAccessToken(): Promise<any> {
23+
24+
}
25+
}
26+
```
27+
28+
### Initialize Client
29+
30+
Pass instance of MyAuthenticationProvider while initializing.
31+
32+
```typescript
33+
import { MyAuthenticationProvider } from "./MyAuthenticationProvider";
34+
35+
let clientOptions: ClientOptions = {
36+
authProvider: new MyCustomAuthenticationProvider()
37+
};
38+
const client = new Client(clientOptions);
39+
```

docs/CustomMiddlewareChain.md

+145-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,148 @@
1-
# Implementing Custom Middleware Chain
1+
# Using Custom Middleware Chain
22

33
## Middleware
44

5-
As name suggests it comes in middle of something and that is request and response cycle. It has access to context object which contains request, response objects and any other options that are specific to middleware. Also it has an access to execute next middleware in the chain.
5+
As name suggests it comes in middle of something and that is request and response cycle. It has access to context object which contains request, response objects and any other options that are specific to middleware. Also it has an access to execute next middleware in the chain.
6+
7+
## Step by step procedure
8+
9+
### Implement Middlewares
10+
11+
Create own set of middlewares by implementing [Middleware](../src/IMiddleware.ts) interface. Here two middlewares are created one for handling Logging and another for handling http request and response.
12+
13+
First middleware is passed with the context object containing request, and other middleware specific options. One has to explicitly make call to execute method of the next middleware with context object once the current middleware work is over.
14+
15+
NOTE: Http message handler should set the response object in the context object.
16+
17+
```typescript
18+
// MyLoggingHandler.ts
19+
import { Middleware } from "@microsoft/microsoft-graph-client";
20+
import { Context } from "@microsoft/microsoft-graph-client";
21+
22+
export class MyLoggingHandler implements Middleware {
23+
24+
private nextMiddleware: Middleware;
25+
26+
public async execute(context: Context): Promise<void> {
27+
try {
28+
let url: string;
29+
if (typeof context.request === "string") {
30+
url = context.request;
31+
} else {
32+
url = context.request.url;
33+
}
34+
console.log(url);
35+
await this.nextMiddleware.execute(context);
36+
} catch(error) {
37+
throw error;
38+
}
39+
}
40+
41+
public setNext(next: Middleware): void {
42+
this.nextMiddleware = next;
43+
}
44+
}
45+
```
46+
47+
```typescript
48+
// MyHttpMessageHandler.ts
49+
import { Middleware } from "@microsoft/microsoft-graph-client";
50+
51+
export class MyHttpMessageHandler implements Middleware {
52+
public async execute(context: Context): Promise<void> {
53+
try {
54+
// For more information about context object refer "Context" section below
55+
let response = await fetch(context.request, context.options);
56+
// Set the response back in the context
57+
context.response = response;
58+
} catch (error) {
59+
throw error;
60+
}
61+
}
62+
}
63+
```
64+
65+
### Create Middleware Chain
66+
67+
Can use own middlewares and the ones shipped with the library [[Here](../src/middleware) are the set of Middlwares shipped with the library] to create the middleware chain. Create a chain out of these one has to link them in sequentially manner in own preferred order using `.setNext()` method.
68+
69+
Using AuthenticationHandler [one shipped with the library] and MyLoggingHandler, and MyHttpMessageHandler [custom ones] to create a middleware chain here.
70+
71+
NOTE: Instead of MSALAuthenticationProvider, one can provide his own Authentication Handler. For more about using custom authentication provider, refer [here](./CustomAuthenticationProvider.md).
72+
73+
```typescript
74+
import { MSALAuthenticationProvider } from "@microsoft/microsoft-graph-client";
75+
import { MyLoggingHandler } from "./MyLoggingHandler";
76+
import { MyHttpMessageHandler } from "./MyHttpMessageHandler";
77+
78+
let authProvider = new MSALAuthenticationProvider("<CLIENT_ID>", ["user.read"]);
79+
let authenticationHandler = new AuthenticationHandler(authProvider);
80+
let myLoggingHandler = new MyLoggingHandler();
81+
let myHttpMessageHandler = new MyHttpMessageHandler();
82+
83+
// Note: myHttpMessageHandler is the last in the chain so there is no need of setting next middleware for it.
84+
authenticationHandler.setNext(myLoggingHandler);
85+
myLoggingHandler.setNext(myHttpMessageHandler);
86+
```
87+
88+
### Initialize Client
89+
90+
Pass first middleware in the chain for initializing the client.
91+
92+
```typescript
93+
let clientOptions: ClientOptions = {
94+
middleware: authenticationHandler
95+
};
96+
const client = new Client(clientOptions);
97+
```
98+
99+
## Passing Options for Middleware
100+
101+
One can pass any middleware specific options or data while initializing the client, this will be available in the `context.middlewareOptions`.
102+
103+
```typescript
104+
let clientOptions: ClientOptions = {
105+
middleware: authenticationHandler,
106+
middlewareOptions: {
107+
loggingPrefix: "MSGraph-Client-Library"
108+
}
109+
}
110+
```
111+
112+
The above middlewareOptions object will be available in the context object that is being passed to the execute method of a middleware.
113+
114+
```typescript
115+
// MyLoggingHandler.ts
116+
import { Middleware } from "@microsoft/microsoft-graph-client";
117+
import { Context } from "@microsoft/microsoft-graph-client";
118+
119+
export class MyLoggingHandler implements Middleware {
120+
121+
private nextMiddleware: Middleware;
122+
123+
public async execute(context: Context): Promise<void> {
124+
try {
125+
let url: string;
126+
if (typeof context.request === "string") {
127+
url = context.request;
128+
} else {
129+
url = context.request.url;
130+
}
131+
if (context.middlewareOptions !== undefined && context.middlewareOptions.loggingPrefix !== undefined) {
132+
console.log(`${context.middlewareOptions.loggingPrefix}: ${url}`);
133+
} else {
134+
console.log(url);
135+
}
136+
await this.nextMiddleware.execute(context);
137+
} catch(error) {
138+
throw error;
139+
}
140+
}
141+
142+
public setNext(next: Middleware): void {
143+
this.nextMiddleware = next;
144+
}
145+
}
146+
```
147+
148+
Refer [MiddlewareOptions](../src/IMiddlewareOptions.ts) interface to know its structure.

docs/GettingRawResponse.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Getting Raw Response
2+
3+
Steps for getting the raw response [i.e [Response Object](https://developer.mozilla.org/en-US/docs/Web/API/Response)]
4+
5+
**Initialize the Client**
6+
7+
```typescript
8+
const options = {
9+
authProvider: yourAuthProvider
10+
};
11+
const client = MicrosoftGraph.Client.init(options);
12+
```
13+
14+
**Save request to get the raw response before calling action**
15+
16+
The graph request will be returned for all the method calls except for actions, because they makes call to the server, so have to save the copy of graph request instance before calling actions
17+
18+
```typescript
19+
const graphRequest = client.api("/me").select("displayName")
20+
let response = await graphRequest.get();
21+
```
22+
23+
**Get the raw response**
24+
25+
Use `.getRawResponse()` method to get the raw response
26+
27+
```typescript
28+
let rawResponse = graphRequest.getRawResponse();
29+
console.log(rawResponse);
30+
```

lib/graph-js-sdk-core.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/graph-js-sdk-web.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
"use strict";
21
/**
32
* -------------------------------------------------------------------------------------------
43
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
54
* See License in the project root for license information.
65
* -------------------------------------------------------------------------------------------
76
*/
8-
Object.defineProperty(exports, "__esModule", { value: true });
9-
//# sourceMappingURL=IGraphError.js.map
7+
export {};

0 commit comments

Comments
 (0)