提交 11e3ac99 authored 作者: vipcxj's avatar vipcxj

完善模块,数据源和接口相关api

上级 8b6f48b1
import mapKeys from 'lodash/mapKeys';
import toPairs from 'lodash/toPairs';
import isUndefined from 'lodash/isUndefined';
import isString from 'lodash/isString'; import isString from 'lodash/isString';
import partial from 'lodash/fp/partial'; import partial from 'lodash/fp/partial';
import request from '../utils/request'; import request, { makeParams } from '../utils/request';
import post from '../utils/post'; import post from '../utils/post';
import { normParams } from '../utils/http-helper';
import { split } from '../utils/filter';
import config from '../utils/config'; import config from '../utils/config';
import { getDomain } from '../utils/auth';
const parseFilters = filtersIn => (filtersIn || []).filter(({ filter }) => !!filter).map(({ key, filter }) => [
split(key, false).map(value => `f-${value}`).join('|'),
filter,
]);
export const datasourceApi = (coordinate) => { export const datasourceApi = (coordinate) => {
const { containerType, containerName, datasourceName } = isString(coordinate) ? { const { containerType, containerName, datasourceName } = isString(coordinate) ? {
containerType: 'global', containerType: 'global',
moduleName: coordinate, datasourceName: coordinate,
} : (coordinate || {}); } : (coordinate || {});
if (containerType === 'global') { if (containerType === 'global') {
return { return {
...@@ -62,25 +53,6 @@ export const datasourceApi = (coordinate) => { ...@@ -62,25 +53,6 @@ export const datasourceApi = (coordinate) => {
} }
}; };
const makeQueryParams = ({ pst, psz, filters = [], sortBys = [], sortTypes = [], params = {} }) => {
let stBy = sortBys.join(',');
stBy = stBy || undefined;
let stType = sortTypes.join(',');
stType = stType || undefined;
return [
...toPairs({ pst, psz, stBy, stType }),
...parseFilters(filters),
...normParams(mapKeys(params, (v, k) => `p-${k}`)),
].filter(v => v && !isUndefined((v[1])));
};
const makeParams = (otherParams, { filters = [], sortBys = [], sortTypes = [], params = {} }) => {
return [
...toPairs(otherParams),
...makeQueryParams({ filters, sortBys, sortTypes, params }),
].filter(v => v && !isUndefined((v[1])));
};
export async function calcGlobalDatasource(name, { pst, psz, filters = [], sortBys = [], sortTypes = [], params = {}, dmPath }) { export async function calcGlobalDatasource(name, { pst, psz, filters = [], sortBys = [], sortTypes = [], params = {}, dmPath }) {
return request(`${config.apiContextPath}/api/datasource/${name}`, makeParams({ pst, psz, dmPath }, { filters, sortBys, sortTypes, params })); return request(`${config.apiContextPath}/api/datasource/${name}`, makeParams({ pst, psz, dmPath }, { filters, sortBys, sortTypes, params }));
} }
...@@ -253,3 +225,93 @@ export async function getModuleDatasourceMeta(mdName, dsName) { ...@@ -253,3 +225,93 @@ export async function getModuleDatasourceMeta(mdName, dsName) {
return request(`${config.apiContextPath}/api/module/user/${mdName}/datasource/${dsName}/meta`); return request(`${config.apiContextPath}/api/module/user/${mdName}/datasource/${dsName}/meta`);
} }
export class Datasource {
constructor(coordinate, dmPath) {
this.api = datasourceApi(coordinate);
this.dmPath = dmPath;
this.dealWithPath = ::this.dealWithPath;
this.query = ::this.query;
this.queryMeta = ::this.queryMeta;
this.count = ::this.count;
this.countMeta = ::this.countMeta;
this.cursor = ::this.cursor;
this.cursorMeta = ::this.cursorMeta;
this.update = ::this.update;
this.updateMeta = ::this.updateMeta;
this.validateUpdate = ::this.validateUpdate;
this.create = ::this.create;
this.createMeta = ::this.createMeta;
this.validateCreate = ::this.validateCreate;
this.remove = ::this.remove;
this.removeMeta = ::this.removeMeta;
this.validateRemove = ::this.validateRemove;
}
async dealWithPath() {
if (this.dmPath) {
return this.dmPath;
} else {
const domain = await getDomain();
return domain.path;
}
}
async query(params = {}, pst = 0, psz = -1, { filters = [], sortBys = [], sortTypes = [] }) {
const dmPath = await this.dealWithPath();
return this.api.query({ pst, psz, filters, sortBys, sortTypes, params, dmPath });
}
async queryMeta(params = {}) {
const dmPath = await this.dealWithPath();
return this.api.query({ params, dmPath });
}
async count(params = {}, { filters = [] }) {
const dmPath = await this.dealWithPath();
return this.api.count({ filters, params, dmPath });
}
async countMeta(params = {}) {
const dmPath = await this.dealWithPath();
return this.api.countMeta({ params, dmPath });
}
async cursor(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.cursor(key, params, dmPath);
}
async cursorMeta(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.cursorMeta(key, params, dmPath);
}
async update(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.update(key, params, dmPath);
}
async updateMeta(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.updateMeta(key, params, dmPath);
}
async validateUpdate(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.validateUpdate(key, params, dmPath);
}
async create(params = {}) {
const dmPath = await this.dealWithPath();
return this.api.create(params, dmPath);
}
async createMeta(params = {}) {
const dmPath = await this.dealWithPath();
return this.api.createMeta(params, dmPath);
}
async validateCreate(params = {}) {
const dmPath = await this.dealWithPath();
return this.api.validateCreate(params, dmPath);
}
async remove(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.remove(key, params, dmPath);
}
async removeMeta(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.removeMeta(key, params, dmPath);
}
async validateRemove(key, params = {}) {
const dmPath = await this.dealWithPath();
return this.api.validateRemove(key, params, dmPath);
}
}
import request from '../utils/request'; import isString from 'lodash/isString';
import partial from 'lodash/fp/partial';
import request, { makeParams } from '../utils/request';
import post from '../utils/post'; import post from '../utils/post';
import doDelete from '../utils/delete'; import doDelete from '../utils/delete';
import config from '../utils/config'; import config from '../utils/config';
import { getDomain } from '../utils/auth';
export const interfaceApi = (coordinate) => {
const { containerType, containerName, interfaceName } = isString(coordinate) ? {
containerType: 'global',
interfaceName: coordinate,
} : (coordinate || {});
if (containerType === 'global') {
return {
invoke: partial(userApi.invokeInterface, [interfaceName]),
validate: partial(userApi.validateState, [interfaceName]),
};
} else if (containerType === 'module') {
return {
invoke: partial(userApi.invokeModuleInterface, [containerName, interfaceName]),
validate: partial(userApi.validateModuleInterface, [containerName, interfaceName]),
};
} else {
throw new Error(`Unsupported containerType: ${containerType}`);
}
};
export const userApi = { export const userApi = {
async getAllInterfaceInfoes() { async getAllInterfaceInfoes() {
...@@ -10,12 +33,18 @@ export const userApi = { ...@@ -10,12 +33,18 @@ export const userApi = {
async getInterfaceInfo(name) { async getInterfaceInfo(name) {
return request(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/info`); return request(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/info`);
}, },
async validateState(name, dmPath) { async validateState(name, params, dmPath) {
return request(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/invoke/validateState`, { dmPath }); return request(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/invoke/validateState`, makeParams({ dmPath }, { params }));
}, },
async invokeInterface(name, params, dmPath) { async invokeInterface(name, params, dmPath) {
return post(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/invoke`, { params, dmPath }); return post(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/invoke`, { params, dmPath });
}, },
async validateModuleInterface(mdName, name, params, dmPath) {
return request(`${config.apiContextPath}/api/module/user/${encodeURIComponent(mdName)}/interface/${encodeURIComponent(name)}/invoke/validateState`, makeParams({ dmPath }, { params }));
},
async invokeModuleInterface(mdName, name, params, dmPath) {
return post(`${config.apiContextPath}/api/module/user/${encodeURIComponent(mdName)}/interface/${encodeURIComponent(name)}/invoke`, { params, dmPath });
},
async getInterfaceDocument(name, dmPath) { async getInterfaceDocument(name, dmPath) {
return request(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/document`, { dmPath }); return request(`${config.apiContextPath}/api/interface/user/${encodeURIComponent(name)}/document`, { dmPath });
}, },
...@@ -53,3 +82,29 @@ export const adminApi = { ...@@ -53,3 +82,29 @@ export const adminApi = {
return post(`${config.apiContextPath}/api/interface/admin/${encodeURIComponent(name)}/document`, { document, dmPath }); return post(`${config.apiContextPath}/api/interface/admin/${encodeURIComponent(name)}/document`, { document, dmPath });
}, },
}; };
export class Interface {
constructor(coordinate, dmPath) {
this.api = interfaceApi(coordinate);
this.dmPath = dmPath;
this.dealWithPath = ::this.dealWithPath;
this.invoke = ::this.invoke;
this.validate = ::this.validate;
}
async dealWithPath() {
if (this.dmPath) {
return this.dmPath;
} else {
const domain = await getDomain();
return domain.path;
}
}
async invoke(params = {}) {
const dmPath = await this.dealWithPath();
return this.api.invoke(params, dmPath);
}
async validate(params = {}) {
const dmPath = await this.dealWithPath();
return this.api.validate(params, dmPath);
}
}
/* eslint-disable no-underscore-dangle */
import { Datasource } from 'datasource';
import request from '../utils/request'; import request from '../utils/request';
import post from '../utils/post'; import post from '../utils/post';
import config from '../utils/config'; import config from '../utils/config';
import { Interface } from './interfaces';
export async function fetchMenus() { export async function fetchMenus() {
return request(`${config.apiContextPath}/api/configure/user/menus`); return request(`${config.apiContextPath}/api/configure/user/menus`);
...@@ -59,3 +62,73 @@ export const userApi = { ...@@ -59,3 +62,73 @@ export const userApi = {
return request(`${config.apiContextPath}/api/module/user/${encodeURIComponent(name)}/document`, { dmPath }); return request(`${config.apiContextPath}/api/module/user/${encodeURIComponent(name)}/document`, { dmPath });
}, },
}; };
export class Module {
constructor(name, dmPath) {
this.name = name;
this.dmPath = dmPath;
this._admin = false;
}
get admin() {
return this._admin;
}
set admin(admin) {
this._admin = admin;
}
get info() {
if (this.admin) {
return adminApi.getInfo(this.name, this.dmPath);
} else {
return userApi.getInfo(this.name, this.dmPath);
}
}
get layout() {
if (this.admin) {
return adminApi.getLayout(this.name, this.dmPath);
} else {
return userApi.getLayout(this.name, this.dmPath);
}
}
get document() {
if (this.admin) {
return adminApi.getDocument(this.name, this.dmPath);
} else {
return userApi.getDocument(this.name, this.dmPath);
}
}
async setDocument(doc) {
if (this.admin) {
return adminApi.setDocument(this.name, doc, this.dmPath);
}
throw new Error('Set admin to true before set document.');
}
get configure() {
if (this.admin) {
return adminApi.getPlainConfigure(this.name, this.dmPath);
} else {
return userApi.getPlainConfigure(this.name, this.dmPath);
}
}
async setConfigure(conf) {
if (this.admin) {
return adminApi.setConfigure(this.name, conf, this.dmPath);
}
throw new Error('Set admin to true before set configure.');
}
getDatasource(name) {
const coordinate = {
containerType: 'module',
containerName: this.name,
datasourceName: name,
};
return new Datasource(coordinate, this.dmPath);
}
getInterface(name) {
const coordinate = {
containerType: 'module',
containerName: this.name,
interfaceName: name,
};
return new Interface(coordinate, this.dmPath);
}
}
import isNil from 'lodash/isNil'; import isNil from 'lodash/isNil';
import defaults from 'lodash/defaults'; import defaults from 'lodash/defaults';
import toPairs from 'lodash/toPairs';
import mapKeys from 'lodash/mapKeys';
import isUndefined from 'lodash/isUndefined';
import { fetch } from './polyfill'; import { fetch } from './polyfill';
import { checkStatus, normParams, parseObject } from './http-helper'; import { checkStatus, normParams, parseObject } from './http-helper';
import middleware from './middleware'; import middleware from './middleware';
import { split } from './filter';
const parseFilters = filtersIn => (filtersIn || []).filter(({ filter }) => !!filter).map(({ key, filter }) => [
split(key, false).map(value => `f-${value}`).join('|'),
filter,
]);
const makeQueryParams = ({ pst, psz, filters = [], sortBys = [], sortTypes = [], params = {} }) => {
let stBy = sortBys.join(',');
stBy = stBy || undefined;
let stType = sortTypes.join(',');
stType = stType || undefined;
return [
...toPairs({ pst, psz, stBy, stType }),
...parseFilters(filters),
...normParams(mapKeys(params, (v, k) => `p-${k}`)),
].filter(v => v && !isUndefined((v[1])));
};
export const makeParams = (otherParams, { filters = [], sortBys = [], sortTypes = [], params = {} }) => {
return [
...toPairs(otherParams),
...makeQueryParams({ filters, sortBys, sortTypes, params }),
].filter(v => v && !isUndefined((v[1])));
};
const defaultOptions = { const defaultOptions = {
headers: { Accept: 'application/json' }, headers: { Accept: 'application/json' },
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论