Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 412aa62

Browse files
Update to allow custom fetch client impl
1 parent 4b90cca commit 412aa62

7 files changed

Lines changed: 23 additions & 65 deletions

File tree

debug/debug.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare var require: any;
22
import pnp from "../src/pnp";
33
import { Logger, LogLevel, ConsoleListener } from "../src/utils/logging";
4+
import { NodeFetchClient } from "../src/net/nodefetchclient";
45

56
// setup the connection to SharePoint using the settings file, you can
67
// override any of the values as you want here, just be sure not to commit
@@ -12,10 +13,8 @@ let settings = require("../../settings.js");
1213

1314
// configure your node options
1415
pnp.setup({
15-
nodeClientOptions: {
16-
clientId: settings.testing.clientId,
17-
clientSecret: settings.testing.clientSecret,
18-
siteUrl: settings.testing.siteUrl
16+
fetchClientFactory: () => {
17+
return new NodeFetchClient(settings.testing.siteUrl, settings.testing.clientId, settings.testing.clientSecret);
1918
}
2019
});
2120

src/configuration/pnplibconfig.ts

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { TypedHash } from "../collections/collections";
2-
3-
declare var global: any;
4-
5-
export interface NodeClientData {
6-
clientId: string;
7-
clientSecret: string;
8-
siteUrl: string;
9-
}
2+
import { HttpClientImpl } from "../net/httpclient";
3+
import { FetchClient } from "../net/fetchclient";
104

115
export interface LibraryConfiguration {
126

@@ -31,14 +25,9 @@ export interface LibraryConfiguration {
3125
defaultCachingTimeoutSeconds?: number;
3226

3327
/**
34-
* If true the SP.RequestExecutor will be used to make the requests, you must include the required external libs
35-
*/
36-
useSPRequestExecutor?: boolean;
37-
38-
/**
39-
* If set the library will use node-fetch, typically for use with testing but works with any valid client id/secret pair
28+
* Defines a factory method used to create fetch clients
4029
*/
41-
nodeClientOptions?: NodeClientData;
30+
fetchClientFactory: () => HttpClientImpl;
4231
}
4332

4433
export class RuntimeConfigImpl {
@@ -47,17 +36,15 @@ export class RuntimeConfigImpl {
4736
private _defaultCachingStore: "session" | "local";
4837
private _defaultCachingTimeoutSeconds: number;
4938
private _globalCacheDisable: boolean;
50-
private _useSPRequestExecutor: boolean;
51-
private _useNodeClient: boolean;
52-
private _nodeClientData: NodeClientData;
39+
private _fetchClientFactory: () => HttpClientImpl;
5340

5441
constructor() {
5542
// these are our default values for the library
5643
this._headers = null;
5744
this._defaultCachingStore = "session";
5845
this._defaultCachingTimeoutSeconds = 30;
5946
this._globalCacheDisable = false;
60-
this._useSPRequestExecutor = false;
47+
this._fetchClientFactory = () => new FetchClient();
6148
}
6249

6350
public set(config: LibraryConfiguration): void {
@@ -78,19 +65,8 @@ export class RuntimeConfigImpl {
7865
this._defaultCachingTimeoutSeconds = config.defaultCachingTimeoutSeconds;
7966
}
8067

81-
if (config.hasOwnProperty("useSPRequestExecutor")) {
82-
this._useSPRequestExecutor = config.useSPRequestExecutor;
83-
}
84-
85-
if (config.hasOwnProperty("nodeClientOptions")) {
86-
this._useNodeClient = true;
87-
this._useSPRequestExecutor = false; // just don't allow this conflict
88-
this._nodeClientData = config.nodeClientOptions;
89-
// this is to help things work when running in node.js, specifically batching
90-
// we shim the _spPageContextInfo object
91-
global._spPageContextInfo = {
92-
webAbsoluteUrl: config.nodeClientOptions.siteUrl,
93-
};
68+
if (config.hasOwnProperty("fetchClientFactory")) {
69+
this._fetchClientFactory = config.fetchClientFactory;
9470
}
9571
}
9672

@@ -110,16 +86,8 @@ export class RuntimeConfigImpl {
11086
return this._globalCacheDisable;
11187
}
11288

113-
public get useSPRequestExecutor(): boolean {
114-
return this._useSPRequestExecutor;
115-
}
116-
117-
public get useNodeFetchClient(): boolean {
118-
return this._useNodeClient;
119-
}
120-
121-
public get nodeRequestOptions(): NodeClientData {
122-
return this._nodeClientData;
89+
public get fetchClientFactory(): () => HttpClientImpl {
90+
return this._fetchClientFactory;
12391
}
12492
}
12593

src/net/httpclient.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { FetchClient } from "./fetchclient";
21
import { DigestCache } from "./digestcache";
32
import { Util } from "../utils/util";
43
import { RuntimeConfig } from "../configuration/pnplibconfig";
5-
import { SPRequestExecutorClient } from "./sprequestexecutorclient";
6-
import { NodeFetchClient } from "./nodefetchclient";
74
import { APIUrlException } from "../utils/exceptions";
85

96
export interface FetchOptions {
@@ -21,7 +18,7 @@ export class HttpClient {
2118
private _impl: HttpClientImpl;
2219

2320
constructor() {
24-
this._impl = this.getFetchImpl();
21+
this._impl = RuntimeConfig.fetchClientFactory();
2522
this._digestCache = new DigestCache(this);
2623
}
2724

@@ -138,17 +135,6 @@ export class HttpClient {
138135
return this.fetch(url, opts);
139136
}
140137

141-
protected getFetchImpl(): HttpClientImpl {
142-
if (RuntimeConfig.useSPRequestExecutor) {
143-
return new SPRequestExecutorClient();
144-
} else if (RuntimeConfig.useNodeFetchClient) {
145-
let opts = RuntimeConfig.nodeRequestOptions;
146-
return new NodeFetchClient(opts.siteUrl, opts.clientId, opts.clientSecret);
147-
} else {
148-
return new FetchClient();
149-
}
150-
}
151-
152138
private mergeHeaders(target: Headers, source: any): void {
153139
if (typeof source !== "undefined" && source !== null) {
154140
let temp = <any>new Request("", { headers: source });

src/net/nodefetchclient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export class NodeFetchClient implements HttpClientImpl {
2929
global.Headers = nodeFetch.Headers;
3030
global.Request = nodeFetch.Request;
3131
global.Response = nodeFetch.Response;
32+
global._spPageContextInfo = {
33+
webAbsoluteUrl: siteUrl,
34+
};
3235
}
3336

3437
public fetch(url: string, options: any): Promise<Response> {

src/types/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export * from "../sharepoint/index";
22
export { FetchOptions, HttpClient } from "../net/httpclient";
3+
export { SPRequestExecutorClient } from "../net/sprequestexecutorclient";
4+
export { NodeFetchClient } from "../net/nodefetchclient";
35
export { IConfigurationProvider } from "../configuration/configuration";
46
export * from "../configuration/providers/index";
5-
export { NodeClientData, LibraryConfiguration } from "../configuration/pnplibconfig";
7+
export { LibraryConfiguration } from "../configuration/pnplibconfig";
68
export { TypedHash, Dictionary } from "../collections/collections";
79
export { Util } from "../utils/util";
810
export * from "../utils/logging";

webpack-serve.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
extensions: ['', '.ts']
1919
},
2020
plugins: [
21-
new webpack.NormalModuleReplacementPlugin(/\.\/nodefetchclient/, "./nodefetchclientbrowser"),
21+
new webpack.NormalModuleReplacementPlugin(/\.\.\/net\/nodefetchclient/, "../net/nodefetchclientbrowser"),
2222
],
2323
module: {
2424
loaders: [

webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = [{
1818
extensions: ['', '.js']
1919
},
2020
plugins: [
21-
new webpack.NormalModuleReplacementPlugin(/\.\/nodefetchclient/, "./nodefetchclientbrowser"),
21+
new webpack.NormalModuleReplacementPlugin(/\.\.\/net\/nodefetchclient/, "../net/nodefetchclientbrowser"),
2222
new webpack.BannerPlugin(config.header, { entryOnly: true, raw: true })
2323
],
2424
module: {
@@ -43,7 +43,7 @@ module.exports = [{
4343
extensions: ['', '.js']
4444
},
4545
plugins: [
46-
new webpack.NormalModuleReplacementPlugin(/\.\/nodefetchclient/, "./nodefetchclientbrowser"),
46+
new webpack.NormalModuleReplacementPlugin(/\.\.\/net\/nodefetchclient/, "../net/nodefetchclientbrowser"),
4747
new webpack.BannerPlugin(config.header, { entryOnly: true, raw: true }),
4848
new webpack.DefinePlugin({
4949
"process.env": {

0 commit comments

Comments
 (0)