UNPKG

@blinkk/editor

Version:

Structured content editor with live previews.

197 lines 6.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceServerApi = exports.LocalServerApi = exports.ServerApi = void 0; const amagakiApi_1 = require("../projectType/amagaki/amagakiApi"); const growApi_1 = require("../projectType/grow/growApi"); const bent_1 = __importDefault(require("bent")); const DEFAULT_LOCAL_PORT = 9090; const postJSON = bent_1.default('json', 'POST'); /** * Example api that returns data through a 'simulated' network. */ class ServerApi { constructor() { this.projectTypes = { amagaki: new amagakiApi_1.AmagakiApi(this), grow: new growApi_1.GrowApi(this), }; this.remoteMediaProviders = []; } get apiBaseUrl() { return `https://api.${window.location.hostname}/`; } get baseUrl() { return '/'; } /** * Verify that the authentication for services that require auth. * * @returns True if the auth check out. */ checkAuth() { return true; } /** * Specific services may need to add additional params to all of * the api request (such as authentication params.) * * @param params Params being sent to the api. * @returns Updated params to send to the api. */ expandParams(params) { return params; } async copyFile(originalPath, path) { return postJSON(this.resolveApiUrl('/file.copy'), this.expandParams({ originalPath: originalPath, path: path, })); } async createFile(path) { return postJSON(this.resolveApiUrl('/file.create'), this.expandParams({ path: path, })); } async createWorkspace(base, workspace) { return postJSON(this.resolveApiUrl('/workspace.create'), this.expandParams({ base: base, workspace: workspace, })); } async deleteFile(file) { return postJSON(this.resolveApiUrl('/file.delete'), this.expandParams({ file: file, })); } async getDevices() { return postJSON(this.resolveApiUrl('/devices.get'), this.expandParams({})); } async getFile(file) { window.history.pushState({}, '', this.resolveUrl(file.path)); return postJSON(this.resolveApiUrl('/file.get'), this.expandParams({ file: file, })); } async getFiles() { return postJSON(this.resolveApiUrl('/files.get'), this.expandParams({})); } async getFileUrl(file) { // TODO: Use preview server to determine urls for files. return Promise.resolve({ path: file.path, url: 'image-landscape.png', }); } async getProject() { return postJSON(this.resolveApiUrl('/project.get'), this.expandParams({})); } async getWorkspace() { return postJSON(this.resolveApiUrl('/workspace.get'), this.expandParams({})); } async getWorkspaces() { return postJSON(this.resolveApiUrl('/workspaces.get'), this.expandParams({})); } async loadWorkspace(workspace) { return Promise.resolve(workspace); } async publish(workspace, data) { return postJSON(this.resolveApiUrl('/publish.start'), this.expandParams({ workspace: workspace, data: data, })); } resolveApiUrl(path) { // Strip off the preceding /. path = path.replace(/\/*/, ''); return `${this.apiBaseUrl}${path}`; } resolveUrl(path) { // Strip off the preceding /. path = path.replace(/\/*/, ''); return `${this.baseUrl}${path}`; } async saveFile(file, isRawEdit) { return postJSON(this.resolveApiUrl('/file.save'), this.expandParams({ file: file, isRawEdit: isRawEdit, })); } async uploadFile(file, options) { // Providers can upload the file to different services. for (const provider of this.remoteMediaProviders) { if (provider.canApply(file, options)) { const uploader = new provider(options); return uploader.upload(file); } } return await postJSON(this.resolveApiUrl('/file.upload'), this.expandParams({ file: file, options: options, })); } } exports.ServerApi = ServerApi; /** * Connects to a locally running version of the api. * * This is used when a user is running `npx @blinkk/editor-server` locally. */ class LocalServerApi extends ServerApi { constructor(port) { super(); this.port = port; } get apiBaseUrl() { return `http://localhost:${this.port}/`; } get baseUrl() { if (this.port === DEFAULT_LOCAL_PORT) { return '/local/'; } return `/local/${this.port}/`; } async ping() { return postJSON(this.resolveApiUrl('/ping'), this.expandParams({})); } } exports.LocalServerApi = LocalServerApi; /** * Connects to the corresponding api server and maps to the right service * api for the request urls. */ class ServiceServerApi extends ServerApi { constructor(service, organization, project, branch, isUnstable, isDev) { super(); this.service = service; this.organization = organization; this.project = project; this.branch = branch || 'main'; this.isUnstable = isUnstable || false; this.isDev = isDev || false; } get apiBaseUrl() { let domain = 'https://api.editor.dev'; if (this.isDev) { domain = 'http://localhost:9090'; } else if (this.isUnstable) { domain = 'https://api.beta.editor.dev'; } const path = `/${this.service}/${this.organization}/${this.project}/${this.branch}/`; return `${domain}${path}`; } get baseUrl() { return `/${this.service}/${this.organization}/${this.project}/${this.branch}/`; } async loadWorkspace(workspace) { // Update the url to use the new branch. this.branch = workspace.name; window.history.pushState({}, '', `/${this.service}/${this.organization}/${this.project}/${this.branch}/`); return Promise.resolve(workspace); } } exports.ServiceServerApi = ServiceServerApi; //# sourceMappingURL=api.js.map