-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
visual-studio-marketplace-base.js
124 lines (113 loc) · 3.45 KB
/
visual-studio-marketplace-base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import Joi from 'joi'
import validate from '../../core/base-service/validate.js'
import { BaseJsonService, NotFound } from '../index.js'
const extensionQuerySchema = Joi.object({
results: Joi.array()
.items(
Joi.object({
extensions: Joi.array()
.items(
Joi.object({
statistics: Joi.array()
.items(
Joi.object({
statisticName: Joi.string().required(),
value: Joi.number().required(),
})
)
.required(),
versions: Joi.array()
.items(
Joi.object({
version: Joi.string().required(),
properties: Joi.array()
.items(
Joi.object({
key: Joi.string().required(),
value: Joi.any().required(),
})
)
.default([]),
})
)
.min(1)
.required(),
releaseDate: Joi.string().required(),
lastUpdated: Joi.string().required(),
})
)
.required(),
})
)
.required(),
}).required()
const statisticSchema = Joi.object().keys({
install: Joi.number().default(0),
updateCount: Joi.number().default(0),
onpremDownloads: Joi.number().default(0),
averagerating: Joi.number().default(0),
ratingcount: Joi.number().default(0),
})
export default class VisualStudioMarketplaceBase extends BaseJsonService {
static keywords = [
'vscode',
'tfs',
'vsts',
'visual-studio-marketplace',
'vs-marketplace',
'vscode-marketplace',
]
static defaultBadgeData = {
label: 'vs marketplace',
color: 'blue',
}
async fetch({ extensionId }) {
const url =
'https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery/'
const body = {
filters: [
{
criteria: [{ filterType: 7, value: extensionId }],
},
],
// Microsoft does not provide a clear API doc. It seems that the flag value is calculated
// as the combined hex values of the requested flags, converted to base 10.
// This was found using the vscode repo at:
// https://github.com/microsoft/vscode/blob/main/src/vs/platform/extensionManagement/common/extensionGalleryService.ts
// This flag value is 0x192.
flags: 402,
}
const options = {
method: 'POST',
headers: {
accept: 'application/json;api-version=3.0-preview.1',
'content-type': 'application/json',
},
body: JSON.stringify(body),
}
return this._requestJson({
schema: extensionQuerySchema,
url,
options,
errorMessages: {
400: 'invalid extension id',
},
})
}
transformExtension({ json }) {
const extensions = json.results[0].extensions
if (extensions.length === 0) {
throw new NotFound({ prettyMessage: 'extension not found' })
}
return { extension: extensions[0] }
}
transformStatistics({ json }) {
const { extension } = this.transformExtension({ json })
const statistics = {}
extension.statistics.forEach(({ statisticName, value }) => {
statistics[statisticName] = value
})
const value = validate({ ErrorClass: Error }, statistics, statisticSchema)
return { statistics: value }
}
}