@wishcbg/bobcode
Version:
基於店家同步功能的需求,讓店家能夠在不同的店家之間同步各項功能設定,並且能夠檢視各項功能的異動情況。 規劃出一套店家設定即程式碼的機制,讓店家能夠以 config file (.sac) 來定義各項功能設定,並且能夠還原店家各項功能設定。 暫且稱這個機制為「Settings as Code」「店家設定即程式碼」。
95 lines (94 loc) • 3.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createApiClient = createApiClient;
const axios_1 = __importDefault(require("axios"));
const error_1 = require("./error");
// API 請求用戶端
function createApiClient(baseUrl, token) {
const client = axios_1.default.create({
baseURL: baseUrl,
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
// 添加錯誤處理
client.interceptors.response.use(response => response, error => {
// 保留原始錯誤資訊以供後續處理
return Promise.reject({
status: error.response?.status,
data: error.response?.data,
message: error.message
});
});
return client;
}
class Bob {
constructor({ baseUrl, token, shopId }) {
this.client = createApiClient(baseUrl, token);
this.shopId = shopId;
}
async findLogicIdSet() {
try {
const response = await this.client.get(`/${this.shopId}/bobcode/findLogicIdSet`);
return response.data;
}
catch (error) {
throw new error_1.ThirtdPartyError(error.message || '未知錯誤', 'Bob API', error.data?.code || 'APPLY_CHANGES_FAILED');
}
}
async upsertLogicIdSet(data) {
try {
const response = await this.client.put(`/${this.shopId}/bobcode/upsertLogicIdSet`, { data });
return response.data;
}
catch (error) {
throw new error_1.ThirtdPartyError(error.message || '未知錯誤', 'Bob API', error.data?.code || 'UPSERT_LOGIC_ID_SET_FAILED');
}
}
async introspect(resourceTypes, logicIdPrefix) {
try {
const response = await this.client.post(`/${this.shopId}/bobcode/introspect`, { resourceTypes, logicIdPrefix });
return response.data;
}
catch (error) {
throw new error_1.ThirtdPartyError(error.message || '未知錯誤', 'Bob API', error.data?.code || 'INTROSPECT_FAILED');
}
}
async applyChanges(apiPath, httpMethod, applyBody) {
console.log('applyChanges', apiPath, httpMethod, applyBody);
try {
const response = await this.client.request({
url: apiPath,
method: httpMethod,
data: applyBody
});
return {
status: 'success',
data: response.data
};
}
catch (error) {
throw new error_1.ThirtdPartyError(error.message || '未知錯誤', 'Bob API', error.data?.code || 'APPLY_CHANGES_FAILED');
}
}
async getExtraInfo(apiPath) {
try {
const response = await this.client.request({
url: apiPath,
method: 'get',
});
return {
status: 'success',
data: response.data
};
}
catch (error) {
throw new error_1.ThirtdPartyError(error.message || '未知錯誤', 'Bob API', error.data?.code || 'GET_EXTRA_INFO_FAILED');
}
}
}
exports.default = Bob;