Skip to content

Commit 7b2c880

Browse files
authored
Merge pull request #24 from feature/cors
Feature/cors
2 parents 35c9890 + 5975328 commit 7b2c880

7 files changed

Lines changed: 92 additions & 100 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The current version is **v1**
1919
### Endpoints
2020

2121
- /{APP_VERSION}/graphql
22+
- /{APP_VERSION}/download
2223
- /{APP_VERSION}/datastore_search
2324
- /{APP_VERSION}/datastore_search/help
2425

app.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require('dotenv').config()
22

33
var createError = require('http-errors')
44
var express = require('express')
5-
var path = require('path')
5+
var cors = require('cors')
66
var cookieParser = require('cookie-parser')
77
var logger = require('morgan')
88
var proxy = require('express-http-proxy')
@@ -11,17 +11,15 @@ const url = require('url')
1111
var indexRouter = require('./routes/index')
1212

1313
var app = express()
14+
app.use(cors())
1415

1516
app.use(logger('dev'))
1617
app.use(express.json())
1718
app.use(express.urlencoded({ extended: false }))
1819
app.use(cookieParser())
19-
// No need for static dir for now as we're building an API only app
20-
// app.use(express.static(path.join(__dirname, 'public')));
2120

2221
app.use('/', indexRouter)
2322

24-
// TODO: maybe moving redirection out of nodejs
2523
app.use(
2624
'/v1/graphql',
2725
proxy(process.env.HASURA_URL, {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"cookie-parser": "~1.4.4",
14+
"cors": "^2.8.5",
1415
"debug": "~2.6.9",
1516
"dotenv": "^8.2.0",
1617
"express": "~4.16.1",

routes/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const APP_VERSION = 'v1'
1313

1414
/* Gets the schema from a GraphQL*/
1515
async function getGraphQLTableSchema(resource_id) {
16-
// console.log("ResourceID: " + resource_id)
17-
1816
const queryForSchema = gql`
1917
{
2018
__type(name: "${resource_id}") {
@@ -118,10 +116,9 @@ router.get(`/${APP_VERSION}/datastore_search`, async function (req, res, next) {
118116
*
119117
*/
120118

121-
DOWNLOAD_FORMATS_SUPPORTED = ['json', 'csv', 'xlsx', 'ods']
119+
DOWNLOAD_FORMATS_SUPPORTED = ['json', 'csv', 'tsv', 'xlsx', 'ods']
122120

123121
router.post(`/${APP_VERSION}/download`, async function (req, res, next) {
124-
console.log(' Download CALLED')
125122
// get the graphql query from body
126123
const query = req.body.query ? req.body.query : req.body
127124
// call GraphQL
@@ -159,15 +156,17 @@ router.post(`/${APP_VERSION}/download`, async function (req, res, next) {
159156
const ws = XLSX.utils.json_to_sheet(gqlRes[k])
160157
XLSX.utils.book_append_sheet(wb, ws, k)
161158
})
162-
if (ext === 'csv' && colSep != ',') {
159+
if (ext === 'tsv' || (ext === 'csv' && colSep != ',')) {
163160
res.set('Content-Type', 'text/csv')
164161
// only send the first sheet
165162
const sname = wb.SheetNames[0]
166163
const ws = wb.Sheets[sname]
164+
// set TAB separator for TSV format
165+
const sep = ext === 'tsv' ? '\t' : colSep
167166
// TODO deal with record separator
168167
// const recSep = (req.query.record_separator || undefined).trim() // req.params.field_separator ||
169168
// const csv = XLSX.utils.sheet_to_csv(ws, { FS: colSep, RS: recSep })
170-
const csv = XLSX.utils.sheet_to_csv(ws, { FS: colSep })
169+
const csv = XLSX.utils.sheet_to_csv(ws, { FS: sep })
171170
const readable = Readable.from(csv, { encoding: 'utf8' })
172171
for await (const chunk of readable) {
173172
if (!res.write(chunk)) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
"""
4+
This file is for manual testing purposes only.
5+
Usage:
6+
7+
1. modify the base url for the data-api download endpoint to test
8+
2. modify the GraphQL query QUERY
9+
10+
run and check the output
11+
"""
12+
13+
import os, sys
14+
import http
15+
import urllib3
16+
import json
17+
import html
18+
19+
20+
BASE_URL = "http://localhost:3000/v1/download"
21+
22+
QUERY = """
23+
query MyQuery {
24+
test_table(limit: 100) {
25+
float_column
26+
int_column
27+
text_column
28+
time_column
29+
}
30+
}
31+
"""
32+
33+
def check_download(http, base_url, encoded_query, fmt=None, sep=None):
34+
url = base_url
35+
if fmt:
36+
url = url + f'?format={fmt}'
37+
if fmt.lower() == 'csv' and sep:
38+
url = url + f'&field_separator={sep}'
39+
40+
response = http.request('POST', url,
41+
headers={'Content-Type': 'application/json'},
42+
body=encoded_query)
43+
44+
print("URL Called: ", response.geturl())
45+
print("Response HEADERS: ", response.headers)
46+
print("Response INFO: ", response.info())
47+
if fmt.lower() in ['json', 'csv'] or fmt is None:
48+
print(response.data.decode("utf-8"))
49+
50+
51+
def main():
52+
http = urllib3.PoolManager()
53+
encoded_query = json.dumps({"query": QUERY})
54+
# json by default with no param
55+
check_download(http, BASE_URL, encoded_query)
56+
# json specifying the param
57+
check_download(http, BASE_URL, encoded_query, fmt='json')
58+
check_download(http, BASE_URL, encoded_query, fmt='xlsx')
59+
# csv basic
60+
check_download(http, BASE_URL, encoded_query, fmt='csv')
61+
# csv by semicolon
62+
check_download(http, BASE_URL, encoded_query, fmt='csv', sep=';')
63+
# csv by pipe
64+
check_download(http, BASE_URL, encoded_query, fmt='csv', sep='|')
65+
66+
67+
if __name__ == '__main__':
68+
main()

test/test-download.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
<pre>
2-
3-
4-
5-
62
<h3>Manual download file from query test</h3>:
73

84
<button id="b-json" download="dwnl.json">JSON</button>
@@ -21,9 +17,6 @@ <h3>Manual download file from query test</h3>:
2117
</div>
2218
</form>
2319

24-
25-
26-
2720
<script>
2821

2922
const QUERY = `
@@ -57,11 +50,8 @@ <h3>Manual download file from query test</h3>:
5750
link.download = filename;
5851

5952
document.body.appendChild(link);
60-
6153
link.click();
62-
6354
document.body.removeChild(link);
64-
6555
return response.body
6656
} catch(err) {
6757
console.error(`Error: ${err}`)
@@ -70,7 +60,7 @@ <h3>Manual download file from query test</h3>:
7060

7161
document.getElementById('b-json').addEventListener('click', async _ => createListener('/v1/download'))
7262
document.getElementById('b-csv').addEventListener('click', async _ => createListener('/v1/download?format=csv'))
73-
document.getElementById('b-tsv').addEventListener('click', async _ => createListener('/v1/download?format=csv&field_separator=%09'))
63+
document.getElementById('b-tsv').addEventListener('click', async _ => createListener('/v1/download?format=tsv'))
7464
document.getElementById('b-xlsx').addEventListener('click', async _ => createListener('/v1/download?format=xlsx'))
7565

7666
document.getElementById('json').value = QUERY

yarn.lock

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,6 @@ browser-stdout@1.3.1:
146146
resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60"
147147
integrity sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==
148148

149-
buffer-writer@2.0.0:
150-
version "2.0.0"
151-
resolved "https://registry.yarnpkg.com/buffer-writer/-/buffer-writer-2.0.0.tgz#ce7eb81a38f7829db09c873f2fbb792c0c98ec04"
152-
integrity sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==
153-
154149
bytes@3.0.0:
155150
version "3.0.0"
156151
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
@@ -263,11 +258,6 @@ combined-stream@^1.0.6, combined-stream@^1.0.8:
263258
dependencies:
264259
delayed-stream "~1.0.0"
265260

266-
commander@^6.1.0:
267-
version "6.2.0"
268-
resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.0.tgz#b990bfb8ac030aedc6d11bc04d1488ffef56db75"
269-
integrity sha512-zP4jEKbe8SHzKJYQmq8Y9gYjtO/POJLgIdKgV7B9qNmABVFVc+ctqSX6iXh4mCpJfRBOabiZ2YKPg8ciDw6C+Q==
270-
271261
commander@~2.14.1:
272262
version "2.14.1"
273263
resolved "https://registry.yarnpkg.com/commander/-/commander-2.14.1.tgz#2235123e37af8ca3c65df45b026dbd357b01b9aa"
@@ -331,6 +321,14 @@ core-util-is@~1.0.0:
331321
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
332322
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=
333323

324+
cors@^2.8.5:
325+
version "2.8.5"
326+
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
327+
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
328+
dependencies:
329+
object-assign "^4"
330+
vary "^1"
331+
334332
crc-32@~1.2.0:
335333
version "1.2.0"
336334
resolved "https://registry.yarnpkg.com/crc-32/-/crc-32-1.2.0.tgz#cb2db6e29b88508e32d9dd0ec1693e7b41a18208"
@@ -897,20 +895,6 @@ js-yaml@3.14.0:
897895
argparse "^1.0.7"
898896
esprima "^4.0.0"
899897

900-
json2csv@^5.0.3:
901-
version "5.0.3"
902-
resolved "https://registry.yarnpkg.com/json2csv/-/json2csv-5.0.3.tgz#7a819a4c88a4e9f769745200dedc97eab93a4bbe"
903-
integrity sha512-e3gEZU/4fp8CVQMHlwT77RayAR7nylCzCYN7jTIbPTEqk0oTaE8GTcBudLgXrHt4ltOs9SAsbveMJT0YK/QUSg==
904-
dependencies:
905-
commander "^6.1.0"
906-
jsonparse "^1.3.1"
907-
lodash.get "^4.4.2"
908-
909-
jsonparse@^1.3.1:
910-
version "1.3.1"
911-
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
912-
integrity sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=
913-
914898
locate-path@^3.0.0:
915899
version "3.0.0"
916900
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
@@ -926,11 +910,6 @@ locate-path@^6.0.0:
926910
dependencies:
927911
p-locate "^5.0.0"
928912

929-
lodash.get@^4.4.2:
930-
version "4.4.2"
931-
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
932-
integrity sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=
933-
934913
log-symbols@4.0.0:
935914
version "4.0.0"
936915
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-4.0.0.tgz#69b3cc46d20f448eccdb75ea1fa733d9e821c920"
@@ -1049,6 +1028,11 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
10491028
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
10501029
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==
10511030

1031+
object-assign@^4:
1032+
version "4.1.1"
1033+
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
1034+
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
1035+
10521036
object-inspect@^1.7.0:
10531037
version "1.8.0"
10541038
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.8.0.tgz#df807e5ecf53a609cc6bfe93eac3cc7be5b3a9d0"
@@ -1121,11 +1105,6 @@ p-try@^2.0.0:
11211105
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
11221106
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==
11231107

1124-
packet-reader@1.0.0:
1125-
version "1.0.0"
1126-
resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-1.0.0.tgz#9238e5480dedabacfe1fe3f2771063f164157d74"
1127-
integrity sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==
1128-
11291108
parseurl@~1.3.2:
11301109
version "1.3.3"
11311110
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4"
@@ -1161,28 +1140,6 @@ picomatch@^2.0.4, picomatch@^2.2.1:
11611140
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad"
11621141
integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==
11631142

1164-
postgres-array@~2.0.0:
1165-
version "2.0.0"
1166-
resolved "https://registry.yarnpkg.com/postgres-array/-/postgres-array-2.0.0.tgz#48f8fce054fbc69671999329b8834b772652d82e"
1167-
integrity sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==
1168-
1169-
postgres-bytea@~1.0.0:
1170-
version "1.0.0"
1171-
resolved "https://registry.yarnpkg.com/postgres-bytea/-/postgres-bytea-1.0.0.tgz#027b533c0aa890e26d172d47cf9ccecc521acd35"
1172-
integrity sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=
1173-
1174-
postgres-date@~1.0.4:
1175-
version "1.0.7"
1176-
resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.7.tgz#51bc086006005e5061c591cee727f2531bf641a8"
1177-
integrity sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==
1178-
1179-
postgres-interval@^1.1.0:
1180-
version "1.2.0"
1181-
resolved "https://registry.yarnpkg.com/postgres-interval/-/postgres-interval-1.2.0.tgz#b460c82cb1587507788819a06aa0fffdb3544695"
1182-
integrity sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==
1183-
dependencies:
1184-
xtend "^4.0.0"
1185-
11861143
prettier@2.1.1:
11871144
version "2.1.1"
11881145
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.1.tgz#d9485dd5e499daa6cb547023b87a6cf51bee37d6"
@@ -1304,11 +1261,6 @@ safe-buffer@^5.1.0:
13041261
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
13051262
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==
13061263

1307-
semver@4.3.2:
1308-
version "4.3.2"
1309-
resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.2.tgz#c7a07158a80bedd052355b770d82d6640f803be7"
1310-
integrity sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=
1311-
13121264
send@0.16.2:
13131265
version "0.16.2"
13141266
resolved "https://registry.yarnpkg.com/send/-/send-0.16.2.tgz#6ecca1e0f8c156d141597559848df64730a6bbc1"
@@ -1360,13 +1312,6 @@ setprototypeof@1.1.1:
13601312
resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.1.tgz#7e95acb24aa92f5885e0abef5ba131330d4ae683"
13611313
integrity sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==
13621314

1363-
split@^1.0.0:
1364-
version "1.0.1"
1365-
resolved "https://registry.yarnpkg.com/split/-/split-1.0.1.tgz#605bd9be303aa59fb35f9229fbea0ddec9ea07d9"
1366-
integrity sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==
1367-
dependencies:
1368-
through "2"
1369-
13701315
sprintf-js@~1.0.2:
13711316
version "1.0.3"
13721317
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
@@ -1486,11 +1431,6 @@ supports-color@^7.1.0:
14861431
dependencies:
14871432
has-flag "^4.0.0"
14881433

1489-
through@2:
1490-
version "2.3.8"
1491-
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
1492-
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=
1493-
14941434
to-regex-range@^5.0.1:
14951435
version "5.0.1"
14961436
resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"
@@ -1531,7 +1471,7 @@ utils-merge@1.0.1:
15311471
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
15321472
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
15331473

1534-
vary@~1.1.2:
1474+
vary@^1, vary@~1.1.2:
15351475
version "1.1.2"
15361476
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
15371477
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
@@ -1599,11 +1539,6 @@ xlsx@^0.16.8:
15991539
wmf "~1.0.1"
16001540
word "~0.3.0"
16011541

1602-
xtend@^4.0.0:
1603-
version "4.0.2"
1604-
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
1605-
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
1606-
16071542
y18n@^4.0.0:
16081543
version "4.0.0"
16091544
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"

0 commit comments

Comments
 (0)