|
| 1 | +import _ from "lodash" |
| 2 | + |
| 3 | +export function viewer_call( |
| 4 | + { schema, params = {} }, |
| 5 | + { request_error_function, response_function, response_error_function } = {}, |
| 6 | +) { |
| 7 | + console.log("viewer_call", schema.route, params) |
| 8 | + const errors_store = use_errors_store() |
| 9 | + const viewer_store = use_viewer_store() |
| 10 | + |
| 11 | + const { valid, error } = validate_schema(schema, params) |
| 12 | + |
| 13 | + if (!valid) { |
| 14 | + errors_store.add_error({ |
| 15 | + code: 400, |
| 16 | + route: schema.route, |
| 17 | + name: "Bad request", |
| 18 | + description: error, |
| 19 | + }) |
| 20 | + throw new Error(schema.route.concat(": ", error)) |
| 21 | + } |
| 22 | + |
| 23 | + const client = viewer_store.client |
| 24 | + |
| 25 | + if (!_.isEmpty(schema.properties)) { |
| 26 | + params = [params] |
| 27 | + } else { |
| 28 | + params = [] |
| 29 | + } |
| 30 | + |
| 31 | + let promise = new Promise((resolve, reject) => { |
| 32 | + if (client) { |
| 33 | + viewer_store.start_request() |
| 34 | + client |
| 35 | + .getConnection() |
| 36 | + .getSession() |
| 37 | + .call(schema.rpc, params) |
| 38 | + .then( |
| 39 | + (value) => { |
| 40 | + if (response_function) { |
| 41 | + console.log("response_function", value) |
| 42 | + response_function(value) |
| 43 | + } |
| 44 | + resolve() |
| 45 | + }, |
| 46 | + (reason) => { |
| 47 | + if (request_error_function) { |
| 48 | + console.log("request_error_function", reason) |
| 49 | + request_error_function(reason) |
| 50 | + } |
| 51 | + reject() |
| 52 | + }, |
| 53 | + ) |
| 54 | + .catch((error) => { |
| 55 | + console.log("error : ", error) |
| 56 | + errors_store.add_error({ |
| 57 | + code: error.code, |
| 58 | + route: schema.route, |
| 59 | + name: error.data.message, |
| 60 | + description: error.data.exception, |
| 61 | + }) |
| 62 | + if (response_error_function) { |
| 63 | + response_error_function(error) |
| 64 | + } |
| 65 | + reject() |
| 66 | + }) |
| 67 | + .finally(() => { |
| 68 | + viewer_store.stop_request() |
| 69 | + }) |
| 70 | + } |
| 71 | + }) |
| 72 | + return promise |
| 73 | +} |
| 74 | + |
| 75 | +export default viewer_call |
0 commit comments