Skip to content

Commit

Permalink
feat: add DELETE /connect/session endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TBonnin committed Sep 26, 2024
1 parent 117b2b0 commit 74a3350
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/server/lib/controllers/connect/deleteSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { DeleteConnectSession } from '@nangohq/types';
import db from '@nangohq/database';
import { asyncWrapper } from '../../utils/asyncWrapper.js';
import * as connectSessionService from '../../services/connectSession.service.js';
import { requireEmptyQuery, requireEmptyBody, zodErrorToHTTP } from '@nangohq/utils';

export const deleteConnectSession = asyncWrapper<DeleteConnectSession>(async (req, res) => {
const emptyQuery = requireEmptyQuery(req);
if (emptyQuery) {
res.status(400).send({ error: { code: 'invalid_query_params', errors: zodErrorToHTTP(emptyQuery.error) } });
return;
}

const emptyBody = requireEmptyBody(req);
if (emptyBody) {
res.status(400).send({ error: { code: 'invalid_body', errors: zodErrorToHTTP(emptyBody.error) } });
return;
}

const deleteSession = await connectSessionService.deleteConnectSession(db.knex, {
id: res.locals.connectSession.id,
accountId: res.locals.account.id,
environmentId: res.locals.environment.id
});

if (deleteSession.isErr()) {
res.status(400).send({
error: {
code: 'internal_error',
message: 'Failed to delete connect session',
payload: {
id: res.locals.connectSession.id,
accountId: res.locals.account.id,
environmentId: res.locals.environment.id
}
}
});
return;
}
res.status(204).send();
});
4 changes: 3 additions & 1 deletion packages/server/lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import { getPublicIntegration } from './controllers/integrations/uniqueKey/getIn
import { getPublicListIntegrations } from './controllers/integrations/getListIntegrations.js';
import { postConnectSessions } from './controllers/connect/postSessions.js';
import { getConnectSession } from './controllers/connect/getSession.js';
import { deleteConnectSession } from './controllers/connect/deleteSession.js';

export const router = express.Router();

Expand Down Expand Up @@ -215,7 +216,8 @@ publicAPI.route('/scripts/config').get(apiAuth, flowController.getFlowConfig.bin
publicAPI.route('/action/trigger').post(apiAuth, syncController.triggerAction.bind(syncController)); //TODO: to deprecate

publicAPI.route('/connect/sessions').post(apiAuth, postConnectSessions);
publicAPI.route('/connect/session').post(connectSessionAuth, getConnectSession);
publicAPI.route('/connect/session').get(connectSessionAuth, getConnectSession);
publicAPI.route('/connect/session').delete(connectSessionAuth, deleteConnectSession);

publicAPI.route('/v1/*').all(apiAuth, syncController.actionOrModel.bind(syncController));

Expand Down
11 changes: 9 additions & 2 deletions packages/types/lib/connect/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type PostConnectSessions = Endpoint<{
allowedIntegrations?: string[] | undefined;
integrationsConfigDefaults?: Record<string, { connectionConfig: Record<string, unknown> }> | undefined;
};
Error: ApiError<'forbidden' | 'invalid_body' | 'invalid_query_params' | 'internal_error'>;
Error: ApiError<'forbidden' | 'internal_error'>;
Success: {
data: ConnectSessionToken;
};
Expand All @@ -29,7 +29,7 @@ export type PostConnectSessions = Endpoint<{
export type GetConnectSession = Endpoint<{
Method: 'GET';
Path: '/connect/session';
Error: ApiError<'forbidden' | 'invalid_body' | 'invalid_query_params' | 'internal_error'>;
Error: ApiError<'forbidden'>;
Success: {
data: {
allowedIntegrations: ConnectSession['allowedIntegrations'];
Expand All @@ -43,3 +43,10 @@ export type GetConnectSession = Endpoint<{
};
};
}>;

export type DeleteConnectSession = Endpoint<{
Method: 'DELETE';
Path: '/connect/session';
Error: ApiError<'forbidden' | 'internal_error'>;
Success: never;
}>;

0 comments on commit 74a3350

Please sign in to comment.