-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(connections): allow patching connection-level webhook_url #6739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kaposke
merged 3 commits into
master
from
gui/nan-6208-add-endpoint-to-edit-connection-level-webhook-url
Jul 13, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/server/lib/controllers/shared/connections/patchConnection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import * as z from 'zod'; | ||
|
|
||
| import db from '@nangohq/database'; | ||
| import { buildTagsFromEndUser, configService, connectionService, EndUserMapper, linkConnection, updateConnectionTags, upsertEndUser } from '@nangohq/shared'; | ||
| import { Err, Ok } from '@nangohq/utils'; | ||
|
|
||
| import { connectionTagsSchema, endUserSchema, webhookUrlSchema } from '../../../helpers/validation.js'; | ||
|
|
||
| import type { RequestLocals } from '../../../utils/express.js'; | ||
| import type { DBEnvironment, DBTeam, EndUserInput, PatchPublicConnection, Tags } from '@nangohq/types'; | ||
| import type { Response } from 'express'; | ||
|
|
||
| export const patchConnectionBodySchema = z.strictObject({ | ||
| end_user: endUserSchema.optional(), | ||
| tags: connectionTagsSchema.optional(), | ||
| webhook_url: webhookUrlSchema | ||
| }); | ||
|
|
||
| export async function handlePatchConnection({ | ||
| res, | ||
| account, | ||
| environment, | ||
| connectionId, | ||
| providerConfigKey, | ||
| body | ||
| }: { | ||
| res: Response<PatchPublicConnection['Reply'], Required<RequestLocals>>; | ||
| account: DBTeam; | ||
| environment: DBEnvironment; | ||
| connectionId: string; | ||
| providerConfigKey: string; | ||
| body: { | ||
| end_user?: EndUserInput | undefined; | ||
| tags?: Tags | undefined; | ||
| webhook_url?: string | undefined; | ||
| }; | ||
| }): Promise<void> { | ||
| const integration = await configService.getProviderConfig(providerConfigKey, environment.id); | ||
| if (!integration) { | ||
| res.status(400).send({ error: { code: 'unknown_provider_config', message: 'Provider does not exists' } }); | ||
| return; | ||
| } | ||
|
|
||
| const connectionRes = await connectionService.getConnection(connectionId, providerConfigKey, environment.id); | ||
| if (connectionRes.error || !connectionRes.response) { | ||
| res.status(404).send({ error: { code: 'not_found', message: 'Failed to find connection' } }); | ||
| return; | ||
| } | ||
|
|
||
| const connection = connectionRes.response; | ||
|
|
||
| // Generate tags from end_user (similar to postSessions.ts and postReconnect.ts) | ||
| const endUserTags = body.end_user ? buildTagsFromEndUser(body.end_user, null) : {}; | ||
| const mergedTags = { ...endUserTags, ...body.tags }; | ||
|
|
||
| if (body.end_user) { | ||
| const endUserTxRes = await db.knex.transaction(async (trx) => { | ||
| const endUserRes = await upsertEndUser(trx, { account, environment, connection, endUser: EndUserMapper.apiToEndUser(body.end_user!) }); | ||
| if (endUserRes.isErr()) { | ||
| return Err('Failed to update end user'); | ||
| } | ||
|
|
||
| if (!connection.end_user_id) { | ||
| await linkConnection(trx, { endUserId: endUserRes.value.id, connection }); | ||
| } | ||
|
|
||
| return Ok(undefined); | ||
| }); | ||
| if (endUserTxRes.isErr()) { | ||
| res.status(500).send({ error: { code: 'server_error', message: endUserTxRes.error.message } }); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (body.end_user || body.tags !== undefined) { | ||
| const tagsRes = await updateConnectionTags(db.knex, { | ||
| connection, | ||
| tags: mergedTags | ||
| }); | ||
| if (tagsRes.isErr()) { | ||
| res.status(400).send({ error: { code: 'invalid_body', message: tagsRes.error.message } }); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (typeof body.webhook_url === 'string') { | ||
| if (body.webhook_url === '') { | ||
| await connectionService.unsetConnectionConfigAttributes(connection, ['webhook_url']); | ||
| } else { | ||
| await connectionService.updateConnectionConfig(connection, { webhook_url: body.webhook_url }); | ||
| } | ||
| } | ||
|
|
||
| res.status(200).send({ success: true }); | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
...es/server/lib/controllers/v1/connections/connectionId/patchConnection.integration.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
|
|
||
| import db from '@nangohq/database'; | ||
| import { seeders } from '@nangohq/shared'; | ||
|
|
||
| import { isSuccess, runServer, shouldBeProtected, shouldRequireQueryEnv } from '../../../../utils/tests.js'; | ||
|
|
||
| import type { DBConnection } from '@nangohq/types'; | ||
|
|
||
| const route = '/api/v1/connections/:connectionId'; | ||
| let api: Awaited<ReturnType<typeof runServer>>; | ||
|
|
||
| describe(`PATCH ${route}`, () => { | ||
| beforeAll(async () => { | ||
| api = await runServer(); | ||
| }); | ||
| afterAll(() => { | ||
| api.server.close(); | ||
| }); | ||
|
|
||
| it('should be protected', async () => { | ||
| const res = await api.fetch(route, { | ||
| method: 'PATCH', | ||
| params: { connectionId: 'test' }, | ||
| query: { env: 'dev', provider_config_key: 'github' }, | ||
| body: {} | ||
| }); | ||
|
|
||
| shouldBeProtected(res); | ||
| }); | ||
|
|
||
| it('should require query env', async () => { | ||
| const { apiKey } = await seeders.seedAccountEnvAndUser(); | ||
| const res = await api.fetch(route, { | ||
| method: 'PATCH', | ||
| token: apiKey.secret, | ||
| params: { connectionId: 'test' }, | ||
| query: { provider_config_key: 'github' } as any, | ||
| body: {} | ||
| }); | ||
|
|
||
| shouldRequireQueryEnv(res); | ||
| }); | ||
|
|
||
| it('should update webhook_url', async () => { | ||
| const { env, apiKey } = await seeders.seedAccountEnvAndUser(); | ||
| await seeders.createConfigSeed(env, 'github', 'github'); | ||
| const conn = await seeders.createConnectionSeed({ env, provider: 'github' }); | ||
|
|
||
| const res = await api.fetch(route, { | ||
| method: 'PATCH', | ||
| token: apiKey.secret, | ||
| params: { connectionId: conn.connection_id }, | ||
| query: { env: env.name, provider_config_key: 'github' }, | ||
| body: { webhook_url: 'https://example.com/webhooks-from-nango' } | ||
| }); | ||
|
|
||
| isSuccess(res.json); | ||
| expect(res.json).toStrictEqual({ success: true }); | ||
|
|
||
| const updatedConn = await db.knex.select('*').from<DBConnection>('_nango_connections').where({ id: conn.id }).first(); | ||
| expect(updatedConn?.connection_config).toMatchObject({ webhook_url: 'https://example.com/webhooks-from-nango' }); | ||
| }); | ||
| }); |
51 changes: 51 additions & 0 deletions
51
packages/server/lib/controllers/v1/connections/connectionId/patchConnection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import * as z from 'zod'; | ||
|
|
||
| import { zodErrorToHTTP } from '@nangohq/utils'; | ||
|
|
||
| import { connectionIdSchema, envSchema, providerConfigKeySchema } from '../../../../helpers/validation.js'; | ||
| import { asyncWrapper } from '../../../../utils/asyncWrapper.js'; | ||
| import { handlePatchConnection, patchConnectionBodySchema } from '../../../shared/connections/patchConnection.js'; | ||
|
|
||
| import type { PatchConnection } from '@nangohq/types'; | ||
|
|
||
| const queryStringValidation = z.strictObject({ | ||
| provider_config_key: providerConfigKeySchema, | ||
| env: envSchema | ||
| }); | ||
|
|
||
| const paramValidation = z.strictObject({ | ||
| connectionId: connectionIdSchema | ||
| }); | ||
|
|
||
| export const patchConnection = asyncWrapper<PatchConnection>(async (req, res) => { | ||
| const queryParamValues = queryStringValidation.safeParse(req.query); | ||
| if (!queryParamValues.success) { | ||
| res.status(400).send({ error: { code: 'invalid_query_params', errors: zodErrorToHTTP(queryParamValues.error) } }); | ||
| return; | ||
| } | ||
|
|
||
| const valBody = patchConnectionBodySchema.safeParse(req.body); | ||
| if (!valBody.success) { | ||
| res.status(400).send({ error: { code: 'invalid_body', errors: zodErrorToHTTP(valBody.error) } }); | ||
| return; | ||
| } | ||
|
|
||
| const paramValue = paramValidation.safeParse(req.params); | ||
| if (!paramValue.success) { | ||
| res.status(400).send({ error: { code: 'invalid_uri_params', errors: zodErrorToHTTP(paramValue.error) } }); | ||
| return; | ||
| } | ||
|
|
||
| const { environment, account } = res.locals; | ||
| const queryParams: PatchConnection['Querystring'] = queryParamValues.data; | ||
| const params: PatchConnection['Params'] = paramValue.data; | ||
|
|
||
| await handlePatchConnection({ | ||
| res, | ||
| account, | ||
| environment, | ||
| connectionId: params.connectionId, | ||
| providerConfigKey: queryParams.provider_config_key, | ||
| body: valBody.data | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.