Files
cloudron-box/dashboard/src/models/TokensModel.js
T

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-01-14 15:52:12 +01:00
import { fetcher } from 'pankow';
2025-03-03 11:22:56 +01:00
import { API_ORIGIN } from '../constants.js';
2025-01-14 15:52:12 +01:00
2025-01-31 21:02:48 +01:00
function create() {
const accessToken = localStorage.token;
2025-01-14 15:52:12 +01:00
return {
name: 'TokensModel',
async list() {
let error, result;
try {
2025-03-03 11:22:56 +01:00
result = await fetcher.get(`${API_ORIGIN}/api/v1/tokens`, { access_token: accessToken });
2025-01-14 15:52:12 +01:00
} catch (e) {
error = e;
}
if (error || result.status !== 200) return [error || result];
return [null, result.body.tokens];
2025-01-14 15:52:12 +01:00
},
2025-03-07 11:53:03 +01:00
async add(name, scope, allowedIpRanges) {
2025-01-15 12:31:35 +01:00
let error, result;
try {
2025-03-07 11:53:03 +01:00
result = await fetcher.post(`${API_ORIGIN}/api/v1/tokens`, { name, scope, allowedIpRanges }, { access_token: accessToken });
2025-01-15 12:31:35 +01:00
} catch (e) {
error = e;
}
if (error || result.status !== 201) return [error || result];
return [null, result.body];
2025-01-15 12:31:35 +01:00
},
2025-01-14 15:52:12 +01:00
async remove(id) {
let error, result;
try {
2025-03-03 11:22:56 +01:00
result = await fetcher.del(`${API_ORIGIN}/api/v1/tokens/${id}`, { access_token: accessToken });
2025-01-14 15:52:12 +01:00
} catch (e) {
error = e;
}
if (error || result.status !== 204) return [error || result];
return [null];
2025-01-14 15:52:12 +01:00
},
};
}
export default {
create,
};