@rahul_tripathi/galaxy
Advanced tools
Comparing version 1.0.0-pub to 1.0.1-beta
77
index.js
@@ -0,1 +1,2 @@ | ||
const GALAXY_VERSION = 'v1.0.1-beta' | ||
module.exports.Galaxy = function Galaxy(pathname) { | ||
@@ -56,4 +57,4 @@ return new Object({ | ||
obj.path = this.path + '/' + obj.path | ||
if(obj.type === 'Star'){ | ||
obj.depth.forEach(child=>{ | ||
if (obj.type === 'Star') { | ||
obj.depth.forEach(child => { | ||
child.path = this.path + '/' + child.path | ||
@@ -67,25 +68,49 @@ }) | ||
} | ||
const responses = { | ||
'404': { | ||
statusCode: 404, | ||
headers: {}, | ||
body: "Path Not Found (Galaxy " + GALAXY_VERSION + ")" | ||
}, | ||
'500': { | ||
statusCode: 500, | ||
headers: {}, | ||
body: "Internal Server Error (Galaxy " + GALAXY_VERSION + ")" | ||
} | ||
} | ||
const buildErr = (event, code) => { | ||
let res = responses[code] | ||
res.headers = { | ||
...res.headers, | ||
...event.headers || {} | ||
} | ||
return new Promise((resolve) => { | ||
resolve(res) | ||
}) | ||
} | ||
function resolver(event) { | ||
if (event.path.match(this.regex)) { | ||
if (this.depth.length > 0) { | ||
for (i in this.depth) { | ||
let local = this.depth[i] | ||
if (event.path.match(local.regex)) { | ||
if (local.type != 'Moon') { | ||
return local.resolve(event) | ||
} else { | ||
if (local.handler) { | ||
if (local.httpMethod === event.httpMethod) { | ||
let params = {} | ||
let ids = local.path.split('/') | ||
let rec = event.path.split('/') | ||
rec.shift() | ||
for (i in ids) { | ||
if (ids[i][0] == '$') { | ||
params[ids[i].substr(1, ids[i].length)] = rec[i].split('?')[0] | ||
try { | ||
if (event.path.match(this.regex)) { | ||
if (this.depth.length > 0) { | ||
for (i in this.depth) { | ||
let local = this.depth[i] | ||
if (event.path.match(local.regex)) { | ||
if (local.type != 'Moon') { | ||
return local.resolve(event) | ||
} else { | ||
if (local.handler) { | ||
if (local.httpMethod === event.httpMethod) { | ||
let params = {} | ||
let ids = local.path.split('/') | ||
let rec = event.path.split('/') | ||
rec.shift() | ||
for (i in ids) { | ||
if (ids[i][0] == '$') { | ||
params[ids[i].substr(1, ids[i].length)] = rec[i].split('?')[0] | ||
} | ||
} | ||
event['params'] = params | ||
return local.handler(event) | ||
} | ||
event['params'] = params | ||
return local.handler(event) | ||
} | ||
@@ -95,10 +120,12 @@ } | ||
} | ||
return buildErr(event, '404') | ||
} else { | ||
return buildErr(event, '404') | ||
} | ||
return {} | ||
} else { | ||
return {} | ||
return buildErr(event, '404') | ||
} | ||
} else { | ||
return {} | ||
} catch (err) { | ||
return buildErr(event, '500') | ||
} | ||
} |
{ | ||
"name": "@rahul_tripathi/galaxy", | ||
"version": "1.0.0-pub", | ||
"version": "1.0.1-beta", | ||
"description": "tiny api routing library for aws lambda", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ | ||
## Installing Galaxy | ||
$ npm -i @rahul_tripathi/galaxy | ||
$ npm i @rahul_tripathi/galaxy | ||
@@ -11,0 +11,0 @@ ## Using Galaxy |
@@ -5,19 +5,131 @@ let galaxy = require('../index') | ||
//Test handler | ||
handler = (api)=>{ | ||
handler = (api) => { | ||
return api | ||
} | ||
let test1Router = galaxy.Star('test1') | ||
test1Router.append(galaxy.Moon('$id',handler,'POST')) | ||
test1Router.append(galaxy.Moon('$id', handler, 'POST')) | ||
let starRouter = galaxy.Star('streams') | ||
starRouter.append(galaxy.Moon('hello', handler ,'GET')) | ||
apiRouter.append(starRouter,test1Router) | ||
apiRouter.append(galaxy.Moon('hello/$id', handler,'GET')) | ||
apiRouter.append(galaxy.Moon('hello/$kk/mnop', handler,'GET')) | ||
apiRouter.append(galaxy.Moon('helloB', handler,'GET')) | ||
console.log(apiRouter.resolve({httpMethod : 'GET',path :'/api/hello/l'})) | ||
console.log(apiRouter.resolve({httpMethod : 'GET',path :'/api/helloB?abc=k'})) | ||
console.log(apiRouter.resolve({httpMethod : 'GET',path :'/api/hello/abc/mnop?abc=k'})) | ||
console.log(apiRouter.resolve({httpMethod : 'POST',path :'/api/test1/a'})) | ||
console.log(apiRouter.resolve({httpMethod : 'GET',path : '/api/streams/kk' })) | ||
starRouter.append(galaxy.Moon('hello', handler, 'GET')) | ||
apiRouter.append(starRouter, test1Router) | ||
apiRouter.append(galaxy.Moon('hello/$id', handler, 'GET')) | ||
apiRouter.append(galaxy.Moon('hello/$kk/mnop', handler, 'GET')) | ||
apiRouter.append(galaxy.Moon('helloB', handler, 'GET')) | ||
console.log(apiRouter.resolve({ | ||
httpMethod: 'GET', | ||
path: '/api/hello/l' | ||
})) | ||
console.log(apiRouter.resolve({ | ||
httpMethod: 'GET', | ||
path: '/api/helloB?abc=k' | ||
})) | ||
console.log(apiRouter.resolve({ | ||
httpMethod: 'GET', | ||
path: '/api/hello/abc/mnop?abc=k' | ||
})) | ||
console.log(apiRouter.resolve({ | ||
httpMethod: 'POST', | ||
path: '/api/test1/a' | ||
})) | ||
console.log(apiRouter.resolve({ | ||
httpMethod: 'GET', | ||
path: '/api/streams/kk' | ||
})) | ||
//err | ||
console.log(apiRouter.resolve({httpMethod : 'POST',path : '/api/not/found' })) | ||
console.log(apiRouter.resolve({ | ||
"body": "", | ||
"resource": "/{proxy+}", | ||
"path": "/api/not/found", | ||
"httpMethod": "GET", | ||
"isBase64Encoded": true, | ||
"queryStringParameters": { | ||
"foo": "bar" | ||
}, | ||
"multiValueQueryStringParameters": { | ||
"foo": [ | ||
"bar" | ||
] | ||
}, | ||
"pathParameters": { | ||
"proxy": "/api/not/found" | ||
}, | ||
"stageVariables": { | ||
"baz": "qux" | ||
}, | ||
"headers": { | ||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | ||
"Accept-Encoding": "gzip, deflate, sdch", | ||
"Accept-Language": "en-US,en;q=0.8", | ||
"Cache-Control": "max-age=0", | ||
"CloudFront-Forwarded-Proto": "https", | ||
"CloudFront-Is-Desktop-Viewer": "true", | ||
"CloudFront-Is-Mobile-Viewer": "false", | ||
"CloudFront-Is-SmartTV-Viewer": "false", | ||
"CloudFront-Is-Tablet-Viewer": "false", | ||
"CloudFront-Viewer-Country": "US", | ||
"Host": "1234567890.execute-api.us-east-1.amazonaws.com", | ||
"Upgrade-Insecure-Requests": "1", | ||
"User-Agent": "Custom User Agent String", | ||
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", | ||
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", | ||
"X-Forwarded-For": "127.0.0.1, 127.0.0.2", | ||
"X-Forwarded-Port": "443", | ||
"X-Forwarded-Proto": "https" | ||
}, | ||
"multiValueHeaders": { | ||
"Accept": [ | ||
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" | ||
], | ||
"Accept-Encoding": [ | ||
"gzip, deflate, sdch" | ||
], | ||
"Accept-Language": [ | ||
"en-US,en;q=0.8" | ||
], | ||
"Cache-Control": [ | ||
"max-age=0" | ||
], | ||
"CloudFront-Forwarded-Proto": [ | ||
"https" | ||
], | ||
"CloudFront-Is-Desktop-Viewer": [ | ||
"true" | ||
], | ||
"CloudFront-Is-Mobile-Viewer": [ | ||
"false" | ||
], | ||
"CloudFront-Is-SmartTV-Viewer": [ | ||
"false" | ||
], | ||
"CloudFront-Is-Tablet-Viewer": [ | ||
"false" | ||
], | ||
"CloudFront-Viewer-Country": [ | ||
"US" | ||
], | ||
"Host": [ | ||
"0123456789.execute-api.us-east-1.amazonaws.com" | ||
], | ||
"Upgrade-Insecure-Requests": [ | ||
"1" | ||
], | ||
"User-Agent": [ | ||
"Custom User Agent String" | ||
], | ||
"Via": [ | ||
"1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)" | ||
], | ||
"X-Amz-Cf-Id": [ | ||
"cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==" | ||
], | ||
"X-Forwarded-For": [ | ||
"127.0.0.1, 127.0.0.2" | ||
], | ||
"X-Forwarded-Port": [ | ||
"443" | ||
], | ||
"X-Forwarded-Proto": [ | ||
"https" | ||
] | ||
} | ||
})) | ||
console.log(apiRouter.resolve('String')) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9790
255