1- import * as utils from './utils'
21import { GraphQLClient , gql } from 'graphql-request'
32import { get } from 'lodash'
4- export const tryParseJson = ( str : string ) => {
5- try {
6- return JSON . parse ( str )
7- } catch {
8- return str
9- }
10- }
113
12-
13- /**
14- * Build image-report url and headers
15- * @param payload
16- */
17- export async function buildUrlHeaders ( payload : Record < string , string | undefined > ) {
18- const esc = encodeURIComponent
19- const headers = { 'authorization' : payload [ 'CF_API_KEY' ] ! }
20- const runtimeName = payload [ 'CF_RUNTIME_NAME' ]
21- let host
22- if ( ! runtimeName ) {
23- host = payload [ 'CF_HOST' ]
24- delete payload [ 'CF_HOST' ]
25- }
26- else {
27- host = await getRuntimeIngressHost ( runtimeName , headers )
28- delete payload [ 'CF_RUNTIME_NAME' ]
29- }
30- delete payload [ 'CF_API_KEY' ]
31- const qs = Object . entries ( payload ) . map ( kv => `${ esc ( kv [ 0 ] ) } =${ esc ( kv [ 1 ] || '' ) } ` ) . join ( '&' )
32- const url = `${ host } /app-proxy/api/image-report?${ qs } `
33- if ( payload [ 'CF_LOCAL' ] ) {
34- return { url : `${ host } /api/image-report?${ qs } ` , headers }
4+ import { errors } from './errors'
5+ export class Utils {
6+ /**
7+ * Build image-report url and headers
8+ * @param payload
9+ */
10+ static async buildUrlHeaders ( payload : Record < string , string | undefined > ) : Promise < { url : string , headers : { authorization : string } } > {
11+ const esc = encodeURIComponent
12+ const headers = { 'authorization' : payload [ 'CF_API_KEY' ] ! }
13+ const runtimeName = payload [ 'CF_RUNTIME_NAME' ]
14+ let host
15+ if ( ! runtimeName ) {
16+ host = payload [ 'CF_HOST' ]
17+ delete payload [ 'CF_HOST' ]
18+ } else {
19+ host = await this . getRuntimeIngressHost ( runtimeName , headers )
20+ delete payload [ 'CF_RUNTIME_NAME' ]
21+ }
22+ delete payload [ 'CF_API_KEY' ]
23+ const qs = Object . entries ( payload ) . map ( kv => `${ esc ( kv [ 0 ] ) } =${ esc ( kv [ 1 ] || '' ) } ` ) . join ( '&' )
24+ const url = `${ host } /app-proxy/api/image-report?${ qs } `
25+ if ( payload [ 'CF_LOCAL' ] ) {
26+ return { url : `${ host } /api/image-report?${ qs } ` , headers }
27+ }
28+ return { url, headers }
3529 }
36- return { url, headers }
37- }
38-
3930
40- export async function getRuntimeIngressHost ( runtimeName : string , headers : Record < string , string > , platformHost = 'https://g.codefresh.io' ) : Promise < string > {
41- const graphQLClient = new GraphQLClient ( `${ platformHost } /2.0/api/graphql` , {
42- headers
43- } )
31+ static async getRuntimeIngressHost ( runtimeName : string , headers : Record < string , string > , platformHost = 'https://g.codefresh.io' ) : Promise < string > {
32+ const graphQLClient = new GraphQLClient ( `${ platformHost } /2.0/api/graphql` , {
33+ headers
34+ } )
4435
45- const getRuntimeIngressHostQuery = gql `
46- query Runtime($name: String!) {
47- runtime(name: $name) {
48- ingressHost
49- }
50- }`
36+ const getRuntimeIngressHostQuery = gql `
37+ query Runtime($name: String!) {
38+ runtime(name: $name) {
39+ ingressHost
40+ }
41+ }`
5142
52- const res = await graphQLClient . request ( getRuntimeIngressHostQuery , { name : runtimeName } )
53- const ingressHost = get ( res , 'runtime.ingressHost' )
54- if ( ! ingressHost ) {
55- const message = res . runtime ? `ingress host is not defined on your '${ runtimeName } ' runtime` : `runtime '${ runtimeName } ' does not exist`
56- throw new errors . ValidationError ( `Validation Error: ${ message } ` )
43+ const res = await graphQLClient . request ( getRuntimeIngressHostQuery , { name : runtimeName } )
44+ const ingressHost = get ( res , 'runtime.ingressHost' )
45+ if ( ! ingressHost ) {
46+ const message = res . runtime ? `ingress host is not defined on your '${ runtimeName } ' runtime` : `runtime '${ runtimeName } ' does not exist`
47+ throw new errors . ValidationError ( `Validation Error: ${ message } ` )
48+ }
49+ return ingressHost
5750 }
58- return ingressHost
59- }
6051
61- export const errors = {
62- EventSourceError : class extends Error {
63- constructor ( message ?: string , name ?: string ) {
64- super ( message )
65- this . name = name || 'EventSourceError'
66- }
67- } ,
68- ValidationError : class extends Error {
69- constructor ( message ?: string , name ?: string ) {
70- super ( message )
71- this . name = name || 'ValidationError'
52+ static tryParseJson = ( str : string ) => {
53+ try {
54+ return JSON . parse ( str )
55+ } catch {
56+ return str
7257 }
7358 }
74- }
59+ }
60+
0 commit comments