UNPKG

@nodescript/cli

Version:
77 lines 3 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { dep } from 'mesh-ioc'; import { ConfigManager } from './config.js'; export class ApiManager { async getWorkspaceModules(workspaceId) { const { modules } = await this.sendGet('/Module/getWorkspaceModules', { workspaceId, }); return modules; } async publishEsm(spec) { return await this.sendPost('/Module/publishEsm', { spec }); } async sendGet(path, params = {}) { const url = new URL(path.replace(/^\//, ''), this.config.options.apiUrl); for (const [k, v] of Object.entries(params)) { url.searchParams.append(k, v); } const authorization = this.getAuthorization(); const res = await fetch(url, { method: 'GET', headers: { authorization, } }); return await this.processResponse(res); } async sendPost(path, params = {}) { const url = new URL(path.replace(/^\//, ''), this.config.options.apiUrl); const authorization = this.getAuthorization(); const res = await fetch(url, { method: 'POST', headers: { authorization, 'content-type': 'application/json' }, body: JSON.stringify(params), }); return await this.processResponse(res); } async processResponse(res) { if (!res.ok) { const errJson = await res.json().catch(err => { return { name: err.name, message: err.message, }; }); const err = new Error(); err.name = errJson.name ?? 'UnknownError'; err.message = errJson.message ?? 'Unknown message'; err.status = res.status; throw err; } return await res.json(); } getAuthorization() { const token = this.config.options.apiToken; if (!token) { throw new Error('Please setup NODESCRIPT_API_TOKEN in .env file'); } return `Bearer ${token}`; } } __decorate([ dep(), __metadata("design:type", ConfigManager) ], ApiManager.prototype, "config", void 0); //# sourceMappingURL=api.js.map