-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathroutes.ts
65 lines (50 loc) · 1.63 KB
/
routes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import _ from 'lodash'
import UrlParse from 'url-parse'
const app_config = require('../../config/app.json')
export const apiUrl = app_config[process.env.CYPRESS_CONFIG_ENV || process.env.CYPRESS_INTERNAL_ENV || 'development'].api_url
const CLOUD_ENDPOINTS = {
api: '',
auth: 'auth',
ping: 'ping',
runs: 'runs',
instances: 'runs/:id/instances',
instanceTests: 'instances/:id/tests',
instanceResults: 'instances/:id/results',
instanceStdout: 'instances/:id/stdout',
instanceArtifacts: 'instances/:id/artifacts',
captureProtocolErrors: 'capture-protocol/errors',
studio: 'studio/bundle/current.tgz',
studioErrors: 'studio/errors',
exceptions: 'exceptions',
telemetry: 'telemetry',
} as const
const parseArgs = function (url, args: any[] = []) {
_.each(args, (value) => {
if (_.isObject(value)) {
url.set('query', _.extend(url.query, value))
return
}
if (_.isString(value) || _.isNumber(value)) {
url.set('pathname', url.pathname.replace(':id', value))
return
}
})
return url
}
const _makeRoutes = (baseUrl: string, routes: typeof CLOUD_ENDPOINTS) => {
return _.reduce(routes, (memo, value, key) => {
memo[key] = function (...args: any[]) {
let url = new UrlParse(baseUrl, true)
if (value) {
url.set('pathname', value)
}
if (args.length) {
url = parseArgs(url, args)
}
return url.toString()
}
return memo
}, {} as Record<keyof typeof CLOUD_ENDPOINTS, (...args: any[]) => string>)
}
export const apiRoutes = _makeRoutes(apiUrl, CLOUD_ENDPOINTS)
export const makeRoutes = (baseUrl) => _makeRoutes(baseUrl, CLOUD_ENDPOINTS)