Skip to content
This repository was archived by the owner on Feb 25, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/*
coverage/*
dist/*
3,532 changes: 1,647 additions & 1,885 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"test-postdeploy": "mocha --reporter xunit --reporter-options output=./junit/test-results.xml -g 'Post-Deploy'",
"lint": "./node_modules/.bin/eslint .",
"semantic-release": "semantic-release",
"docs": "npx jsdoc2md -c .jsdoc.json --files 'src/*.js' > docs/API.md",
"commit": "git-cz",
"build": "wsk-builder -v",
"deploy": "wsk-builder -v --deploy --test=/_status_check/healthcheck.json",
"deploy-sequences": "wsk-builder --no-build -no-hints -l latest -l major -l minor",
"deploy-ci": "wsk-builder -v --deploy --test=/_status_check/healthcheck.json --pkgVersion=ci$CIRCLE_BUILD_NUM -l ci"
},
"wsk": {
"namespace": "helix",
"name": "helix-services/data-embed@${version}"
},
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion src/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ function embed(url, params, log) {
body: [],
};
}
log.info(`found handler for ${url}: ${matching.name}`);

return matching.extract(url, params);
return matching.extract(url, params, log);
}

module.exports = embed;
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ async function main(params) {
body: 'Expecting a datasource',
};
}

log.info(`data-embed for datasource ${url}`);
const qbquery = loadquerystring(params.__ow_query, 'hlx_');
log.debug('QB query', qbquery);
const filter = createfilter(qbquery);
log.debug('QB filter', filter);
const result = await embed(url.toString(), params, log);

const { body } = result;
delete result.body;
log.debug('result', result);
log.debug(`result body size: ${JSON.stringify(body).length}`);
const filtered = filter(body);
log.info(`filtered result ${filtered.length} rows. size: ${JSON.stringify(filtered).length}`);
return {
...result,
body: filter(result.body),
body: filtered,
};
}

Expand Down
54 changes: 12 additions & 42 deletions src/matchers/excel.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,22 @@ async function extract(url, params, log = console) {
log,
});

const client = await drive.getClient();

const item = await drive.getDriveItemFromShareLink(url);
const worksheetsuri = `/drives/${item.parentReference.driveId}/items/${item.id}/workbook/worksheets/`;
const worksheets = await client.get(worksheetsuri);
const worksheetname = worksheets.value[0].name;
const workbook = drive.getWorkbook(item);

const tablesuri = `${worksheetsuri}${worksheetname}/tables/`;
const tables = await client.get(tablesuri);
const worksheetNames = await workbook.getWorksheetNames();
const worksheetName = worksheetNames[0];
const worksheet = workbook.worksheet(worksheetName);
const tableNames = await worksheet.getTableNames();
const body = await (async () => {
if (!tables.value.length) {
log.info(`worksheet ${worksheetname} has no tables: ${tablesuri}, getting range instead`);

const rangeuri = `${worksheetsuri}${worksheetname}/usedRange`;
const range = await client.get(rangeuri);

const rows = range.values;
const columnames = rows.shift();

const rowvalues = rows.map((row) => columnames.reduce((obj, name, index) => {
// eslint-disable-next-line no-param-reassign
obj[name] = row[index];
return obj;
}, {}));

return rowvalues;
if (!tableNames.length) {
log.info(`worksheet ${worksheetName} has no tables, getting range instead`);
return worksheet.usedRange().getRowsAsObjects();
}
const tablename = tables.value[0].name;

const columnsuri = `${tablesuri}${tablename}/columns/`;
const columns = await client.get(columnsuri);

const columnnames = columns.value.map(({ name }) => name);
const rowvalues = columns.value[0].values
.map((_, rownum) => columnnames.reduce((row, name, colnum) => {
const [value] = columns.value[colnum].values[rownum];
// eslint-disable-next-line no-param-reassign
row[name] = value;
return row;
}, {}));

// discard the first row
rowvalues.shift();

return rowvalues;
log.info(`fetching table data for worksheet ${worksheetName} with name ${tableNames[0]}`);
return worksheet.table(tableNames[0]).getRowsAsObjects();
})();

log.info(`returning ${body.length} rows.`);
return {
statusCode: 200,
headers: {
Expand All @@ -95,6 +64,7 @@ async function extract(url, params, log = console) {
}

module.exports = {
name: 'excel',
required: ['AZURE_WORD2MD_CLIENT_ID', 'AZURE_HELIX_USER', 'AZURE_HELIX_PASSWORD'],
pattern: (url) => /^https:\/\/.*\.sharepoint\.com\//.test(url),
extract,
Expand Down
1 change: 1 addition & 0 deletions src/matchers/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Parser = require('rss-parser');
const parser = new Parser();

module.exports = {
name: 'feed',
required: [],
pattern: (url) => {
if (/\/feeds\/|[&?]feed=atom/.test(url)) {
Expand Down
1 change: 1 addition & 0 deletions src/matchers/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async function extract(url, params, log = console) {
}

module.exports = {
name: 'google',
required: ['GOOGLE_DOCS2MD_CLIENT_ID', 'GOOGLE_DOCS2MD_CLIENT_SECRET', 'GOOGLE_DOCS2MD_REFRESH_TOKEN'],
pattern: (url) => /^https:\/\/docs\.google\.com\/spreadsheets\/d\/.*/.test(url),
extract,
Expand Down
1 change: 1 addition & 0 deletions src/matchers/run-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async function extract(url, params, log = console) {
}

module.exports = {
name: 'run-query',
required: [],
pattern: (url) => /(^https:\/\/adobeioruntime\.net\/api\/v1\/web\/helix\/helix-services\/run-query@.*)/.test(url)
|| /^\/?_query\/run-query\/.*$/.test(new URL(url).pathname),
Expand Down
107 changes: 27 additions & 80 deletions test/excel.test.js

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions test/fixtures/book-with-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const data = [
['Country', 'Code', 'Number'],
['Japan', 'JP', 3],
['Germany', 'DE', 5],
['USA', 'US', 7],
['Switzerland', 'CH', 27],
['France', 'FR', 99],
];

const tables = [{
name: 'table',
headerNames: data[0],
rows: data.slice(1),
}];

const namedItems = [];

module.exports = {
name: 'book-with-tables',
tables,
sheets: [{
name: 'Sheet1',
tables,
namedItems,
usedRange: {
address: 'Sheet1!A1:B4',
addressLocal: 'A1:B4',
values: data,
},
}],
namedItems,
};
39 changes: 39 additions & 0 deletions test/fixtures/book-without-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const data = [
['Country', 'Code', 'Number'],
['Japan', 'JP', 3],
['Germany', 'DE', 5],
['USA', 'US', 7],
['Switzerland', 'CH', 27],
['France', 'FR', 99],
];

const tables = [];

const namedItems = [];

module.exports = {
name: 'book-with-tables',
tables,
sheets: [{
name: 'Sheet1',
tables,
namedItems,
usedRange: {
address: 'Sheet1!A1:B4',
addressLocal: 'A1:B4',
values: data,
},
}],
namedItems,
};
27 changes: 27 additions & 0 deletions test/fixtures/example-data-sheet1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"Country": "Japan",
"Code": "JP",
"Number": 3
},
{
"Country": "Germany",
"Code": "DE",
"Number": 5
},
{
"Country": "USA",
"Code": "US",
"Number": 7
},
{
"Country": "Switzerland",
"Code": "CH",
"Number": 27
},
{
"Country": "France",
"Code": "FR",
"Number": 99
}
]