2025-02-06 18:22:07 +01:00
import { fetcher } from 'pankow' ;
import DomainsModel from './DomainsModel.js' ;
2025-03-03 11:22:56 +01:00
import { API _ORIGIN } from '../constants.js' ;
2025-02-06 18:22:07 +01:00
function create ( ) {
const accessToken = localStorage . token ;
const domainsModel = DomainsModel . create ( ) ;
return {
async list ( ) {
const page = 1 ;
const per _page = 1000 ;
let error , result ;
try {
2025-03-03 11:22:56 +01:00
result = await fetcher . get ( ` ${ API _ORIGIN } /api/v1/archives ` , { page , per _page , access _token : accessToken } ) ;
2025-02-06 18:22:07 +01:00
} catch ( e ) {
error = e ;
}
if ( error || result . status !== 200 ) return [ error || result ] ;
return [ null , result . body . archives ] ;
} ,
async remove ( id ) {
let error , result ;
try {
2025-03-03 11:22:56 +01:00
result = await fetcher . del ( ` ${ API _ORIGIN } /api/v1/archives/ ${ id } ` , { access _token : accessToken } ) ;
2025-02-06 18:22:07 +01:00
} catch ( e ) {
error = e ;
}
if ( error || result . status !== 204 ) return [ error || result ] ;
return [ null ] ;
} ,
async restore ( id , data ) {
// preflight domain checks
if ( ! data . overwriteDns ) {
const allDomains = [ { domain : data . domain , subdomain : data . subdomain } ] . concat ( Object . keys ( data . secondaryDomains ) . map ( function ( k ) {
return {
domain : data . secondaryDomains [ k ] . domain ,
subdomain : data . secondaryDomains [ k ] . subdomain
} ;
} ) ) ;
2025-02-06 21:02:07 +01:00
for ( const domain of allDomains ) {
2025-02-06 18:22:07 +01:00
const [ error , result ] = await domainsModel . checkRecords ( domain . domain , domain . subdomain ) ;
if ( error ) return [ error ] ;
const fqdn = domain . subdomain + '.' + domain . domain ;
if ( result . error ) {
if ( result . error . reason === 'Access Denied' ) return [ { type : 'provider' , fqdn , message : 'DNS credentials for ' + domain . domain + ' are invalid. Update it in Domains & Certs view' } ] ;
return [ { type : 'provider' , fqdn , message : result . error . message } ] ;
}
if ( result . needsOverwrite ) {
return [ { type : 'externally_exists' , fqdn , message : 'DNS Record already exists. Confirm that the domain is not in use for services external to Cloudron' } ] ;
}
}
}
let error , result ;
try {
2025-03-03 11:22:56 +01:00
result = await fetcher . post ( ` ${ API _ORIGIN } /api/v1/archives/ ${ id } /unarchive ` , data , { access _token : accessToken } ) ;
2025-02-06 18:22:07 +01:00
} catch ( e ) {
error = e ;
}
if ( error || result . status !== 202 ) return [ error || result ] ;
return [ null , result . body ] ;
} ,
} ;
}
export default {
create ,
} ;