Files
cloudron-box/src/routes/dockerregistries.js
T
Girish Ramakrishnan 12e073e8cf use node: prefix for requires
mostly because code is being autogenerated by all the AI stuff using
this prefix. it's also used in the stack trace.
2025-08-14 12:55:35 +05:30

87 lines
3.5 KiB
JavaScript

'use strict';
exports = module.exports = {
list,
add,
get,
del,
update,
load
};
const assert = require('node:assert'),
AuditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
dockerRegistries = require('../dockerregistries.js'),
HttpError = require('@cloudron/connect-lastmile').HttpError,
HttpSuccess = require('@cloudron/connect-lastmile').HttpSuccess,
safe = require('safetydance');
async function load(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
const [error, result] = await safe(dockerRegistries.get(req.params.id));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Registry not found'));
req.resource = result;
next();
}
async function list(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const [error, results] = await safe(dockerRegistries.list());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { registries: results.map(dockerRegistries.removePrivateFields) }));
}
function get(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
assert.strictEqual(typeof req.user, 'object');
next(new HttpSuccess(200, dockerRegistries.removePrivateFields(req.resource)));
}
async function add(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be a string'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
const [error, id] = await safe(dockerRegistries.add(req.body, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { id }));
}
async function update(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
assert.strictEqual(typeof req.body, 'object');
assert.strictEqual(typeof req.resource, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be a string'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
const [updateError] = await safe(dockerRegistries.update(req.resource, req.body, AuditSource.fromRequest(req)));
if (updateError) return next(BoxError.toHttpError(updateError));
next(new HttpSuccess(204, {}));
}
async function del(req, res, next) {
assert.strictEqual(typeof req.resource, 'object');
const [error] = await safe(dockerRegistries.del(req.resource, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
}