update main/mixer | create api common interface
This commit is contained in:
parent
da6edab899
commit
19d7743b1d
|
@ -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<object|Error>;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Defines HTTP methods subset
|
||||||
|
export enum HTTP_METHOD {
|
||||||
|
GET,
|
||||||
|
POST,
|
||||||
|
PUT,
|
||||||
|
DELETE
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Parameters{
|
||||||
|
get: object;
|
||||||
|
post: object;
|
||||||
|
constructor(){ this.get = {}; this.post = {}; }
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import { createServer } from 'http'
|
import { createServer } from 'http'
|
||||||
import { Socket } from 'net';
|
import { Socket } from 'net';
|
||||||
import Mixer from './mixer'
|
import Mixer from './mixer'
|
||||||
|
import CarrefourItems from './api/items';
|
||||||
|
|
||||||
|
|
||||||
const mixer: Mixer = new Mixer();
|
const mixer: Mixer = new Mixer();
|
||||||
|
@ -10,9 +11,10 @@ const mixer: Mixer = new Mixer();
|
||||||
const server = createServer(mixer.http_handler);
|
const server = createServer(mixer.http_handler);
|
||||||
|
|
||||||
// 2. bind 404 error
|
// 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');
|
sock.end('HTTP/1.1 400 Bad Request\r\n\r\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
// 3. listen on given port
|
// 3. listen on given port
|
||||||
server.listen(8000);
|
server.listen(8000);
|
||||||
|
console.log('+ Listen :8080')
|
50
src/mixer.ts
50
src/mixer.ts
|
@ -1,24 +1,68 @@
|
||||||
import { IncomingMessage, ServerResponse, Server } from "http";
|
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 {
|
export default class Mixer {
|
||||||
|
|
||||||
|
static valid_urls = ["/", "/oauth"];
|
||||||
|
|
||||||
|
carrefour: CarrefourItems = new CarrefourItems();
|
||||||
|
|
||||||
constructor(){
|
constructor(){
|
||||||
console.log("mixer created");
|
console.log("Mixer Initialised");
|
||||||
}
|
}
|
||||||
|
|
||||||
http_handler(req: IncomingMessage, res: ServerResponse){
|
http_handler(req: IncomingMessage, res: ServerResponse){
|
||||||
|
|
||||||
|
let urlObj : UrlWithParsedQuery = parseURL(req.url,true)
|
||||||
|
|
||||||
// reject invalid requests
|
// reject invalid requests
|
||||||
if( req.method != 'POST' ){
|
if( req.method != 'POST' ){
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||||
res.end(JSON.stringify({reason: 'invalid HTTP method; only POST allowed'}))
|
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.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();
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue