| 23 | } |
| 24 | |
| 25 | export default class WebService<T extends WebAPI = WebAPI, P extends object = object> implements Serializable { |
| 26 | onChangeName: Signal<() => void> = new Signal; |
| 27 | onChangeAPIParams: Signal<() => void> = new Signal; |
| 28 | onChangeParams: Signal<() => void> = new Signal; |
| 29 | onChangeIcon: Signal<() => void> = new Signal; |
| 30 | |
| 31 | name: string; |
| 32 | api: T; |
| 33 | icon?: Icon; |
| 34 | params?: P; |
| 35 | |
| 36 | static deserialize(data: WebServiceData): WebServiceOptions<WebAPI> { |
| 37 | if (!data || |
| 38 | typeof data != 'object' || |
| 39 | typeof data.name != 'string' || |
| 40 | typeof data.api != 'string') { |
| 41 | throw new Error(`Unknown WebService : ${JSON.stringify(data)}`); |
| 42 | } |
| 43 | const credential = apiManager.getCredentialById(data.api); |
| 44 | const api = apiManager.createAPIForCredential(credential); |
| 45 | const options: WebServiceOptions<WebAPI> = {name: data.name, api}; |
| 46 | if (typeof data.icon == 'string') |
| 47 | options.icon = new Icon({chieURL: data.icon}); |
| 48 | if (typeof data.apiParams == 'object') |
| 49 | options.apiParams = data.apiParams; |
| 50 | if (typeof data.params == 'object') |
| 51 | options.params = data.params; |
| 52 | return options; |
| 53 | } |
| 54 | |
| 55 | constructor(options: WebServiceOptions<T, P>) { |
| 56 | if (!options.name || !options.api) |
| 57 | throw new Error('Must pass name and api to WebService'); |
| 58 | this.name = options.name; |
| 59 | this.api = options.api; |
| 60 | this.icon = options.icon ?? new Icon({name: 'bot'}); |
| 61 | // The params are cloned since they may be used to create other assistants. |
| 62 | this.api.params = Object.assign({}, options.apiParams); |
| 63 | this.params = Object.assign({}, options.params); |
| 64 | } |
| 65 | |
| 66 | serialize() { |
| 67 | const data: WebServiceData = { |
| 68 | name: this.name, |
| 69 | api: this.api.credential.id, |
| 70 | }; |
| 71 | if (this.icon) |
| 72 | data.icon = this.icon.getChieURL(); |
| 73 | if (!isEmptyObject(this.api.params)) |
| 74 | data.apiParams = nonNullAssign({}, this.api.params); |
| 75 | if (!isEmptyObject(this.params)) |
| 76 | data.params = nonNullAssign({}, this.params); |
| 77 | return data; |
| 78 | } |
| 79 | |
| 80 | destructor() { |
| 81 | // Nothing to destructor by default. |
| 82 | } |
nothing calls this directly
no outgoing calls
no test coverage detected