Skip to content
This repository was archived by the owner on Feb 25, 2022. It is now read-only.

Commit af87a92

Browse files
author
Marquise Rosier
authored
Merge pull request #44 from adobe/support-big-query
feat(run-query.js): adds helix-run-query support
2 parents 9f87faf + 5c98f7d commit af87a92

File tree

7 files changed

+278
-24
lines changed

7 files changed

+278
-24
lines changed

package-lock.json

Lines changed: 117 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
},
3232
"homepage": "https://github.com/adobe/helix-data-embed#readme",
3333
"dependencies": {
34+
"@adobe/helix-fetch": "1.6.0",
3435
"@adobe/helix-epsagon": "1.3.1",
3536
"@adobe/helix-onedrive-support": "2.3.0",
37+
"@adobe/helix-shared": "7.3.0",
3638
"@adobe/helix-status": "7.1.3",
3739
"@adobe/openwhisk-action-logger": "2.2.0",
3840
"@adobe/openwhisk-action-utils": "4.2.2",

src/embed.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
const feed = require('./matchers/feed');
1313
const excel = require('./matchers/excel');
1414
const google = require('./matchers/google');
15+
// eslint-disable-next-line camelcase
16+
const runQuery = require('./matchers/run-query');
1517

1618
const matchers = [
17-
feed, excel, google,
19+
feed, excel, google, runQuery,
1820
];
1921

2022
function hasParams(list, params) {

src/matchers/run-query.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2020 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
/* eslint-disable camelcase */
13+
const { fetch } = require('@adobe/helix-fetch');
14+
const { utils } = require('@adobe/helix-shared');
15+
16+
async function extract(url, params, log = console) {
17+
const host = 'https://adobeioruntime.net';
18+
const path = '/api/v1/web/helix/helix-services/run-query@v2/';
19+
const query = url.split('/').pop();
20+
const resource = `${host}${path}${query}`;
21+
const DEFAULT_CACHE = 'max-age=600';
22+
23+
const results = await fetch(url.startsWith(host) ? url : resource);
24+
const statusCode = utils.propagateStatusCode(results.status);
25+
const logLevel = utils.logLevelForStatusCode(results.status);
26+
const cacheControl = results.headers.get('cache-control');
27+
28+
try {
29+
if (!results.ok) {
30+
throw new Error(await results.text());
31+
}
32+
return {
33+
statusCode,
34+
headers: {
35+
'Content-Type': 'application/json',
36+
'Cache-Control': cacheControl || DEFAULT_CACHE,
37+
},
38+
body: (await results.json()).results,
39+
};
40+
} catch (e) {
41+
log[logLevel](`data request to ${resource} failed ${e.message}`);
42+
return {
43+
statusCode,
44+
headers: {
45+
'Content-Type': 'application/json',
46+
'Cache-Control': 'max-age=60',
47+
},
48+
body: [],
49+
};
50+
}
51+
}
52+
53+
module.exports = {
54+
required: [],
55+
pattern: (url) => /(^https:\/\/adobeioruntime\.net\/api\/v1\/web\/helix\/helix-services\/run-query@.*)/.test(url)
56+
|| /^\/?_query\/run-query\/.*$/.test(new URL(url).pathname),
57+
extract,
58+
};

0 commit comments

Comments
 (0)