@softvisio/core
Version:
Softisio core
71 lines (56 loc) • 1.76 kB
JavaScript
import Accounts from "#lib/api/cloudflare/accounts";
import DnsRecords from "#lib/api/cloudflare/dns-records";
import User from "#lib/api/cloudflare/user";
import Zones from "#lib/api/cloudflare/zones";
import fetch from "#lib/fetch";
import mixins from "#lib/mixins";
// NOTE https://api.cloudflare.com/
const BASE_URL = new URL( "https://api.cloudflare.com/client/v4/" );
export default class Cloudflare extends mixins( Accounts, DnsRecords, User, Zones ) {
constructor ( token, email ) {
super();
this.
this.
}
// protected
async _doRequest ( method, path, params, body ) {
const url = new URL( path, BASE_URL );
if ( params ) url.search = new URLSearchParams( params );
if ( body ) body = JSON.stringify( body );
const res = await fetch( url, {
method,
"headers": {
"Content-Type": "application/json",
...this.
},
body,
} );
if ( !res.ok ) return result( res );
try {
const data = await res.json();
if ( !data.success ) {
return result( [ 500, data.errors.join( ", " ) ] );
}
return result( 200, data.result );
}
catch ( e ) {
return result.catch( e );
}
}
// private
if ( this.
return {
"X-Auth-Email": this.
"X-Auth-Key": this.
};
}
else {
return {
"Authorization": "Bearer " + this.
};
}
}
}