Skip to content

Commit 5f40c0f

Browse files
committed
feat(Tagging): Allow tags to be set by client
1 parent d164453 commit 5f40c0f

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

lib/client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ export interface Config {
6464
* String map specifying default headers
6565
*/
6666
header: Header;
67+
/**
68+
* Append tags to helper requests like `lookupPostcode` and `lookupUDPRN`
69+
*
70+
* Tags attached to the client are overwritten on an request if it is also specified in the helper request options
71+
*/
72+
tags: string[];
6773
}
6874

6975
interface Defaults {
@@ -170,6 +176,7 @@ export class Client {
170176
readonly timeout: number;
171177
readonly agent: Agent;
172178
readonly header: Header;
179+
readonly tags: string[];
173180
readonly postcodes: PostcodeResource;
174181
readonly addresses: AddressResource;
175182
readonly udprn: UdprnResource;
@@ -187,6 +194,7 @@ export class Client {
187194
this.strictAuthorisation = config.strictAuthorisation;
188195
this.timeout = config.timeout;
189196
this.agent = config.agent;
197+
this.tags = config.tags;
190198
this.header = { ...Client.defaults.header, ...config.header };
191199
this.postcodes = createPostcodeResource(this);
192200
this.addresses = createAddressResource(this);
@@ -262,7 +270,7 @@ export class Client {
262270
appendAuthorization({ client: this, header, options });
263271
appendIp({ header, options });
264272
appendFilter({ query, options });
265-
appendTags({ query, options });
273+
appendTags({ client: this, query, options });
266274
appendPage({ query, options });
267275

268276
const queryOptions: Request = { header, query };
@@ -287,7 +295,7 @@ export class Client {
287295
appendAuthorization({ client: this, header, options });
288296
appendIp({ header, options });
289297
appendFilter({ query, options });
290-
appendTags({ query, options });
298+
appendTags({ client: this, query, options });
291299

292300
const request: Request = { header, query };
293301
if (options.timeout !== undefined) request.timeout = options.timeout;

lib/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,20 @@ export const appendFilter = ({
172172
};
173173

174174
interface AppendTagsOptions {
175+
client: Client;
175176
query: StringMap;
176177
options: Taggable;
177178
}
178179

179180
// Adds tags to query
180181
export const appendTags = ({
182+
client,
181183
query,
182184
options,
183185
}: AppendTagsOptions): StringMap => {
184-
const { tags } = options;
186+
let tags: string[] | undefined;
187+
if (client.tags.length) tags = client.tags;
188+
if (options.tags) tags = options.tags;
185189
if (tags !== undefined) query.tags = tags.join(",");
186190
return query;
187191
};

test/helper/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const defaultConfig: Config = Object.freeze({
3131
timeout: TIMEOUT,
3232
agent: new TestAgent(),
3333
header: {},
34+
tags: [],
3435
});
3536

3637
const TEN_SECONDS = 10000;

test/util.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,42 @@ describe("appendFilter", () => {
219219
});
220220

221221
describe("appendTags", () => {
222+
it("appends tags if configured on client", () => {
223+
const tags = ["client"];
224+
const client = new Client({ ...newConfig(), tags });
225+
const query = {};
226+
const options = { tags };
227+
const result = appendTags({ client, query, options });
228+
assert.equal(query, result);
229+
assert.equal(result.tags, "client");
230+
});
231+
232+
it("gives precendence to tags from options", () => {
233+
const tags = ["client"];
234+
const client = new Client({ ...newConfig(), tags });
235+
const query = {};
236+
const queryTags = ["foo", "bar"];
237+
const options = { tags: queryTags };
238+
const result = appendTags({ client, query, options });
239+
assert.equal(query, result);
240+
assert.equal(result.tags, "foo,bar");
241+
});
242+
222243
it("appends tags to query object", () => {
244+
const client = new Client({ ...newConfig() });
223245
const query = {};
224246
const tags = ["foo", "bar"];
225247
const options = { tags };
226-
const result = appendTags({ query, options });
248+
const result = appendTags({ client, query, options });
227249
assert.equal(query, result);
228250
assert.equal(result.tags, "foo,bar");
229251
});
230252

231253
it("does not change headers if left unspecified", () => {
254+
const client = new Client({ ...newConfig() });
232255
const query = {};
233256
const options = {};
234-
const result = appendTags({ query, options });
257+
const result = appendTags({ client, query, options });
235258
assert.deepEqual(result, {});
236259
});
237260
});

0 commit comments

Comments
 (0)