mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Options for plugin API functions which fetch data from the selected table or record
Summary: Adds a new interface `FetchSelectedOptions` with three keys (including the preexisting `keepEncoded`) and adds/updates an optional `options: FetchSelectedOptions` to six related functions which fetch data from the selected table or record. The `keepEncoded` and `format` options have different default values for different methods for backwards compatibility, but otherwise the different methods now have much more similar behaviour. The new `includeColumns` option allows fetching all columns which was previously only possible using `docApi.fetchTable` (which wasn't always a great alternative) but this requires full access to avoid exposing more data than before and violating user expectations. Eventually, similar options should be added to `docApi.fetchTable` to make the API even more consistent. Discussion: https://grist.slack.com/archives/C0234CPPXPA/p1696510548994899 Test Plan: Added a new nbrowser test with a corresponding fixture site and document, showing how the functions have different default option values but are all configurable now. Reviewers: georgegevoian Reviewed By: georgegevoian Differential Revision: https://phab.getgrist.com/D4077
This commit is contained in:
BIN
test/fixtures/docs/FetchSelectedOptions.grist
vendored
Normal file
BIN
test/fixtures/docs/FetchSelectedOptions.grist
vendored
Normal file
Binary file not shown.
11
test/fixtures/sites/fetchSelectedOptions/index.html
vendored
Normal file
11
test/fixtures/sites/fetchSelectedOptions/index.html
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<script src="/grist-plugin-api.js"></script>
|
||||
<script src="page.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>FetchSelectedOptions</h1>
|
||||
<pre id="data"></pre>
|
||||
</body>
|
||||
</html>
|
||||
101
test/fixtures/sites/fetchSelectedOptions/page.js
vendored
Normal file
101
test/fixtures/sites/fetchSelectedOptions/page.js
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
/* global document, grist, window */
|
||||
|
||||
function setup() {
|
||||
const data = {
|
||||
default: {},
|
||||
options: {},
|
||||
};
|
||||
let showCount = 0;
|
||||
|
||||
function showData() {
|
||||
showCount += 1;
|
||||
if (showCount < 12) {
|
||||
return;
|
||||
}
|
||||
document.getElementById('data').innerHTML = JSON.stringify(data, null, 2);
|
||||
}
|
||||
|
||||
grist.onRecord(function (rec) {
|
||||
data.default.onRecord = rec;
|
||||
showData();
|
||||
});
|
||||
grist.onRecords(function (recs) {
|
||||
data.default.onRecords = recs;
|
||||
showData();
|
||||
});
|
||||
grist.fetchSelectedTable().then(function (table) {
|
||||
data.default.fetchSelectedTable = table;
|
||||
showData();
|
||||
});
|
||||
grist.fetchSelectedRecord(1).then(function (rec) {
|
||||
data.default.fetchSelectedRecord = rec;
|
||||
showData();
|
||||
});
|
||||
grist.viewApi.fetchSelectedTable().then(function (table) {
|
||||
data.default.viewApiFetchSelectedTable = table;
|
||||
showData();
|
||||
});
|
||||
grist.viewApi.fetchSelectedRecord(2).then(function (rec) {
|
||||
data.default.viewApiFetchSelectedRecord = rec;
|
||||
showData();
|
||||
});
|
||||
|
||||
try {
|
||||
grist.onRecord(function (rec) {
|
||||
data.options.onRecord = rec;
|
||||
showData();
|
||||
}, {keepEncoded: true, includeColumns: 'normal', format: 'columns'});
|
||||
} catch (e) {
|
||||
data.options.onRecord = String(e);
|
||||
showData();
|
||||
}
|
||||
try {
|
||||
grist.onRecords(function (recs) {
|
||||
data.options.onRecords = recs;
|
||||
showData();
|
||||
}, {keepEncoded: true, includeColumns: 'all', format: 'columns'});
|
||||
} catch (e) {
|
||||
data.options.onRecords = String(e);
|
||||
showData();
|
||||
}
|
||||
grist.fetchSelectedTable(
|
||||
{keepEncoded: true, includeColumns: 'all', format: 'rows'}
|
||||
).then(function (table) {
|
||||
data.options.fetchSelectedTable = table;
|
||||
showData();
|
||||
}).catch(function (err) {
|
||||
data.options.fetchSelectedTable = String(err);
|
||||
showData();
|
||||
});
|
||||
grist.fetchSelectedRecord(1,
|
||||
{keepEncoded: true, includeColumns: 'normal', format: 'rows'}
|
||||
).then(function (rec) {
|
||||
data.options.fetchSelectedRecord = rec;
|
||||
showData();
|
||||
}).catch(function (err) {
|
||||
data.options.fetchSelectedRecord = String(err);
|
||||
showData();
|
||||
});
|
||||
grist.viewApi.fetchSelectedTable(
|
||||
{keepEncoded: false, includeColumns: 'all', format: 'rows'}
|
||||
).then(function (table) {
|
||||
data.options.viewApiFetchSelectedTable = table;
|
||||
showData();
|
||||
}).catch(function (err) {
|
||||
data.options.viewApiFetchSelectedTable = String(err);
|
||||
showData();
|
||||
});
|
||||
grist.viewApi.fetchSelectedRecord(2,
|
||||
{keepEncoded: false, includeColumns: 'normal', format: 'rows'}
|
||||
).then(function (rec) {
|
||||
data.options.viewApiFetchSelectedRecord = rec;
|
||||
showData();
|
||||
}).catch(function (err) {
|
||||
data.options.viewApiFetchSelectedRecord = String(err);
|
||||
showData();
|
||||
});
|
||||
|
||||
grist.ready();
|
||||
}
|
||||
|
||||
window.onload = setup;
|
||||
@@ -530,6 +530,123 @@ describe('CustomView', function() {
|
||||
const opinions = await api.getDocAPI(doc.id).getRows('Opinions');
|
||||
assert.equal(opinions['A'][0], 'do not zap plz');
|
||||
});
|
||||
|
||||
it('allows custom options for fetching data', async function () {
|
||||
const mainSession = await gu.session().teamSite.login();
|
||||
const doc = await mainSession.tempDoc(cleanup, 'FetchSelectedOptions.grist', {load: false});
|
||||
await mainSession.loadDoc(`/doc/${doc.id}`);
|
||||
|
||||
await gu.toggleSidePanel('right', 'open');
|
||||
await gu.getSection('TABLE1 Custom').click();
|
||||
await driver.find('.test-config-widget-url').click();
|
||||
await gu.sendKeys(`${serving.url}/fetchSelectedOptions`, Key.ENTER);
|
||||
await gu.waitForServer();
|
||||
|
||||
const expected = {
|
||||
"default": {
|
||||
"fetchSelectedTable": {
|
||||
"id": [1, 2],
|
||||
"A": [["a", "b"], ["c", "d"]],
|
||||
},
|
||||
"fetchSelectedRecord": {
|
||||
"id": 1,
|
||||
"A": ["a", "b"]
|
||||
},
|
||||
// The viewApi methods don't decode data by default, hence the "L" prefixes.
|
||||
"viewApiFetchSelectedTable": {
|
||||
"id": [1, 2],
|
||||
"A": [["L", "a", "b"], ["L", "c", "d"]],
|
||||
},
|
||||
"viewApiFetchSelectedRecord": {
|
||||
"id": 2,
|
||||
"A": ["L", "c", "d"]
|
||||
},
|
||||
// onRecords returns rows by default, not columns.
|
||||
"onRecords": [
|
||||
{"id": 1, "A": ["a", "b"]},
|
||||
{"id": 2, "A": ["c", "d"]}
|
||||
],
|
||||
"onRecord": {
|
||||
"id": 1,
|
||||
"A": ["a", "b"]
|
||||
},
|
||||
},
|
||||
"options": {
|
||||
// This is the result of calling the same methods as above,
|
||||
// but with the values of `keepEncoded` and `format` being the opposite of their defaults.
|
||||
// `includeColumns` is also set to either 'normal' or 'all' instead of the default 'shown',
|
||||
// which means that the 'B' column is included in all the results,
|
||||
// and the 'manualSort' columns is included in half of them.
|
||||
"fetchSelectedTable": [
|
||||
{"id": 1, "manualSort": 1, "A": ["L", "a", "b"], "B": 1},
|
||||
{"id": 2, "manualSort": 2, "A": ["L", "c", "d"], "B": 2},
|
||||
],
|
||||
"fetchSelectedRecord": {
|
||||
"id": 1,
|
||||
"A": ["L", "a", "b"],
|
||||
"B": 1
|
||||
},
|
||||
"viewApiFetchSelectedTable": [
|
||||
{"id": 1, "manualSort": 1, "A": ["a", "b"], "B": 1},
|
||||
{"id": 2, "manualSort": 2, "A": ["c", "d"], "B": 2}
|
||||
],
|
||||
"viewApiFetchSelectedRecord": {
|
||||
"id": 2,
|
||||
"A": ["c", "d"],
|
||||
"B": 2
|
||||
},
|
||||
"onRecords": {
|
||||
"id": [1, 2],
|
||||
"manualSort": [1, 2],
|
||||
"A": [["L", "a", "b"], ["L", "c", "d"]],
|
||||
"B": [1, 2],
|
||||
},
|
||||
"onRecord": {
|
||||
"id": 1,
|
||||
"A": ["L", "a", "b"],
|
||||
"B": 1
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
async function getData() {
|
||||
await driver.findContentWait('#data', /\{/, 1000);
|
||||
const data = await driver.find('#data').getText();
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
await inFrame(async () => {
|
||||
const parsed = await getData();
|
||||
assert.deepEqual(parsed, expected);
|
||||
});
|
||||
|
||||
// Change the access level away from 'full'.
|
||||
await setAccess("read table");
|
||||
await gu.waitForServer();
|
||||
|
||||
await inFrame(async () => {
|
||||
const parsed = await getData();
|
||||
// The default options don't require full access, so the result is the same.
|
||||
assert.deepEqual(parsed.default, expected.default);
|
||||
|
||||
// The alternative options all set includeColumns to 'normal' or 'all',
|
||||
// which requires full access.
|
||||
assert.deepEqual(parsed.options, {
|
||||
"onRecord":
|
||||
"Error: Access not granted. Current access level read table",
|
||||
"onRecords":
|
||||
"Error: Access not granted. Current access level read table",
|
||||
"fetchSelectedTable":
|
||||
"Error: Access not granted. Current access level read table",
|
||||
"fetchSelectedRecord":
|
||||
"Error: Access not granted. Current access level read table",
|
||||
"viewApiFetchSelectedTable":
|
||||
"Error: Access not granted. Current access level read table",
|
||||
"viewApiFetchSelectedRecord":
|
||||
"Error: Access not granted. Current access level read table"
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
async function inFrame(op: () => Promise<void>) {
|
||||
|
||||
Reference in New Issue
Block a user