From 71085f0a1afaf17175504a4437421125e3e3621a Mon Sep 17 00:00:00 2001 From: Cleve Stuart <90649124+cleve-fauna@users.noreply.github.com> Date: Tue, 29 Mar 2022 17:31:58 -0700 Subject: [PATCH] Fix backward compatibility bug introduced in 4.5.3 that causes query to return a differnt type (#633) * Fix backward compatibility bug introduced in 4.5.3 that causes query to return a differnt type * Update CHANGELOG.md * remove metrics from queryoptions type * Fix client test with invalid metrics flag * Fix test * Fix test * Deprecate 4.5.3 --- CHANGELOG.md | 3 +++ README.md | 9 +++++---- concourse/scripts/publish.sh | 1 + package-lock.json | 4 ++-- package.json | 2 +- src/Client.js | 11 +++-------- src/types/Client.d.ts | 4 ++-- test/client.test.js | 24 ++++++++++-------------- 8 files changed, 27 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09a209f1c..7fe27aa60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 4.5.4 +- Disable ability to configure a client and the query method from returning metrics when calling query - fixing bug introduced in 4.5.3 that breaks backward compatibility. Continue supporting queryWithMetrics. [#633](https://github.com/fauna/faunadb-js/pull/633). + ## 4.5.3 - Enable the client to return metrics on queries [#625](https://github.com/fauna/faunadb-js/pull/625) [#628](https://github.com/fauna/faunadb-js/pull/628) diff --git a/README.md b/README.md index 38c0167f7..561bdccd8 100644 --- a/README.md +++ b/README.md @@ -137,10 +137,11 @@ let client = new faunadb.Client({ }) ``` -The `response` object is shaped differently for clients when the `metrics` option -is/not set. The default client setting returns the `response` body at root level. -When the client is configured to return `metrics`, the `response` object is -structured as follows: +#### Querying and Returning the metrics of your queries + +The `response` object is shaped differently for clients when calling `queryWithMetrics`; +it includes the value of the response along with a metrics field giving data on ops, +time, and transaction retires consumed by your query: ```javascript { diff --git a/concourse/scripts/publish.sh b/concourse/scripts/publish.sh index 1521bed4b..ad32416ea 100755 --- a/concourse/scripts/publish.sh +++ b/concourse/scripts/publish.sh @@ -19,6 +19,7 @@ then echo "Publishing a new version..." echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc npm publish + npm deprecate faunadb-js@4.5.3 "4.5.3 is is deprecated as it contains a bug that changed the type returned by query for typescript users" rm .npmrc echo "faunadb-js@$PACKAGE_VERSION published to npm" > ../slack-message/publish diff --git a/package-lock.json b/package-lock.json index 62dfca363..4b9d1d428 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "faunadb", - "version": "4.5.3", + "version": "4.5.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "faunadb", - "version": "4.5.3", + "version": "4.5.4", "hasInstallScript": true, "license": "MPL-2.0", "dependencies": { diff --git a/package.json b/package.json index daccb2983..92bf44bea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "faunadb", - "version": "4.5.3", + "version": "4.5.4", "apiVersion": "4", "description": "FaunaDB Javascript driver for Node.JS and Browsers", "homepage": "https://fauna.com", diff --git a/src/Client.js b/src/Client.js index d87add6af..0fdcdd486 100644 --- a/src/Client.js +++ b/src/Client.js @@ -184,7 +184,6 @@ function Client(options) { queryTimeout: null, http2SessionIdleTime: http2SessionIdleTime.value, checkNewVersion: false, - metrics: false, }) if (http2SessionIdleTime.shouldOverride) { @@ -194,7 +193,6 @@ function Client(options) { this._observer = options.observer this._http = new http.HttpClient(options) this.stream = stream.StreamAPI(this) - this._globalQueryOptions = { metrics: options.metrics } } /** @@ -298,13 +296,10 @@ Client.prototype.close = function(opts) { * @return {external:Promise} {value, metrics} An object containing the FaunaDB response object and the list of query metrics incurred by the request. */ Client.prototype.queryWithMetrics = function(expression, options) { - options = Object.assign({}, this._globalQueryOptions, options, { - metrics: true, - }) - return this._execute('POST', '', query.wrap(expression), null, options) + return this._execute('POST', '', query.wrap(expression), null, options, true) } -Client.prototype._execute = function(method, path, data, query, options) { +Client.prototype._execute = function(method, path, data, query, options, returnMetrics = false) { query = util.defaults(query, null) if ( @@ -358,7 +353,7 @@ Client.prototype._execute = function(method, path, data, query, options) { 'x-txn-retries', ] - if (options && options.metrics) { + if (returnMetrics) { return { value: responseObject['resource'], metrics: Object.fromEntries(Array.from(Object.entries(response.headers)). diff --git a/src/types/Client.d.ts b/src/types/Client.d.ts index b024c5159..d0d1e3d00 100644 --- a/src/types/Client.d.ts +++ b/src/types/Client.d.ts @@ -31,7 +31,7 @@ export interface QueryOptions extends Partial< Pick< ClientConfig, - 'secret' | 'queryTimeout' | 'fetch' | 'observer' | 'metrics' + 'secret' | 'queryTimeout' | 'fetch' | 'observer' > > { signal?: AbortSignal @@ -62,7 +62,7 @@ interface MetricsResponse { export default class Client { constructor(opts?: ClientConfig) - query(expr: ExprArg, options?: QueryOptions): Promise | Promise> + query(expr: ExprArg, options?: QueryOptions): Promise queryWithMetrics( expr: ExprArg, options?: QueryOptions diff --git a/test/client.test.js b/test/client.test.js index 27f628578..5c6c31d85 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -40,18 +40,18 @@ describe('Client', () => { expect(client._http._baseUrl.endsWith(':0')).toBeFalsy() }) - test('returns query metrics if the metrics flag is set', async () => { - var metricsClient = util.getClient({ metrics: true }) - const response = await metricsClient.query(query.Add(1, 1)) - assertMetric(response.metrics, 'x-compute-ops') - assertMetric(response.metrics, 'x-byte-read-ops') - assertMetric(response.metrics, 'x-byte-write-ops') - assertMetric(response.metrics, 'x-query-time') - assertMetric(response.metrics, 'x-txn-retries') + test('the client does not support a metrics flag', async () => { + expect(() => util.getClient({ metrics: true })).toThrow(new Error('No such option metrics')) }) - test('returns query metrics if using the queryWithMetrics function', async () => { + test('query does not support a metrics flag', async () => { + const response = await client.query(query.Add(1, 1)) + expect(response).toEqual(2) + }) + + test('queryWithMetrics returns the metrics and the response value', async () => { const response = await client.queryWithMetrics(query.Add(1, 1)) + expect(response.value).toEqual(2) assertMetric(response.metrics, 'x-compute-ops') assertMetric(response.metrics, 'x-byte-read-ops') assertMetric(response.metrics, 'x-byte-write-ops') @@ -59,16 +59,12 @@ describe('Client', () => { assertMetric(response.metrics, 'x-txn-retries') }) - test('query metrics response has correct structure', async () => { + test('queryWithMetrics returns the metrics', async () => { const response = await client.queryWithMetrics(query.Add(1, 1)) expect(Object.keys(response).sort()). toEqual(['metrics', 'value']) }) - test('client with metrics returns expected value', async () => { - const response = await client.queryWithMetrics(query.Add(1, 1)) - expect(response.value).toEqual(2) - }) test('paginates', () => { return createDocument().then(function(document) {