-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
customizedClientHeaders.js
97 lines (78 loc) · 2.93 KB
/
customizedClientHeaders.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* You can create your own policy and inject it into the default pipeline, or create your own Pipeline.
* A request policy is a filter triggered before and after a HTTP request. With a filter, we can tweak HTTP requests and responses.
* For example, add a customized header, update URL or create logs. A HTTP pipeline is a group of policy factories.
*
* Here we provide a sample to demonstrate how to customize the x-ms-client-request-id header for all outgoing HTTP requests.
* This sample is just to demo the feature. Feel free to move the classes into one file in your code.
*
* @summary customize request headers such as `X-Ms-Client-Request-Id` using an HTTP policy
**/
const {
newPipeline,
AnonymousCredential,
BlobServiceClient,
BaseRequestPolicy,
} = require("@azure/storage-blob");
// Load the .env file if it exists
require("dotenv").config();
// Create a policy factory with create() method provided
class RequestIDPolicyFactory {
prefix;
// Constructor to accept parameters
constructor(prefix) {
this.prefix = prefix;
}
// create() method needs to create a new RequestIDPolicy object
create(nextPolicy, options) {
return new RequestIDPolicy(nextPolicy, options, this.prefix);
}
}
// Create a policy by extending from BaseRequestPolicy
class RequestIDPolicy extends BaseRequestPolicy {
prefix;
constructor(nextPolicy, options, prefix) {
super(nextPolicy, options);
this.prefix = prefix;
}
// Customize HTTP requests and responses by overriding sendRequest
// Parameter request is WebResource type
async sendRequest(request) {
// Customize client request ID header
request.headers.set(
"x-ms-client-request-id",
`${this.prefix}_SOME_PATTERN_${new Date().getTime()}`
);
// response is HttpOperationResponse type
const response = await this._nextPolicy.sendRequest(request);
// Modify response here if needed
return response;
}
}
// Main function
async function main() {
const account = process.env.ACCOUNT_NAME || "<account name>";
const accountSas = process.env.ACCOUNT_SAS || "<account SAS>";
// Create a default pipeline with newPipeline
const pipeline = newPipeline(new AnonymousCredential());
// Inject customized factory into default pipeline
pipeline.factories.unshift(new RequestIDPolicyFactory("Prefix"));
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net${accountSas}`,
pipeline
);
const result = await blobServiceClient.listContainers().byPage().next();
if (result.done) {
throw new Error("Expected at least one page of containers.");
}
// Extract the raw response from the result.
const { _response } = result.value;
// Check customized client request ID
console.log(_response.request.headers.get("x-ms-client-request-id"));
}
main().catch((error) => {
console.error(error);
process.exit(1);
});