@bbc/consumer-contracts
Advanced tools
Comparing version 4.3.6 to 4.4.0
@@ -58,4 +58,14 @@ import Joi, { ValidationOptions } from "joi"; | ||
consumer: string; | ||
/** | ||
* Supports old style setup functions such as those used in conjunction with the now retired \@ibl/request library. | ||
* @deprecated use asyncBefore instead | ||
*/ | ||
before?: (callback: (val: any) => void) => void; | ||
/** | ||
* Supports old tear-down setup functions such as those used in conjunction with the now retired \@ibl/request library. | ||
* @deprecated use asyncAfter instead | ||
*/ | ||
after?: (callback: (val: any) => void) => void; | ||
asyncBefore?: () => Promise<unknown>; | ||
asyncAfter?: () => Promise<unknown>; | ||
retries?: number; | ||
@@ -80,4 +90,6 @@ retryDelay?: number; | ||
consumer: string; | ||
before?: (callback: (val: any) => void) => void; | ||
after?: (callback: (val: any) => void) => void; | ||
before?: ContractOptions['before']; | ||
after?: ContractOptions['after']; | ||
asyncBefore?: ContractOptions['asyncBefore']; | ||
asyncAfter?: ContractOptions['asyncAfter']; | ||
retries: number; | ||
@@ -84,0 +96,0 @@ retryDelay: number; |
@@ -110,2 +110,11 @@ "use strict"; | ||
} | ||
if (this.asyncBefore) { | ||
try { | ||
await this.asyncBefore(); | ||
} | ||
catch (error) { | ||
callback(error, undefined); | ||
return; | ||
} | ||
} | ||
try { | ||
@@ -159,2 +168,11 @@ let retries = this.retries; | ||
} | ||
if (this.asyncAfter) { | ||
try { | ||
await this.asyncAfter(); | ||
} | ||
catch (error) { | ||
callback(error, undefined); | ||
return; | ||
} | ||
} | ||
callback(undefined, schemaValidationResult); | ||
@@ -161,0 +179,0 @@ } |
{ | ||
"name": "@bbc/consumer-contracts", | ||
"version": "4.3.5", | ||
"version": "4.3.6", | ||
"description": "Consumer driven contracts for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -30,2 +30,9 @@ import { createBuilder } from "@bbc/http-transport"; | ||
export function assertIsAsyncBeforeAfter(fn: BeforeAfterFn): asserts fn is () => Promise<void> { | ||
if (fn.length !== 0) { | ||
throw new Error('Asynchronous before functions do not accept callbacks!') | ||
} | ||
} | ||
export type TransformedResponse = { | ||
@@ -94,2 +101,4 @@ url: string; | ||
type BeforeAfterFn = (() => Promise<unknown>) | ((callback: (val: any) => void) => void); | ||
export type ContractOptions = { | ||
@@ -100,4 +109,4 @@ request: ContractRequest; | ||
consumer: string; | ||
before?: (callback: (val: any) => void) => void; | ||
after?: (callback: (val: any) => void) => void; | ||
before?: BeforeAfterFn; | ||
after?: BeforeAfterFn; | ||
retries?: number; | ||
@@ -123,4 +132,4 @@ retryDelay?: number; | ||
consumer: string; | ||
before?: (callback: (val: any) => void) => void; | ||
after?: (callback: (val: any) => void) => void; | ||
before?: ContractOptions['before']; | ||
after?: ContractOptions['after']; | ||
retries: number; | ||
@@ -205,8 +214,21 @@ retryDelay: number; | ||
if (this.before) { | ||
let beforeHasErrored = false; | ||
this.before((val) => { | ||
callback(val, undefined); | ||
if (val instanceof Error) beforeHasErrored = true; | ||
}); | ||
if (beforeHasErrored) return; | ||
if (this.before.constructor.name === 'AsyncFunction') { | ||
// Promise-based before | ||
assertIsAsyncBeforeAfter(this.before); | ||
try { | ||
await this.before(); | ||
} catch (error) { | ||
callback(error, undefined); | ||
return; | ||
} | ||
} else { | ||
// Old-style callback before | ||
let beforeHasErrored = false; | ||
this.before((val) => { | ||
callback(val, undefined); | ||
if (val instanceof Error) beforeHasErrored = true; | ||
}); | ||
if (beforeHasErrored) return; | ||
} | ||
} | ||
@@ -255,8 +277,21 @@ | ||
if (this.after) { | ||
let afterHasErrored = false; | ||
this.after((val) => { | ||
callback(val, undefined); | ||
if (val instanceof Error) afterHasErrored = true; | ||
}); | ||
if (afterHasErrored) return; | ||
if (this.after.constructor.name === 'AsyncFunction') { | ||
// Promise-based after | ||
assertIsAsyncBeforeAfter(this.after); | ||
try { | ||
await this.after(); | ||
} catch (error) { | ||
callback(error, undefined); | ||
return; | ||
} | ||
} else { | ||
// Old-style callback after | ||
let afterHasErrored = false; | ||
this.after((val) => { | ||
callback(val, undefined); | ||
if (val instanceof Error) afterHasErrored = true; | ||
}); | ||
if (afterHasErrored) return; | ||
} | ||
} | ||
@@ -263,0 +298,0 @@ |
{ | ||
"name": "@bbc/consumer-contracts", | ||
"version": "4.3.6", | ||
"version": "4.4.0", | ||
"description": "Consumer driven contracts for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -248,4 +248,6 @@ <h1 align="center"> | ||
If your contract requires some setup (e.g. populating an API with data) you can use the `before` property. It takes a function that will be run before the contract executes. The setup function receives a callback argument that you should call once your setup is complete. | ||
If your contract requires some setup (e.g. populating an API with data) you can use the `before` property. It takes a function that will be run before the contract executes. | ||
You can either pass a function that receives a callback argument which will be called when setup is complete: | ||
```js | ||
@@ -268,6 +270,26 @@ module.exports = new Contract({ | ||
Or pass an asynchronous function that will be awaited on: | ||
```js | ||
module.exports = new Contract({ | ||
name: "Contract name", | ||
consumer: "Consumer name", | ||
before: async function () { | ||
// setup | ||
}, | ||
request: { | ||
// ... | ||
}, | ||
response: { | ||
// ... | ||
}, | ||
}); | ||
``` | ||
### `after` _optional_ | ||
If your contract requires some cleanup you can use the `after` property. It takes a function that will be run after the contract executes. The after function receives a callback argument that you should call once your cleanup is complete. | ||
If your contract requires some cleanup you can use the `after` property. It takes a function that will be run after the contract executes. | ||
You can either pass a function that receives a callback argument which will be called when cleanup is complete: | ||
```js | ||
@@ -290,2 +312,20 @@ module.exports = new Contract({ | ||
Or pass an asynchronous function that will be awaited on: | ||
```js | ||
module.exports = new Contract({ | ||
name: "Contract name", | ||
consumer: "Consumer name", | ||
request: { | ||
// ... | ||
}, | ||
response: { | ||
// ... | ||
}, | ||
after: async function () { | ||
// cleanup | ||
}, | ||
}); | ||
``` | ||
### `joiOptions` _optional_ | ||
@@ -292,0 +332,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
63699
36
1058
441
1