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 6fabe59
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 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: 'server_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();
});
10 changes: 5 additions & 5 deletions packages/server/lib/controllers/connect/postSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const postConnectSessions = asyncWrapper<PostConnectSessions>(async (req,
let linkedProfileId: number;
if (getLinkedProfile.isErr()) {
if (getLinkedProfile.error.code !== 'not_found') {
res.status(500).send({ error: { code: 'internal_error', message: 'Failed to get linked profile' } });
res.status(500).send({ error: { code: 'server_error', message: 'Failed to get linked profile' } });
return;
}
// create linked profile if it doesn't exist yet
Expand All @@ -78,7 +78,7 @@ export const postConnectSessions = asyncWrapper<PostConnectSessions>(async (req,
environmentId: res.locals.environment.id
});
if (createLinkedProfile.isErr()) {
res.status(500).send({ error: { code: 'internal_error', message: 'Failed to create linked profile' } });
res.status(500).send({ error: { code: 'server_error', message: 'Failed to create linked profile' } });
return;
}
linkedProfileId = createLinkedProfile.value.id;
Expand All @@ -103,7 +103,7 @@ export const postConnectSessions = asyncWrapper<PostConnectSessions>(async (req,
: null
});
if (updateLinkedProfile.isErr()) {
res.status(500).send({ error: { code: 'internal_error', message: 'Failed to update linked profile' } });
res.status(500).send({ error: { code: 'server_error', message: 'Failed to update linked profile' } });
return;
}
}
Expand All @@ -119,7 +119,7 @@ export const postConnectSessions = asyncWrapper<PostConnectSessions>(async (req,
integrationsConfigDefaults: req.body.integrationsConfigDefaults || null
});
if (createConnectSession.isErr()) {
res.status(500).send({ error: { code: 'internal_error', message: 'Failed to create connect session' } });
res.status(500).send({ error: { code: 'server_error', message: 'Failed to create connect session' } });
return;
}
// create a private key for the connect session
Expand All @@ -132,7 +132,7 @@ export const postConnectSessions = asyncWrapper<PostConnectSessions>(async (req,
ttlInMs: 30 * 60 * 1000 // 30 minutes
});
if (createPrivateKey.isErr()) {
res.status(500).send({ error: { code: 'internal_error', message: 'Failed to create session token' } });
res.status(500).send({ error: { code: 'server_error', message: 'Failed to create session token' } });
return;
}
const [token, privateKey] = createPrivateKey.value;
Expand Down
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'>;
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'>;
Success: never;
}>;

0 comments on commit 6fabe59

Please sign in to comment.