UNPKG

@ginstone/nga-api

Version:

58 lines (57 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NgaUploadClient = void 0; const axios_1 = require("axios"); const UploadBody_1 = require("../response/body/UploadBody"); const NgaException_1 = require("../exception/NgaException"); const headers = { 'Content-Type': 'multipart/form-data' }; /** * 上传客户端 */ class NgaUploadClient { constructor() { this.instance = axios_1.default.create({ headers, }); } /** * 上传 * @param attachUrl 上传地址,从post准备请求中获取 * @param params 上传参数 */ upload(attachUrl, params) { return this.instance.post(attachUrl, params.build(), { params: { v2: "1", func: "upload", __inchst: "UTF8", origin_domain: "bbs.nga.cn", __output: "11", } }).then(res => { const { error_code, error } = res.data; if (error_code) { switch (error_code) { case 3: throw new NgaException_1.NgaException(400, [error_code, '上传令牌已过期, 请刷新页面重新获取']); case 6: throw new NgaException_1.NgaException(400, [error_code, '文件类型错误']); default: throw { error_code, error }; } } return new UploadBody_1.UploadBody(res.data, params.file, params.description); }).catch(reason => { if (reason.response) { if (reason.response.status === 413) { throw new NgaException_1.NgaException(reason.response.status, [reason.response.status, '文件过大:' + params.file.name]); } else { throw new NgaException_1.NgaException(reason.response.status, [reason.response.status, reason.response.statusText,]); } } else { throw reason; } }); } } exports.NgaUploadClient = NgaUploadClient;