-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Carmine
authored and
Carmine
committed
Apr 1, 2019
1 parent
5ce98f2
commit c493001
Showing
11 changed files
with
186 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
sudo: required | ||
language: node_js | ||
node_js: | ||
- '10' | ||
- '10' | ||
before_install: | ||
- openssl aes-256-cbc -K $encrypted_5d8f15d35f17_key -iv $encrypted_5d8f15d35f17_iv | ||
-in secrets.zip.enc -out secrets.zip -d | ||
- unzip secrets.zip | ||
- openssl aes-256-cbc -K $encrypted_5d8f15d35f17_key -iv $encrypted_5d8f15d35f17_iv | ||
-in secrets.zip.enc -out secrets.zip -d | ||
- unzip secrets.zip | ||
install: | ||
- npm install | ||
- npm install | ||
script: | ||
- npm run compile | ||
- npm test | ||
- npm run coveralls | ||
- npm run codacy | ||
- npm run compile | ||
- npm run test:coverage | ||
- npm run coveralls | ||
- npm run codacy | ||
env: | ||
global: | ||
secure: ytiwXpn7JDc0fhB2KQa2z/OM9LNyVqf/5XCg6LX1WtXRn3R6qYfqCMBUq+eu/Pvym5SlPUwPwZEzRwSBUBivAhNsSPMNGnzNVyX+OxVruXlaD+Wsx3ROTGnwTpPEh7JUXXpytWmG6IAXwyhOvnhAgILjDFaRPkRNc/9Y1zsRTtFxiaisN/6X4IFxGRdI6OVobLEDKmnUBa2nDw5fZws+hOGWlv7b1NqbzXJ1yrOTH0E/uPsill3aWqXuDSEtv4VhAk0z9Xyw4Bi28XtDNQNSou8LkGmN4QzXHdKz73UgY3RaKQ99PVhyTZz0TrvL54BSB/poUoXnu3ZZODKA9GIrthnHW6rn3u0ovnHRAA+L2CLBTqFbGoctOe3XhnUcDBmcvH3FnE+IcLdSeeEjDO+oGIZdut2af2aPj+rA7Hd76+I/CGEf8osnfZVuLYYWW+semuTCF4rDSI+EPu8Rb3w3POCzskkinONnxdYQpcEw01ltMBUnJxLi7fYVpLeSNsOs5rBMOL+LlbsnG505UvmqaFlzyt7GzvcCXW0hrBAARjq3ADAmtSBVZuzMWebN2X93wjZZ9SLyVtCdVNtCxCwA3X0zYJ1dH6roSNi99gMDG9fxbb+Hhc/0+9+u7Sn2CMvOLy9LiV+lm0MeANDOitRb6OTLuOKlLLjr8lvvJxf9qwE= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,112 @@ | ||
import * as pathToRegexp from 'path-to-regexp'; | ||
import { OpenAPIV3 } from 'openapi-types'; | ||
import { URL } from 'url'; | ||
|
||
interface ServerUrlVariables { | ||
[key: string]: ServerUrlValues; | ||
} | ||
interface ServerUrlValues { | ||
enum: string[]; | ||
default?: string; | ||
} | ||
|
||
export default class BasePath { | ||
public readonly variables: { [key: string]: { enum: string[] } } = {}; | ||
public readonly variables: ServerUrlVariables = {}; | ||
public readonly path: string = ''; | ||
private allPaths: string[] = null; | ||
|
||
constructor(server: OpenAPIV3.ServerObject) { | ||
// break the url into parts | ||
// baseUrl param added to make the parsing of relative paths go well | ||
const serverUrl = new URL(server.url, 'http://localhost'); | ||
let urlPath = decodeURI(serverUrl.pathname).replace(/\/$/, ''); | ||
let urlPath = this.findUrlPath(server.url); | ||
if (/{\w+}/.test(urlPath)) { | ||
// has variable that we need to check out | ||
urlPath = urlPath.replace(/{(\w+)}/g, (substring, p1) => `:${p1}`); | ||
} | ||
this.path = urlPath; | ||
for (const variable in server.variables) { | ||
if (server.variables.hasOwnProperty(variable)) { | ||
this.variables[variable] = { enum: server.variables[variable].enum }; | ||
const v = server.variables[variable]; | ||
const enums = v.enum || []; | ||
if (enums.length === 0 && v.default) enums.push(v.default); | ||
|
||
this.variables[variable] = { | ||
enum: enums, | ||
default: v.default, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public hasVariables() { | ||
public hasVariables(): boolean { | ||
return Object.keys(this.variables).length > 0; | ||
} | ||
|
||
public all(): string[] { | ||
if (!this.hasVariables()) return [this.path]; | ||
if (this.allPaths) return this.allPaths; | ||
// TODO performance optimization | ||
// ignore variables that are not part of path params | ||
const allParams = Object.entries(this.variables).reduce((acc, v) => { | ||
const [key, value] = v; | ||
const params = value.enum.map(e => ({ | ||
[key]: e, | ||
})); | ||
acc.push(params); | ||
return acc; | ||
}, []); | ||
|
||
const allParamCombos = cartesian(...allParams); | ||
const toPath = pathToRegexp.compile(this.path); | ||
const paths = new Set(); | ||
for (const combo of allParamCombos) { | ||
paths.add(toPath(combo)); | ||
} | ||
this.allPaths = Array.from(paths); | ||
return this.allPaths; | ||
} | ||
|
||
public static fromServers(servers: OpenAPIV3.ServerObject[]) { | ||
if (!servers) { | ||
return [new BasePath({ url: '' })]; | ||
} | ||
return servers.map(server => new BasePath(server)); | ||
} | ||
|
||
private findUrlPath(u) { | ||
const findColonSlashSlash = p => { | ||
const r = /:\/\//.exec(p); | ||
if (r) return r.index; | ||
return -1; | ||
}; | ||
const findFirstSlash = p => { | ||
const r = /\//.exec(p); | ||
if (r) return r.index; | ||
return -1; | ||
}; | ||
|
||
const fcssIdx = findColonSlashSlash(u); | ||
const startSearchIdx = fcssIdx !== -1 ? fcssIdx + 3 : 0; | ||
const startPathIdx = findFirstSlash(u.substring(startSearchIdx)); | ||
if (startPathIdx === -1) return '/'; | ||
|
||
const pathIdx = startPathIdx + startSearchIdx; | ||
return u.substring(pathIdx); | ||
} | ||
} | ||
|
||
function cartesian(...arg) { | ||
const r = [], | ||
max = arg.length - 1; | ||
function helper(obj, i) { | ||
const values = arg[i]; | ||
for (var j = 0, l = values.length; j < l; j++) { | ||
const a = { ...obj }; | ||
const key = Object.keys(values[j])[0]; | ||
a[key] = values[j][key]; | ||
if (i == max) r.push(a); | ||
else helper(a, i + 1); | ||
} | ||
} | ||
helper({}, 0); | ||
return r; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.