diff --git a/.gitignore b/.gitignore index a0c842c..b52ee89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /node_modules /package-lock.json /dist -*.js \ No newline at end of file +*.js +types/request\.d\.ts diff --git a/cnf/api_keys.json b/cnf/api_keys.json new file mode 100644 index 0000000..1a1cdcd --- /dev/null +++ b/cnf/api_keys.json @@ -0,0 +1,6 @@ +{ + "carrefour": { + "app_id": "1e70cf82-6062-4067-8b6c-e5f34c0643b7", + "app_secret": "eO8cF7qL5hX2wG0bT5hY6aV2kM7oE8aT2hT4iE8iP5aK3aT4pH" + } +} \ No newline at end of file diff --git a/src/mixer.ts b/src/mixer.ts index e03a58f..fc9e83b 100644 --- a/src/mixer.ts +++ b/src/mixer.ts @@ -27,18 +27,28 @@ export default class Mixer { async http_handler(req: IncomingMessage, res: ServerResponse){ - let urlObj : UrlWithParsedQuery = parseURL(req.url,true) - printf('\n -- new client --\n') + let urlObj : UrlWithParsedQuery = parseURL(req.url, true) // reject invalid requests - if( req.method != 'POST' ) return Mixer.http_error(res, 'only POST method allowed'); + if( req.method != 'GET' ) return Mixer.http_error(res, 'only GET method allowed'); else if( Mixer.valid_urls.indexOf(urlObj.pathname) < 0 ) return Mixer.http_error(res, `only URI [${Mixer.valid_urls}] allowed`); + printf('\n -- new request --\n') try{ let query_result = await this.query(req, res) - res.writeHead(200, { 'Content-Type': 'application/json' }) - res.end(JSON.stringify( this.format(query_result) )); + + // if accept is set to HTML -> html format + if( req.headers['accept'] && req.headers['accept'].includes("json") ){ + res.writeHead(200, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify( this.format(query_result) )); + + // fallback to JSON by default + } else{ + res.writeHead(200, { 'Content-Type': 'text/html' }) + let query = parseURL(req.url,true).query['q'] + res.end(this.toHTML(query, this.format(query_result)) ); + } }catch(err){ @@ -47,6 +57,8 @@ export default class Mixer { } + printf(' -- response sent --\n') + } @@ -95,4 +107,49 @@ export default class Mixer { } } + + toHTML(request:any, input:any) : string { + let html = ` +
+