From 19d7743b1de46bb0e41c54ad2fecb5a1920b4ec9 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 22 Oct 2018 17:41:49 +0200 Subject: [PATCH] update main/mixer | create api common interface --- src/api/interface.ts | 28 +++++++++++++++++++++++++ src/main.ts | 4 +++- src/mixer.ts | 50 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 src/api/interface.ts diff --git a/src/api/interface.ts b/src/api/interface.ts new file mode 100644 index 0000000..173e29f --- /dev/null +++ b/src/api/interface.ts @@ -0,0 +1,28 @@ +export default interface Client { + + + /** Processes an API call to the given resource and returns a Promise with the + * error description OR the result data as JSON + * + * @param method the HTTP METHOD to use + * @param path The API path + * @param params The API-specific options + */ + call(method: HTTP_METHOD, path: String, params?: Parameters) : Promise; + +} + + +// Defines HTTP methods subset +export enum HTTP_METHOD { + GET, + POST, + PUT, + DELETE +} + +export class Parameters{ + get: object; + post: object; + constructor(){ this.get = {}; this.post = {}; } +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 0106471..128a7cd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { createServer } from 'http' import { Socket } from 'net'; import Mixer from './mixer' +import CarrefourItems from './api/items'; const mixer: Mixer = new Mixer(); @@ -10,9 +11,10 @@ const mixer: Mixer = new Mixer(); const server = createServer(mixer.http_handler); // 2. bind 404 error -server.on('clientError', (err: Error, sock: Socket) => { +server.on('error', (err: Error, sock: Socket) => { sock.end('HTTP/1.1 400 Bad Request\r\n\r\n'); }); // 3. listen on given port server.listen(8000); +console.log('+ Listen :8080') \ No newline at end of file diff --git a/src/mixer.ts b/src/mixer.ts index 031cc69..b454d6f 100644 --- a/src/mixer.ts +++ b/src/mixer.ts @@ -1,24 +1,68 @@ import { IncomingMessage, ServerResponse, Server } from "http"; +import CarrefourItems from "./api/items"; +import { parse as parseURL, UrlWithParsedQuery } from "url"; +import { ParsedUrlQuery } from "querystring"; export default class Mixer { + static valid_urls = ["/", "/oauth"]; + + carrefour: CarrefourItems = new CarrefourItems(); + constructor(){ - console.log("mixer created"); + console.log("Mixer Initialised"); } http_handler(req: IncomingMessage, res: ServerResponse){ + let urlObj : UrlWithParsedQuery = parseURL(req.url,true) + // reject invalid requests if( req.method != 'POST' ){ res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({reason: 'invalid HTTP method; only POST allowed'})) - }else if( req.url != "/" ){ + return; + }else if( Mixer.valid_urls.indexOf(urlObj.pathname) < 0 ){ res.writeHead(200, { 'Content-Type': 'application/json' }) - res.end(JSON.stringify({reason: 'invalid URI; only / allowed'})) + res.end(JSON.stringify({reason: `invalid URI; only [${Mixer.valid_urls}] allowed`})) + return; } + // if query + if( urlObj.pathname == "/" ){ + let err:Error = Mixer.query(req, res); + if( err != null ){ + res.writeHead(200, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({reason: `query error: ${err.toString()}`})) + } + + // if oauth + } else { + let err:Error = Mixer.oauth(req, res); + if( err != null ){ + res.writeHead(200, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({reason: `query error: ${err.toString()}`})) + } + } res.end(); } + + static query(req: IncomingMessage, res: ServerResponse) : Error{ + + let query: ParsedUrlQuery = parseURL(req.url,true).query + console.log('query is:', query['q']) + + return null; + + } + + static oauth(req: IncomingMessage, res: ServerResponse) : Error{ + console.log(`oauth(${req},${res})`); + let query: ParsedUrlQuery = parseURL(req.url,true).query + console.log('query is:', query); + return new Error('OAuth error'); + } + } \ No newline at end of file