mirror of
https://github.com/logto-io/logto.git
synced 2025-01-13 21:30:30 -05:00
871789e763
* chore: align routers return process of updating DB * chore: align connector router output
84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
import { Connectors } from '@logto/schemas';
|
|
import { object, string } from 'zod';
|
|
|
|
import { getConnectorInstances, getConnectorInstanceById } from '@/connectors';
|
|
import { ConnectorInstance } from '@/connectors/types';
|
|
import koaGuard from '@/middleware/koa-guard';
|
|
import { updateConnector } from '@/queries/connector';
|
|
|
|
import { AuthedRouter } from './types';
|
|
|
|
const transpileConnectorInstance = ({ connector, metadata }: ConnectorInstance) => ({
|
|
...connector,
|
|
metadata,
|
|
});
|
|
|
|
export default function connectorRoutes<T extends AuthedRouter>(router: T) {
|
|
router.get('/connectors', async (ctx, next) => {
|
|
const connectorInstances = await getConnectorInstances();
|
|
ctx.body = connectorInstances.map((connectorInstance) => {
|
|
return transpileConnectorInstance(connectorInstance);
|
|
});
|
|
|
|
return next();
|
|
});
|
|
|
|
router.get(
|
|
'/connectors/:id',
|
|
koaGuard({ params: object({ id: string().min(1) }) }),
|
|
async (ctx, next) => {
|
|
const {
|
|
params: { id },
|
|
} = ctx.guard;
|
|
const connectorInstance = await getConnectorInstanceById(id);
|
|
ctx.body = transpileConnectorInstance(connectorInstance);
|
|
|
|
return next();
|
|
}
|
|
);
|
|
|
|
router.patch(
|
|
'/connectors/:id/enabled',
|
|
koaGuard({
|
|
params: object({ id: string().min(1) }),
|
|
body: Connectors.createGuard.pick({ enabled: true }),
|
|
}),
|
|
async (ctx, next) => {
|
|
const {
|
|
params: { id },
|
|
body: { enabled },
|
|
} = ctx.guard;
|
|
const { metadata } = await getConnectorInstanceById(id);
|
|
const connector = await updateConnector({ set: { enabled }, where: { id } });
|
|
ctx.body = { ...connector, metadata };
|
|
|
|
return next();
|
|
}
|
|
);
|
|
|
|
router.patch(
|
|
'/connectors/:id',
|
|
koaGuard({
|
|
params: object({ id: string().min(1) }),
|
|
body: Connectors.createGuard
|
|
.omit({ id: true, type: true, enabled: true, createdAt: true })
|
|
.partial(),
|
|
}),
|
|
async (ctx, next) => {
|
|
const {
|
|
params: { id },
|
|
body,
|
|
} = ctx.guard;
|
|
const { metadata, validateConfig } = await getConnectorInstanceById(id);
|
|
|
|
if (body.config) {
|
|
await validateConfig(body.config);
|
|
}
|
|
|
|
const connector = await updateConnector({ set: body, where: { id } });
|
|
ctx.body = { ...connector, metadata };
|
|
|
|
return next();
|
|
}
|
|
);
|
|
}
|