@curvenote/cli
Version:
CLI Client library for Curvenote
153 lines (152 loc) • 5.6 kB
JavaScript
import { projectFromDTO, blockFromDTO, versionFromDTO, userFromDTO, myUserFromDTO, teamFromDTO, siteConfigFromDTO, } from '@curvenote/blocks';
import { selectors } from './store/index.js';
import { users, teams, blocks, projects, siteconfigs, versions, templates, } from './store/api/index.js';
import { versionIdToURL } from './utils/index.js';
/** Base class for API models */
class BaseTransfer {
constructor(session, id) {
this.modelKind = '';
this.$fromDTO = () => {
throw new Error('Must be set in base class');
};
this.$createUrl = () => {
throw new Error('Must be set in base class');
};
this.id = id;
this.session = session;
}
get data() {
if (this.$data)
return this.$data;
throw new Error(`${this.modelKind}: Must call "get" first`);
}
set data(data) {
this.id = data.id;
this.$data = this.$fromDTO(data.id, data);
if (this.$receive)
this.session.store.dispatch(this.$receive(data));
}
async get(query) {
var _a;
const url = this.$createUrl();
const state = this.session.store.getState();
const fromSession = (_a = this.$selector) === null || _a === void 0 ? void 0 : _a.call(this, state, this.id);
if (fromSession) {
this.session.log.debug(`Loading ${this.modelKind} from cache: "${url}"`);
this.data = fromSession;
return this;
}
this.session.log.debug(`Fetching ${this.modelKind}: "${url}"`);
const { ok, json } = await this.session.get(url, query);
if (!ok) {
if ('message' in json) {
console.log('mode.get throw 1');
throw new Error(`${this.modelKind}: (${url}) ${json.message}`);
}
console.log('mode.get throw 2');
throw new Error(`${this.modelKind}: Not found (${url}) or you do not have access.`);
}
this.data = json;
return this;
}
}
export class MyUser extends BaseTransfer {
constructor(session) {
super(session, '');
this.modelKind = 'User';
this.$fromDTO = myUserFromDTO;
this.$createUrl = () => {
var _a, _b;
let audience = (_b = (_a = this.session.activeTokens.session) === null || _a === void 0 ? void 0 : _a.decoded) === null || _b === void 0 ? void 0 : _b.aud;
if (audience && !(audience === null || audience === void 0 ? void 0 : audience.endsWith('/')))
audience = audience.replace(/\/$/, '');
return `${audience}/my/user`;
};
this.$receive = users.actions.receive;
}
}
export class User extends BaseTransfer {
constructor() {
super(...arguments);
this.modelKind = 'User';
this.$fromDTO = userFromDTO;
this.$createUrl = () => {
return `${this.session.config.editorApiUrl}/users/${this.id}`;
};
this.$receive = users.actions.receive;
this.$selector = selectors.selectUser;
}
}
export class Team extends BaseTransfer {
constructor() {
super(...arguments);
this.modelKind = 'Team';
this.$fromDTO = teamFromDTO;
this.$createUrl = () => {
return `${this.session.config.editorApiUrl}/teams/${this.id}`;
};
this.$receive = teams.actions.receive;
this.$selector = selectors.selectTeam;
}
}
export class Project extends BaseTransfer {
constructor() {
super(...arguments);
this.modelKind = 'Project';
this.$fromDTO = projectFromDTO;
this.$createUrl = () => {
return `${this.session.config.editorApiUrl}/projects/${this.id}`;
};
this.$receive = projects.actions.receive;
this.$selector = selectors.selectProject;
}
}
export class RemoteSiteConfig extends BaseTransfer {
constructor() {
super(...arguments);
this.modelKind = 'SiteConfig';
this.$fromDTO = siteConfigFromDTO;
this.$createUrl = () => {
return `${this.session.config.editorApiUrl}/sites/${this.id}`;
};
this.$receive = siteconfigs.actions.receive;
this.$selector = selectors.selectSiteConfig;
}
}
export class Block extends BaseTransfer {
constructor() {
super(...arguments);
this.modelKind = 'Block';
this.$fromDTO = blockFromDTO;
this.$createUrl = () => {
return `${this.session.config.editorApiUrl}/blocks/${this.id.project}/${this.id.block}`;
};
this.$receive = blocks.actions.receive;
this.$selector = selectors.selectBlock;
}
}
export class Version extends BaseTransfer {
constructor() {
super(...arguments);
this.modelKind = 'Version';
this.$fromDTO = versionFromDTO;
this.$createUrl = () => {
return `${this.session.config.editorApiUrl}${versionIdToURL(this.id)}`;
};
this.$receive = versions.actions.receive;
this.$selector = selectors.selectVersion;
}
}
export class Template extends BaseTransfer {
constructor() {
super(...arguments);
this.modelKind = 'Template';
// TODO better unpacking and defaults on the dto contents
this.$fromDTO = (id, json) => ({ id, ...json });
this.$createUrl = () => {
return `${this.session.config.editorApiUrl}/templates/${this.id}`;
};
this.$receive = templates.actions.receive;
this.$selector = selectors.selectTemplate;
}
}