1 import { catchError } from 'rxjs/operators'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { ComponentPagination, RestExtractor, RestService } from '@app/core'
5 import { PluginService } from '@app/core/plugins/plugin.service'
12 RegisteredServerSettings,
14 } from '@shared/models'
15 import { environment } from '../../../../environments/environment'
18 export class PluginApiService {
19 private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins'
22 private authHttp: HttpClient,
23 private restExtractor: RestExtractor,
24 private restService: RestService,
25 private pluginService: PluginService
28 getPluginTypeLabel (type: PluginType) {
29 if (type === PluginType.PLUGIN) {
30 return $localize`plugin`
33 return $localize`theme`
37 pluginType: PluginType,
38 componentPagination: ComponentPagination,
41 const pagination = this.restService.componentToRestPagination(componentPagination)
43 let params = new HttpParams()
44 params = this.restService.addRestGetParams(params, pagination, sort)
45 params = params.append('pluginType', pluginType.toString())
47 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
48 .pipe(catchError(res => this.restExtractor.handleError(res)))
51 searchAvailablePlugins (
52 pluginType: PluginType,
53 componentPagination: ComponentPagination,
57 const pagination = this.restService.componentToRestPagination(componentPagination)
59 let params = new HttpParams()
60 params = this.restService.addRestGetParams(params, pagination, sort)
61 params = params.append('pluginType', pluginType.toString())
63 if (search) params = params.append('search', search)
65 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
66 .pipe(catchError(res => this.restExtractor.handleError(res)))
69 getPlugin (npmName: string) {
70 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
72 return this.authHttp.get<PeerTubePlugin>(path)
73 .pipe(catchError(res => this.restExtractor.handleError(res)))
76 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
77 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
78 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
80 return this.authHttp.get<RegisteredServerSettings>(path)
82 catchError(res => this.restExtractor.handleError(res))
86 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
87 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
88 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
90 return this.authHttp.put(path, { settings })
91 .pipe(catchError(res => this.restExtractor.handleError(res)))
94 uninstall (pluginName: string, pluginType: PluginType) {
95 const body: ManagePlugin = {
96 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
99 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
100 .pipe(catchError(res => this.restExtractor.handleError(res)))
103 update (pluginName: string, pluginType: PluginType) {
104 const body: ManagePlugin = {
105 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
108 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
109 .pipe(catchError(res => this.restExtractor.handleError(res)))
112 install (npmName: string) {
113 const body: InstallOrUpdatePlugin = {
117 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
118 .pipe(catchError(res => this.restExtractor.handleError(res)))
121 getPluginOrThemeHref (type: PluginType, name: string) {
122 const typeString = type === PluginType.PLUGIN
126 return `https://www.npmjs.com/package/peertube-${typeString}-${name}`