refactor(server): rename configkv get or throw
This commit is contained in:
@@ -213,7 +213,7 @@ async function handleCheckoutSessionCompleted(
|
||||
|
||||
// Add flux for one-time payments
|
||||
if (session.mode === 'payment' && session.amount_total) {
|
||||
const fluxPerCent = await configKV.get('FLUX_PER_CENT')
|
||||
const fluxPerCent = await configKV.getOrThrow('FLUX_PER_CENT')
|
||||
await fluxService.addFlux(userId, session.amount_total * fluxPerCent)
|
||||
}
|
||||
}
|
||||
@@ -303,7 +303,7 @@ async function handleInvoiceEvent(
|
||||
|
||||
// Add flux when a subscription invoice is paid
|
||||
if (invoice.status === 'paid' && invoice.amount_paid && subscriptionId) {
|
||||
const fluxPerCent = await configKV.get('FLUX_PER_CENT')
|
||||
const fluxPerCent = await configKV.getOrThrow('FLUX_PER_CENT')
|
||||
await fluxService.addFlux(customer.userId, invoice.amount_paid * fluxPerCent)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ export function createV1CompletionsRoutes(fluxService: FluxService, configKV: Co
|
||||
|
||||
const body = await c.req.json()
|
||||
|
||||
const fluxPerRequest = await configKV.get('FLUX_PER_REQUEST')
|
||||
const fluxPerRequest = await configKV.getOrThrow('FLUX_PER_REQUEST')
|
||||
await fluxService.consumeFlux(user.id, fluxPerRequest)
|
||||
|
||||
const response = await fetch(`${env.BACKEND_LLM_BASE_URL}chat/completions`, {
|
||||
|
||||
@@ -23,7 +23,7 @@ describe('configKVService', () => {
|
||||
// --- get ---
|
||||
|
||||
it('get should throw 503 when key is not set', async () => {
|
||||
await expect(service.get('FLUX_PER_CENT'))
|
||||
await expect(service.getOrThrow('FLUX_PER_CENT'))
|
||||
.rejects
|
||||
.toThrow('Config key "FLUX_PER_CENT" is not set in Redis')
|
||||
})
|
||||
@@ -31,14 +31,14 @@ describe('configKVService', () => {
|
||||
it('get should return numeric value when key is set', async () => {
|
||||
redis._store.set('config:FLUX_PER_CENT', '5')
|
||||
|
||||
const value = await service.get('FLUX_PER_CENT')
|
||||
const value = await service.getOrThrow('FLUX_PER_CENT')
|
||||
expect(value).toBe(5)
|
||||
})
|
||||
|
||||
it('get should read from correct prefixed key', async () => {
|
||||
redis._store.set('config:FLUX_PER_REQUEST', '3')
|
||||
|
||||
await service.get('FLUX_PER_REQUEST')
|
||||
await service.getOrThrow('FLUX_PER_REQUEST')
|
||||
expect(redis.get).toHaveBeenCalledWith('config:FLUX_PER_REQUEST')
|
||||
})
|
||||
|
||||
@@ -68,7 +68,7 @@ describe('configKVService', () => {
|
||||
it('set then get should round-trip correctly', async () => {
|
||||
await service.set('INITIAL_USER_FLUX', 500)
|
||||
|
||||
const value = await service.get('INITIAL_USER_FLUX')
|
||||
const value = await service.getOrThrow('INITIAL_USER_FLUX')
|
||||
expect(value).toBe(500)
|
||||
})
|
||||
|
||||
@@ -81,7 +81,7 @@ describe('configKVService', () => {
|
||||
]
|
||||
redis._store.set('config:FLUX_PACKAGES', JSON.stringify(packages))
|
||||
|
||||
const value = await service.get('FLUX_PACKAGES')
|
||||
const value = await service.getOrThrow('FLUX_PACKAGES')
|
||||
expect(value).toEqual(packages)
|
||||
})
|
||||
|
||||
@@ -101,7 +101,7 @@ describe('configKVService', () => {
|
||||
]
|
||||
await service.set('FLUX_PACKAGES', packages)
|
||||
|
||||
const value = await service.get('FLUX_PACKAGES')
|
||||
const value = await service.getOrThrow('FLUX_PACKAGES')
|
||||
expect(value).toEqual(packages)
|
||||
})
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ export function createConfigKVService(redis: Redis) {
|
||||
return parseValue(key, raw)
|
||||
},
|
||||
|
||||
async get<K extends keyof ConfigDefinitions>(key: K): Promise<ConfigDefinitions[K]> {
|
||||
async getOrThrow<K extends keyof ConfigDefinitions>(key: K): Promise<ConfigDefinitions[K]> {
|
||||
const value = await this.getOptional(key)
|
||||
if (value === null)
|
||||
throw createServiceUnavailableError(`Config key "${key}" is not set in Redis`, 'CONFIG_NOT_SET')
|
||||
|
||||
@@ -15,7 +15,7 @@ export function createFluxService(db: Database, configKV: ConfigKVService) {
|
||||
})
|
||||
|
||||
if (!record) {
|
||||
const initialFlux = await configKV.get('INITIAL_USER_FLUX')
|
||||
const initialFlux = await configKV.getOrThrow('INITIAL_USER_FLUX')
|
||||
;[record] = await db.insert(schema.userFlux).values({
|
||||
userId,
|
||||
flux: initialFlux,
|
||||
|
||||
Reference in New Issue
Block a user