Skip to content

Commit

Permalink
PI-1551 Enable setting a custom URL for the back-end API (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-bcl committed Oct 11, 2023
1 parent e1e16b0 commit 5d4278c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
15 changes: 10 additions & 5 deletions packages/probation-search-frontend/data/probationSearchClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import superagent from 'superagent'
import OAuthClient from './oauthClient'
import config, { Environment } from '../config'
import environments, { EnvironmentConfig } from '../environments'

export default class ProbationSearchClient {
constructor(
private oauthClient: OAuthClient,
private dataSource: Environment | ProbationSearchResult[],
private dataSource: 'dev' | 'preprod' | 'prod' | EnvironmentConfig | ProbationSearchResult[],
) {}

async search({
Expand All @@ -20,7 +20,7 @@ export default class ProbationSearchClient {
return Promise.resolve(this.localSearch(this.dataSource, pageNumber, pageSize))
}
const token = await this.oauthClient.getSystemClientToken(asUsername)
const apiConfig = config[this.dataSource].searchApi
const apiConfig = this.getApiConfig(this.dataSource).searchApi
const response = await superagent
.post(`${apiConfig.url}/phrase?page=${pageNumber - 1}&size=${pageSize}}`)
.auth(token, { type: 'bearer' })
Expand All @@ -40,10 +40,15 @@ export default class ProbationSearchClient {
content,
probationAreaAggregations: [],
size: content.length,
totalElements: this.dataSource.length,
totalPages: Math.ceil(this.dataSource.length / size),
totalElements: data.length,
totalPages: Math.ceil(data.length / size),
}
}

private getApiConfig(dataSource: 'dev' | 'preprod' | 'prod' | EnvironmentConfig): EnvironmentConfig {
if (dataSource === 'dev' || dataSource === 'preprod' || dataSource === 'prod') return environments[dataSource]
return dataSource
}
}

export interface ProbationSearchRequest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
export type Environment = 'dev' | 'preprod' | 'prod'
export type Environment = 'local' | 'dev' | 'preprod' | 'prod'
export interface ApiConfig {
url: string
timeout: number
}
export interface EnvironmentConfig {
export type EnvironmentConfig = {
searchApi: ApiConfig
}

const config: { [key in Environment]: EnvironmentConfig } = {
const environments: { [key in Environment]: EnvironmentConfig } = {
local: null,
dev: {
searchApi: { url: 'https://probation-offender-search-dev.hmpps.service.justice.gov.uk', timeout: 5000 },
},
Expand All @@ -19,4 +20,4 @@ const config: { [key in Environment]: EnvironmentConfig } = {
},
}

export default config
export default environments
3 changes: 2 additions & 1 deletion packages/probation-search-frontend/routes/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import getPaginationLinks, { Pagination } from '../utils/pagination'
import getSuggestionLinks, { SuggestionLink } from '../utils/suggestions'
import wrapAsync from '../utils/middleware'
import addParameters from '../utils/url'
import { Environment, EnvironmentConfig } from '../environments'

export default function probationSearchRoutes({
environment,
Expand Down Expand Up @@ -141,7 +142,7 @@ interface PostOptions {
}

export type ProbationSearchRouteOptions = {
environment: 'local' | 'dev' | 'preprod' | 'prod'
environment: Environment | EnvironmentConfig
oauthClient: OAuthClient
router: Router
localData?: ProbationSearchResult[]
Expand Down
15 changes: 14 additions & 1 deletion server/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Environment } from '@ministryofjustice/probation-search-frontend/environments'

const production = process.env.NODE_ENV === 'production'

function get<T>(name: string, fallback: T, options = { requireInProduction: false }): T | string {
Expand Down Expand Up @@ -31,7 +33,7 @@ export interface ApiConfig {

export default {
buildNumber: get('BUILD_NUMBER', '1_0_0', requiredInProduction),
environment: get('ENVIRONMENT', 'local', requiredInProduction) as 'local' | 'dev' | 'preprod' | 'prod',
environment: customApiUrl() ?? (get('ENVIRONMENT', 'local', requiredInProduction) as Environment),
productId: get('PRODUCT_ID', 'UNASSIGNED', requiredInProduction),
gitRef: get('GIT_REF', 'xxxxxxxxxxxxxxxxxxx', requiredInProduction),
production,
Expand Down Expand Up @@ -94,3 +96,14 @@ export default {
},
domain: get('INGRESS_URL', 'http://localhost:3000', requiredInProduction),
}

function customApiUrl() {
return process.env.API_URL
? {
searchApi: {
url: process.env.API_URL,
timeout: +get('API_TIMEOUT', 5000),
},
}
: null
}

0 comments on commit 5d4278c

Please sign in to comment.