UNPKG

scaleway-sync-firewall

Version:
99 lines (98 loc) 2.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); function baseUrl(zone, ...to) { const path = to.join('/').replace(/\/{2,}/, '/'); return `https://cp-${zone}.scaleway.com/${path.replace(/^\/+|\/$/, '')}`; } /** * Abstract Model foundation class. */ class Model { /** * Model constructor. */ constructor(entry, zone, meta) { this.entry = entry; this.zone = zone; this.meta = meta || {}; } /** * Fetch the given resource by path. */ static find(zone, id) { return this.client(zone).get(id).then(({ data }) => data); } /** * Fetch a single resource by ID. */ // @ts-ignore static get(zone, id) { // @ts-ignore return this.find(zone, id).then((response) => new this(response[this.subKey], zone)); } /** * Axios client for this Model. */ static client(zone) { return axios_1.default.create({ baseURL: baseUrl(zone, this.path), headers: { 'X-Auth-Token': process.env.AUTH_TOKEN, } }); } ; /** * Axios client for this Model instance. */ get client() { // @ts-ignore const client = this.constructor.client(this.zone); // @ts-ignore client.defaults.baseURL = baseUrl(this.zone, this.constructor.path, this.path); return client; } /** * Delete the current resource. */ delete() { return this.client.delete('/'); } /** * Patch the current resource. */ patch(data) { return this.client.put('/', Object.assign({}, this.entry, data)); } /** * Create a new resource using the current model entry. */ create() { const payload = Object.assign({}, this.entry); delete payload.id; return this.client.post('../', payload); } /** * Register a has-many relationship between the current and given model. */ hasMany(model, zone, path, meta) { return model.find(zone, path).then((documents) => { return documents[model.subKey].map((document) => new model(document, zone, meta)); }); } } /** * Prepare a Model class for the given service path. */ function default_1(baseServicePath) { class PreparedModel extends Model { } PreparedModel.path = baseServicePath; return PreparedModel; } exports.default = default_1; ;