11import { GraphQLClient , gql } from 'graphql-request'
2+ import { get } from 'lodash'
23export const tryParseJson = ( str : string ) => {
34 try {
45 return JSON . parse ( str )
@@ -15,27 +16,14 @@ export const tryParseJson = (str: string) => {
1516 export async function buildUrlHeaders ( payload : Record < string , string | undefined > ) {
1617 const esc = encodeURIComponent
1718 const headers = { 'authorization' : payload [ 'CF_API_KEY' ] ! }
18- const runtimeName = payload [ 'CF_RUNTIME_NAME' ] as string
19+ const runtimeName = payload [ 'CF_RUNTIME_NAME' ]
1920 let host
2021 if ( ! runtimeName ) {
21- host = payload [ 'CF_HOST' ] as string
22+ host = payload [ 'CF_HOST' ]
2223 delete payload [ 'CF_HOST' ]
2324 }
2425 else {
25- const platformHost = payload [ 'CF_HOST_URL' ] || 'https://g.codefresh.io'
26- const graphQLClient = new GraphQLClient ( `${ platformHost } /2.0/api/graphql` , {
27- headers
28- } )
29-
30- const getRuntimeIngressHostQuery = gql `
31- query Runtime($name: String!) {
32- runtime(name: $name) {
33- ingressHost
34- }
35- }`
36-
37- const res = await graphQLClient . request ( getRuntimeIngressHostQuery , { name : runtimeName } )
38- host = res . runtime . ingressHost as string
26+ host = getRuntimeIngressHost ( runtimeName , headers )
3927 delete payload [ 'CF_RUNTIME_NAME' ]
4028 }
4129 delete payload [ 'CF_API_KEY' ]
@@ -48,6 +36,27 @@ export const tryParseJson = (str: string) => {
4836}
4937
5038
39+ async function getRuntimeIngressHost ( runtimeName : string , headers : Record < string , string > , platformHost = 'https://g.codefresh.io' ) : Promise < string > {
40+ const graphQLClient = new GraphQLClient ( `${ platformHost } /2.0/api/graphql` , {
41+ headers
42+ } )
43+
44+ const getRuntimeIngressHostQuery = gql `
45+ query Runtime($name: String!) {
46+ runtime(name: $name) {
47+ ingressHost
48+ }
49+ }`
50+
51+ const res = await graphQLClient . request ( getRuntimeIngressHostQuery , { name : runtimeName } )
52+ const ingressHost = get ( res , 'runtime.ingressHost' )
53+ if ( ! ingressHost ) {
54+ const message = res . runtime ? `ingress host is not defined on your '${ runtimeName } ' runtime` : `runtime '${ runtimeName } ' does not exist`
55+ throw new errors . ValidationError ( `Validation Error: ${ message } ` )
56+ }
57+ return ingressHost
58+ }
59+
5160export const errors = {
5261 EventSourceError : class extends Error {
5362 constructor ( message ?: string , name ?: string ) {
0 commit comments