Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit acea66a

Browse files
wrumsbybessbd
authored andcommitted
Export BasePlugin (#408)
BasePlugin is not described in the TypeScript types published with this module (i.e. types/index.d.ts) which means that it is difficult to write custom plugins in TypeScript or use VS Code with a jsconfig.json file with "checkJS": true. This commit exposes BasePlugin on the Cloudant factory method so that it can be imported directly from @cloudant/cloudant. This means that custom plugin authors do not need to require @cloudant/cloudant/plugins/base.
1 parent 23b045c commit acea66a

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# UNRELEASED
2+
- [FIXED] Expose BasePlugin.
3+
14
# 4.2.2 (2019-10-23)
25
- [FIXED] Stopped disabling the IAM auth plugin after failed IAM
36
authentications. Subsequent requests will re-request authorization,

cloudant.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var debug = require('debug')('cloudant:cloudant');
2020
var nanodebug = require('debug')('nano');
2121

2222
const Client = require('./lib/client.js');
23+
const BasePlugin = require('./plugins/base.js');
24+
25+
Cloudant.BasePlugin = BasePlugin; // expose base plugin
2326

2427
// Parse an object (i.e. { account: "myaccount", password: "mypassword" }) and
2528
// return a URL.
@@ -81,7 +84,7 @@ function Cloudant(options, callback) {
8184
var nano = Nano(nanoOptions);
8285

8386
nano.cc = cloudantClient; // expose Cloudant client
84-
nano.basePlugin = require('./plugins/base.js'); // expose base plugin
87+
nano.basePlugin = BasePlugin; // expose base plugin on nano
8588

8689
// ===========================
8790
// Cloudant Database Functions

types/index.d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,43 @@ declare namespace cloudant {
211211
params: nano.DocumentViewParams
212212
): Request;
213213
}
214+
215+
// types for plugins
216+
217+
interface PluginConfig {}
218+
interface PluginState {
219+
maxAttempt: number;
220+
}
221+
222+
type PluginCallbackFunction = (state: PluginState) => void;
223+
224+
interface PluginRequest {
225+
method: 'get' | 'post' | 'options';
226+
headers: {},
227+
uri: string;
228+
}
229+
230+
interface PluginPostRequest extends PluginRequest {
231+
method: 'post';
232+
body?: string | {};
233+
}
234+
235+
interface PluginResponse {
236+
request: PluginRequest;
237+
headers: {};
238+
statusCode: number;
239+
}
240+
241+
export class BasePlugin {
242+
public static pluginVersion: number;
243+
public static id: string
244+
public disabled: boolean;
245+
public _cfg: PluginConfig;
246+
public constructor(client: nano.DocumentScope<{}>, config?: PluginConfig)
247+
public onRequest(state: PluginState, request: PluginRequest, callback: PluginCallbackFunction): void
248+
public onResponse(state: PluginState, response: PluginResponse, callback: PluginCallbackFunction): void
249+
public onError(state: PluginState, error: Error, callback: PluginCallbackFunction): void
250+
}
214251
}
215252

216253
export = cloudant;

types/tests.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,68 @@
1616
import cloudant = require('@cloudant/cloudant');
1717
import nano = require('nano');
1818

19+
const { BasePlugin } = cloudant;
20+
21+
interface ICustomPluginConfig extends cloudant.PluginConfig {
22+
customLoggingEnabled?: boolean;
23+
}
24+
25+
class CustomPlugin extends BasePlugin {
26+
public static id = 'custom';
27+
28+
constructor(client: nano.DocumentScope<{}>, configuration: ICustomPluginConfig) {
29+
const cfg = Object.assign({
30+
customLoggingEnabled: true
31+
}, configuration);
32+
33+
super(client, cfg);
34+
}
35+
36+
// tslint:disable-next-line:max-line-length
37+
public onRequest(state: cloudant.PluginState, request: cloudant.PluginRequest, callback: cloudant.PluginCallbackFunction) {
38+
const { customLoggingEnabled } = this._cfg as ICustomPluginConfig;
39+
40+
if (customLoggingEnabled) {
41+
// tslint:disable-next-line:no-console
42+
console.log('%s request made to %s with headers %o', request.method, request.uri, request.headers);
43+
}
44+
45+
callback(state);
46+
}
47+
48+
// tslint:disable-next-line:max-line-length
49+
public onResponse(state: cloudant.PluginState, response: cloudant.PluginResponse, callback: cloudant.PluginCallbackFunction) {
50+
const { customLoggingEnabled } = this._cfg as ICustomPluginConfig;
51+
52+
if (customLoggingEnabled) {
53+
// tslint:disable-next-line
54+
console.log('%d response for %s request made to %s', response.statusCode, response.request.method, response.request.uri);
55+
}
56+
57+
callback(state);
58+
}
59+
60+
// tslint:disable-next-line:max-line-length
61+
public onError(state: cloudant.PluginState, error: Error, callback: cloudant.PluginCallbackFunction) {
62+
const { customLoggingEnabled } = this._cfg as ICustomPluginConfig;
63+
64+
if (customLoggingEnabled) {
65+
// tslint:disable-next-line:no-console
66+
console.error(error);
67+
}
68+
69+
callback(state);
70+
}
71+
}
72+
1973
/*
2074
* Instantiate with configuration object
2175
*/
2276
const config: cloudant.Configuration = {
2377
account: 'my-cloudant-account',
2478
maxAttempt: 3,
2579
password: 'my-password',
26-
plugins: 'retry'
80+
plugins: [CustomPlugin, 'retry']
2781
};
2882

2983
const cfgInstance = cloudant(config);

0 commit comments

Comments
 (0)