Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
feat: Hooking up supaglue proxy link
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Feb 7, 2024
1 parent 3d0e85f commit af75c36
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
1 change: 1 addition & 0 deletions main/app/api/[...trpc]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const handler = (req: Request) =>
createContext({
headers: req.headers,
nangoSecretKey: env.NANGO_SECRET_KEY,
supaglueApiKey: env.SUPAGLUE_API_KEY,
}),
})

Expand Down
3 changes: 2 additions & 1 deletion main/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {z} from 'zod'
export const env = createEnv({
server: {
NANGO_SECRET_KEY: z.string(),
SUPAGLUE_API_KEY: z.string(),
},
client: {
NEXT_PUBLIC_NANGO_PUBLIC_KEY: z.string(),
},
runtimeEnv: {
NANGO_SECRET_KEY: process.env.NANGO_SECRET_KEY,
SUPAGLUE_API_KEY: process.env.SUPAGLUE_API_KEY,
NEXT_PUBLIC_NANGO_PUBLIC_KEY: process.env.NEXT_PUBLIC_NANGO_PUBLIC_KEY,
},

})
13 changes: 4 additions & 9 deletions packages/api/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ const providerByName = {
salesforce: salesforceProvider,
}

export function createContext(opts: {
headers: Headers
nangoSecretKey: string
}): RouterContext {
return {
headers: opts.headers,
nangoSecretKey: opts.nangoSecretKey,
providerByName,
}
export function createContext(
opts: Omit<RouterContext, 'providerByName'>,
): RouterContext {
return {...opts, providerByName}
}
34 changes: 21 additions & 13 deletions packages/vdk/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export type ProviderFromRouter<TRouter extends AnyRouter, TInstance = {}> = {
*/
export const PLACEHOLDER_BASE_URL = 'http://placeholder'

export const featureFlags = {
// Switch this over after credentials migration
mode: 'supaglue' as 'supaglue' | 'nango',
}

export async function proxyCallProvider({
input,
ctx,
Expand All @@ -46,19 +51,22 @@ export async function proxyCallProvider({
ctx: RemoteProcedureContext
}) {
const instance = ctx.provider.__init__({
proxyLinks: [
(req, next) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
const baseUrl = (instance as any)?.clientOptions?.baseUrl as
| string
| undefined
if (baseUrl && baseUrl !== PLACEHOLDER_BASE_URL) {
req.headers.set(nangoProxyLink.kBaseUrlOverride, baseUrl)
}
return next(req)
},
ctx.nangoLink,
],
proxyLinks:
featureFlags.mode === 'nango'
? [
(req, next) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
const baseUrl = (instance as any)?.clientOptions?.baseUrl as
| string
| undefined
if (baseUrl && baseUrl !== PLACEHOLDER_BASE_URL) {
req.headers.set(nangoProxyLink.kBaseUrlOverride, baseUrl)
}
return next(req)
},
ctx.nangoLink,
]
: [ctx.supaglueLink],
})

// verticals.salesEngagement.listContacts -> listContacts
Expand Down
11 changes: 10 additions & 1 deletion packages/vdk/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import type {OpenApiMeta} from '@lilyrose2798/trpc-openapi'
import {initTRPC, TRPCError} from '@trpc/server'
import {nangoProxyLink} from './nangoProxyLink'
import type {Provider} from './provider'
import {supaglueProxyLink} from './supaglueProxyLink'

export type RouterContext = {
nangoSecretKey: string
supaglueApiKey: string
headers: Headers
providerByName: Record<string, Provider>
}
Expand Down Expand Up @@ -52,14 +54,21 @@ export const remoteProcedure = publicProcedure.use(
ctx.headers.get('x-provider-config-key') ?? providerName,
})

const supaglueLink = supaglueProxyLink({
apiKey: ctx.headers.get('x-api-key') ?? ctx.supaglueApiKey,
customerId,
providerName,
})

return next({
ctx: {
...ctx,
path,
connectionId: customerId,
customerId,
providerName,
provider,
nangoLink,
supaglueLink,
},
})
},
Expand Down
6 changes: 5 additions & 1 deletion packages/worker/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {initNangoSDK} from '@opensdks/sdk-nango'

// Dedupe this with main/env.ts
export const env = createEnv({
server: {NANGO_SECRET_KEY: z.string(), POSTGRES_URL: z.string()},
server: {
NANGO_SECRET_KEY: z.string(),
SUPAGLUE_API_KEY: z.string(),
POSTGRES_URL: z.string(),
},
runtimeEnv: process.env,
})

Expand Down
4 changes: 2 additions & 2 deletions packages/worker/routines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export async function syncConnection({

const supaglue = initBYOSupaglueSDK({
headers: {
'x-customer-id': connection_id,
'x-provider-name': provider_config_key,
'x-customer-id': connection_id, // This relies on customer-id mapping 1:1 to connection_id
'x-provider-name': provider_config_key, // This relies on provider_config_key mapping 1:1 to provider-name
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export const apolloProvider = {
__init__: ({proxyLinks}) =>
initApolloSDK({
api_key: '', // This will be populated by Nango, or you can populate your own
links: (defaultLinks) => [...proxyLinks, ...defaultLinks],
links: (defaultLinks) => [
...defaultLinks.slice(0, -1),
...proxyLinks, // proxy links shoudl be in the middle...
...defaultLinks.slice(-1),
],
}),
listContacts: async ({instance}) => {
const res = await instance.POST('/v1/contacts/search', {})
Expand Down

0 comments on commit af75c36

Please sign in to comment.