Created
December 19, 2017 02:07
-
-
Save wisetc/7b1ffa3a29664a0a741c8f0634d4bc41 to your computer and use it in GitHub Desktop.
Standard front-end data model methods by transforming back-end unified api of my company. 面向表的编程。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { XTRequest } from './configure' | |
const IBASIC_API_BASE_URL = 'http://192.168.2.103:5555/ibasic-api' | |
export const iBaseAPI = new XTRequest(IBASIC_API_BASE_URL, ['custom', 'supplier', 'warehouse']).models | |
const api = iBaseAPI.custom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urljoin from 'url-join' | |
import { camelCase } from 'lodash' | |
import _axios from './configure' | |
export const pageSize = 999 | |
// 贤二 service api. | |
export class XTRequest { | |
map = { | |
list: 'list', | |
create: 'insert', | |
update: 'update', | |
destroy: 'delete', | |
retrieve: 'get' | |
} | |
constructor(baseUrl, tables) { | |
this.baseUrl = baseUrl | |
this.tables = tables | |
this.generateModels() | |
} | |
getRequestUrl(method, tableName) { | |
let action = camelCase(`${this.map[method]}-${tableName}`) | |
return urljoin(this.baseUrl, camelCase(tableName), action) | |
} | |
generateModels() { | |
let models = {} | |
this.tables.forEach(table => { | |
let model = { | |
list: (params) => _axios.post(this.getRequestUrl('list', table), { pageSize, ...params }), | |
create: (form) => _axios.post(this.getRequestUrl('create', table), { data: JSON.stringify(form) }), | |
update: (form) => _axios.post(this.getRequestUrl('update', table), { data: JSON.stringify(form) }), | |
destroy: (id) => _axios.post(this.getRequestUrl('destroy', table), { id }), | |
retrieve: (id) => _axios.post(this.getRequestUrl('retrieve', table), { id }) | |
} | |
models[camelCase(table)] = model | |
}) | |
this.models = models | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment