fix: refactor scopes for service token

This commit is contained in:
Christel Westerberg
2024-10-07 16:48:23 +02:00
parent 2ea0adbf98
commit 71b03143ce
8 changed files with 128 additions and 110 deletions
+14 -26
View File
@@ -125,29 +125,17 @@ export const safeProtectedProcedure = t.procedure.use(async function (opts) {
})
})
function createServiceProcedure(serviceName: ServiceTokenScope) {
return t.procedure.use(async (opts) => {
const { access_token } = await fetchServiceToken([serviceName])
if (!access_token) {
throw internalServerError(`Failed to obtain ${serviceName} service token`)
}
return opts.next({
ctx: {
serviceToken: access_token,
},
})
export const serviceProcedure = t.procedure.use(async (opts) => {
const { access_token } = await fetchServiceToken()
if (!access_token) {
throw internalServerError(`Failed to obtain service token`)
}
return opts.next({
ctx: {
serviceToken: access_token,
},
})
}
export const bookingServiceProcedure = createServiceProcedure(
ServiceTokenScopeEnum.booking
)
export const hotelServiceProcedure = createServiceProcedure(
ServiceTokenScopeEnum.hotel
)
export const profileServiceProcedure = createServiceProcedure(
ServiceTokenScopeEnum.profile
)
})
export const serverActionProcedure = t.procedure.experimental_caller(
experimental_nextAppDirCaller({
@@ -178,11 +166,11 @@ export const protectedServerActionProcedure = serverActionProcedure.use(
// NOTE: This is actually save to use, just the implementation could change
// in minor version bumps. Please read: https://trpc.io/docs/faq#unstable
export const contentStackUidWithHotelServiceProcedure =
contentstackExtendedProcedureUID.unstable_concat(hotelServiceProcedure)
export const contentStackUidWithServiceProcedure =
contentstackExtendedProcedureUID.unstable_concat(serviceProcedure)
export const contentStackBaseWithProfileServiceProcedure =
contentstackBaseProcedure.unstable_concat(profileServiceProcedure)
export const contentStackBaseWithServiceProcedure =
contentstackBaseProcedure.unstable_concat(serviceProcedure)
export const contentStackBaseWithProtectedProcedure =
contentstackBaseProcedure.unstable_concat(protectedProcedure)