Skip to content

Commit

Permalink
refactor: ♻️ merge config at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingallday committed Aug 11, 2023
1 parent 0291c3a commit 22e0d74
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 48 deletions.
12 changes: 7 additions & 5 deletions packages/nuxt-ripple/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const assetCacheTime = 31536000 // 1 year
export default defineNuxtConfig({
runtimeConfig: {
basicAuth: 0,
config: {
apiPrefix: '/api/v1',
auth: {
username: 'dpc',
password: 'sdp'
tide: {
config: {
apiPrefix: '/api/v1',
auth: {
username: 'dpc',
password: 'sdp'
}
}
},
public: {
Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt-ripple/server/middleware/basic-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { defineEventHandler, useRuntimeConfig } from '#imports'
//

export default defineEventHandler((event: any) => {
const { basicAuth, config } = useRuntimeConfig()
const { basicAuth, tide } = useRuntimeConfig()

// If `basicAuth` is empty, do not prompt for authentication
if (!basicAuth || basicAuth === 0 || !config?.auth) {
if (!basicAuth || basicAuth === 0 || !tide.config.auth) {
return
}

// Format our credentials to their corresponding header:
// `user:pass` becomes `Basic dXNlcjpwYXNz`
const credentials = Buffer.from(
`${config.auth.username}:${config.auth.password}`
`${tide.config.auth.username}:${tide.config.auth.password}`
).toString('base64')
const validAuthHeaders = `Basic ${credentials}`
const authHeader = getHeader(event, 'authorization')
Expand Down
12 changes: 9 additions & 3 deletions packages/nuxt-ripple/server/plugins/tide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ export default defineNitroPlugin(async (NitroApp) => {
`error loading tide runtimeConfig, check nuxt.config.ts runtimeConfig key is set`
)
}
if (!config.config) {
if (!config.tide.config) {
logger.error(
`error loading tide internal runtimeConfig, check nuxt.config.ts runtimeConfig key is set`
)
}
const pageApi = new TidePageApi(config.public.tide, config.config, logger)
const siteApi = new TideSiteApi(config.public.tide, config.config, logger)
const pageApi = new TidePageApi(
{ ...config.public.tide, ...config.tide },
logger
)
const siteApi = new TideSiteApi(
{ ...config.public.tide, ...config.tide },
logger
)
NitroApp.tide = {
pageApi,
siteApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe('TideApiBase', () => {
mapping: {
site: exampleMapping,
event: exampleMapping
}
},
...exampleApiConfig
},
exampleApiConfig,
mockLogger
)
it('should return mapped data', async () => {
Expand Down Expand Up @@ -83,9 +83,9 @@ describe('TideApiBase', () => {
mapping: {
site: exampleMapping,
event: exampleMapping
}
},
...exampleApiConfig
},
exampleApiConfig,
mockLogger
)
it('should call http client get method', async () => {
Expand Down
19 changes: 5 additions & 14 deletions packages/ripple-tide-api/src/services/tide-api-base.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
import HttpClient from './http-client.js'
import { get } from 'lodash-es'
import type {
RplTideModuleConfig,
RplTideModuleInternalConfig
} from './../../types'
import type { RplTideModuleConfig } from './../../types'
import getHierarchicalMenu from './lib/site-menu.js'
import { ApplicationError, WrappedAxiosError } from '../errors/errors.js'
import axios from 'axios'
import { ILogger } from '../logger/logger'

export default class TideApiBase extends HttpClient {
tide: RplTideModuleConfig
internal: RplTideModuleInternalConfig
debug: boolean | undefined
constructor(
tide: RplTideModuleConfig,
internal: RplTideModuleInternalConfig,
logger: ILogger
) {
if (!tide || !internal) {
constructor(tide: RplTideModuleConfig, logger: ILogger) {
if (!tide) {
throw new Error('Error - No configuration specified')
}
super(
{
client: tide.client,
baseUrl: `${tide.baseUrl}${internal.apiPrefix}`,
auth: internal.auth
baseUrl: `${tide.baseUrl}${tide.config?.apiPrefix}`,
auth: tide.config?.auth
},
logger
)
this.tide = tide
this.internal = internal
this.debug = tide.debug
}

Expand Down
9 changes: 2 additions & 7 deletions packages/ripple-tide-api/src/services/tide-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import TideApiBase from './tide-api-base.js'
import defaultMapping from './lib/default-mapping.js'
import type {
RplTideModuleConfig,
RplTideModuleInternalConfig,
IRplTideModuleMapping,
IRplTideDynamicComponentMapping
} from './../../types'
Expand All @@ -21,12 +20,8 @@ export default class TidePageApi extends TideApiBase {
sectionId: string
path: string

constructor(
tide: RplTideModuleConfig,
internal: RplTideModuleInternalConfig,
logger: ILogger
) {
super(tide, internal, logger)
constructor(tide: RplTideModuleConfig, logger: ILogger) {
super(tide, logger)
this.site = tide.site
this.sectionId = ''
this.path = ''
Expand Down
14 changes: 3 additions & 11 deletions packages/ripple-tide-api/src/services/tide-site.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import jsonapiParse from 'jsonapi-parse'
import TideApiBase from './tide-api-base.js'
import type {
RplTideModuleConfig,
RplTideModuleInternalConfig,
IRplTideModuleMapping
} from './../../types'
import type { RplTideModuleConfig, IRplTideModuleMapping } from './../../types'
import { ApplicationError } from '../errors/errors.js'
import { ILogger } from '../logger/logger'

export default class TideSite extends TideApiBase {
site: string
siteMapping: IRplTideModuleMapping | null

constructor(
tide: RplTideModuleConfig,
internal: RplTideModuleInternalConfig,
logger: ILogger
) {
super(tide, internal, logger)
constructor(tide: RplTideModuleConfig, logger: ILogger) {
super(tide, logger)
this.site = tide.site
this.siteMapping = null
this.logLabel = 'TideSite'
Expand Down
6 changes: 5 additions & 1 deletion packages/ripple-tide-api/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ export interface RplTideModuleConfig {
* nuxt-proxy options
*/
proxy?: ModuleOptions
/**
* Private config merged at runtime
*/
config?: RplTideModuleInternalConfig
}

export interface RplTideModuleInternalConfig {
interface RplTideModuleInternalConfig {
/**
* Basic Auth credentials
*/
Expand Down

0 comments on commit 22e0d74

Please sign in to comment.