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 getPluginTypeOptions () {
31 label: $localize`Plugins`,
32 value: PluginType.PLUGIN
35 label: $localize`Themes`,
36 value: PluginType.THEME
41 getPluginTypeLabel (type: PluginType) {
42 if (type === PluginType.PLUGIN) {
43 return $localize`plugin`
46 return $localize`theme`
50 pluginType: PluginType,
51 componentPagination: ComponentPagination,
54 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
56 let params = new HttpParams()
57 params = this.restService.addRestGetParams(params, pagination, sort)
58 params = params.append('pluginType', pluginType.toString())
60 return this.authHttp.get<ResultList<PeerTubePlugin>>(PluginApiService.BASE_PLUGIN_URL, { params })
61 .pipe(catchError(res => this.restExtractor.handleError(res)))
64 searchAvailablePlugins (
65 pluginType: PluginType,
66 componentPagination: ComponentPagination,
70 const pagination = this.restService.componentPaginationToRestPagination(componentPagination)
72 let params = new HttpParams()
73 params = this.restService.addRestGetParams(params, pagination, sort)
74 params = params.append('pluginType', pluginType.toString())
76 if (search) params = params.append('search', search)
78 return this.authHttp.get<ResultList<PeerTubePluginIndex>>(PluginApiService.BASE_PLUGIN_URL + '/available', { params })
79 .pipe(catchError(res => this.restExtractor.handleError(res)))
82 getPlugin (npmName: string) {
83 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName
85 return this.authHttp.get<PeerTubePlugin>(path)
86 .pipe(catchError(res => this.restExtractor.handleError(res)))
89 getPluginRegisteredSettings (pluginName: string, pluginType: PluginType) {
90 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
91 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings'
93 return this.authHttp.get<RegisteredServerSettings>(path)
95 catchError(res => this.restExtractor.handleError(res))
99 updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) {
100 const npmName = this.pluginService.nameToNpmName(pluginName, pluginType)
101 const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/settings'
103 return this.authHttp.put(path, { settings })
104 .pipe(catchError(res => this.restExtractor.handleError(res)))
107 uninstall (pluginName: string, pluginType: PluginType) {
108 const body: ManagePlugin = {
109 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
112 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/uninstall', body)
113 .pipe(catchError(res => this.restExtractor.handleError(res)))
116 update (pluginName: string, pluginType: PluginType) {
117 const body: ManagePlugin = {
118 npmName: this.pluginService.nameToNpmName(pluginName, pluginType)
121 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/update', body)
122 .pipe(catchError(res => this.restExtractor.handleError(res)))
125 install (npmName: string) {
126 const body: InstallOrUpdatePlugin = {
130 return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body)
131 .pipe(catchError(res => this.restExtractor.handleError(res)))
134 getPluginOrThemeHref (type: PluginType, name: string) {
135 const typeString = type === PluginType.PLUGIN
139 return `https://www.npmjs.com/package/peertube-${typeString}-${name}`